var ready = false;
var thumb_ready = false;
var image_ready = false;
$(document).ready(function(){
	$("#thumbnail-container-inner a").click(function(event) {
		event.preventDefault();
		$(".zoom-extent").hide();
		
		$("#photo-container-inner img").animate({top: 0, left: 0,width: $("#photo-container-inner").outerWidth(), height: $("#photo-container-inner").outerHeight()}, {duration: 0, queue: false});
		ready = false;
		$("#thumbnail-container-inner a").removeClass('active');
		$(this).addClass('active');
		var src_large = $(this).find("img").attr('src');
		var src_zoom = $(this).find("img").attr('src');
		src_large = src_large.replace('/86x57!/', '/2140x1384!/');
		src_zoom = src_zoom.replace('/86x57!/', '/197x127!/');
		thumb_ready = false;
		image_ready = false;
		$("#photo-container-inner img").hide();
		$(".zoom-box img").hide();
		$("#photo-container-inner img").attr("src",src_large);
		$(".zoom-box img").attr("src",src_zoom);
	});
	
	$("#photo-container-inner img").load(function() {
		$(this).fadeIn();
		image_ready = true;
	});
	
	$(".zoom-box img").load(function() {
		$(this).fadeIn();
		thumb_ready = true;
	});
	
	$("#thumbnail-container-inner a").eq(0).click();
	
	$(".zoom-box").hover( function (e) {
		if (thumb_ready && thumb_ready) {
			$(this).find(".zoom-extent").show();
			
			var offset = $(this).offset();
			var mouseX = e.pageX  - offset.left;
			var mouseY = e.pageY - offset.top;
			var boxW = $(this).find(".zoom-extent").outerWidth();
			var boxH = $(this).find(".zoom-extent").outerHeight();
			
			var posX = mouseX - (boxW * 0.5);
			var posY = mouseY - (boxH * 0.5);
			
			var ratio = $("#photo-container-inner").outerWidth() / $(this).outerWidth();
			
			//alert(posX);
			
			if (posX < 0) {
				posX = 0;
			} else if (posX + boxW > $(this).outerWidth()) {
				posX = $(this).outerWidth() - boxW;
			}
			
			if (posY < 0) {
				posY = 0;
			} else if (posY + boxH > $(this).outerHeight()) {
				posY = $(this).outerHeight() - boxH;
			}
			
			ready = false;
			
			$(this).find(".zoom-extent").css("left", posX + "px");
			$(this).find(".zoom-extent").css("top", posY + "px");
			
			$("#photo-container-inner img").animate({top: -(posY * 4 * ratio) + "px", left: -(posX * 4 * ratio),width: $("#photo-container-inner").outerWidth() * 4, height: $("#photo-container-inner").outerHeight() * 4}, {duration: 125, complete: function() {ready = true;}});
		}
		
	}, function () {
		$(this).find(".zoom-extent").hide();
		
		$("#photo-container-inner img").animate({top: 0, left: 0,width: $("#photo-container-inner").outerWidth(), height: $("#photo-container-inner").outerHeight()}, {duration: 250, queue: false});
		ready = false;
	});
	
	$(".zoom-box").mousemove( function (e) {
		if (thumb_ready && thumb_ready) {
			var offset = $(this).offset();
			var mouseX = e.pageX  - offset.left;
			var mouseY = e.pageY - offset.top;
			var boxW = $(this).find(".zoom-extent").outerWidth();
			var boxH = $(this).find(".zoom-extent").outerHeight();
			
			var posX = mouseX - (boxW * 0.5);
			var posY = mouseY - (boxH * 0.5);
			
			var ratio = $("#photo-container-inner").outerWidth() / $(this).outerWidth();
			
			//alert(posX);
			
			if (posX < 0) {
				posX = 0;
			} else if (posX + boxW > $(this).outerWidth()) {
				posX = $(this).outerWidth() - boxW;
			}
			
			if (posY < 0) {
				posY = 0;
			} else if (posY + boxH > $(this).outerHeight()) {
				posY = $(this).outerHeight() - boxH;
			}
			
			if (ready) {
				$(this).find(".zoom-extent").css("left", posX + "px");
				$(this).find(".zoom-extent").css("top", posY + "px");
				
				$("#photo-container-inner img").css("left", -(posX * 4 * ratio) + "px");
				$("#photo-container-inner img").css("top", -(posY * 4 * ratio) + "px");
				//$("#photo-container-inner img").animate({top: -(posY * 4 * ratio) + "px", left: -(posX * 4 * ratio)}, {duration: 250, queue: false});
			}	
		}
	});
	
	$(window).resize(function() {
		$("#wrapper").height("auto");
		$("#footer").css("position", "relative");
		var w = $(window).width();
		var h = ($(window).height() < $("#wrapper").height()) ? $("#wrapper").height() : $(window).height();
		
		var true_h = $("#wrapper").height();
		
		if (h > true_h) {
			$("#footer").css("position", "absolute");
		}

		w = (w > 1124) ? w : 1124;
		h = (h > 768) ? h : 768;
		
		$("#background").height(h);
		$("#background").width(w);
		
		$("#background .pattern").height(h);
		$("#background .pattern").width(w);
		
		$("#wrapper").width(w);
		$("#wrapper").height(h);
		
		$("#header").width(w);
		
		
		var img_w = $("#background img").width();
		var img_h = $("#background img").height();
		
		var ratio = img_w/img_h;
		
		
		if (img_w > img_h && ((w/ratio) >= h)) {
			$("#background img").width(w);
			$("#background img").height(w/ratio);
		} else {
			$("#background img").width(h * ratio);
			$("#background img").height(h);
		}
		
		/*
		$("#background img").css("position", "absolute");
		
		var l = $("#background").width() - $("#background img").width();
		
		$("#background img").css("left", (l < 0) ? l : 0);
		*/
	});
	
	$("#background img").load(function() {
		$(window).resize();
	});
	
	setTimeout(function() { $(window).resize(); }, 500);
	
	$('.image-rotator').cycle({ 
		fx:      'fade', 
		easing:  'easeInOutExpo', 
		delay:   -3000,
		speed: 3000
	});
	
	$("#warranty-page h4").each(function() {
		$(this).next(".item-text").hide();
	});
	
	$("#warranty-page h4").click(function() {
		$("#warranty-page h4").next(".item-text").hide();
		$(this).next(".item-text").show();
		$(window).resize();
	});
	
	$("#warranty-page h4").eq(0).click();

$(".gallery-item h6 a").vAlign();
	
});
