var IE=(navigator.userAgent.toLowerCase().indexOf("msie")!=-1);
var ds=document.documentElement.style;

if(IE){
  ds.scrollbar3dlightColor="#888";
  ds.scrollbarArrowColor="#fff";
  ds.scrollbarBaseColor="#888";
  ds.scrollbarDarkShadowColor="#888";
  ds.scrollbarFaceColor="#00f";
  ds.scrollbarHighlightColor="#ccc";
  ds.scrollbarShadowColor="#ccc";
}else ds.overflowY="scroll";

function ael(elm,evt,f){
  if(elm.addEventListener)elm.addEventListener(evt, function(){eval(f)}, false);
  else if(elm.attachEvent)elm.attachEvent("on"+evt, function(){eval(f)});
}

function wload(){

  if(elm=document.getElementById("bigpic")){
  if(document.documentElement.scrollHeight>0){
    ael(elm,"click","window.location.href='"+elm.parentNode.getAttribute("href")+"';");
    elm.parentNode.parentNode.replaceChild(elm,elm.parentNode);
    document.documentElement.style.overflow="hidden";

    swm=document.documentElement.scrollWidth;shm=document.documentElement.scrollHeight;
    sl=Math.floor((swm-document.documentElement.clientWidth)/2);
    st=Math.floor((shm-document.documentElement.clientHeight)/2);
    window.scrollTo(sl,st);

    ael(window,"resize","resizechk();");
    if(document.addEventListener)document.addEventListener("mousemove", scrollmouse, false);
    else if(document.attachEvent)document.attachEvent("onmousemove", scrollmouse);

    resizechk();
    setInterval("scrollit();",33);

  }}

  if(elm=document.getElementById("sboxs")){
    ael(elm,"mouseover","hbut(elm,true);");
    ael(elm,"mouseout","hbut(elm,false);");
  }

  if(elm=document.getElementById("sb4")){
    ael(elm,"mouseover","hbut(elm,true);");
    ael(elm,"mouseout","hbut(elm,false);");
  }

  if(elm=document.getElementById("edest")){
    var i,j,tmp=["m","i","r","a","n","d","i","a","o","l","i","s","t","d","i","o","d","e","s",
      "e","t","a","h","y","l","e","u","g","e","r","t","a","b","o","o","c","y","a","n","s","i","t",
      "t","i","n","o","d","y","k","a","d","i","r","a","d","a","u","s","f","y","c","a","g","o","o","m","m","i","s"];
    for(i=0,j=tmp.length;i<j;i++)tmp[i]=(i==58)?".":(i==29)?"@":(i==16)?":":(i%3!=0)?"":tmp[i];
    tmp=tmp.join("");elm.replaceChild(i=document.createElement("a"),elm.firstChild);
    i.setAttribute("href",tmp);i.appendChild(document.createTextNode(tmp.substr(7)));
  }

  if(elm=document.getElementById("videolink")){
    document.getElementById("videoembed").style.display="block";
    ael(elm,"click","embed(elm);");
  }

  i=1;
  while(elm=document.getElementById("prod"+i)){
    elma=document.createElement("input");
    elm.appendChild(elma);
    elma.value=elm.firstChild.nodeValue;
    ael(elma,"change","vognupdate(elm,true);");
    ael(elma,"keyup","vognupdate(elm,false);");
    elm.removeChild(elm.firstChild);
    i++;
  }

  if(elm=document.getElementsByName("itemsapage")[0]){
    ael(elm,"change","elm.form.submit()");
  }

}




function vognupdate(elm,t){
  v=elm.value;
  if(v.indexOf(0)==0)elm.value=parseFloat(elm.value);
  if(v.indexOf(" ")!=-1)elm.value=parseFloat(elm.value);
  v=elm.value;

    if(parseInt(v)!=v&&v.length!=0){
      alert("Kun hele tal er tilladt");elm.value=1;v=1;
      tmp=elm.parentNode.getAttribute("id");
      tmparr=[tmp,v,document.getElementById(tmp+"n").firstChild.nodeValue];
      vognupdates(tmparr);
    }
    else if(v.length>3){
      alert("Max antal er 999 stk.");elm.value=999;v=999;
      tmp=elm.parentNode.getAttribute("id");
      tmparr=[tmp,v,document.getElementById(tmp+"n").firstChild.nodeValue];
      vognupdates(tmparr);
    }
    else if(elm.value.length>0||(t)){
      tmp=elm.parentNode.getAttribute("id");
      tmparr=[tmp,v,document.getElementById(tmp+"n").firstChild.nodeValue];
      vognupdates(tmparr);
    }
}

var xmldoc;

function vognupdates(tmp){
  var xmlobj,xmlres,url="http://www.toystokids.com/lager.php?i="+tmp[2].toLowerCase()+"&stk="+tmp[1];

  if(window.XMLHttpRequest)xmlobj=new XMLHttpRequest();
  else if(window.ActiveXObject)xmlobj=new ActiveXObject("Microsoft.XMLHTTP");
  else return false;

  xmlobj.open("GET",url,true);

  xmlobj.onreadystatechange=function(){
    if(xmlobj.readyState==4){xmldoc=xmlobj.responseXML;vognupdater(tmp);}
  }
  xmlobj.send(null);
}

