 //boxes.js

var offset=1;
var sbw;
var oldsbw;
var oldheight;
var shopboxh;
var oldimgboxw;
var oldimgboxh;
var oldimgboxl;
var oldimgboxtop;
var imgboxw;
var imgboxh;
var shopopen = false;
var pulldowntimer; 
var countrypulldownalreadyloaded = false;
var pulldownboxcleanupdisabled=false;
var countrypulldowndisabled = false;
var cpdcleanuptimer;
var origqty;
var checkoutloading = false;
var shopcontent;
var gmtoffset;
var knightblurb;
var btextfocustimer;
var bodyscrolltop;

function boxesload() {
  if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
//  if (/MSIE/.test(navigator.userAgent)) {
    offset=0;
  }
  var a= document.getElementById("shopbox");
  a.style.display="";
  shopboxh = parseInt(a.style.height);
  sbw = parseInt(a.style.width);
  sbt=parseInt(a.style.top);
  b=document.getElementById("shopboxbdr");
  b.style.display = "";
  b.style.height = shopboxh;
  b.style.width = sbw;
  b.style.top = sbt-offset;
  c=document.getElementById("shopboxtxt");
  c.style.display = "";
  c.style.height = shopboxh;
  c.style.width = sbw;
  c.style.top = sbt;
  d = document.getElementById('navbox');
  d.style.display = "";
  var nw = parseInt(d.style.width);
  d.style.width = nw-offset*2
  navboxposition();
  var e=new Date();
  gmtoffset = e.getTimezoneOffset();
  //setTimeout('knightimgblink()', 2000);
}

function navboxposition() {
  var bw= document.body.clientWidth;
  var n;
  var m;
  var a= document.getElementById("shopbox");
  var w = parseInt(a.style.width);
  if (bw<780) { 
    n=20; //bw/2-355;
    m=(750-w+1-offset*2);
  }
  else {
    n=bw/2-375; 
    m=(bw/2+375-w+1-offset*2);
  }
  document.getElementById('navbox').style.left = n; 
  document.getElementById('shopbox').style.left = m+offset;
  document.getElementById('shopboxbdr').style.left = m;
  document.getElementById('shopboxtxt').style.left = m;
}

function shopexpand() {
  if (!shopopen) {
    shopopen=true;
    setTimeout("document.getElementById('shopboxclosedtxt').style.display = 'none';",500);
    oldsbw = sbw;
    sbw = 565;
    oldheight = shopboxh;
    shopboxh=450;
    var timer=500;
    var step = Math.round((sbw-oldsbw)/15);
    for (x=oldsbw; x<=sbw; x+= step) {
      setTimeout("sbexpand('"+x+"');", timer);
      timer += 20;
    }
    setTimeout("shopboxtxtresize(); document.getElementById('shopboxopentxt').style.display='';",timer);
  }
}

function sbexpand(x) {
  var bw= document.body.clientWidth;
  var m;
  if (bw<780) { 
    m=(750-x+1-offset*2);
  }
  else {
    m=(bw/2+375-x+1-offset*2);
  }
  var h=x*(shopboxh/sbw);
  //document.write(h);
  var a=document.getElementById('shopbox');
  var b=document.getElementById('shopboxbdr');
  a.style.width=x;
  a.style.left=m;
  a.style.height=h;
  a.style.backgroundColor ="#101014";
  a.style.opacity=0.95;
  a.style.filter="alpha(opacity='95')";  
  b.style.width=x;
  b.style.left=m;
  b.style.height=h;  
}

function shopboxtxtresize() {
  var c=document.getElementById('shopboxtxt');
  var bw= document.body.clientWidth;
  if (bw<780) { 
    var m=(750-sbw+1-offset*2);
  }
  else {
    var m=(bw/2+375-sbw+1-offset*2);
  }
  c.style.width=sbw;
  c.style.left=m;
  c.style.height=shopboxh;
  document.getElementById('shopboxopentxt').style.width = sbw-8-offset*8;
  var a=document.getElementById('shopbox');
  var b=document.getElementById('shopboxbdr');
  a.style.width=sbw;
  b.style.width=sbw;
  a.style.left=m;
  b.style.left=m;
}

