// ---------------------------------------------------------------------
//                             onLoad Handler
// ---------------------------------------------------------------------
var old = window.onload; // catch any existing onload calls 1st
window.onload = function () {
  if (old) { // execute existing onloads
    old();
  };
  for (var ii = 0; arguments.callee.actions.length > ii; ii++) {
    arguments.callee.actions[ii]();
  };
};
window.onload.actions = [];

// ---------------------------------------------------------------------
//                             fixLinks
// ---------------------------------------------------------------------
function fixLinks() {
  if (!document.getElementsByTagName) return null;
  var server = document.location.hostname;
  var anchors = document.getElementsByTagName("a");
  var id, href, title;
  for(var i=0; i < anchors.length; i++){
    if(!anchors[i].href) continue;
    href = anchors[i].href;
    title = anchors[i].title;
    id = anchors[i].id;
    if(href.indexOf(server) == -1){ // Href is not a file on my server
      if(href.indexOf("javascript:") == -1){ // Href is not a javascript call
        if(!anchors[i].onclick){ // Href does not have an onclick event
          if(href.indexOf("mailto:") == -1){ // Href is not a mailto:
            if (((href.indexOf("http://") != -1) ||
                 (href.indexOf("https://") != -1)) &&
                 href.indexOf('ctsavesenergy.org') == -1 ){ // Href is not relative (for Safari)
                                                            // or pointing to our server (from Atomz)
              anchors[i].setAttribute("target","_blank");
              anchors[i].setAttribute("title",title + " [This Link Will Open in a New Window]");
            }
          }
        }
      }
    }
  }
  return null;
}
window.onload.actions.push(fixLinks);

// sIFR replace statements (for the pretty fonts)
function runMyIFR() {
  if (sIFR != null && sIFR.replaceElement != null) {
    sIFR.replaceElement('#main h1',
                        'http://www.ctsavesenergy.org/fonts/basenine.swf', 
                        '#0076c0', '#000000', '#000000', '#ffffff',
                        0, 0, 0, 0);
    if ( document.getElementById('programnav') ) {
      sIFR.replaceElement('#main #programnav h2', 'http://www.ctsavesenergy.org/fonts/basenine.swf', 
                          '#efa11a', '#000000', '#000000', '#ebebeb',
                          0, 0, 0, 0);
    }
    sIFR.replaceElement('#main h2', 'http://www.ctsavesenergy.org/fonts/basenine.swf', 
                        '#0076c0', '#000000', '#000000', '#ffffff',
                        0, 0, 0, 0);
    sIFR.replaceElement('#main h3', 'http://www.ctsavesenergy.org/fonts/basenine.swf', 
                        '#0076c0', '#000000', '#000000', '#ffffff',
                        0, 0, 0, 0);
  };
};
window.onload.actions.push(runMyIFR);

function programNav() {
  if ( !document.getElementById ||
       !document.getElementsByTagName ||
       !document.getElementById('programnav') ) return false;
  var target = document.getElementsByTagName('h1')[0];
  var cntnr  = document.getElementById('programnav');
  var links  = cntnr.getElementsByTagName('a');
  var images = new Object();
  for (var i=0; i<links.length; i++) {
    images[i] = new Image();
    images[i].src = 'images/home/home-h1-'+links[i].className+'.jpg';
    links[i].onmouseover = function() {
        target.style.backgroundImage = 'url(images/home/home-h1-'+this.className+'.jpg)';
      }
    links[i].onmouseout = function() {
        target.style.backgroundImage = 'url(images/home/home-h1.jpg)';
      }
  }
  return true;
}
window.onload.actions.push(programNav);

function setSearch() {
  if ( !document.getElementById ||
       !document.getElementsByTagName ||
       !document.getElementById('nav-search') ) return false;
  var cntnr = document.getElementById('nav-search');
  var label = cntnr.getElementsByTagName('label')[0];
  var txt = label.firstChild.nodeValue;
  var field = document.getElementById('searchBox');
  field.value = txt;
  field.onfocus = function() {
    if (this.value == txt) {
      this.value = '';
    }
  }
  field.onblur = function() {
    if (this.value == '') {
      this.value = txt;
    }
  }
  label.style.visibility = 'hidden';
  cntnr.getElementsByTagName('form')[0].onsubmit = function() {
    var srchBxVal = document.getElementById('searchBox').value;
    var labelVal = document.getElementById('nav-search').getElementsByTagName('label')[0].firstChild.nodeValue;
    var goBtnVal = document.getElementById('srch-submit').value;
    if ( srchBxVal == labelVal ) {
      alert( 'Please enter a search query before pressing \u201C'+goBtnVal+'\u201D' );
      return false;
    } else {
      return true;
    }
  }
  return true;
}
window.onload.actions.push(setSearch);

// ---------------------------------------------------------------------
//                             AutoBlink
// (c) 2005 Chris Ridings   http://www.searchguild.com
// Redistribute at will but leave this message intact
// ---------------------------------------------------------------------
var autoBlinkCount;
function autoBlink() {
  if (document.getElementById && document.createElement) {
	autoBlinkCount=document.links.length;
	setTimeout("checklinks()",500);
  }
}
function checklinks() {
  if (!(autoBlinkCount==document.links.length)) {
    // Something changed the links!
    // Iterate for an id of _goog
    for (i=0; i < document.links.length; i++) {
      if (document.links[i].id.substring(0,5)=="_goog") {
        // If we find an id of _goog then remove the link!
        var tr = document.links[i].parentTextEdit.createTextRange();
        tr.moveToElementText(document.links[i]);
        tr.execCommand("Unlink",false);
        tr.execCommand("Unselect",false);
      }
    }
  }
  setTimeout("checklinks()",500);
}
window.onload.actions.push(autoBlink);