/**
*	Product Set Controller
*	@author:	Chris Chang
*	@created:	09/01/2010
*/

var ProductSetController  = (function(psc) {

	/////////////////////////////////////////////////////
	//	GLOBALS & INIT
	/////////////////////////////////////////////////////
	
	$(document).ready(function() {	
		$('.ps_toggle').click(function() {
			if($(this).hasClass('selected')) {
				removeIt($(this));
			}
			else {
				addIt($(this));
			}
		});
		$('.ps_toggle').mouseover(function() {
			if($(this).hasClass('selected')) {
				//removeIt($(this));
			}
			else {
				addIt($(this));
			}
		});
		if (document.getElementById('mycarousel')!=null)
				document.getElementById('mycarousel').style.visibility = 'visible';
	});

	/////////////////////////////////////////////////////
	//	PRIVATE METHODS
	/////////////////////////////////////////////////////
	
	/**
	*	Swipe out the specified product
	*/
	function removeIt(o) {
		o.removeClass('selected');
		var obj = o.parent().next('.ps_content');
		obj.animate({
	   		width: '0px',
	    	opacity: '0'
	  	}, 500, 'linear', function() {
	  		obj.hide();
	  	});
	
	}
	
	/**
	*	Swipe in the specified product
	*/
	function addIt(o) {
		//Close any open products
		var objs = $('.prodSetProduct .selected');
		if(objs.length > 0) {
			objs.each(function(index){
				removeIt($(this));
			});
		}
		o.addClass('selected');
		var obj = o.parent().next('.ps_content');
		obj.show();
		obj.animate({
	   		width: '334px',
	    	opacity: '1'
	  	}, 500, 'linear');
	}
	
	return psc;
}(ProductSetController || {}));


/**
*	Fila Carousel Controller
*	@author:	Chris Chang
*	@created:	09/01/2010
*/
var CarouselController  = (function(cc) {

	/////////////////////////////////////////////////////
	//	GLOBALS & INIT
	/////////////////////////////////////////////////////

	var carouselIndex = 1;	// keeps track of the carousel index
	
	/**
	*	Init
	*/
	$(document).ready(function() {	
		jQuery('#mycarousel').jcarousel({ 
			 visible: 1
			,buttonPrevHTML: "<div id='carouselLeft'></div>"
			,buttonNextHTML: "<div id='carouselRight'></div>"
			,initCallback: mycarousel_initCallback
			,itemFirstInCallback: mycarousel_itemFirstInCallback
		});
	});
	
	/////////////////////////////////////////////////////
	//	PRIVATE METHODS
	/////////////////////////////////////////////////////

	/**
	*	Set currently selected index in the carousel controls
	*/
	function setDots(index) {
		$('#carouselControls li a').removeClass('active');
		$('#carouselControls li a').slice(index-1, index).addClass('active');
	}
	
	/**
	*	Called when the carousel is first initialized
	*/
	function mycarousel_initCallback(carousel) {
	    jQuery('#carouselControls li a').bind('click', function() {
	    	var index = jQuery(this).attr('href')*1;	    	
	        carousel.scroll(jQuery.jcarousel.intval(index));

	        return false;
	    });
	    
	    var nextBtns = {a: '#mycarousel-next', b: '#carouselRight'};
	    var prevBtns = {a: '#mycarousel-prev', b: '#carouselLeft'};
	    
	    for (var x in nextBtns) {
		    $(nextBtns[x]).bind('click', function() {
		    	if(carouselIndex >= $('#mycarousel li').length) {
		    		carousel.scroll(1);
		    		return false;
		    	}
		    	
		    	carousel.scroll(carouselIndex+1);

		        return false;
		    });
	    }
	 
	    for (var x in prevBtns) {
		    $(prevBtns[x]).bind('click', function() {
		    	if(carouselIndex <= 1) {
		    		carousel.scroll($('#mycarousel li').length);
		    		return false;
		    	}
		    	carousel.scroll(carouselIndex-1);
		    	
		        return false;
		    });
	    }

	};

	/**
	*	Called when item because the first visible slide in the carousel
	*/
	function mycarousel_itemFirstInCallback (carousel, li, index, state) {
		carouselIndex = index;
		setDots(index);
	}

	return cc;
}(CarouselController || {}));