function shopcontract() {
  setTimeout("document.getElementById('shopboxopentxt').style.display = 'none';", 500);
  var tempbw = sbw;
  sbw=oldsbw;
  oldsbw = tempbw;
  var tempheight=shopboxh;
  shopboxh = oldheight;
  oldheight = tempheight;
  var timer=500;
  var step = (oldsbw-sbw)/15;
  for (x=oldsbw; x>=sbw; x-= step) {
    setTimeout("sbexpand('"+x+"');", timer);
    timer += 20;
  }
  setTimeout("closedshopdisplay(); shopopen=false;", timer);
  cleanupshoppulldowns();
  if (document.getElementById('itemaddeditform')) {
    submititemaddedform(addeditemresponse);
  }
  else if (checkoutloading) {
    setTimeout("loadcheckout();", timer);
  }
  else {
    setTimeout("clickedfalse();", timer);
  }
}

function shoptoggle() {
  if (shopopen) { shopcontract(); }
  else { 
    shopexpand();
    setTimeout("clickedfalse();", 500);
  }
}

function cleanupshoppulldowns() {
  //finishpulldown doesnt need this since form always gets submitted if its present, contents reloaded
  if (document.getElementById('countryselectinput')) {
    document.getElementById('countrypulldownbox').style.display='none';
    setTimeout("document.getElementById('countryselectinput').disabled='';",10);
    clearTimeout(cpdcleanuptimer);
  }
  if (document.getElementById('currencyselectinput')) {
    document.getElementById('currencypulldownbox').style.display = 'none';
  }
}

function closedshopdisplay() {
  shopboxtxtresize();
  document.getElementById('shopboxclosedtxt').style.display='';
  var a=document.getElementById('shopbox');
  a.style.backgroundColor ="#333341";
  a.style.opacity=0.4;
  a.style.filter="alpha(opacity='40')";
}

function imagebig(id,url)  {
  document.getElementById('enlargedimageboxtxt').style.display='none';
  document.getElementById('enlargedimagebox').style.display='none';
  var a = document.getElementById('enlargedimageimg');
  a.onload = function() { imagebigloaded(id); }
  a.src = url;
  if (loadinganimno==0) { loadinganimno =1;  loadinganim(); }
  setTimeout("clickedfalse()", 500);
}

function imagebigloaded(id) {
  var b;
	bodyscrolltop = document.body.scrollTop;
  if (b = document.getElementById(id)) {
		var bp = findPos(b);
		oldimgboxw = parseInt(b.width);
		oldimgboxh = parseInt(b.height);
		oldimgboxl = bp[0];
		oldimgboxtop = bp[1];
		var c = document.getElementById('enlargedimageimg'); 
		document.getElementById('enlargedimageboxtxt').style.display=''; //so ie6 can get metrics
		imgboxw = c.width+16; //8px padding
		imgboxh = c.height+52; //8px paddingx2 + 18x2 for link
		document.getElementById('enlargedimageboxtxt').style.display='none';
		var timer=0;
		var step = (imgboxw-oldimgboxw)/25;
		for (x=oldimgboxw; x<=imgboxw; x+= step) {
			setTimeout("imgboxexpand('"+x+"');", timer);
			timer += 20;
		}
		var d= document.getElementById('enlargedimageboxtxt');
		var bw= document.body.clientWidth;
		d.style.top = 80+1+offset+bodyscrolltop; //8 padding
		d.style.left = bw/2-(imgboxw-16)/2+offset;  //8 padding
		//setTimeout("document.getElementById('enlargedimageboxtxt').style.display='';",timer);
		setTimeout("imgboxexpandfini()", timer);
		setTimeout("document.getElementById('enlargedimagebox').style.display=''; loadinganimno=0;",20); //loadinganimno lag for ie6
	}
}

function imgboxexpand(x) {
  var bw= document.body.clientWidth;
  var a = document.getElementById('enlargedimagebox');
  var oldctrx = oldimgboxl+oldimgboxw/2;
  var newctrx = bw/2;
  var ratio = (x-oldimgboxw)/(imgboxw-oldimgboxw);
  a.style.left = (oldctrx+(newctrx-oldctrx)*ratio)-x/2;
  a.style.width = x; 
  var h=x*(imgboxh/imgboxw);
  a.style.height = h;
  var newctry = 80+h/2+bodyscrolltop;
  var oldctry = oldimgboxtop + oldimgboxh/2;
  a.style.top = (oldctry+(newctry-oldctry)*ratio)-h/2; 

}

