// JavaScript Document

$(document).ready(function () {
	
	$("#breadcrumbs li:last").addClass("last");
	
	$("div.highlights ul li a").hover(function () {
		$(this).addClass("hover");
	}, function () {
		$(this).removeClass("hover");
	});
	
	$("div.category-pic[pic]").each(function () {
		$(this).css("background-image","url(images/"+$(this).attr("pic") + ")");
	});
	
	$("div.row ul.sub-row:eq(0)").css("border-top","none");
	
	$("div.simple_overlay").append("<div class=\"close\">&nbsp;&nbsp;&nbsp;&nbsp;</div>");
	
	$("a.click-for-larger[rel]").each(function () {
		
		var w = parseInt($(this).attr("thumbwidth"));
		var h = parseInt($(this).attr("thumbheight"));
		var thumb = $(this).attr("thumbsrc");
		$(this).css().attr("href","javascript:;").overlay({
			expose: '#ccc',
			effect: 'apple'
		});
	});
	
	$(".click-for-larger").click(function(){

		s.events=s.linkTrackEvents="event13"; //tells the code which links to send with the click event

		s.linkTrackVars="events,products"; //tells the code which variables to send with the click event

		s.tl(true,"o","Click for Larger"); //function that sends the click data to SiteCatalyst

		});
	
	// LOGIN
	
	$("#login-btn").click(function() {
		$("form#sign-in").html("Hello <strong>John Smith</strong>. <a href='javascript:;' style='font-size: 10px;text-decoration: underline;'>That's not me.</a>");
	});
	/*
	$("a.movie-clip[rel]").attr("href","javascript:;").overlay({
		expose: '#ccc',
		effect: 'apple'
	});
	*/
	
	purchaseTable = new PurchaseTable("purchase-table");
	
	$("input.sub-total").focus(function () {
		if ($(this).attr("disabled") != "disabled")
		{
			$(this).attr("disabled","disabled");
			$(this).parents("tr:eq(0)").next().fadeIn().find("input:eq(0)").focus();
			$(this).parent().attr("rowspan","2");
			$("#tooltip").hide();
		}
	}).hover(function () {
		var x = $(this).left() - 60;
		var y = $(this).top() - 25;
		$("#tooltip").css({
			left: x+"px",
			top: y+"px"
		}).show();
	}, function () {
		$("#tooltip").hide();
	});
	
	// Adds the class "left-nav-item" to the a tags of immediate children only
	$("#left-navigation > li.menu > a").addClass("left-nav-item");
	
	// Sub menu open and close control
	$("#left-navigation > li.menu").hover(function () {
		$(this).children(".sub-menu").show();
		$(this).addClass("hover");
	}, function () {
		$(this).children(".sub-menu").hide();
		$(this).removeClass("hover");
	});
	
	// Sub menu a tag hover control
	$("#left-navigation div.sub-menu a").hover(function () {
		$(this).addClass("hover");
	}, function () {
		$(this).removeClass("hover");
	});
	
	$("div[img]").each(function () {
		var img = $(this).attr("img");
		$(this).css("background-image","url(" + img + ")").addClass("img");
	});
	
	$("ul.segmented[vbarcolor]").addClass("v-bar").each(function () {
		var color = $(this).attr("vbarcolor");
		$(this).children(":gt(0)").css("border-left-color",color);
	});
	
	$("ul.segmented[hbarcolor]").each(function () {
		var color = $(this).attr("hbarcolor");
		$(this).css({"border-top":"solid 1px "+color});
	});
	
	$("ul.left-nav-item-ul li").each(function () {
		if ($(this).children("ul").length > 0)
		{
			$(this).addClass("has-menu").hover(
				function () {
					$(this).children("ul").show();
					$(this).children("a").addClass("hover");
				},
				function () {
					$(this).children("ul").hide();
					$(this).children("a").removeClass("hover");
				}
			).children("a").addClass("has-menu");
		}
	}).hover(
		function () { $(this).addClass("hover").children("a").addClass("hover"); },
		function () { $(this).removeClass("hover").children("a").removeClass("hover"); }
	);
	
	$("ul.left-nav-item-ul ul.sub-category").each(function () {
//		$(this).css("left",$(this).parent().eWidth() + "px");
	});
	
	$("div.left-nav-dropdown").hover(
		function () {
			$(this).addClass("hover").children("ul:eq(0)").slideDown("fast");
		},
		function () {
			$(this).removeClass("hover").children("ul:eq(0)").slideUp("fast");
		}
	).each(function () {
		var h = $(this).children("div:eq(0)").eHeight();
		$(this).children("ul:eq(0)").css("top",h+"px");
	});
	
	$("div.left-nav-dropdown ul li, ").hover(function () {
		$(this).addClass("hover");
	}, function () {
		$(this).removeClass("hover");
	});
	
	$("div[img]").each(function () {
		var img = $(this).attr("img");
		$(this).css("background-image","url(" + img + ")").addClass("img");
	});
	
	// Sub menu a tag hover control
	$("#left-navigation div.sub-menu a").hover(
		function () {
			$(this).addClass("hover");
		}, function () {
			$(this).removeClass("hover");
		}
	);
	
		/* keep this last */
	$("ul.segmented").each(function () {
		
		$(this).children(":eq(0)").addClass("first");
		
		var parentWidth = $(this).eWidth();
		var len = $(this).children().length;
		var customChildren = 0;
		$(this).children("[customwidth]").each(function () {
			var cWidth = $(this).attr("customwidth");
			if (cWidth.indexOf("%") > -1)
				cWidth = parentWidth * (parseInt(cWidth) / 100);
			$(this).css("width",cWidth+"px");
			customChildren++;
		});
		var ccLen = (len - customChildren);
		ccLen = (ccLen == 0) ? 1 : ccLen;
		var width = Math.ceil(parentWidth / ccLen);
		$(this).children(":not([customwidth])").css("width",width+"px");
		var firstChild =  $(this).children(":eq(0)");
		var lastChild =  $(this).children(":eq(" + (len-1) + ")");
		
		while (firstChild.top() != lastChild.top() && width > 10)
			$(this).children(":not([customwidth])").css("width",(--width)+"px");
		
		var maxHeight = 0;
		$(this).children().each(function () {
			var h = $(this).height();
			maxHeight = Math.max(maxHeight,h);
		}).css("height",maxHeight+"px");
		
		$(this).css("height",$(this).children(":eq(0)").eHeight() + "px");
	});
	
});

