$(document).ready(function() {
	
	$('#product_spotlight').cycle({
		fx:			'fade',
		timeout:	5000,
		speed:		1000,
		pause:		1,
		prev:		'#spotlightPrev',
		next:		'#spotlightNext'
	});
	
	$('.product_rotator').cycle({
		fx:			'fade',
		timeout:	0,
		speed:		0,
		next:		'.wrapper_product_banner .controls .next',
		prev:		'.wrapper_product_banner .controls .prev'
	});
	
	$('#category_list_controls').toggle();
	//$('.wrapper_sizes .sizes').toggle();
	$('#category_list').cycle({
			fx: 'scrollHorz',
			prev: '.cat_prev',
			next: '.cat_next',
			timeout: 5000,
			speed: 1500,
			pause: 1
	});
	$('.category_slider').mouseenter(function(event){
		event.stopPropagation();
		var desc = '#' + $(this).attr('id') + '_description';
		var hasul = $(desc + ' > ul').size();
		if(hasul > 0){
			$(desc).stop(true,true);
			$(desc).slideToggle({
				duration: 400,
				easing: 'easeInOutSine'
			});
			//$(this).height($(this).parent().height());
		}
	});
	$('.category_slider').mouseleave(function(event){
		event.stopPropagation();
		var desc = '#' + $(this).attr('id') + '_description';
		var hasul = $(desc + ' > ul').size();
		if(hasul > 0){
			$(desc).stop(true,true);
			$(desc).slideToggle({
				duration: 400,
				easing: 'easeInOutSine'
			});
			//$(this).height($(this).parent().height());
		}
	});
	
	/*$('.wrapper_sizes h3').click(function(event){
		event.stopPropagation();
		var list = '#' + $(this).parent().attr('id') + ' .sizes';
		$(list).stop(true,true);
		$(list).slideToggle({
			duration: 400,
			easing: 'easeInOutSine'
		});
		//$(this).height($(this).parent().height());
	});*/
	
	
	function mycarousel_initCallback(carousel) {
		/*$('.jcarousel-control a').bind('click', function() {
			carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
			return false;
		});

		$('.jcarousel-scroll select').bind('change', function() {
			carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
			return false;
		});*/

		$('#childNext').bind('click', function() {
			carousel.next();
			return false;
		});

		$('#childPrev').bind('click', function() {
			carousel.prev();
			return false;
		});
	};
	
	function trigger(carousel, state){
		$("#childCurrentFirst").html(carousel.first);
		$("#childCurrentLast").html(carousel.last);
	}
	
	function mycarousel_buttonPrevCallback(carousel, button, enabled){
		if(enabled){
			$('#childPrev').removeClass('disabled');
		}else{
			$('#childPrev').addClass('disabled');
		}
	}
	
	function mycarousel_buttonNextCallback(carousel, button, enabled){
		if(enabled){
			$('#childNext').removeClass('disabled');
		}else{
			$('#childNext').addClass('disabled');
		}
	}

	
	$('.productChildren').jcarousel({
		scroll:	4,
		buttonNextHTML: null,
        buttonPrevHTML: null,
		initCallback: mycarousel_initCallback,
		itemLoadCallback: trigger,
		buttonPrevCallback: mycarousel_buttonPrevCallback,
		buttonNextCallback: mycarousel_buttonNextCallback
	});
	
	/*$('.productChildren .child').mouseenter(function(){
		var desc = '#' + $(this).attr('id') + '_sizelist';
		$(desc).slideToggle({
			duration: 400,
			easing: 'easeInOutSine'
		});
	});
	$('.productChildren .child').mouseleave(function(){
		var desc = '#' + $(this).attr('id') + '_sizelist';
		$(desc).slideToggle({
			duration: 400,
			easing: 'easeInOutSine'
		});
	});*/
	/*$('.category_slider').mouseout(function(){
		var desc = '#' + $(this).attr('id') + '_description';
		$(desc).hide();
	});*/
	
	//$('#tabs').tabs();
	
	$('.childTitle').equalHeights();
	
	//$('.childTitle').bind("tabsshow", function(){
	//});
	
	$('#productPopup').show(1000);
	$('#productPopup').delay(2000).hide(1000);
	
	var oldURL = $.cookie('oldURL');
	var currentURL = $(location).attr('href');
	
//	console.log('Previous: ' + oldURL);
//	console.log('Current: ' + currentURL);
//	$('#tabs').tabs('select', 'tabs-2');
//	alert($.cookie('tabs'));
	if(oldURL == currentURL){
		
		var $tabs = $('#tabs').tabs({
			cookie: {
				// store cookie for a day, without, it would be a session cookie
				expires: 1,
				name: 'tabs'
			}
		});
		if ($.cookie('tabs') == 1) {
			$tabs.tabs('select', 'tabs-2');
		} else {
			$tabs.tabs('select', 'tabs-1');
		}
	} else {
		$.cookie('tabs',null); //reset tabs cookie
		var $tabs = $('#tabs').tabs({
			cookie: {
				// store cookie for a day, without, it would be a session cookie
				expires: 1,
				name: 'tabs'
			}
//			selected: 0  // select first tab
		});
		$tabs.tabs('select', 'tabs-1');
	}
	$.cookie('oldURL', currentURL, {expires: 1});

	$('#product-select').click(function() { // bind click event to link
		$tabs.tabs('select',1); // switch to second tab
		return false;
	});
	
	$('#currentURL').html(' ' + $(location).attr('href') + ' ');
	
	$('.clear').before('<div class="clear"><!-- --></div>');
});


$(document).unload(function() {
	$.cookie('oldURL', null);
	var current = $(location).attr('href');
	//alert(current);
	$.cookie('oldURL', current, {expires: 1});
});