function vognupdater(tmp){
  var stk=xmldoc.getElementsByTagName("stk")[0].firstChild.nodeValue;
  var pris=xmldoc.getElementsByTagName("pris")[0].firstChild.nodeValue;
  var status=xmldoc.getElementsByTagName("status")[0].firstChild.nodeValue;

  tmparr=[tmp[0],tmp[1],"p","t"];

  for(i=2;i<tmparr.length;i++){
    tmparr[i]=document.getElementById(tmp[0]+tmparr[i]).firstChild;
  }tmparr[4]=document.getElementById(tmp[0]+"s");
   plus=document.getElementById(tmp[0]+"plus");
   minus=document.getElementById(tmp[0]+"minus");
   tmparr[0]=document.getElementById(tmp[0]).firstChild;

  smiley="http://www.toystokids.com/";

  tmp[1]=parseFloat(tmp[1]);
  stk=parseFloat(stk);

  tmparr[2].nodeValue=pris+",00";
  if(stk<tmp[1]&&status<0){
    tmparr[4].setAttribute("src",smiley+"g.png");
    tmparr[0].value=stk;
    plus.style.visibility="hidden";
    minus.style.visibility="visible";
  }
  else if(stk<tmp[1]&&status==0){
    tmparr[0].value=stk;
    plus.style.visibility="hidden";
    minus.style.visibility="visible";
  }
  else if(tmp[1]<=stk&&status!=0){
    tmparr[4].setAttribute("src",smiley+"g.png");
    plus.style.visibility=(tmp[1]<stk||status>0)?"visible":"hidden";
    minus.style.visibility=(tmp[1]==1)?"hidden":"visible";
  }
  else if(tmp[1]<=stk&&status==0){
    plus.style.visibility=(tmp[1]<stk||status>0)?"visible":"hidden";
    minus.style.visibility=(tmp[1]==1)?"hidden":"visible";
  }
  else if(tmp[1]>stk&&status>0){
    tmparr[4].setAttribute("src",smiley+"r.png");
    plus.style.visibility=tmp[1]>998?"hidden":"visible";
    minus.style.visibility=(tmp[1]==1)?"hidden":"visible";
  }

  if(tmparr[0].value==0)tmparr[0].parentNode.parentNode.parentNode.style.display="none";
  tmparr[3].nodeValue=(pris*tmparr[0].value)+",00";

  calctotal();
}

function calctotal(){

  var total=66;
  var i=1;
  var vognnr=["block","none"];
  while(elm=document.getElementById("prod"+i+"t")){
    total+=parseFloat(elm.firstChild.nodeValue.replace(",","."));
    if(document.getElementById("prod"+i+"s").getAttribute("src").indexOf("r.png")!=-1)vognnr=["none","block"];i++;
  }
  document.getElementsByName("pris")[0].value=total;
    if((total<150&&document.getElementById("ordreklar"))
   ||(total>149&&!document.getElementById("ordreklar")))window.location.reload(true);
  total=""+total;
  document.getElementById("vognt2").firstChild.nodeValue=(""+total)+" DKK";
total=total+",00";
  document.getElementById("vognt").firstChild.nodeValue=(""+total).replace(".",",");

  for(j=1;j<3;j++)document.getElementById("vogn"+j).style.display=vognnr[j-1];
}




ael(window,"load","wload();");

function embed(){
  if(elm=document.getElementById("embedfile"))elm.parentNode.removeChild(elm);
  i=document.createElement("iframe");
  i.setAttribute("src",document.getElementById("videourl").firstChild.nodeValue);
  i.setAttribute("id","embedfile");
  document.getElementById("videolink").appendChild(i);
}

function hbut(elm,t){
  var i=elm.style;
  i.color=(t)?"#008":"#fff";
  i.background=(t)?"#0ff":"#00a";
}

function browser() {
    var browser=navigator.userAgent;   
    if (browser.indexOf('Gecko')>-1) return "FF"; //Mozilla og Netscape
    else if (browser.indexOf('Opera')>-1) return "Opera"; //Opera
    else if (browser.indexOf('MSIE')>-1) return "IE"; //Internet Explorer
    else return false;
}


var sl=st=swm=shm=slo=sto=sw=sh=swx=shx=swpx=shpx=0;

function resizechk(){

  sw=document.documentElement.clientWidth;
  sh=document.documentElement.clientHeight;
  swx=sw/3;swpx=sw/60;shx=sh/3;shpx=sh/60;

}

function scrollmouse(e){

  e=e?e:event;
  var x=e.clientX;
  var y=e.clientY;

  if(x<swx)slo=(20-Math.floor(x/swpx))*-2;
  else if(x>sw-swx)slo=Math.ceil((x-(sw-swx))/swpx)*2;
  else slo=0;

  if(y<shx)sto=(20-Math.floor(y/shpx))*-2;
  else if(y>sh-shx)sto=Math.ceil((y-(sh-shx))/shpx)^2;
  else sto=0;

}

function scrollit(){

  sl+=slo;st+=sto;
  if(sl<0)sl=slo=0;
   else if(sl>swm-sw){sl=swm-sw;slo=0;}
  if(st<0)st=sto=0;
   else if(st>shm-sh){st=shm-sh;sto=0;}
  window.scrollTo(sl,st);

}