// Finding the x and y position of an element
// Finding the true eight of an element
(function($) {
$.fn.top = function ()		{ return findPos($(this)[0]).y; }
$.fn.left = function ()		{ return findPos($(this)[0]).x; }
$.fn.eWidth = function () {
	return $(this).width()
	+ parseInt($(this).css("padding-left")) || 0
	+ parseInt($(this).css("padding-right")) || 0
	+ parseInt($(this).css("border-left-width")) || 0
	+ parseInt($(this).css("border-right-width")) || 0;
}
$.fn.eHeight = function ()	{
	return $(this).height()
	+ parseInt($(this).css("padding-top")) || 0
	+ parseInt($(this).css("padding-bottom")) || 0
	+ parseInt($(this).css("border-top-width")) || 0
	+ parseInt($(this).css("border-bottom-width")) || 0;
}
})(jQuery);

function findPos (obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return {x:curleft, y:curtop};
}

var purchaseTable;
PurchaseTable = function (id)
{
	var me = this;
	
	var table = $(id);
	
	if (table.length == 0)
		return false;
	
	var chooserRows = [];
	
	$("#" + id + " tr.color-chooser").each(function (i) {
		chooserRows.push(new ColorChooserRow($(this), i));
	});
	
	$("#" + id + " input.sub-total").change(function () {
		$(this).parents("tr:eq(0)").next().find("input:eq(0)").val($(this).val());
	});
	
	this.hideRowIfUnfocusedAndZeroSum = function (index) {
		var r = chooserRows[index];
		if (r.total() == 0 && r.hasFocus() == false)
		{
			r.jq.fadeOut();
			r.jq.prev().children("td:last-child").removeAttr("rowspan");
			r.jq.prev().find("input").removeAttr("disabled");
		}
	}
}

ColorChooserRow = function (obj, index)
{
	var me = this;
	
	var inputs = obj.find("input");
	
	this.jq = obj;
	
	this.total = function ()
	{
		var sum = 0;
		inputs.each(function () {
			sum += parseInt($(this).val());
		});
		return sum;
	}
	
	inputs.focus(function () {
		me.hasFocus(true);
	}).blur(function () {
		me.hasFocus(false);
		// setTimeout allows for the code to let go of the process long enough for the next tabbed element to claim focus.
		setTimeout("purchaseTable.hideRowIfUnfocusedAndZeroSum(" + index + ")", 100);
	}).change(function () {
		obj.prev().find("input:eq(0)").val(me.total());
	});
	
	var hasFocus = false;
	this.hasFocus = function (bool)
	{
		if (arguments.length == 1)	{ hasFocus = bool; return me; }
		return hasFocus;
	}
}


