/* title: stitches javascript
   author: m.rankin
   date: 22-mar-07 */
function main(page){
   var o6mac=/opera\/6.+macintosh/i;
   if(!o6mac.test(navigator.userAgent)){
      frameBuster();
      writeFact();
      if(page=='courses')setCourses();
      if(page=='contact'||page=='privacy')writeMail(page);
   }
}
/* anti-frame script */
function frameBuster(){
   if(window.top.location!=window.self.location){
      alert("WARNING: This site is guilty of content and bandwidth theft!\n"+
         "The rightful owner(s) of the requested page reside at:\n\n   "+
         location.href+"\n\nYou are being redirected there now.");
      window.top.location=window.self.location;
   }
}
/* email script */
function writeMail(page){
   var anchor=addContent("a",email());
   anchor.setAttribute("href","mailto:"+email());
   if(page=='contact'){
      var listItem=addContent("li");
      var content=document.getElementById("content").childNodes;
      for(var i=0;i<content.length;i++){
         if(content[i].nodeName=="UL"||content[i].tagName=="UL"){
            listItem.appendChild(anchor);
            content[i].appendChild(listItem);
         }
      }
   }else if(page=='privacy'){
      var sibling=document.getElementsByTagName("br");
      var mailText=sibling[0].parentNode;
      mailText.appendChild(anchor);
   }
}
function email(){
   var name="nimda",domain="ua+moc+diatsrifsehctits";
   var dot=/\+/g;
   return reverse(name)+"@"+reverse(domain.replace(dot,"."));
}
function reverse(xStr){
   var yStr="";
   for(var i=xStr.length-1;i>=0;i--)
      yStr+=xStr.charAt(i);
   return yStr;
}
/* courses script */
function setCourses(){
   var content=document.getElementById("content").childNodes;
   var courses=new Array();
   for(var i=0;i<content.length;i++)if(content[i].className=="course")
      courses.push(content[i]);
   for(var i=0;i<courses.length;i++){
      addToggle(courses[i]);
      var tags=courses[i].childNodes;
      for(var j=0;j<tags.length;j++)switchStyle(tags[j]);
   }
}
function addToggle(div){
      var regex=/MSIE\s[567]/;
      var para=addContent("p",false);
      var anchor=addContent("a","Learn more...");
      if(regex.test(navigator.userAgent)){
         para.className="toggle";
         anchor.href="#";
         anchor.onclick=function(){toggleDetails(this);return false;}
      }else{
         para.setAttribute("class","toggle");
         anchor.setAttribute("href","#");
         anchor.setAttribute("onclick","toggleDetails(this);return false;");
      }
      para.appendChild(anchor);
      var nodeIndex=(regex.test(navigator.userAgent))?0:1;
      div.insertBefore(para,div.childNodes[nodeIndex]);
}
function toggleDetails(x){
   var aText=x.firstChild;
   aText.nodeValue=(aText.nodeValue=="Learn more...")?"Hide details...":"Learn more...";
   var thisDiv=x.parentNode.parentNode.childNodes;
   for(var i=0;i<thisDiv.length;i++)switchStyle(thisDiv[i]);
}
function switchStyle(x){
   if((x.firstChild&&x.firstChild.nodeName!="A")&&(x.nodeName=="P"||x.tagName=="P"))
      x.style.display=(x.style.display=="none")?"block":"none";
}
/* random fact */
function writeFact(){
   var factDiv=document.getElementById("fact");
   var fact=new Array(addContent("h4","First Aid Fact"),addContent("p",randomFact()));
   for(var i=0;i<fact.length;i++)
      factDiv.appendChild(fact[i]);
}
function randomFact(){
   var factArray=new Array(
      "8 people die each year in Australia by testing a 9v battery on their tongue.",
      "20% of all people call friends, neighbours, relates before dialling '000'.",
      "1 in 6 children and 1 in 10 adults suffer asthma.",
      "10 - 20 people die of Anaphylaxis every year.",
      "1.2 million Australians have diabetes, but half of them don't know it .....yet!.",
      "In 70% of all cases, the cause of epilepsy cannot be identified.",
      "1 in 12 Australians have diabetes at the moment and 1/3 of all children born in 2005 will become diabetic.",
      "If rescuers are unwilling to do rescue breathing they should do chest compressions only at a rate of 100-120 per minute.",
      "The psychological value of rest & reassurance is as important in first aid as the treatment that you give.",
      "A human heart will cease beating within four minutes after breathing stops.",
      "Permanent brain damage can occur within four to six minutes after breathing stops.",
      "For an injured victim, immediate first aid can make the difference between complete recovery and permanent disability.",
      "Research shows that 90% of injuries are predictable and preventable.",
      "If you delay CPR by 1 minute they have a 10% less chance of survival. If you delay CPR by 2 minutes they have a 20% less chance of survival. If you delay CPR by 3 minutes they have a 30%           less chance of survival and brain damage starts.");
   var x=Math.floor(Math.random()*factArray.length);
   return factArray[x];
}
/* credits popup */
function credits(){
   var w=Math.floor(screen.availWidth/3);
   var h=Math.floor(screen.availHeight/3);
   var x=Math.floor((screen.availWidth/2)-(w/2));
   var y=Math.floor((screen.availHeight/2)-(h/2));
   var params="width="+w+",height="+h+",left="+x+",top="+y+"location=0"+
              "menubar=0,resizable=1,scrollbars=0,status=0,toolbar=0";
   window.open('credits.htm','Credits',params);
}
/* functions */
function addContent(tag,value){
   var x=document.createElement(tag);
   if(value){
      var y=document.createTextNode(value);
      x.appendChild(y);
   }
   return x;
}