function imgboxexpandfini() {
  document.getElementById('enlargedimageboxtxt').style.display='';
  var a = document.getElementById('enlargedimagebox');
  var bw= document.body.clientWidth;
  a.style.left = bw/2-imgboxw/2;
  a.style.width= imgboxw;
  a.style.top=80+bodyscrolltop;
  a.style.height=imgboxh;
}

function findPos(obj) {
  var curleft = curtop = 0;
  if (obj.offsetParent) {
    do {
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    } while (obj = obj.offsetParent);
  return [curleft,curtop];
  }
}

function imageclose() {
  document.getElementById('enlargedimageboxtxt').style.display='none';
  clickedfalse();
  document.getElementById('enlargedimagebox').style.display='none';
  a=document.getElementById('enlargedimageimg');
  a.onload = null;
  a.src = "";
}

function shopadd(id) {
  document.getElementById('shopopencontents').innerHTML = "<span style='font-size: 11px; color: #444444;'><i>Loading....</i></span>";
  shoploader("/products/shopadd/"+id);
  shopexpand();
}

function qtyinputfocus(id) {
  var a=document.getElementById(id);
	var temp = a.value;
	a.value="";
	a.focus();
	a.value=temp;
	a.select();
	a.style.backgroundColor = "#A3E3F5"; //"#e9b36c";
	//a.style.fontWeight = "bold"; //doesnt work?
	a.style.color = "#000000";
}

function qtyinputblur(id) {
  var a;
  if (a=document.getElementById(id)) {
    a.style.backgroundColor = "#000000";
    a.style.color = "#A3E3F5";
    var n= parseInt(a.value);
    if (n > 99 ) { n=99; }
    if (n < 1 || !n) { n =1; }
    a.value = n;
    checkqtychanged();
  }
}

function qtyincrease() {
  var a=document.getElementById('currentitem_qty');
  var n= parseInt(a.value);
  n+=1;
  if (n > 99 ) { n=99; }
  if (n < 1 || !n) { n =1; }
  a.value = n;
  setTimeout("clickedfalse()", 500);
  checkqtychanged();
}

function qtydecrease() {
  var a=document.getElementById('currentitem_qty');
  var n= parseInt(a.value);
  n-=1;
  if (n < 1 || !n ) { n=1; }
  a.value = n;
  setTimeout("clickedfalse()", 500);
  checkqtychanged();
}

function checkqtychanged() {
  var a;
  if (a=document.getElementById('currentitem_qty')) {
    var b;
    if (b=document.getElementById('estimatedcost')) {
      if (parseInt(a.value) == origqty) {
        b.style.display = "";
      }
      else {
        b.style.display = "none";
        document.getElementById('shipcostupdatelink').style.display = "";
      }
    }
    else { 
      setTimeout('checkqtychanged()', 500)
    }
  }
}


function finishpulldown() {
  clearTimeout(pulldowntimer); //in case called by onclick instead of onmouseover
  var b;
  if ((b=document.getElementById('finishpulldownbox')) && !clicked) {
    if (b.style.display != "" ) {
      var a=document.getElementById('currentitem_finish');
      b.style.display = "";
      b.style.left=a.offsetLeft;
      b.style.top=a.offsetTop;
      a.focus();
    }
  }
}

function finishpulldowncleanup() {
  document.getElementById('currentitem_finish').disabled='true';
  if (bubbleopen || bulge) {
    setTimeout('finishpulldowncleanup()', 50);
  }
  else {
    document.getElementById('finishpulldownbox').style.display='none';
    setTimeout("document.getElementById('currentitem_finish').disabled='';",10);
  }
}

function selectfinish(finish) {
  var a=document.getElementById('currentitem_finish');
  setTimeout('clickedfalse();', 500);
  a.value = finish;
  a.style.width = parseInt(finish.length*8.5)+"px";
  a.blur();
}

