


jQuery.boss = {
	winSize : function() {
		var de = document.documentElement;
		var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
		var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
		return [w,h];
	},
	jax : function(url, callback, callbackno) {
		var req;
		if(window.XMLHttpRequest){
			req = new XMLHttpRequest();
		}else if (window.ActiveXObject){
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}else{req = null;}
		var processRequest = function(){
			if (req.readyState == 4) {
				if (req.status == 200) {
					if (callback){
						if(callbackno){
							//callback(req.responseText);
							callback();
						}else{
							callback(req.responseXML.getElementsByTagName('bosssitexml')[0]);
						}
					}
				}else{
					if (callback && callbackno){callback(true);}
				}
			}
		};
		
		req.onreadystatechange = processRequest;
		try{
			req.open("GET", url, true);
			req.send(null);
		}
		catch(e){}
	},
	formatCurrency : function(num) {
		num = num.toString().replace(/\$|\,/g,'');
		if(isNaN(num)){num = "0";}
		var sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		var cents = num%100;
		num = Math.floor(num/100).toString();
		if(cents<10){cents = "0" + cents;}
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++){num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));}
		return (((sign)?'':'-') + '$' + num + '.' + cents);
	},
	toFloat : function(st){return (st.replace("$","").replace(",","","gi"))*1;}

};

var newWindow = null;
var b = "Boss";
var bd = "."+b;
var bh = "#"+b;
var BPC = "#BossPopCountries";
var BTC = b+"TextareaCounter";
var aImg = "/boss/resources/images/liw.gif";
var al;
$(function(){
	al = new Image();// preload image
	al.src = aImg;
	if(!document.createElement)return;
	if(!(typeof OnPageLoad != 'function')) OnPageLoad();
	if($("body").attr("class") == b+"PopImage")$("body").children("img:first").load(function(){BossWinResize($("body").get(0))});
	$(bd+"Slide").css("display","none");
	$("textarea.BossLimitedTextarea").keydown(function(){TextCounter(this,"",250);return true;}).keyup(function(){TextCounter(this,"",250);return true;});
	$(BPC).change(function(){PopCountries(false);return false;}).load(function(){PopCountries(true);});
	$("#paymentType").load(function(){SetForPayment(this);});
	$(bh+"CartCountries").focus(function(){$("#BossCartForm").submit();});
	$(bd+"ShipSelect,#giftWrapState").click(function(){CalcShipCosts(this);});
	CalcShipCosts($(bd+"ShipSelect:first").get(0));
	CalcShipCosts($("#giftWrapState").get(0),true);
	$("select").each(function(){
		if(this.name.indexOf("attSelect") > -1){
			if(this.id.indexOf("True") > -1 && $("#itemQuantity").get(0)){
				$(this).change(function(){PopQuant(this.value,true);BossOptionImages(this);});
				PopQuant(this.value,false);
			}else{
				$(this).change(function(){BossOptionImages(this);});
	}}});
	$(bd+"Toggle").click(function(){
		var id = $(this).attr("id");
		$("#" + $(this).attr("id").replace("a-","")).slideToggle();
		$(bd+"Toggle").each(function (){
			if(id != $(this).attr("id"))$("#" + $(this).attr("id").replace("a-","")).slideUp();
	})});
	var h = 0;
	$(bd+"Album").find("img").each(function (){if($(this).height() > h)h = $(this).height();});
	var t = bd+"Thumb";
	if($(t).length > 0)h+=($(t).css("padding-top").replace("px","")*1+$(t).css("padding-bottom").replace("px","")*1);
	$(bd+"Album > "+t).height(h);

	$("#sameAddress").change(function(){
		var t = $("#BossCreateUserForm > .BossRightColumn");
		if($(this).get(0).checked){
			t.fadeOut();
		}else{
			t.fadeIn();
		}
	});
	$("#BossVariations").before('<select id="BossVariations-tmp" name="variation" class="BossInput" />').find("li > a").each(function(){var selected = "";
    if ($(this).hasClass('selected')) selected = ' class="selected"';
     $("#BossVariations-tmp").append('<option' + selected + ' value="' + $(this).attr("href") + '">' + $(this).text() + '</option>');
    }).end().remove();
    $('#BossVariations-tmp').attr("id", "BossVariations").change(function () {window.location = $("option:selected", this).val();});
    $('#BossVariations option:first,#BossVariations option.selected').attr('selected','selected');
});


