document.write("<style type=\"text/css\"> .jqpreload img { display:none; } #category-list ul li { display:none; } #jewels-item { height:560; } </style>");

var currentpage = null;
var items = null;
var totalpages = null;

 	$(function(){
		
		if($(".artisan").length){
			var artisans = $(".artisan");
			artisans.each( function(){ 
				$(this).find("em").css("bottom","0px").fadeOut(0);
				$(this).hover( function(){
					$(this).find("em").fadeIn("fast");
				},
				function(){
					$(this).find("em").fadeOut("fast");
				});
			} );
		}
		
		/*
				Fade in Jewels BG image
		*/
		if($(".jqpreload").length){
			$(".jqpreload").each(function(){
				var image_source = $(this).find("img").attr("src");
				var img = new Image();
				var parent_element = $(this).attr("id");
				$(img).load(function(){ $("#" + parent_element + " img").fadeIn("slow");  }).attr("src",image_source);
			});
		}
		
		if($("#jewels-item").length){		
			$("#jewels-item").height(560);
			
			$("#itemlink").show();

			$("#prevlink").hover(function(){ 
				var thumbpos = ($("#item").css("top")=="0px") ? $("#prevlink").position().top-61 : $("#prevlink").position().top + $("#nextlink").height() + 5;
				$("#prevthumb").css({left:($("#prevlink").position().left)+"px", top:thumbpos + "px"}).fadeIn("fast"); 
			}, function(){ $("#prevthumb").fadeOut("fast"); });
			
			$("#nextlink").hover(function(){ 
				var thumbpos = ($("#item").css("top")=="0px") ? $("#nextlink").position().top-61 : $("#nextlink").position().top + $("#nextlink").height() + 5;
				$("#nextthumb").css({left:($("#nextlink").position().left+$("#nextlink").width()-65)+"px", top:thumbpos + "px"}).fadeIn("fast");
			}, function(){ $("#nextthumb").fadeOut("fast"); });
			
			$("#iteminfo, #itemmainimage").css("cursor","pointer");
			$("#iteminfo, #itemmainimage").hover(function(){ $("#itemlink").css("color","#666"); }, function(){ $("#itemlink").css("color","#999"); });
			$("#iteminfo, #itemmainimage").click(function(){
				if($("#item").css("top")=="-481px"){
					$("#itemlink").html("Details &amp; More Images &raquo;");
					$("#item").animate({top:"0px"},1000);
				}
				else if($("#item").css("top")=="0px"){
					$("#itemlink").html("&laquo; Return to Main Item View");
					$("#item").animate({top:"-481px"},1000);
				}
			});
			
			$("#item-medium").css("cursor","crosshair").hover(function(){ $("#item-description").hide(); $("#item-zoom").fadeIn("fast"); $("#zoom-tool-box").fadeTo("fast",.5); },function(){ $("#item-zoom,#zoom-tool-box").hide(); $("#item-description").fadeIn("fast"); });
			
			$("#item-medium").mousemove(function(e){
				var offset = $("#item-medium").offset();

				var xpos = e.pageX-offset.left-32;
				if(xpos > 300-69) xpos = 300-69;
				if(xpos < 0) xpos = 0;
				
				var ypos = e.pageY-offset.top-25;
				if(ypos > 180-55) ypos = 180-55;
				if(ypos < 0) ypos = 0;
				
				$("#zoom-tool-box").css({left:xpos+"px",top:ypos+"px"});
				$("#item-zoom img").css({left:(((xpos+1)/300) * -1600)+"px",top:(((ypos+1)/180) * -960)+"px"});
    	});
			
			$("#itemdetails ul li:not(.selected) img").fadeTo("fast",.40);
			$("#itemdetails ul li").hover(function(){
				if(!$(this).hasClass("selected")){
					$(this).find("img").fadeTo("fast",1);
				}
			},
			function(){
				if(!$(this).hasClass("selected")){
					$(this).find("img").fadeTo("fast",.4);
				}
			});
			$("#itemdetails ul li").click(function(e){
				e.preventDefault();
				if(!$(this).hasClass("selected")){
					$("#itemdetails ul li.selected").removeClass("selected").find("img").fadeTo("fast",.4);
					$(this).addClass("selected");
					changeSelectedImage($(this).find("a").attr("rel"));
				}
			});
			
			$("#zoom-tool-icon").show();
//			$("#iteminfo").click();
		}
			
		/*
			Build category item list
		*/
		if($("#category-list ul li").length != -1){
			$("#category-list ul").height(483);

			items = $("#category-list ul li");
			$(items).hide();
		
			$(items).each(function(){
				var img = $(this).find("img").clone();

				var a = $(this).find("a");

				var linktext = $(a).text();
				$(a).empty().text(linktext).wrap('<div></div>');
				
				$(this).prepend(img);
			});
			
			var match = self.location.href.match(/#\/page\/([0-9]+)\/$/);
			currentpage = (match) ? Number(match[1]) : 1;	

			if(items.length % 9){
				var newlength = items.length;
				for(var i = 0; i < (9 - items.length % 9); i++){
					newlength ++;
					var li = $("<li class=\"blank\">&nbsp;</li>").hide();
					if(newlength%3 == 0) $(li).addClass("third");
					$("#category-list ul").append(li);
				}
				items = $("#category-list ul li");
			}

			if(items.length > 9){
				$("#category-prevnext-nav").show();
				$("#category-prevnext-nav #category-next").click(nextPage);
				$("#category-prevnext-nav #category-previous").click(previousPage);
			
				totalpages = Math.floor(items.length / 9);
				if( items.length % 9 ) totalpages ++;
			}
		
			$(items).height(160);
		
			showPage();

			$("#category-list ul li:not('.blank')").hover(function(){
				$(this).css({'z-index' : '1000'}); /*Add a higher z-index value so this image stays on top*/ 
				$(this).find('img').css({"border" : "thin black solid"});
			    $(this).find('img').addClass("hover").stop() /* Add class of "hover", then stop animation queue buildup*/
				.animate({
					width: '253.75px', /* Set new width */
					height: '200px', /* Set new height */
					marginTop: '-25px', /* The next 4 lines will vertically align this image */ 
					marginLeft: '-25px',
				}, 200); /* this value of "200" is the speed of how fast/slow this hover animates */
				var div = $(this).find("div");
				$(this).find("div").css({'z-index' : '1001'}); /* Set z-index back to 0 */
				$(this).find("div").css({'color' : 'red'}); /* Set z-index back to 0 */
				div.fadeIn("fast");
				window.status = $(div).text();
			},
			function(){
				$(this).css({'z-index' : '0'}); /* Set z-index back to 0 */
				$(this).find('img').css({"border" : "none"});
				$(this).find('img').removeClass("hover").stop()  /* Remove the "hover" class , then stop animation queue buildup*/
					.animate({
						width: '203px', /* Set width back to default */
						height: '160px', /* Set height back to default */
						marginTop: '0', /* Set alignment back to default */
						marginLeft: '0',
					}, 400);
				$(this).find("div").fadeOut("fast");
				$(this).find("div").css({'z-index' : '0'}); /* Set z-index back to 0 */
				window.status = "";
			});
			$("#category-list ul li:not('.blank')").click(function(e){
				e.preventDefault();
				window.location = $(this).find("a").attr("href");
			});
		}
	});
	
	function changeSelectedImage(changeTo){
		var med = "/uploads/images/"+changeTo+"-medium.jpg";
		var full = "/uploads/images/"+changeTo+"-full.jpg";
		
		$("#item-medium-image img").hide().attr("src",med).fadeIn("fast");
		$("#item-zoom img").attr("src",full);
	}
	
	function nextPage(){
		if(currentpage < totalpages) showPage( currentpage + 1 );
	}
	function previousPage(){
		if(currentpage > 1) showPage( currentpage - 1);
	}
	function showPage(page){
		$("#category-prevnext-nav #category-previous a, #category-prevnext-nav #category-next a, #category-prevnext-nav #category-filter").hide();
		if(page){
			hidePage();
			currentpage=page;
		}

		itemOnStart = (currentpage-1) * 9;
		
		showItems();		
	}
	function hidePage(){
		$("#category-list ul li").hide();
	}
	
	var itemOnStart = null;
	var itemOnCount = null;
	
	function showItems(){
		if(!itemOnCount) itemOnCount = itemOnStart;

		if(itemOnCount < itemOnStart + 9){
			$(items[itemOnCount]).fadeIn(300, function(){ itemOnCount ++; showItems(); });
    }
		else{
			itemOnCount = null;
			showNextPrev();
		}
	}
	
	function showNextPrev(){
		if(currentpage < totalpages) $("#category-prevnext-nav #category-next a").attr("href","#/page/" + (currentpage + 1) + "/").fadeIn("fast");
		if(currentpage > 1) $("#category-prevnext-nav #category-previous a").attr("href","#/page/" + (currentpage - 1) + "/").fadeIn("fast");	
		if($("#category-filter").length) $("#category-filter, #category-filter a").fadeIn("fast");
	}
	
function debug(msg){
	if (window.console && window.console.log)
      window.console.log(msg);	
}