function submititemaddedform(functionname) {
  var url = document.getElementById('itemaddeditform').action; // products/currentitempost
  var params = "";
  params += "qty="+document.getElementById('currentitem_qty').value;
  params += "&finish="+document.getElementById('currentitem_finish').value;
  params += "&authenticity_token="+document.getElementsByName('authenticity_token')[0].value;
  posttoxmlhttp(url, functionname, params);
  if (loadinganimno==0) { loadinganimno =1;  loadinganim(); }
}

function addeditemresponse() {
  if (xmlhttppost.readyState==4) {// 4 = "loaded"
    if (xmlhttppost.status==200) {// 200 = OK
      loadinganimno =0;
      setTimeout("addeditemresponsedo();", 500);
    }
    else {
      //alert("Problem retrieving XML data");
    }
  }
}

function addeditemresponsedo() {
  var b;
  if (b = document.getElementById('shopopencontents')) {
    b.innerHTML=xmlhttppost.responseText;
    b.style.display = "";
    eval(document.getElementById('shopfunction').innerHTML);
  }
  var c;
  if (c = document.getElementById('shopfunction')) { eval(c.innerHTML); }
  document.getElementById('knightblurb').innerHTML = knightblurb;
  clickedfalse();
}

function curritemdel() {
  if (loadinganimno==0) { loadinganimno =1;  loadinganim(); }
  setTimeout("shoploader('products/shoplist/delete');", 500);
  cleanupshoppulldowns();
}

function itemedit(newcurrentitemno) {
  if (loadinganimno==0) { loadinganimno =1;  loadinganim(); }
  if (document.getElementById('itemaddeditform')) { //if called from shopadd
    document.getElementById('itemaddeditform').action = "/products/editfromshopadd/"+newcurrentitemno;
    submititemaddedform(addeditemresponse);
  }
  else { //if called from shoplist
    setTimeout("shoploader('products/editfromshoplist/"+newcurrentitemno+"');", 500);
  }
  cleanupshoppulldowns();
}

function newfocus() {
//  var a = document.getElementById('finishpulldownbox');
//  document.getElementById('currentitem_qty').blur();
//  if (a.style.display == '') {
//    a.style.display = 'none';
//    document.getElementById('currentitem_finish').blur();
//  }
}

function updateshopqty(n) {
  if (n==0) {
    var txt = "<span style='color: #777777'>is</span> OPEN";
  }
  else {
    var txt=n+" <span style='color: #777777'>item";
    if (n != 1) { txt += "s"; }
    txt += " chosen</span>";
  }
  document.getElementById("shopboxcornertxt").innerHTML=txt;
}

//function DELETEloadshippingestimate() { //event handler is shiploaded() //this is only called by 'update' now?
//  //loader("products/shipping_estimate", "shippingestimate", 0);
//  shipopenxmlhttp('products/shipping_estimate', shiploaded);
//  //countrypulldowndisabled=false;
//  var a;
//  if (a=document.getElementById('currentitem_qty')) {
//    origqty = parseInt(a.value);
//  }
//}

function shiploaded() {
  if (shipxmlhttp.readyState==4) {// 4 = "loaded"
    if (shipxmlhttp.status==200) {// 200 = OK
      loadinganimno =0;
      var a;
      if (a = document.getElementById('shippingestimate')) { //in cases the item was deleted while talking to canada post
        a.innerHTML = shipxmlhttp.responseText;
        countrypulldowndisabled = false;
      }
      setTimeout("clickedfalse();",500);
      if (checkoutloading) { loadcheckout(); }
    }
    else {
      //alert("Problem retrieving XML data");
    }
  }
}

function countrypulldown() {
  clearTimeout(pulldowntimer); //in case called by onclick instead of onmouseover
  var b=document.getElementById('countrypulldownbox');
  if (b.style.display != "" && !countrypulldowndisabled && !clicked) {
    var a=document.getElementById('countryselectinput');
    b.style.left=findPos(a)[0]-2;
    document.getElementById('countrypulldownbox').style.display='';
    setTimeout("respositioncountrypulldown();", 10);
    a.focus();
    if (!countrypulldownalreadyloaded) {
      b.style.top=findPos(a)[1];
      openxmlhttp("products/countrypulldown", countrypulldownloaded);
      if (loadinganimno==0) { loadinganimno =1;  loadinganim(); }
    }
    else { b.style.top = "10px"; }
  }
  //else { respositioncountrypulldown(); }
}

