var $j = jQuery.noConflict();
var currentIndex = 1;   
// Use jQuery via $j(...)
$j(window).load(function(){
       
       initSlides();
       //initPanZoom();
       initThumbs();
       initSwatches();
});

function initSwatches(){

	var swatch = $j('.product_iteration');
	swatch.each(function(i){
	
		$j(this).click(function(e){
			e.preventDefault();
			$j('#product_thumbs a:eq(' + i +')').trigger('click');
			slideToIndex(i, i);
		});
	
	});

}

function initScrollers(){

	var left = $j("#left a");
	var right = $j("#right a");
	$j(left).hide();
	
	var thumbs = $j('#product_thumbs a');
	if(thumbs.length <= 3){
		$j(left).hide();
		$j(right).hide();
	}
	
	$j(left).click(function(e){
		
		e.preventDefault();
		
		thumbs.each(function(i){
			
			if($j(this).hasClass('onDisplay')){
				slideToIndex(i - 1, i-1);
				
				return false;
			}
		
		});
		
	});
	
	$j(right).click(function(e){
		
		e.preventDefault();
		
		thumbs.each(function(i){
			
			if($j(this).hasClass('onDisplay')){
				
				slideToIndex(i + 1, i+1);
				
				return false;
				
			}
		
		});
		
	});

}

function slideToIndex(index, currentIndex){

	var left = $j("#left a");
	var right = $j("#right a");
	
	var originalIndex = index;
	
	var thumbs = $j('#product_thumbs a');
	if(index +3 >= thumbs.length){
		index = thumbs.length - 3;
	}
	
	if(index == 0){
		$j(left).hide();
	} else if (thumbs.length > 3){
		$j(left).show();
	}
	
	
	
	if(index+3 == thumbs.length){
		$j(right).hide();
	}else if (thumbs.length > 3){
		$j(right).show();
	}
	
	

	var masker = $j("#masker");
	var slideBy = index * 92;
		
	
	$j(masker).animate(
		{
			left: "-" + slideBy + "px"
		},
		300
	
	);

	$j('#product_thumbs a').removeClass('onDisplay');
	var onDisplay = $j('#product_thumbs a:eq(' + originalIndex + ')').addClass('onDisplay');

}


function initThumbs(){

	var thumbs = $j('#product_thumbs a');
	thumbs.each(function(i){
		
		if(i == 0){
			$j("#zoom_control").attr("href", $j(this).attr('href'));
		}
	
		$j(this).click(function(e){
			e.preventDefault();
			$j("#product_shot").empty();
			$j(this).clone().addClass('panFull').prependTo("#main_image");
			$j("#zoom_control").attr("href", $j(this).attr('href'));
			$j("#product_price .price_info").removeClass('onDisplay');
			$j("#product_price .price_info:eq(" + i + ")").addClass('onDisplay');
			
			$j(".product_style").removeClass('onDisplay');
			$j(".product_style:eq(" + i + ")").addClass('onDisplay');
			
		});
		
		
	
	});
	initScrollers();
}

function initSlides(){

	var slides_hidden = $j("#slides img").not('.onDisplay').removeClass('hide').fadeTo(1,0);
	setInterval("crossFadeImages()", 3000);
}

function crossFadeImages() {

	var slides = $j("#slides img");
	
	
	if(slides.length > 1){
	
		slides.each(function(i, val){
			
			if(i == currentIndex){
				//alert(i);
				$j(this).fadeTo('slow', 1);
			} else {
				$j(this).fadeTo('slow', 0);
			}
			
		});
			
	}
	
	currentIndex++;
	
	if(currentIndex == slides.length){
		window.location.href = collection;
		currentIndex = 0;
	}	

}



