/*
*  This addEvent function and associated functions were written by 
*  Dean Edwards, 2005 with input from Tino Zijdel, Matthias Miller, Diego Perini
*  http://dean.edwards.name/weblog/2005/10/add-event2/
*
*  This domLoad object is written by David Shuford (aka Kravvitz)
*  <http://www.dynamicsitesolutions.com/> is based on code written by 
*  Dean Edwards <http://dean.edwards.name/> (with input from John Resig 
*  <http://ejohn.org/> and Matthias Miller <http://www.outofhanwell.com/blog/>)
*  and two derivative works based on Dean's script by Rob Cherny 
*  <http://cherny.com/> and Tanny O'Haley <http://tanny.ica.com/>.
*
*  Free use of this script is permitted for commercial and non-commercial 
*  applications, subject to the requirement that this comment block be kept 
*  and not be altered.  The data and executable parts of the script may be 
*  changed as needed.  Dynamic Site Solutions makes no warranty regarding 
*  fitness of use or correct function of the script.  If you have any comments
*  or questions about this script, feel free to send them to 
*  <script_info@dynamicsitesolutions.com>.
*
*  Last Updated: 2008-03-22
*/

function addEvent(element,type,handler){
  if((type=="DOMContentLoaded") || (type.toLowerCase()=="domload")) {
    domLoad.add(handler);
    return;
  }
  if(element.addEventListener){
    element.addEventListener(type,handler,false);
  } else {
    // assign each event handler a unique ID
    if(!handler.$$guid) handler.$$guid=addEvent.guid++;
    // create a hash table of event types for the element
    if(!element.events) element.events={};
    // create a hash table of event handlers for each element/event pair
    var handlers=element.events[type];
    if(!handlers){
      handlers=element.events[type]={};
      // store the existing event handler (if there is one)
      if(element["on" + type]){
        handlers[0]=element["on" + type];
      }
    }
    // store the event handler in the hash table
    handlers[handler.$$guid]=handler;
    // assign a global event handler to do all the work
    element["on" + type]=handleEvent;
  }
};
// a counter used to create unique IDs
addEvent.guid=1;

function removeEvent(element,type,handler){
  if(element.removeEventListener){
    element.removeEventListener(type,handler,false);
  } else {
    // delete the event handler from the hash table
    if(element.events && element.events[type]){
      delete element.events[type][handler.$$guid];
    }
  }
};

function handleEvent(event){
  var returnValue=true;
  // grab the event object (IE uses a global event object)
  event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event,this);
  // get a reference to the hash table of event handlers
  var handlers=this.events[event.type];
  // execute each event handler
  for(var i in handlers){
    this.$$handleEvent=handlers[i];
    if(this.$$handleEvent(event) === false){
      returnValue=false;
    }
  }
  return returnValue;
};

function fixEvent(e,el){
  // add W3C standard event methods
  e.preventDefault=fixEvent.preventDefault;
  e.stopPropagation=fixEvent.stopPropagation;
  try{  // IE5/Mac generates an error here sometimes
    e.target=e.target||e.srcElement;
  }catch(ex){}
  e.currentTarget=e.currentTarget||el;
  if(e.type.indexOf('mouse')!=-1)
    e.relatedTarget=(e.relatedTarget?e.relatedTarget:
      (e.type=='mouseout'?e.toElement:e.fromElement));
  return e;
};
fixEvent.preventDefault=function(){
  this.returnValue=false;
};
fixEvent.stopPropagation=function(){
  this.cancelBubble=true;
};

// Tino Zijdel  http://therealcrisp.xs4all.nl/upload/addEvent_dean.html
// This little snippet fixes the problem that the onload attribute on the 
// body-element will overwrite previous attached events on the window object 
// for the onload event
if(!window.addEventListener){
  document.onreadystatechange=function(){
    if(window.onload && window.onload != handleEvent){
      addEvent(window,'load',window.onload);
      window.onload=handleEvent;
    }
  }
}


if(![].push){ // for old browsers that don't natively support Array.push()
  Array.prototype.push=function(){
    for(var i=0,l=arguments.length;i<l;i++) this[this.length]=arguments[i];
    return this.length;
  }
}

