/*
 *
 * pCarousel - Pokémon carousels for Rumble Blast
 * 
 */

$.fn.pcarousel = function(options){
	var defaults = {
		leftArrow:'#1_0_left_arrow',
		rightArrow:'#1_0_right_arrow',
		itemsPerSlide: 1,
		captions: true,
		callback: {},
		scene: '',
		subscene: ''
	};
	
	var $$ = $(this);
	var obj = $.extend({}, defaults, options);
	
	return this.each(function(){
		var listItems = $$.children("li");
		var savedImagePosition = 0;
		
		// Start reset
		$(obj.leftArrow).unbind();
		$(obj.rightArrow).unbind();
		var initialMarginLeft = listItems.eq(0).css("margin-left");
		if(initialMarginLeft != "0px"){
			listItems.animate({"margin-left":"-="+initialMarginLeft}, 0);
		}
		$(obj.rightArrow).removeClass("disabledRight");
		// End reset
		
		var refreshDisabledStatus = function(){
			if(savedImagePosition == 0){
				$(obj.leftArrow).addClass("disabledLeft");
				$(obj.leftArrow).removeClass("hoverLeft");
				$(obj.rightArrow).removeClass("disabledRight");
			} else if(savedImagePosition == listItems.length - 1){
				$(obj.leftArrow).removeClass("disabledLeft");
				$(obj.rightArrow).addClass("disabledRight");
				$(obj.rightArrow).removeClass("hoverRight");
			} else {
				$(obj.leftArrow).removeClass("disabledLeft");
				$(obj.rightArrow).removeClass("disabledRight");
			}
			
			if(typeof obj.callback == "function"){
				obj.callback(savedImagePosition);
			}
		}
		
		$(obj.leftArrow).addClass("disabledLeft");
		
		$(obj.leftArrow).hover(function(){
			if(!$(this).hasClass("disabledLeft")){
				$(this).addClass("hoverLeft");
			}
		}, function(){
			$(this).removeClass("hoverLeft");
		});
		
		$(obj.rightArrow).hover(function(){
			if(!$(this).hasClass("disabledRight")){
				$(this).addClass("hoverRight");
			}
		}, function(){
			$(this).removeClass("hoverRight");
		});
		
		$(obj.leftArrow).bind(userX.clickEvt,function(evt){
			//evt.preventDefault();
			if(savedImagePosition > 0){
				listItems.animate({"margin-left":"+="+$$.width()}, 500);
				savedImagePosition--;
				refreshDisabledStatus();
				
				var _currScId = (obj.scene != '') ? obj.scene : Contents[MainStage.currScene].id;
				var _currSubScId = '';
				if(Contents[MainStage.currScene].subcontents != undefined){
					_currSubScId = (obj.subscene != '') ? obj.subscene : Contents[MainStage.currScene].subcontents[Contents[MainStage.currScene].currentSubScene].id || "";
				}
				PET.track({type:'event',category:PET.cat_screenshots, action:PLANG.lang+'|'+_currScId+'|'+_currSubScId, label:PLANG.lang+'|'+(savedImagePosition+1)});
			}
		});
		
		$(obj.rightArrow).bind(userX.clickEvt,function(evt){
			//evt.preventDefault();
			if(savedImagePosition < (listItems.length - 1)){
				listItems.animate({"margin-left":"-="+$$.width()}, 500);
				savedImagePosition++;
				refreshDisabledStatus();
				
				var _currScId = (obj.scene != '') ? obj.scene : Contents[MainStage.currScene].id;
				var _currSubScId = '';
				if(Contents[MainStage.currScene].subcontents != undefined){
					_currSubScId = (obj.subscene != '') ? obj.subscene : Contents[MainStage.currScene].subcontents[Contents[MainStage.currScene].currentSubScene].id || "";
				}
				PET.track({type:'event',category:PET.cat_screenshots, action:PLANG.lang+'|'+_currScId+'|'+_currSubScId, label:PLANG.lang+'|'+(savedImagePosition+1)});
			}
		});
	});
}




/*
*
* pCarousel - Pokémon carousels for Rumble Blast
* 
*/

$.fn.pcarouselnews = function(options){
	
	var defaults = {
	};
	options = $.extend(defaults, options);
	
	var $$ = $(this);
	
	return this.each(function(){
		var listItems = $$.find("ul.list-left li");
		var contentItems = $$.find("div.arrayOfContents div.content");
		
		listItems.each(function(i){
			var thisItem = $(this);
			
			thisItem.bind(userX.clickEvt,function(){
				if(isAnimating){ return false; }
				listItems.removeClass("selected hover");
				thisItem.addClass("selected");
				showContent(i);
			});
			
			thisItem.hover(function(){
				if(!thisItem.hasClass("selected")){ thisItem.addClass("hover"); }
			}, function(){
				thisItem.removeClass("hover");
			});
		});
		
		var isAnimating = false;
		var currentMenu = 0;
		var currentContent = 0;
		var itemsPerMenuSlide = 5;
		contentItems.eq(0).show();
		
		var refreshMoreArrows = function(){
			if(currentMenu > 0){ $("div.moreButton div.upArrow").show();
			} else { $("div.moreButton div.upArrow").hide(); }
			if((currentMenu + 1) * itemsPerMenuSlide < listItems.length){ $("div.moreButton div.downArrow").show();
			} else { $("div.moreButton div.downArrow").hide(); }
		}
		
		if(listItems.length > itemsPerMenuSlide){
			$("div.moreButton").fadeIn(300);
			
			$("div.moreButton").bind(userX.clickEvt,function(){
				var _nHeight = $('ul#list_titles_news').height();
				
				if($("div.moreButton div.downArrow").is(':visible')){
					if(!isAnimating && (currentMenu + 1) * itemsPerMenuSlide < listItems.length){
						listItems.eq(0).animate({"margin-top": -(++currentMenu * _nHeight) + "px"}, 300, function(){
							isAnimating = false;
						});
						isAnimating = true;
						refreshMoreArrows();
					}
				} else if($("div.moreButton div.upArrow").is(':visible')){
					if(!isAnimating && currentMenu > 0){
						listItems.eq(0).animate({"margin-top": -(--currentMenu * _nHeight) + "px"}, 300, function(){
							isAnimating = false;
						});
						isAnimating = true;
						refreshMoreArrows();
					}
				}
			});
		}
		
		var showContent = function(i){
			var newPos = 0;
			
			if(i == currentContent){
				return false;
			} else if(i < currentContent){
				newPos = -options.width;
			} else if(i > currentContent){
				newPos = options.width;
			}
			
			contentItems.eq(i).css({"margin-left": newPos + "px"});
			contentItems.eq(i).show();
			
			contentItems.eq(currentContent).animate({"margin-left": -newPos}, 500);
			
			contentItems.eq(i).animate({"margin-left": 0}, 500, function(){
				currentContent = i;
				isAnimating = false;
			});
			
			var _currScId = Contents[MainStage.currScene].id;
			var _currSubScId = Contents[MainStage.currScene].subcontents[Contents[MainStage.currScene].currentSubScene].id || "";
			PET.track({type:'event',category:PET.cat_news, action:PLANG.lang+'|'+_currScId+'|'+_currSubScId, label:PLANG.lang+'|news-'+(i+1)});
			
			isAnimating = true;
		}
	});
}
