openindex = -1;

/*function toggle(elem) {
	var liclass = elem.className;
	if (liclass == "closed") {
		elem.className = "open";
          
		/*var linkwidth = elem.clientWidth;
		var listwidth = elem.childNodes[3].clientWidth;
		var linum = elem.childNodes[3].childNodes.length;
		if(linkwidth > listwidth) {
			elem.childNodes[3].style.width = linkwidth - 2 + "px";
			for(var i=0;i<linum;i++) {
				if (elem.childNodes[3].childNodes[i].nodeName == "LI") {
					elem.childNodes[3].childNodes[i].style.width = linkwidth - 2 + "px";
				}
			}
		}
	} else {
		elem.className = "closed";
	}
}*/
function clearthis(elem,match) {
	if(elem.value == match) {
		elem.className = "headbar_search_box_focus";
		elem.value = "";
	}
}
function fillthis(elem,match) {
	if(elem.value == "") {
		elem.className = "headbar_search_box_blur";
		elem.value = match;
	}
}

function setupQuant() {
	document.getElementById("quant_changer").style.display = "none";
	document.getElementById("buy_amount").className = "default";
	updateQDisplay();
}

function swapQuantChange() {
	changingQuant = 1;
	lastquant = document.getElementById("quant_edit").value;
	document.getElementById("quant_default").style.display = "none";
	document.getElementById("quant_changer").style.display = "block";
	document.getElementById("buy_amount").className = "open";
	document.getElementById("quant_edit").focus();
	return false;
}
function quantChange(elem) {
	newquant = document.getElementById("quant_edit").value;
}
function applyQuantChange() {
	changingQuant = 0;
	updateQDisplay();
	document.getElementById("quant_default").style.display = "block";
	document.getElementById("quant_changer").style.display = "none";
	return false;
}
function cancelQuantChange() {
	document.getElementById("quant_edit").value = lastquant;
	updateQDisplay();
	document.getElementById("quant_default").style.display = "block";
	document.getElementById("quant_changer").style.display = "none";
	document.getElementById("buy_amount").className = "open";
	return false;
}
function testQuant() {
	if (changingQuant = 1) {
		applyQuantChange()
	}
}
function updateQDisplay() {
	var curquant = document.getElementById("quant_edit").value
	document.getElementById("buying_quant").innerHTML = curquant;
	if (curquant > 1) {
		document.getElementById("buying_plural").innerHTML = "s";
	} else {
		document.getElementById("buying_plural").innerHTML = "";
	}
}

(function(){
	var 
		foldingMenu = window.foldingMenu = function( eventype,method,elems ) {
			method = method.split("|");
			elems = elems.split(",");
			eTree = method[1].split(",");
			switch (method[0]) {
				case "parent":
					return new foldingMenu.initp( eventype,elems,eTree );
					break;
				case "sibling":
					return new foldingMenu.inits( eventype,elems,eTree );
					break;
				case "direct":
					return new foldingMenu.initd( eventype,elems,eTree );
					break;
				default:
					alert("It's borked.");
			}
		};

	foldingMenu = foldingMenu.prototype = {
		initp: function( eventype,elems,eTree ) {
			var actioner = " " + eTree[0];
			var submenu = " " + eTree[1];
			for (i=0;i<elems.length;i++) {
				$(elems[i] + actioner).each(function(e){
					if (!($(this).children(submenu).hasClass(".nowidth"))) {
						var thiswidth = $(this).width();
						var thischildwidth = $(this).children(submenu).width();
						if (thiswidth > thischildwidth) {
							$(this).children(submenu).css({width: thiswidth});
						} else {
							$(this).children(submenu).css({width: thischildwidth-2});
						}
						var thisfinalwidth = $(this).children(submenu).width();
						$(this).children(".submenu-inner").children("ul").children("li").children("a").css({width: thisfinalwidth-48});
						//$(this).children(".submenu").children(".menu_bottom").children(".bottom").css({width: thisfinalwidth-14});
					}
				});
				$(elems[i] + actioner + submenu).hide();
				$(elems[i] + " .open").each(function() {
					var tag_type = this.tagName;
					if (this.tag_type != "A") { $(this).show(); }
				});
				$(elems[i] + " .open").parents(submenu).show();
				$(elems[i] + actioner + submenu + ":visible").parent().children("a").addClass("sub_parent expanded");
				$(elems[i] + actioner + submenu + ":hidden").parent().children("a").addClass("sub_parent");
				switch (eventype) {
					case "click":
						$(elems[i] + actioner + submenu).parent().children("a").click(function() {
							$(this).next(submenu).toggle("fast");
							$(this).toggleClass("expanded").blur();
							return false;
						});
						break;
					case "hover":
						$(elems[i] + actioner + submenu).parent().hover(function() {
							onLink = 1;
							curSub = $(submenu).index($(this).children(submenu));
							$(this).siblings().removeClass("curHover");
							$(this).addClass("curHover");
							$(this).children(submenu).slideDown(1,	function(e){
																		if(onLink == 0) {
																			$(submenu + ":eq(" + curSub + ")").hide();
																		}
																		$(this).parent().siblings(":not(.curHover)").children(submenu).hide();
																	});
							$(this).children("a").toggleClass("expanded");
						}, function() {
							onLink = 0;
							$(this).removeClass("curHover");
							$(this).children(submenu).hide();
							$(this).children("a").toggleClass("expanded");
						});
						break;
				}
			}
		},

		inits: function( eventype,elems,eTree ) {
			var actioner = " " + eTree[0];
			var submenu = " " + eTree[1];
			for (i=0;i<elems.length;i++) {
				$(elems[i] + submenu).hide();
				$(elems[i] + " .open").each(function() {
					var tag_type = this.tagName;
					if (this.tag_type != "A") { $(this).show(); }
				});
				$(elems[i] + " .open").parents(submenu).show();
				$(elems[i] + submenu + ":visible").prev(actioner).children("a").addClass("sub_parent expanded");
				$(elems[i] + submenu).prev(actioner).children("a").addClass("sub_parent");
				switch (eventype) {
					case "click":
						$(elems[i] + submenu).prev(actioner).click(function() {
							$(this).next(submenu).toggle("fast");
							$(this).children("a").toggleClass("expanded").blur();
							return false;
						});
						break;
					case "hover":
						$(elems[i] + submenu).prev(actioner).hover(function() {
							$(this).next(submenu).show("fast");
							$(this).children("a").toggleClass("expanded");
						}, function() {
							$(this).next(submenu).hide();
							$(this).children("a").toggleClass("expanded");
						});
						break;
				}
			}
		},
		
		initd: function( eventype,elems,eTree ) {
			var expander = " " + eTree[0];
			var submenu = " " + eTree[1];
			
		}
	}
})();