/**
	CUSTOM SELECT BOX CODE
---------------------------------------*/

// JavaScript Document

$(document).ready(function () {
	
	
	$("body").append('<div class="select-dropsheet"></div>');
	selectDropsheet = $("body > div.select-dropsheet");
	
	selectDropsheet.click(function () {
		currentSelectBox.close();
	});
	
	$("ul.select").each(function () {
		new SelectBox($(this));
	});
	
	$("ul.select li").click(function() {
		location.href = $(this).attr("goto");
	});
	
});

var selectDropsheet;
var currentSelectBox;

SelectBox = function (jqObj)
{
	var me = this;
	
	jqObj.wrap('<div class="select-wrapper"></div>');
	
	jqObj.before('<div class="select-value"></div>').after('<div class="select-checkmark"></div>');
	
	var cb = jqObj.next();
	var valObj = jqObj.prev();
	
	var topOffset = 0;
	
	this.length = function () { return jqObj.children().length; }
	
	var opened = false;
	
	jqObj.parent().click(function () {
		if (!opened)
		{
			$(this).addClass("focused");
			jqObj.css("top",topOffset+"px").show();
			cb.show();
			selectDropsheet.show();
			opened = true;
			currentSelectBox = me;
		}
	});
		
	valObj.css("width",(jqObj.width() - 30) + "px");
	
	jqObj.css("width",(jqObj.width() + parseInt(jqObj.parent().css("padding-right"))) + "px");
	
	jqObj.children().each(function () {
		var icn = $(this).attr("icn");
		var extensionIndex = icn.lastIndexOf(".");
		
		icnFull = icn.substring(0, extensionIndex);
		
		if (jQuery.browser.msie && jQuery.browser.version < 7) {
 		   icnFull += ".gif";
		} else {
			icnFull += ".png";
		}
		
		if (icn != undefined)
			$(this).addClass("icn").prepend('<div class="icn" style="background-image: url(' + icnFull + ');"></div>');
	});
	
	jqObj.children().hover(
		function () {
			$(this).addClass("hover");
			if ($(this).prevAll().length == selectedIndex)
				cb.addClass("hover");
			else
				cb.removeClass("hover");
		},
		function () { $(this).removeClass("hover"); cb.removeClass("hover"); }
	).click(function () {
		me.selectedIndex($(this).prevAll().length);
	});
	
	this.selectedIndex = function (index)
	{
		if (index > -2 && index < me.length())
		{
			selectedIndex = index;
			if (selectedIndex > -1)
			{
				topOffset = 0;
				for (var i=0; i<index; i++)
					topOffset -= $(jqObj.children())[i].offsetHeight;
				var pTop = $(jqObj.parent())[0].offsetTop;
				cb.css("top",(12 - Math.min(0, topOffset + pTop))+"px").addClass("hover");
				topOffset -= Math.min(0, topOffset + pTop);
				valObj.html(jqObj.children("li:eq("+index+")").html());
				if (jqObj.children("li:eq("+index+")").hasClass("icn"))
					valObj.addClass("icn");
				else
					valObj.removeClass("icn");
			} else {
				cb.hide();
				valObj.html("");
			}
			jqObj.fadeOut("fast", function () { opened = false; jqObj.parent().removeClass("focused"); });
			cb.fadeOut("fast");
			selectDropsheet.hide();
		}
	}
	
	this.close = function ()
	{
		cb.hide();
		jqObj.hide();
		selectDropsheet.hide();
		currentSelectBox = null;
		opened = false;
		jqObj.parent().removeClass("focused");
	}
	
	var selectedLi = jqObj.children("li[selected=selected]");
	if (selectedLi)
		me.selectedIndex(selectedLi.prevAll().length);
	
}

function stringFilter(input) {
	s = input.value.toLowerCase();
	filteredValues = " abcdefghijklmnopqrstuvwxyz,.!@#$%^&*()-_=+;:`~{}[]<>?/"; // Characters stripped out
	var i;
	var returnString = "";
	for (i = 0; i < s.length; i++) { // Search through string and append to unfiltered values to returnString.
		var c = s.charAt(i);
		if (filteredValues.indexOf(c) == -1)
			returnString += c;
	}
	input.value = returnString;
}

socheck = function	()	{}
