var slideshowTimeout = null;

jQuery(document).ready(function(){
	
	// Products buttons tab effect
	jQuery('.product .more, .product .add-to-cart').hover(
		function() {
			jQuery(this).addClass('up');
		
			if (jQuery(this).hasClass('more'))
			{
				jQuery(this).parent().find('.add-to-cart').removeClass('up');
			} else {
				jQuery(this).parent().find('.more').removeClass('up');
			}
		}, null);
	
	// Menu hover handler
	jQuery("#menu-main li, #menu-main a").hover(
		function() {
			jQuery(this).addClass("hover");
		}, 
		function() {
			jQuery(this).removeClass("hover");
		}
	);
	
	/*
	// Menu hover handler
	jQuery("#menu-main li, #menu-main a").mouseover(
		function(){
			jQuery(this).addClass("hover");
	    }).mouseout(
	    function(){
	    	jQuery(this).removeClass("hover");
	    }
	);
 	*/
	
	slideshowTimeout = setTimeout("changeSlide(null, false)",5000);
	
	// Home slideshow
	jQuery('#slideshow .commands li').click(
		function() {
			if (jQuery(this).hasClass('active')) {
				
			} else {
				var thisIndex = jQuery('#slideshow .commands li').index(jQuery(this));
				changeSlide(thisIndex, true);
			}
		}
	)
	
	// External link
	jQuery('a[rel=external]').click(function(){
		this.target = "_blank";
	});
	
	
});

function changeSlide(slideIndex, clear) {
	if (clear === true) {
		clearTimeout(slideshowTimeout);
	}
	if (slideIndex == null) {
		var slides = jQuery('#slideshow .slides li');
		var slidesLength = slides.length;
		var activeIndex = slides.index(jQuery('#slideshow .slides li.active'));
		if (activeIndex == slidesLength-1) {
			slideIndex = 0;
		} else {
			slideIndex = activeIndex+1;
		}
	}
	
	var activeSlide = jQuery('#slideshow .slides .active');
	var newSlide = jQuery('#slideshow .slides li').eq(slideIndex);
	activeSlide.removeClass('active').css({left:0}).animate({left:'-100%'}, 500, function() { jQuery(this).css({left:'100%'}); });
	newSlide.addClass('active').css({left:'100%'}).animate({left:0}, 500);
	jQuery('#slideshow .commands .active').removeClass('active');
	jQuery('#slideshow .commands li').eq(slideIndex).addClass('active');
	
	slideshowTimeout = setTimeout("changeSlide(null, false)",5000);
}

// Horizontal slideshow
function createSlideshowHoriz(object, limit)
{
	var currentPosition = 0;
	var slideWidth = 160;
	var slides = jQuery(object+' .product');
	var numberOfSlides = slides.length;
	var limitRight = numberOfSlides - limit;
	
	if (numberOfSlides > 0)
	{

		// Hide left arrow control on first load
		manageControlsHoriz(object, limitRight, currentPosition);

		// Create event listeners for .controls clicks
		jQuery(object+' .control')
		.bind('click', function(){
			// Determine new position
			currentPosition = (jQuery(this).hasClass('right'))
			? currentPosition+1 : currentPosition-1;

			// Hide / show controls
			manageControlsHoriz(object, limitRight, currentPosition);

			// Move slideInner using margin-left
			jQuery(object+' .slideshow').animate({
				marginLeft : slideWidth*(-currentPosition)
			});
			return false;
		});
	} 
	else
	{
		jQuery(object+' .control.left').hide();
		jQuery(object+' .control.right').hide();
	}
}

//manageControls: Hides and shows controls depending on currentPosition
function manageControlsHoriz(object, limitRight, position){
	// Hide left arrow if position is first slide
	if(position==0){ jQuery(object+' .control.left').hide() }
	else{ jQuery(object+' .control.left').show() }
	// Hide right arrow if position is last slide
	if(position==limitRight){ jQuery(object+' .control.right').hide() }
	else{ jQuery(object+' .control.right').show() }
}

// Vertical slideshow
function createSlideshowVert(object, limit)
{
	var currentPosition = 0;
	var slideHeight = 185;
	var slides = jQuery(object+' .product');
	var numberOfSlides = slides.length;
	var limitBottom = numberOfSlides - limit;

	if (numberOfSlides > 0)
	{
		// Hide left arrow control on first load
		manageControlsVert(object, limitBottom, currentPosition);
	
		// Create event listeners for .controls clicks
		jQuery(object+' .control')
		.bind('click', function(){
			// Determine new position
			currentPosition = (jQuery(this).hasClass('down'))
			? currentPosition+1 : currentPosition-1;
	
			// Hide / show controls
			manageControlsVert(object, limitBottom, currentPosition);
	
			// Move slideInner using margin-left
			jQuery(object+' .slideshow').animate({
				marginTop : slideHeight*(-currentPosition)
			});
	
			return false;
		});
	}
	else
	{
		jQuery(object+' .control.up').hide();
		jQuery(object+' .control.down').hide();
	}
}

//manageControls: Hides and shows controls depending on currentPosition
function manageControlsVert(object, limitBottom, position){
	// Hide up arrow if position is first slide
	if(position==0){ jQuery(object+' .control.up').hide() }
	else{ jQuery(object+' .control.up').show() }
	// Hide down arrow if position is last slide
	if(position==limitBottom){ jQuery(object+' .control.down').hide() }
	else{ jQuery(object+' .control.down').show() }
}
