var d = document;        
var w = window;     
var dc = document.cookie;      
var loc = location.href; 
var nObj = d.getElementById;
var agt = navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);
var prefs = 'preferencesHolder';
var navSections = new Array('usage','journalism','attic','writings','search','subscribe');

    
function openWin(dest,winName,theDetails) {	
  newWin = w.open(dest, winName,theDetails);	
}

function checkAt(email) {
  if (email.indexOf('@') == -1 || email.indexOf('.') < 3 || email.length < 5) { alert('Check the email address.'); return false; }
}

function setStyle(i, p, v) {
	var sObj = d.getElementById(i);
	sObj.style[p] = v;
}

function getStyle(i, p) {
 var sObj = d.getElementById(i);
 var sheets = d.styleSheets;
 if(p == 'n'){ p = 'none'; }
 else if(p == 'b'){ p = 'block'; }

 var s = eval("sObj.style." + p);
 if((s != "") && (s != null)) { return s; }

 if(sObj.currentStyle) {
  var s = eval("sObj.currentStyle." + p);
  if((s != "") && (s != null)) { return s; }
 } 

 if(sheets.length > 0) {
  for(var x = 0; x < sheets.length; x++){
   var rules = sheets[x].cssRules;
   if(rules.length > 0) {
    for(var y = 0; y < rules.length; y++) {
     var z = rules[y].style;
     if(((z[p] != "") && (z[p] != null)) && (rules[y].selectorText == '#' + i)) { return z[p]; }
    }
   }
  }  
 } return null;
}

function hideShowEl(i,v) {
  /*
  var sObj = d.getElementById(i); 
  if(v == 'n'){ sObj.style.display = 'none'; }
  else if(v == 'b'){ sObj.style.display = 'block'; }
  else if(v == 'i'){ sObj.style.display = 'inline'; }
  else if(v == '') {
    if(sObj.style.display == 'none') sObj.style.display = 'block';  
    else if(sObj.style.display == 'block') sObj.style.display = 'none';
  }
  */
}

function hideShowImg(i,v,imgN,imgB){
  var sObj = d.getElementById(i).style.display; 
  var img = i + 'Img';
  if(sObj == 'none') d.images(img).src = imgB; 
  else if(sObj == 'block') d.images(img).src = imgN; 
  hideShowEl(i,v);
}

function hideShowList(i,v){
  if(v == '') {
    var sObj = d.getElementById(i); 
    if(sObj.style.display == 'none') v = 'b';  
    else if(sObj.style.display == 'block') v = 'n';
  }  
  for(x = 0; x < navSections.length; x++){ hideShowEl(navSections[x],'n'); }
  hideShowEl(i,v)
  if(agt.indexOf('mac') == -1){
    drawIcon('navSections')
  }
}

function writeAttr(i,sT,sA,nT,nA,nAv){
 var sObj = d.getElementById(i); 
 var tag = sObj.getElementsByTagName(sT);
 var space, date, wrap, marker = '';
 if(d.createElement){ for(x=0;x<tag.length;x++){ 
   value = d.createTextNode(tag[x].getAttribute(sA));
   spacer = d.createTextNode(' ');
   wrap = d.createElement(nT); wrap.setAttribute(nA,nAv);
   wrap.appendChild(spacer); wrap.appendChild(value); tag[x].appendChild(wrap);
 }}
} 