var domLoad={
  fnAr:[],
  inited:false,
  domDoneLoading:false,
  add:function(fn){
    (domLoad.domDoneLoading)?fn():this.fnAr.push(fn);
    this.init();
  },
  init:function(){
    if(this.inited) return;
    this.inited=true;
    // Mozilla (Firefox, NS7.x, etc.), Opera 9+
    if(document.addEventListener)
      document.addEventListener("DOMContentLoaded",domLoad.loaded,false);
    // for Safari, Konqueror, iCab
    if(/WebKit|KHTML|iCab/i.test(navigator.userAgent) && document.readyState){
      domLoad._timer=setInterval(function(){
        if(/loaded|complete/.test(document.readyState)){
          clearInterval(domLoad._timer);
          delete domLoad._timer;
          domLoad.loaded();
        }
      },10);
    }
    // for IE5+/Win (using their proprietary conditional compilation feature)
    /*@cc_on @if((@_win32 || @_win64) && (@_jscript_version >= 5))
      document.write("<script defer src='//:' onreadystatechange="+
        "\"if(this.readyState=='complete')domLoad.loaded();\"><\/script>");
    @end @*/
    // a normal window.onload handler will be used if none of the others work
    addEvent(window,'load',domLoad.loaded);
  },
  loaded:function(){
    if(domLoad.domDoneLoading) return;
    domLoad.domDoneLoading=true;
    // Safari 1.0 didn't like it when I used a local variable as a  
    // reference to domLoad.fnAr
    for(var i=0,l=domLoad.fnAr.length;i<l;i++)
      try{domLoad.fnAr[i]();}catch(ex){}
    domLoad.fnAr=[];
  }
};
function hasClassName(el,c){
  c=c.replace(/\-/g,'\\-');
  return (new RegExp('(^|\\s)'+c+'(\\s|$)')).test(el.className);
}
function addClassName(el,c){
  if(hasClassName(el,c)) return;
  var curClass=el.className||'';
  el.className=curClass+((curClass.length>0)?' ':'')+c;
}
function removeClassName(el,c){
  var re=new RegExp('\\s*'+c.replace(/\-/g,'\\-')+'\\s*');
  el.className=el.className.replace(re,' ').replace(/^\s*|\s*$/g,'');
}
function replaceClassName(el,oldC,newC){
  removeClassName(el,oldC),addClassName(el,newC);
}

var isMSIE=/*@cc_on!@*/false; // http://dean.edwards.name/weblog/2007/03/sniff/
/*@cc_on @if(@_jscript && !(@_win32 || @_win64) && (@_jscript_version < 5.5))
var isIEmac=true; @else @*/ var isIEmac=false; /*@end @*/
var isWebkit=(navigator.userAgent.search(/Webkit\/(\d+)/i)!=-1);
var isWebkit500plus=false;
if(isWebkit && (Number(RegExp.$1)>=500)) isWebkit500plus=true;

var ReplaceCheckboxes={
  images:[
    '/i/main/check/0.gif',
    '/i/main/check/1.gif'
  ],
  init:function(){
    if(!document.getElementsByTagName || isIEmac) return;
    if(window.opera && !opera.version) return; // if older Opera version
    if(isWebkit && !isWebkit500plus) return;
    // Safari 1.0-2.x don't support checking a checkbox by clicking on its 
    // label. It seems to be fixed in 3.x.

    // we're making sure images are enabled
    var d=document, b=d.getElementsByTagName('body')[0];
    var img = b.appendChild(d.createElement('img'));
    if(!window.opera) img.className='offLeft';
    else img.setAttribute('style','position:absolute;bottom:0;right:0;');
    img.src=ReplaceCheckboxes.images[1]; // this can be any image
    img.onload=function(){
      ReplaceCheckboxes.realInit();
      if(img.parentNode) img.parentNode.removeChild(img);
    }
    img.src=ReplaceCheckboxes.images[1]; // setting src a 2nd time avoids bugs
  },
  realInit:function(){
    if(!document.getElementsByTagName || isIEmac) return;
    ReplaceCheckboxes.pairLabels();
    var inputs=document.getElementsByTagName('input'),cb,i=0,lbl;
    while(cb=inputs[i++]){
      if((cb.type.toLowerCase()!='checkbox')||!hasClassName(cb,'replace'))
        continue;
      if(!cb._lbl) continue;
      addClassName(cb,'offLeft');
      addClassName(cb._lbl,(cb.checked?'checkbox':'uncheckbox'));
      cb._lbl.onclick=cb.onclick=ReplaceCheckboxes.click;
      cb.onfocus=ReplaceCheckboxes.focus;
      cb.onblur=ReplaceCheckboxes.blur;
    }
  },
  pairLabels:function(){
    if(!document.getElementsByTagName || isIEmac) return;
    var labels=document.getElementsByTagName('label'),lbl,i=0,fld,k,idCnt=0;
    var re=/input|textarea|select/i;
    while(lbl=labels[i++]){
      fld=null,k=0;
      if(lbl.htmlFor){
        fld=document.getElementById(lbl.htmlFor);
      } else {
        var kids=(isMSIE&&lbl.all)?lbl.all:lbl.getElementsByTagName('*');
        while(fld=kids[k++]) if(re.test(fld.nodeName)) break;
        if(fld && fld.id){       // mainly for IE5-6, which don't support 
          lbl.htmlFor = fld.id;  // implicit association of a label with its 
        } else if(fld) {         // form control, so need it done explicitly
          while(document.getElementById('zfc'+idCnt)) idCnt++;
          lbl.htmlFor = fld.id = 'zfc'+idCnt;
          idCnt++;
        }
      }
      if(!fld) continue;
      lbl._fc=fld,fld._lbl=lbl;
    }
  },
  click:function(){
    var lbl=this._lbl||this;
    if((this.nodeName.toLowerCase()=='input'?this:this._fc).checked)
      replaceClassName(lbl,'uncheckbox','checkbox');
    else
      replaceClassName(lbl,'checkbox','uncheckbox');
  },
  focus:function(){addClassName(this._lbl,'cbFocus');},
  blur:function(){removeClassName(this._lbl,'cbFocus');}
};
addEvent(window,'domload',ReplaceCheckboxes.init);

