/* 
*  Copyright 2007-2008 Dynamic Site Solutions.
*  Free use of this script is permitted for 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.  Terms for use of this script
*  in commercial applications may be negotiated; for this, or for other
*  questions, contact "license-info@dynamicsitesolutions.com".
*
*  Script by: Dynamic Site Solutions -- http://www.dynamicsitesolutions.com/
*  Created: 2006-11-04; Last Updated: 2008-05-14
*/

function isEmpty(s){return ((s=='')||/^\s*$/.test(s));}
function isset(v){return (typeof(v)!='undefined');}
if(![].inArray){
  Array.prototype.inArray = function(val){
  	for(var i=0,j=this.length;i<j;i++){
      if(this[i] === val) return true;
    }
  	return false;
  }
}
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,'');
}
var isMSIE=/*@cc_on!@*/false; // http://dean.edwards.name/weblog/2007/03/sniff/

/*
   Opera 7.x and Netscape 6.0-7.0 have issues with this.
*/

var filterChart = {
  init: function(){
    if(!document.getElementsByTagName || !document.createTextNode) return;
    // IE5/Mac generates errors, so if we detect it we exit the function.
    /*@cc_on @if(@_jscript && !(@_win32 || @_win16)) return; @end @*/
    var tbl=filterChart.findTable();
    if(!tbl) return;
    var th = tbl.getElementsByTagName('thead');
    if(!th) return;
    else th = th[0].getElementsByTagName('th');
    if(!th || !th.length) return;
    var box = document.createElement('div');
    box.id='browserCbs';
    if(isMSIE) box.style.zoom='1';
    box.style.border='1px solid #093';
    box.style.margin='1em auto';
    box.style.padding='.8em';
    box.style.textAlign='center';
    var box2 = document.createElement('form');
    box2.onsubmit=function(){return false;}
    box2=box.appendChild(box2);
    var list=document.createElement('ul');
    list.style.margin='0 0 .6em';
    list.style.padding='0';
    list.style.textAlign='left';
    addClassName(list,'clearfix');
    if(isMSIE) list.style.zoom='1';
    for(var i=1;i<th.length;i++) {
      if(th[i].title && !isEmpty(th[i].title)) {
        list.appendChild(filterChart.addCheckbox(th[i].title,i-1));
      }
    }
    box2.appendChild(list);
    var btn = document.createElement('input');
    btn.type='button';
    btn.value='Update Chart';
    btn.onclick=function(){
      filterChart.hideShowSelectedBrowsers();
    }
    box2.appendChild(btn);
    if(tbl.nextSibling) tbl.parentNode.insertBefore(box, tbl.nextSibling);
    else tbl.parentNode.appendChild(box);
    if(filterChart.defaultHiddenColumns.length){
      var p=document.createElement('p');
      p=tbl.parentNode.insertBefore(p,tbl);
      p.style.textAlign='center';
      p.appendChild(document.createTextNode(
        'Note: Not all columns are shown by default.'));
    }
    filterChart.hideShowSelectedBrowsers();
  },
  addCheckbox : function(str,num) {
    var b=document.createElement('li'),l,c;
    b.style.cssFloat=b.style.styleFloat='left';
    b.style.width='13em';
    b.style.margin='0';
    b.style.padding='0';
    b.style.listStyleType='none';
    l=document.createElement('label');
    l.htmlFor='browserCb'+num;
    var chk = !filterChart.defaultHiddenColumns.inArray(str);
    if(isMSIE) {
      c=document.createElement('<input type="checkbox" id="browserCb'+        
      num+'" value="'+num+'" '+(chk?'checked':'')+'>');
    } else {
      c=document.createElement('input');
      c.type='checkbox';
      c.id='browserCb'+num;
      c.value=num;
      if(c.setAttribute) c.setAttribute('value',num); // for Konqueror
      if(chk) c.checked=true;
      // to fix a Netscape 6 bug
      if(c.setAttribute && chk) c.setAttribute('checked','checked');
    }
    l.appendChild(c);
    l.appendChild(document.createTextNode(' '+str));
    b.appendChild(l);
    return b;
  },
  findTable : function() {
    var tbls=document.getElementsByTagName('table'),re=/(^|\\s)chart(\\s|$)/;
    for(var i=0;tbls.length>i;i++) {
      if(re.test(tbls[i].className)) return tbls[i];
    }
    return null;
  },
  hideShowSelectedBrowsers : function() {
    // I'm detecting Konqueror because this function was crashing it. Giving 
    // the table display:none while the cells are hidden keeps it from crashing.
    var ua = navigator.userAgent.toLowerCase();
    var konq = ((ua.indexOf('konqueror')!=-1) && (document.all || 
    (ua.indexOf('khtml')!=-1)));
    var f=document.getElementById('browserCbs');
    var cbs=f.getElementsByTagName('input');
    var tbl=filterChart.findTable();
    if(konq) tbl.style.display='none';
    var rows=tbl.getElementsByTagName('tr');
    var cells=null,cellType='',offset=0,idx;
    for(var i=0;i<rows.length;i++) {
      if(rows[i].parentNode.tagName.toLowerCase()!='tbody')
        cellType='th',offset=1;
      else
        cellType='td',offset=0;
      cells=rows[i].getElementsByTagName(cellType);
      for(var k=0;k<cbs.length;k++) {
        if(cbs[k].type!='checkbox') continue;
        idx = parseInt(cbs[k].value,10)+offset;
        if(!isset(cells[idx]) || !isset(cells[idx].style)) continue;
        cells[idx].style.display=(cbs[k].checked?'':'none');
      }
    }
    if(konq) setTimeout(function(){tbl.style.display='';},1); // for Konqueror
  },
  defaultHiddenColumns: [
    'Mozilla Firefox 1.0.x',
    'IE4/Win',
    'Opera 8.x',
    'Opera 7.x',
    'Opera 6.x',
    'Safari 1.3',
    'Safari 1.2',
    'Safari 1.0',
    'Konqueror 3.2-3.5',
    'iCab 3.0',
    'Netscape 4.x'
  ]
}

var LoadHandler = {
  handlers:[],
  add:function(fn){
    if(window.onload!=LoadHandler.theHandler) LoadHandler._push(window.onload);
    LoadHandler._push(fn);
    window.onload=LoadHandler.theHandler;
  },
  _push:function(fn){
    if(typeof(fn)!='function') return;
    LoadHandler.handlers[LoadHandler.handlers.length]=fn;
  },
  theHandler:function(){
    var handlers=LoadHandler.handlers,i=-1,fn;
    while(fn=handlers[++i]) fn();
  }
}

LoadHandler.add(filterChart.init);


