var Popup = {
	show: function(content, className, title) {
		// if we don't have a background to bind to, create it
		if ($("body").find("#background").length === 0) {
			$("body").append("<div id='background'></div>");
		}
		
		var self = $("div#dialog");
		
		self.addClass(className);
		self.find("div.inner").html(content);
		self.find("div.inner").addClass("summary-dialog")
		
		var title = title ? title : "Description";
		
		self.find("h2").text(title);
		
		var width = self.width();  
		var height = self.height();  
		
		var top = $(window).height()/2 - (height/2);
		var left = $(window).width()/2 - (width/2);
	
		if (self.css("position") == "absolute") {
			top = top + $(window).scrollTop();
		}
		
		self.css({top:top, left: left, zIndex: 10002});
	
		self.fadeIn();
		
		$("#background").css("opacity", "0.5");
		$("#background").fadeIn();
		
		self.find("a.close").click(function() {
	    	self.fadeOut(function() {
	    		self.removeClass(className);
		    	self.find("div.inner").removeClass("summary-dialog");
	    	});
	    	$("#background").fadeOut();
			return false;
		});
		
	}
};

$(document).ready(function() {
	
	function getMatchingParts(parts, href){
		var match = [];
		for (var i=parts.length-1;i>=0; i--){
			match.splice(0,0,parts[i]);
			if (href.indexOf(match.join("/")) <= 0){
				return match.length -1;
			}
		}
		return match.length;
	}
	
	$("a.showMore").click ( function(){
		$(this).siblings("ul.more").show();		
		$(this).hide();
		return false;
		}
	);
	
	$("div.summary-text a").click(function(){
		var summary = $(this).parent("div.summary-text");
		//summary.hide();
		Popup.show(summary.parent().find("div.full-text").text(), "");
		return false;
	});
	
	
	$("a#show-attributes").click(function(){
		
		Popup.show($("div.expanded-attributes").html(), "show-attributes", "Full Specification");
		return false;

	
	});
	

	
	$("div.image").hover(function(){
		$(this).children("img.zoom").css("display", "block");
		},
		function(){
			$(this).children("img.zoom").css("display", "none");		
		});
	
	$("ul.popupImages li a").lightBox({
		fixedNavigation:true,
		imageLoading: $.mapPath('images/lightbox/lightbox-ico-loading.gif', $.getSite()),
		imageBtnPrev: $.mapPath('images/lightbox/lightbox-btn-prev.gif', $.getSite()),	
		imageBtnNext: $.mapPath('images/lightbox/lightbox-btn-next.gif', $.getSite()),
		imageBtnClose: $.mapPath('images/lightbox/lightbox-btn-close.gif', $.getSite()),
		imageBlank: $.mapPath('images/lightbox/lightbox-blank.gif', $.getSite())
	});
	
	$("div.product div.image img").click(function(){
		
		$("h5.flyout").removeClass("open"); 	
		$("div.flyout-container").hide().removeClass("open"); 	

		var url;
		if($(this).hasClass("zoom")){
			url = $("div.product div.image img.main-image").attr("src");
		}
		else {
			url = $(this).attr("src");
		}
		var parts = url.split("/");
		var popup = null;
		var mostMatches = 0;
		$("ul.popupImages li a").each(function(){
			var matching = getMatchingParts(parts, $(this).attr("href"));
			if (matching > mostMatches){
				popup = $(this);
				mostMatches = matching;
			}			
		});
		
		popup.click();
		return false;
		
	});

	
	
	if ($("div.inner ul.images").jcarousel != null){
		$("div.inner ul.images").jcarousel();
	}
	
	function bindProductImageLoader() {
		$("div.product div.image").append("<div class='loader-overlay'></div>");
	
		$("div.product ul.images li a").click(function() {
			$("div.product div.image img.main-image").imageLoad($(this).attr("href"), $("div.product div.image"));
			return false;
		});
	}
	
	bindProductImageLoader();
});
