var oldX = false;
var oldY = false;
var viewType = "";
$(document).ready(function()
{

/***************************************************************************************************
*************************************************************************** outer.php: menu  *******
****************************************************************************************************/
$("#menu").css("left", $("#outer").offset().left);
$("#menu a.submenu_link").bind("click", function(event)
{
	event.preventDefault();
	var id = $(this).attr("id")+"_submenu";
	var $submenu = $("#"+id);
	
	if ($submenu.css("display") == "block")
	{
		$submenu.slideUp("fast");
	}
	else
	{
		$(".submenu:not(#"+id+")").slideUp("fast");
		$submenu.slideDown("fast");
	}
});
if (window.selectedCollection!==undefined)
{
	$("#menu_collections_submenu").show();
	$('#menu_collections_submenu a[href*="'+selectedCollection+'"]').addClass("selected");
}

LoadStorage();
viewType = Storage["CollectionViewType"];


if (window.overrViewType!==undefined)
	viewType = overrViewType;
	
$(".collection_link").each(function()
{
	var href = $(this).attr("href").split("/");
	var tmp = [];
	for (var k = 0; k < href.length; k++)
		if (href[k]!="")
			tmp[tmp.length] = href[k];
	href = tmp;
	if (href[href.length-1] == "View-All")
	{
		var tmp = [];
		for (var k = 0; k < href.length-1; k++)
			tmp[tmp.length] = href[k];
		href = tmp;
	}
	if (viewType!="")
		href[href.length] = viewType;
	href = "/"+href.join("/")+"/";
	$(this).attr("href", href);
});



/***************************************************************************************************
************************************************************************** outer.php: search *******
****************************************************************************************************/


$("#skusearch_submit").bind("click", skusearchSubmit);
$("#skusearch_input").bind("keyup", function(event)
{
	if (event.keyCode == 13) {event.preventDefault();skusearchSubmit();}
	if (event.keyCode == 27) {event.preventDefault();$(this).val("").blur()};
});

/***************************************************************************************************
************************************************************************** outer.php: footer *******
****************************************************************************************************/

var footerReplace = function()
{
	if ($(window).height() - $("#footer").offset().top > 30)
	{
		$("#footer").css("margin-top", ($(window).height() - $("#footer").offset().top-30)+"px");
	}
	else $("#footer").css("margin-top", 0);
}
if (window.resizeCallback !== undefined)
	resizeCallback();
footerReplace();
oldX = $(window).width();
oldY = $(window).height();
$(window).resize(function()
{
	if (window.resizeCallback !== undefined)
		resizeCallback();
	if ((oldX!=$(window).width()) || (oldY!=$(window).height()))
	{
		footerReplace();
		oldX = $(window).width();
		oldY = $(window).height();
	}
});
/***************************************************************************************************
************************************************************************** favorites sharing *******
****************************************************************************************************/	
$("#favorites_share").bind("click", function(event)
{
	event.preventDefault();
	if ($("#favshare_window").size() == 0)
	{
		var html = "<div id='favshare_window' class='escclose'>\
		<h1>Share your Favorite Styles:</h1>\
		<label for='favshare_emails'>Emails:<label>\
		<input type='text' id='favshare_emails' />\
		<label for='favshare_text'>Message:</label>\
		<textarea id='favshare_text'>Please take a look at my favorite styles at ValStefani.com:\n\
		"+Collection.share_link+"</textarea>\
		<div class='submit_outer'><input type='button' value='Send!' /></div>\
		<div class='closelink'><a href='#' id='favshare_close'>close</a></div>";
		$("body").append(html);
		$("#favshare_window .closelink a").bind("click", function(event)
		{
			event.preventDefault();
			$("#favshare_window").fadeOut("fast");
		});
	}
	$("#favshare_window").show().position({my:"center center", at:"center center", of:$(window), collision:"none"}).hide().fadeIn("fast");
});

/***************************************************************************************************
************************************************************************** escape handling *********
****************************************************************************************************/
$(document).bind("keydown", function(event)
{
	if (event.keyCode == 27)
	{
		$(".escclose:visible").fadeOut("fast");
	}
});




});
function skusearchSubmit(event)
{
	$.get("/stylesearch.php?action=search&value="+$("#skusearch_input").val(), {}, function(data)
	{
		if (data.found==1)
			window.location.href=data.url;
		else
		{
			$("#skusearch_input").val('');
			$("#notfound_helper").stop().fadeIn("fast", function()
			{
				setTimeout(function(){$("#notfound_helper").stop().fadeOut("fast");}, 1000);
			});
		}
	}, "json");
}
function ShowFavoritesNeedReg()
{
	if ($("#favorites_need_reg").size() == 0)
	{
		$("body").append("<div id='favorites_need_reg' class='escclose'>Please <a href='/Content/Login/'>sign in</a> or <a href='/Content/Login/'>register</a> to add styles to your Favorites.<div class='closelink'><a href='#' id='favorites_need_reg_close_link'>close</a></div>");
		$("#favorites_need_reg_close_link").bind("click", function(event){event.preventDefault();$("#favorites_need_reg").fadeOut("fast")});
	}
	$("#favorites_need_reg").show().position({my:"center center", at:"center center", of:$(window)}).hide().fadeIn("fast");
}


function RemoveFavorite(sku)
{
	if (!confirm("Remove Style #"+sku+" from your favorites?")) return;
	$.post("/misc.php?action=remove_favorite", {sku:sku}, function(data)
	{
		if (data.error) {alert(data.error);return;}
		else             window.location.reload();
	}, "json");
}