function drawIcon(a){
 if((d.createTextNode) && (agt.indexOf('mac') == -1)) {
  var menu = d.getElementById(a);
  var icons = menu.getElementsByTagName('B')
  var containers = menu.getElementsByTagName('DIV')
  var icon, blurb, section = ''
  for(x = 0; x < icons.length; x++){
	var iObj = icons[x];
	var lObj = icons[x].parentNode;
    var section = ' what\'s in \"' + lObj.childNodes[1].nodeValue + '\"';
    if(containers[x].style.display == 'block') { icon = '\-'; blurb = 'Click to hide' + section } 
    else { icon = '\+'; blurb = 'Click to show' + section }  
	var tObj = d.createTextNode(icon);
    if(!iObj.hasChildNodes()) iObj.appendChild(tObj)
    else iObj.replaceChild(tObj,iObj.childNodes[0])
    lObj.setAttribute('href','javascript:hideShowList(\'' + containers[x].id + '\',\'\')')
    lObj.setAttribute('onmouseover','setStyle(\'' + icons[x].id + '\',\'background\',\'#FFC\')')
    lObj.setAttribute('onmouseout','setStyle(\'' + icons[x].id + '\',\'background\',\'none\')')
    lObj.setAttribute('title',blurb)
    hideShowEl(icons[x].id,'i')
//    boldNavSection()
  }
 } else if((d.all) && (agt.indexOf('mac') == -1)){
    var a = eval(a);
    for(x = 0; x < a.length; x++){
      if(x < a.length){ var i = a[x] + 'Icon'; }
      var sObj = d.getElementById(a[x]).style.display;
      var iObj = d.getElementById(i);
      if(agt.indexOf('mac') == -1){
        if(sObj == 'none') { iObj.innerHTML = '\+'; }
        else if(sObj == 'block') { iObj.innerHTML = '\-'; }
        hideShowEl(i,'i')
      }
    }    
  }
}

function boldNavSection(){
  var the_link = sObj.getElementsByTagName('A')   
  for(x = 0; x < the_link.length; x++){
    the_url = the_link[x].getAttribute('href')
    the_loc = location.href
    alert(the_url + ' ' + the_loc)
    if(the_loc.indexOf(the_url) != -1){
      the_link[x].setAttribute('title','Go now!')
      the_link[x].setAttribute('style','font-weight: bold;')
    } else the_link[x].setAttribute('title','Go!')
  }  
}

function pageSizer(){
  if(w.innerWidth){
    (w.innerWidth<570) ? hideShowEl('expandMe','b') : hideShowEl('expandMe','n')
  } else if(d.body.clientWidth) { 
    (d.body.clientWidth>600) ? hideShowEl('expandMe','n') : hideShowEl('expandMe','b')
  }	
}

function getCookie(name){  
  var the_cookie = name + "="; 
  if (d.cookie.length > 0) {        
    begin = d.cookie.indexOf(the_cookie);  
    if (begin != -1) {       
      begin += the_cookie.length;
      end = d.cookie.indexOf(";", begin);
        if (end == -1) end = d.cookie.length;
      return unescape(d.cookie.substring(begin, end));
    } 
  } return null;
}

function setCookie(name, value){
  var time = new Date();
  time.setTime(time.getTime() + (1000*60*60*24*365));//expires in a year
  d.cookie = name + "=" + escape(value) + "; path=/; expires=" + time.toGMTString();
}

function deleteCookie(name) {
  if (getCookie(name)) d.cookie = name + "=" + "; path=/; expires=Mon, 01 Jan 01 01:01:01 GMT";
}	

function randomArray(a) {
  return a[Math.floor(Math.random()*a.length)];
}

function checkLink(the_link){
  if((the_link.indexOf('%3Fnone') == -1) && 
     (the_link.indexOf('%23') == -1) && 
	 (the_link.indexOf('mailto%3A') == -1) && 
	 (the_link.indexOf('javascript%3A') == -1) && 
	 (the_link.indexOf('glossary/defn') == -1) && 
	 (the_link.indexOf('http%3A\/\/www.saila') != -1))
     { return true } else { return false }
}
 
function whatTemplate(tmp){
   var links = d.getElementsByTagName('A');
   for(x = 0; x < links.length; x++){
    var the_link = escape(links[x])
    if(checkLink(the_link)) links[x].setAttribute('href',unescape(the_link) + '?' + tmp)
  }
}
 
function emailMe(sbj){
  var user = "craig";
  var domain = "saila.com";
  var mailto = "mailto:" + user + "@" + domain;
  if(sbj.length > 0) mailto = mailto + "?subject=" + sbj;
  location.href = mailto;
} 
 
 
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
} 
 