$(document).ready(function() {
	setupHoverImages();
	
	setupSublinks();
	
	highlightCurrentPageLinks();
	// $("#debug").html("test");
	//setupAccessoriesImages();
	//setupAccessoryTabs();
});
/*
function setupAccessoryTabs()
{
	$(".categories .accessory-tab").each(function()
	{
		$(this).click(function(){
			$(this).siblings("a").removeClass("selected");
			$(this).addClass("selected");
			
			var accessory = $(this).attr("id").split("-tab")[0];
			var accessoryView = $(this).parent(".categories").siblings("#"+accessory+"-view");
			accessoryView.siblings(".view").addClass("hidden");
			accessoryView.removeClass("hidden");
		});
	});
}

function setupAccessoriesImages()
{
	$(".thumbnailExpander").each(function(){
		var img = $(this).find("img:eq(0)");
		var imgInfo = img.attr("src").split(".");
		
		var xOffSet = parseInt(imgInfo[1]);
		var yOffSet = parseInt(imgInfo[2]);
		
		// Crop Images to thumbnail.
		img.css("left", -parseInt(xOffSet));
		img.css("top", -parseInt(yOffSet));
		
		
		var origLeft = parseInt($(this).css("left"));
		var origTop = parseInt($(this).css("top"));
		var origBottom = parseInt($(this).css("bottom"));
		$(this).hover(
			function(e){
				$(this).css("z-index", 2);
				
				var newLeft = origLeft;
				if($(this).hasClass("expandLeft"))
				{
					newLeft = newLeft - (317-130);
				}
				
				var animParams = {
					"top": 0,
					"left": newLeft,
					"width": 317,
					"height": 480
				};
				
				$(this).stop().animate(animParams, 100, "linear", function(){});
				
				$(this).find("img").stop().animate(
					{
						top:0,
						left:0
					},100, "linear", function(){});
			},
			function(e){
				var animParams = {
					"top": origTop,
					"left": origLeft,
					"width": 130,
					"height": 130
				};
				$(this).stop().animate(animParams, 100, "linear", function(){$(this).css("z-index", 0);});
			
				$(this).find("img").stop().animate(
					{
						top:-yOffSet,
						left:-xOffSet
					},100, "linear", function(){});
			})
		
	})
	
	
	
}
*/
function highlightCurrentPageLinks()
{
	$("a.hover").each(function()
	{
		var href = $(this).attr("href");
		if(location.href.indexOf(href) !== -1)
		{
			$(this).addClass("selected");
			imgHighlight($(this).children("img"));
		}
	})
}

function setupHoverImages()
{
	$("a.hover").hover(function(e)
	{
		imgHighlight($(this).children("img"));
	}, 
	function(e)
	{
		if(!$(this).hasClass("selected"))
		{
			imgDeHighlight($(this).children("img"));
		}
	})
}

function imgHighlight(img)
{
	var src = img.attr("src");
	
	if(src.indexOf("Hover") !== -1)
		return;
			
	var insertIndex = src.lastIndexOf(".");
	var newSrc = src.substring(0, insertIndex) + "Hover" + src.substring(insertIndex);
	img.attr("src", newSrc);
}
function imgDeHighlight(img)
{
	var src = img.attr("src");
	var newSrc = src.split("Hover").join("");
	img.attr("src", newSrc);
}