function countrypulldownloaded() {
  if (xmlhttp.readyState==4) {// 4 = "loaded"
    if (xmlhttp.status==200) {// 200 = OK
      var a=document.getElementById('countrypulldownbox');
      a.innerHTML=xmlhttp.responseText;
      countrypulldownalreadyloaded = true;
      loadinganimno =0;
      a.style.top = "10px";
      a.style.width="290px";
      //respositioncountrypulldown();
    }
    else {
      //alert("Problem retrieving XML data");
    }
  }
}

function countrypulldowncleanup() {
  if (pulldownboxcleanupdisabled==false) {
    document.getElementById('countryselectinput').disabled='true';
    if (bubbleopen || bulge) {
      cpdcleanuptimer = setTimeout('countrypulldowncleanup()', 50);
    }
    else {
      document.getElementById('countrypulldownbox').style.display='none';
      //setTimeout("document.getElementById('finishpulldownbox').style.display='none';", 500);
      setTimeout("document.getElementById('countryselectinput').disabled='';",10);
//      respositioncountrypulldown();
      navboxposition();
    }
  }
  else {
    pulldownboxcleanupdisabled=false;
    var b=document.getElementById('countryselectinput')
    b.disabled='';
    b.focus();
  }
}

function countrylistbyletterloaded() {
  if (xmlhttp.readyState==4) {// 4 = "loaded"
    if (xmlhttp.status==200) {// 200 = OK
      var a=document.getElementById('countrylistbox');
      a.innerHTML=xmlhttp.responseText;
      loadinganimno =0;
      clickedfalse();
      respositioncountrypulldown();
    }
    else {
      //alert("Problem retrieving XML data");
    }
  }
}

function selectcountry(country) {
  var a=document.getElementById('countryselectinput');
  setTimeout('selectcountrydo();', 600);
  setTimeout('clickedfalse();', 750);
  a.value = country;
  var w = parseInt(country.length*8);
  if (w<90) { w=90; }
  a.style.width = w+"px";
  a.blur();
//  var url = 'products/changecountry';
  var url = document.getElementById('changecountryform').action; // products/changecountry
  var params = "";
  params += "country="+a.value;
  params += "&authenticity_token="+document.getElementsByName('authenticity_token')[0].value;
  shipposttoxmlhttp(url, countrychanged, params);
  if (loadinganimno==0) { loadinganimno =1;  loadinganim(); }
  countrypulldowndisabled=true;
}

function selectcountrydo() {
  if (countrypulldowndisabled) {
    var str = "<span style='font-size: 11px; color: #444444;'><i>Loading....</i></span>"
    document.getElementById('shippingestimate').innerHTML = str;
  }
}

function countrychanged() {
  if (shipxmlhttp.readyState==4) {// 4 = "loaded"
    if (shipxmlhttp.status==200) {// 200 = OK
      loadinganimno =0;
      setTimeout("countrychangeddo();", 500);
    }
    else {
      //alert("Problem retrieving XML data");
    }
  }
}

function countrychangeddo() {
  clickedfalse();
  if (a=document.getElementById('shippingestimate')) {
    a.innerHTML=shipxmlhttp.responseText;
  }
  checkqtychanged();
  countrypulldowndisabled=false;
}

function countryalph(letter) {
  if (loadinganimno==0) { loadinganimno =1;  loadinganim(); }
  openxmlhttp("products/countrylistbyletter/"+letter, countrylistbyletterloaded);
}

function respositioncountrypulldown() {
  navboxposition();
  var b=document.getElementById('countrypulldownbox');
  var a=document.getElementById('countryselectinput');
  b.style.left=findPos(a)[0]-2;
}

function updateshipping() {
  setTimeout('selectcountrydo();', 600);
  setTimeout('clickedfalse();', 750);
  if (loadinganimno==0) { loadinganimno =1;  loadinganim(); }
  var a;
  params = "";
  if (a = document.getElementById('currentitem_qty')) {
    var params = "qty="+a.value;
    origqty = parseInt(a.value);
  }
  var b;
  params += "&authenticity_token="+document.getElementsByName('authenticity_token')[0].value;
  shipposttoxmlhttp("products/shipqtychanged", shiploaded, params);
  countrypulldowndisabled = true;
}