var boc = [];//var bir = [];
var BossOptionImages = function(theSel){
var j = 0;
var s = b+"ImageNotFound";
$(bd+"Image").find("img").each(function(){
	var o = this.src;var oi = new Image();
	if(o.indexOf(aImg) < 0){
		$("select").each(function(){
			if(this.name.indexOf("attSelect") > -1)for(var j = 0;j < this.length;j++){if(theSel.options[j])o = o.toLowerCase().replace('-'+AlphaNum(theSel.options[j].text).toLowerCase()+'.','.');}
		});
		var ni = FileNameSwitch(o,"-"+AlphaNum(theSel.options[theSel.selectedIndex].text)+".");var ti = this;
		var newHeight = (Math.ceil(ti.style.height.replace("px","")/2)) + "px";
		function di(failed){
			if(failed){
				if(boc[theSel.name] && ti.src){//bir[ni] && 
					$(ti).parent().find("."+s).html("Image not available!");
					$(ti).parent("a").after($(ti));
				}else{
					$(ti).parent().find("."+s).remove();
				}
			}else{
				$(ti).prev("a").append($(ti));
				if(!boc[theSel.name]){
					boc[theSel.name] = true;
					if(j > 0)BossOptionImages(theSel);
				}
				//bir[ni] = true;
				ti.src = oi.src;
				$(ti).parent().find("."+s).remove();
		}j++;}
		var l = '<img src="'+aImg+'" style="width:24px;height:24px;" alt=""/>';
		if($(ti).parent().find("."+s).size() > 0){
			$(ti).parent().find("."+s).html(l)
		}else{
			$('<div style="width:'+ti.style.width+';height:'+newHeight+';padding-top:'+newHeight+';" class="'+s+'">'+l+'</div>').insertBefore(ti);
		}
		oi.src = ni;
		$.boss.jax(ni,di,true);
	}
});}
function AlphaNum(stIn){return stIn.replace(/[^a-zA-Z 0-9]+/g,'').replace(/ /gi, "").toLowerCase();}

function TextCounter(field,cntfield,maxlimit){	
if (field.value.length > maxlimit)	field.value = field.value.substring(0, maxlimit);
var st = (maxlimit - field.value.length) + " Characters Left";
var node;
(cntfield.length > 0)?node = $("#" + cntfield).get(0):node = $(field).parent().find("."+BTC).get(0);
if(node.innerHTML){
	node.innerHTML = st;
}else{
    if(node.firstChild)node.firstChild.nodeValue = st;
}}

function BoostWin(t1,t2){newWindow = window.open(t1,b+"PopUp",t2);newWindow.focus();}
function LookForImg(b){
if(b.firstChild){
	var a = b.firstChild;
	if(a.src){
		return a;
	}else if(a.nextSibling && a.nextSibling.src){
		return a.nextSibling;
	}
}
return null;
}
function ImageSwap(theImg,theExt){
if(!theExt)theExt = "-on.";
theImg.src = FileNameSwitch(theImg.src,theExt);
}
function FileNameSwitch(s,e){
var g = 'gif';var j = 'jpg';var p = 'png';
if(s.indexOf(e) > -1){
	return s.replace(e+g,'.'+g).replace(e+j,'.'+j).replace(e+p,'.'+p);
}else{
	return s.replace('.'+g,e+g).replace('.'+j,e+j).replace('.'+p,e+p);
}}

function BossCloseWin(){
if (newWindow != null && !newWindow.closed)newWindow.close();
}