function initCorners(elem) {
	$(elem + " ul ul a").append('<span class="corner tl"></span><span class="corner tr"></span><span class="corner bl"></span><span class="corner br"></span><span class="bar top"></span><span class="bar right"></span><span class="bar bottom"></span><span class="bar left"></span>');
	$(elem + " .menu_bottom").append('<span class="corner bl"></span><span class="bar bottom"></span><span class="corner br"></span>');
}

function seasonboxAnimate(elem) {
	previndex = openindex;
	openindex = $(elem).parent().children().index(elem);
	$(".season.open").removeClass("open");
	$(".season .prev:visible").fadeOut(200);
	$(".season .descrip").hide();
	$(elem).addClass("open");
	var divoffset = parseInt($(elem).css("left"));
	var imgoffset = parseInt($(elem).children("img").css("left"));
	if (previndex >= 0) {
		$(".season.open img").show().css({"left" : 0});
	} else {
		$(".season.open img").show().css({"left" : (divoffset + imgoffset)}).animate({"left" : -10},500);
	}
	$(".season.open").css({"width" : 784, "left" : 0},500);
	$(".season:not(.open)").each(function(openindex){
		var tempindex = $(this).parent().children().index(this);
		var parentwidth = $("#seasonbox_main").width();
		if (tempindex > openindex) {
			var animto = parentwidth-(30*(4-tempindex));
		} else {
			var animto = 30*tempindex;
		}
		var imgoffset = $(this).children("img").attr("rel");
		$(this).children("img").fadeOut(500).animate({"left" : imgoffset},200);
		$(this).animate({"width" : 30, "left" : animto},500);
	});
	$(elem).children(".descrip").fadeIn(500);
}

$(document).ready(function() {
	
	//initCorners("#nav-wrapper");
     
	foldingMenu("hover","parent|li,ul","#headermenu");
	foldingMenu("hover","parent|li,div.submenu-inner","#submenu");
	
	$(".hitgallery").click(function(){
		$("#firstfirst").click();
		return false;
	});
	$(".uselessbox").hide();
	$(".uselessmore").fadeIn(500);
	$(".uselessmore").click(function(){
		var getgrab = $(this).attr("rel");
		$("#uselessmore_" + getgrab).css({"padding" : "1px 0 0"}).slideDown(1000);
		$(this).hide();
		return false;
	});
	
	if ($(".superbox_container").length > 1) {
		$(".superbox_page").hide();
	}
	if (window.location.search.indexOf("od=1") >= 0) {
		$(".default_page").show();
	}
	$(".superbox_image").append("<p>click to enlarge photo</p>");
	$(".first .superbox_header .curve").html("<p class=\"expandhowto\">click a tab to expand</p>");
	$(".superbox_header").click(function(){
		if($(this).next().children(".superbox_page").is(":visible")) {
			$(this).next().children(".superbox_page").slideUp("medium");
		} else {
			$(this).next().children(".superbox_page").slideDown("medium");
		}
	});
	
	$("#hugebox .largeimage").hide();
	$(".thumb").hover(function(){
		$("#instructions:visible").fadeOut("fast");
		var getit = $(this).attr("id").replace("_t","");
		$("#hugebox .largeimage:visible:not(#" + getit + ")").fadeOut("fast");
		$("#hugebox .largeimage#" + getit).fadeIn("fast");
	});
	$(".largeimage").click(function(){
		var getit = $(this).attr("id") + "_t";
		var goTo = $("#" + getit).children("a").attr("href");
		window.location = goTo;
	});
	$("#loading").fadeOut(1000);
	
	$(".season img").each(function(){
		$(this).attr("rel",($(this).css("left")));
	});
	$(".season .descrip").hide();
	$(".season").mouseover(function(){
		if (!($(this).is(":animated"))) {
			if (!($(this).hasClass(".open"))) {
				seasonboxAnimate(this);
			}
		}
	});
	$(".season").mousemove(function(){
		if (!($(this).is(":animated"))) {
			if (!($(this).hasClass(".open"))) {
				seasonboxAnimate(this);
			}
		}
	});
 
 /*$("#bigger_closebutton").click(function(){
     $("#bigger_invitebox").toggle("slow");
     //$("#invite_button").slideToggle("slow");
     
     if($(this).css("border-bottom-color") == '#000'){
          $(this).css("border-bottom-color", "#FFF");
     }
     else{
          $(this).css("border-bottom-color", "#000");
     }
 });*/

});