var results;

$(document).ready(function(){
	var insetHover = false;
				
	//Add the corner images to any zoompan or saved design containers. Add the loading gif to any containers with an inset image.
	$('.zoomPan').prepend("<img src=\"/images/corners.png\" class=\"corners\" />");
	$('.savedDesign').prepend("<img src=\"/images/corners.png\" class=\"corners\" />");	
	$('.category').prepend("<img src=\"/images/corners.png\" class=\"corners\" />");
	//$('.inset').parent().prepend("<img src=\"/images/loading.gif\" class=\"load\" />");
	
	//Hide the loading gif until needed
	//$('img.load').hide();
	
	//Don't zoom in on the when hovering over the inset image. If we're currently zoomed in and hover over the inset - zoom back out.
	$('.inset').hover(function() {
		insetHover = true;
		if ($(this).parent().hasClass('hover')) {
			$(this).parent().children('.largeActive').stop().fadeTo(200, 0);
		}
	},function(){
		insetHover = false;
		if ($(this).parent().hasClass('hover')) {
			$(this).parent().children('.largeActive').stop().fadeTo(500, 1.0);
		}
	});
					
	//change source of product images when rotating locations
	$('.inset').click(function(e) {
		var id = $(this).parent().attr('id');
		
		if (results[id].currLoc < results[id].locations) {
		    results[id].currLoc++;
		} else {
		    results[id].currLoc = 1;
		}
		
		if ((results[id].currLoc + 1) <= results[id].locations) {
			$(this).attr('src', results[id]['loc' + (results[id].currLoc + 1) + 'is']);
		} else {
			$(this).attr('src', results[id]['loc1is']);
		}
		
		$(this).parent().children().children('.largeActive').attr('src', 
		    	results[id]['loc' + results[id].currLoc + 'zm']);
		$(this).parent().children().children('.smallActive').attr('src', 
		    	results[id]['loc' + results[id].currLoc + 'pr']);
		
		return false;
	});
				
	//fade to the large image
	$('.zoomPan').hover(function() {
		$(this).addClass('hover');
		if(insetHover != true) {
			$(this).children('.largeActive').css({opacity: "0"});
			$(this).children('.largeActive').stop().fadeTo(500, 1.0);
		}
	},function(){
		$(this).children('.largeActive').stop();
		$(this).removeClass('hover');
		$(this).children('.largeActive').stop().fadeTo(400, 0);
	});
	
	//pan the image
	$('.zoomPan').mousemove(function(e) {
		var divWidth = $(this).width();
		var divHeight = $(this).height();
		var imgWidth = $(this).children('.largeActive').width();
		var imgHeight = $(this).children('.largeActive').height();
		var divOffset = $(this).offset();
		var leftPan = (e.pageX - divOffset.left) * (divWidth - (imgWidth)) / (divWidth+1*2);
		var topPan = (e.pageY - divOffset.top) * (divHeight - (imgHeight)) / (divHeight+1*2);
		$(this).children('.largeActive').css({position: "absolute", left: leftPan, top: topPan});
	});
	
	$(".goToPage").change(function() {
		var targetUrl = "/search/browse.action";
		var page = parseInt($(this).val());
		var findCid = new RegExp("[?&]cid(?:=([^&]*))?","i");
		var cid = findCid.exec(window.location.search);
			
		if(cid != null){
			targetUrl += cid[0];
		}
	
		if (page > 0) {
			targetUrl += "&page=" + page;
		}
			
		window.location = targetUrl;
	});

});