function payforthis() {
  if (loadinganimno==0) { loadinganimno =1;  loadinganim(); }
  params = ""
  if (document.getElementById('itemaddeditform')) {
    params += "qty="+document.getElementById('currentitem_qty').value;
    params += "&finish="+document.getElementById('currentitem_finish').value;
  }
  params += "&country="+document.getElementById('countryselectinput').value;
  params += "&authenticity_token="+document.getElementsByName('authenticity_token')[0].value;
  shipposttoxmlhttp('products/precheckout', loadcheckout, params);
}

function currencypulldown() {
  clearTimeout(pulldowntimer); //in case called by onclick instead of onmouseover
  var b = document.getElementById('currencypulldownbox');
  if (b.style.display != "" && !clicked) {
    var a = document.getElementById('currencyselectinput');
    b.style.left=findPos(a)[0]-2;
    b.style.top=findPos(a)[1];
    b.style.display = "";
    a.focus();
  }
}

function currencypulldowncleanup() {
  document.getElementById('currencyselectinput').disabled='true';
  if (bubbleopen || bulge) {
    setTimeout('currencypulldowncleanup()', 50);
  }
  else {
    var a=document.getElementById('currencypulldownbox')
    a.style.display='none';
    setTimeout("document.getElementById('currencyselectinput').disabled='';",10);
  }
}

function selectcurrency(currencycommondescrip) {
  var url = document.getElementById('changecurrencyform').action; //action is 'nav/changecurrency''
  var params = "";
  params += "commondescrip="+currencycommondescrip;
  params += "&url="+maincontent;
  params += "&authenticity_token="+document.getElementsByName('authenticity_token')[0].value;
  posttoxmlhttp(url, currencychanged, params);
  if (loadinganimno==0) { loadinganimno =1;  loadinganim(); }
  var a = document.getElementById('currencyselectinput');
  a.value = currencycommondescrip;
  a.blur();
  setTimeout("cleanupshoppulldowns();", 500);
  if (document.getElementById('shopopencontents')) {
    setTimeout( "document.getElementById('shopopencontents').style.display='none';", 500);
  }
}

function currencychanged() {
  if (xmlhttppost.readyState==4) {// 4 = "loaded"
    if (xmlhttppost.status==200) {// 200 = OK

      var reg =new RegExp("products\/");
      if (reg.test(maincontent)) {
        shopopen = false;
        loadmain(maincontent);
        setTimeout('shopopen = true;', 10);
      }
      
      reg =new RegExp("choosepaymethod");
      if (reg.test(maincontent)) {
        loadmain(maincontent);
      }

      if (document.getElementById('itemaddeditform')) {
        submititemaddedform(addeditemresponse);
      } 
      else {
        if (document.getElementById('shopopencontents')) {
          shoploader(shopcontent);
        }
      }

      setTimeout("currencychangeddo();", 10);
      if (loadinganimno==0) { loadinganimno =1;  loadinganim(); }
    }
    else {
      //alert("Problem retrieving XML data");
    }
  }
}

function currencychangeddo() {
  if (document.getElementById('shopopencontents')) {
    str = "<span style='font-size: 11px; color: #444444;'><i>Loading....</i></span>"
    document.getElementById('shopopencontents').innerHTML = str;
    document.getElementById('shopopencontents').style.display='';
  }
}

//function knightimgblink() {
//  var a;
//  if (a = document.getElementById('knightimg')) {
//    a.src = "images/knight_hoodedskull.gif";
//    setTimeout("document.getElementById('knightimg').src='images/knight.gif';",350);
//    setTimeout("knightimgblink()", 25000);
//  }
//}

function emailtofocus(id) {
  if (!clicked) {
    var a;
    if (a=document.getElementById(id)) {
      a.focus();
      a.style.backgroundColor = "#A3E3F5"; //"#e9b36c";
      a.style.color = "#000000";
    }
  }
}

function emailtoblur(id) {
  var a;
  if (a=document.getElementById(id)) {
    a.style.backgroundColor = "#000000";
    a.style.color = "#A3E3F5";
  }
}