function BossWinResize(body){
if(document.images[0]){
img = document.images[0];
var ws = $.boss.winSize();var bs = body.style;
var wp = bs.paddingLeft.replace("px","")*1 + bs.paddingRight.replace("px","")*1;
if(bs.marginLeft.replace("px","")*1 > 0)wp += bs.marginLeft.replace("px","")*1 + bs.marginRight.replace("px","")*1;
var hp = bs.paddingTop.replace("px","")*1 + bs.paddingBottom.replace("px","")*1;
if(bs.marginTop.replace("px","")*1 > 0)hp += bs.marginTop.replace("px","")*1 + bs.marginBottom.replace("px","")*1;
var ha = 0;
$("div").each(function(){if($(this).attr("class") == "BossCloseWindow")ha = 20;});
window.resizeBy((img.width + wp) - ws[0],(img.height + hp + ha) - ws[1]);
self.focus();
window.onblur = function(){self.close();return true;}
}
}


function SetPayment(pt){
var m = $("#mp");
if(pt.val() == "A" || pt.val() == "7"){
	m.val("Complete Purchase");
}else if(pt.val() == "3" || pt.val() == "4"){
	m.val("Proceed");
}else{
	m.val("Make Payment");
}}

function PopQuant(attValueId,doFade){
var iq = "#itemQuantity";
var s = $(iq).get(0);var a = s.value;var b = "Loading...";
if(s.options){
	s.options[0] = new Option(b,b);
	s.value = b;
	$.get("/boss.boss?bj=101&id=" + attValueId,function(data){quantify(data);});
	function quantify(msg){
		var aq = $("attributequantity",msg);
		var id = aq.attr("attnumber")*1;
		if(id < 0)	id = aq.attr("itemnumber")*1;
		if(id < 1)id = 1;
		while (s.options.length) {s.options[0] = null;}
		for(var j = 1;j<=id;j++){s.options[j-1] = new Option(j,j);}
		s.value = a;
}}
if(doFade)$(iq).highlightFade({color:'#FF0',speed:2000,iterator:'sinusoidal'});
}

function CalcShipCosts(radio,other){
if(radio){
	var plus = 0;
	var minus = 0;var g = "giftWrap";
	
	if(radio.id == g+"State"){
		var p = $("#"+g+"Price").html();
		var t = $.boss.toFloat(p);
		var g = $("#giftWrapDisplay");
		if($("#giftWrapState").get(0).checked){
			plus = t;
			g.html(p);
			$("#WithGiftWrap").val("1");
		}else if(!other){
			minus = t;
			g.html("&nbsp;");
			$("#WithGiftWrap").val("0");
		}
		var d = $("#smallTotal").get(0);
		if(d)$("#smallTotal").html($.boss.formatCurrency($.boss.toFloat(d.innerHTML) - minus + plus));

	}else{
		var n = radio.parentNode.nextSibling;var m;
		if(radio.checked != true || n.innerHTML == "&nbsp;"){
			$(".BossShipSelect").each(function(){
				if(this.value == radio.value){
					radio.checked = true;
					m = $(radio).next("span").html();
					n.innerHTML = m
					plus += $.boss.toFloat(m);
				}else{
					this.checked = false;
					var t = this.parentNode.nextSibling;
					if(t.innerHTML != "&nbsp;"){
						minus += $.boss.toFloat(t.innerHTML);
					}
					t.innerHTML = "&nbsp";
			}});
		}
		$("#shipTemp").val(radio.value.replace("ship",""));
	}
	var b = bh+"FinalCartPrice";var b2 = $(b).get(0);
	if(b2)$(b).html($.boss.formatCurrency($.boss.toFloat($(b).get(0).innerHTML) - minus + plus));
}}





function PopCountries(firsttime){
var i = 0;
var bc = $(bh+"Countries");
var a = bc.val();
if($(BPC).val() == "3")i = 1;
$.get("/boss.boss?bj=100&oj=" + i,function(data){PC(data);});
function PC(node){
	node = node.firstChild.getElementsByTagName("country");
	while (bc.get(0).options.length) {bc.get(0).options[0] = null;}
	var id = "";var name = "";
	for(var j = 0;j<node.length;j++){bc.get(0).options[j] = new Option(node[j].getAttribute("name"),node[j].getAttribute("id"));}
	bc.val(a);
}
if(!firsttime)bc.highlightFade({color:'#FF0',speed:1000,iterator:'sinusoidal'});
}


function CheckFrames(){if(self != top){top.location.href = window.location.href;}}
function ConfirmEntry(direct,message){
input_box=confirm(message);if (input_box==true)this.window.location=direct;
}