function setupSublinks()
{
	$(".hasSubLinks").mouseenter(function(e){$(this).siblings(".expand").addClass("visible");});
	$("#nav").mouseleave(function(){$(".expand").removeClass("visible")});
	$("#nav li").children("a").mouseenter(function()
	{
		$(this).parent().siblings("li").children(".hasSubLinks").siblings(".expand").removeClass("visible");
	})
	
	$("a.hasSubLinks").click(function(e)
	{
		e.preventDefault();
	})
	
	//TODO: REMOVE
	// $(".expand").hover(function(){
	// 	imgHighlight($("#collections-link").children("img:eq(0)"));
	// },
	// function(){
	// 	imgDeHighlight($("#collections-link").children("img:eq(0)"));
	// });
	
	$(".hasSubLinks").siblings(".expand").children(".sublinks").children("a").click(function(e){
		e.preventDefault();
		
		imgHighlight($(this).addClass("selected").children("img:eq(0)"));
		// $("#debug").html("clickk");
		$(this).siblings("a").each(function(){
			imgDeHighlight($(this).removeClass("selected").children("img:eq(0)"));
		});
		
		var href = $(this).attr("href");
				
		var splitUrl = window.location.href.split(".php");
		var pageName = splitUrl[0].substring(splitUrl[0].lastIndexOf("/")+1);
				
		var curLocation =  window.location.href;
		var index = curLocation.indexOf(".php");
		var pageName = curLocation.substring(curLocation.lastIndexOf("/")+1, index);
		if(index == -1 || href.indexOf(pageName) == -1)
		{
			document.location = href;
		}
		else
		{
			window.location.hash = href.split("#")[1];
			if(pageName == "collections")
			{
				refreshJacSlide();
			}
			else if(pageName == "accessories")
			{
				refreshThumbnailViewer();
			}
		}
	});
	
	$(".hasSubLinks").siblings(".expand").children(".sublinks").children("a").hover(function(e){
			var collection = $(this).attr("href").split("collection=")[1];
			$("#homePageCover").attr("src", "images/homePageCover/"+collection+".jpeg");
		},
		function(e)
		{
			$("#homePageCover").attr("src", "images/homePageCover/homepageImage.jpg");
		});
};

function refreshThumbnailViewer()
{
	var hash = window.location.hash.substring(1).split("&");
	var collection = "1";
	for(var i=0; i<hash.length; i++)
	{
		var keyValue = hash[i].split("=");
		if(keyValue[0] == "collection")
		{
			
			collection = keyValue[1];
			break;
		}
	}
	
	$(".thumbsHolder").hide();
	$("#collection"+collection).show();
}

function refreshJacSlide()
{
	var hash = window.location.hash.substring(1).split("&");
	var collection = "1";
	for(var i=0; i<hash.length; i++)
	{
		var keyValue = hash[i].split("=");
		if(keyValue[0] == "collection")
		{
			
			collection = keyValue[1];
			break;
		}
	}
	
/*	if(collection == "2")
	{
		$("#collection1").hide();
		$("#collection3").hide();
		$("#collection2").show();
	}
	else if(collection == "3")
	{
		$("#collection1").hide();
		$("#collection2").hide();
		$("#collection3").show();
	}
	else
	{
		$("#collection2").hide();
		$("#collection3").hide();
		$("#collection1").show();
	}
*/
//        $("div.jacSlide").hide();
//	$("#collection"+collection).show();

}

$(window).load(function() {
	$(".showOnLoad").removeClass("showOnLoad");
	initJacSlideShow();
	
	refreshJacSlide();
	refreshThumbnailViewer();
	
	$(".hideOnLoad").css("display", "none");
});

function initJacSlideShow()
{
	var o = {
		enableMouse: true,
		childSpacing: 20,
		childSlideSpeed: 2500,
		centerPosition: 259
	}
	$(".jacSlide").jac(o);
}

$(function(){
	jQuery('img.maxAtOrigImageSize').maxImage({
		horizontalOffset: 0, verticalOffset: 150, horizontalAlign: 'center', verticalAlign: 'left', maxAtOrigImageSize: true, afterResize: function(image){
			$('#nav').css('padding-top', (image.height() + 10) + 'px');
		}
	});
	/*Dialog popup */
	$("#dialog").dialog({ 
		autoOpen: false, title:"LEGAL STATEMENT", show: 'clip', hide: 'clip', modal: true, width: 650, height: 550
	});
	$('#dialog_link').click(function() {
		$("#dialog").dialog('open'); return false;
	});
});

