/* JAVASCRIPT \\\
**************************************************
TITLE: Base JavaScripts
DESCRIPTION:
   This file contains a set of JavaScript classes
	and functions that are used site-wide.
AUTHOR: David Mingos
CONTACT: http://dmdesigns.com/contact/
///
**************************************************
*/

restoreDefault = function() {
   if (!this.value){
      this.value=this.defaultValue;
      this.style.color = '#999999';
   }
}

clearDefault = function() {
   if (this.value==this.defaultValue){
      this.value='';
      this.style.color = '#000000';
   }
}


/*
for this you shoulc pass it an array of elements you need to check
to see if they exist an then apply the event handlers to them.
*/
setInputHandlers = function() {
   for(var i=0;i<document.forms.length;i++){
      f=document.forms[i];
      for(var j=0;j<f.length;j++){
         c=f[j];
         if(c.type == 'text' || c.type == 'textarea'){
            c.onfocus=clearDefault;
            c.onblur=restoreDefault;
         }
      }
   }
}

addEvent = function(obj, evType, fn, useCapture){
   if (obj.addEventListener){
      obj.addEventListener(evType, fn, useCapture);
      return true;
   } else if (obj.attachEvent){
      var r = obj.attachEvent("on"+evType, fn);
      return r;
   } else {
      // be silent
      // alert("Handler could not be attached");
   }
}

removeEvent = function(obj, evType, fn, useCapture){
   if (obj.removeEventListener) {
      obj.removeEventListener(evType, fn, useCapture);
      return true;
   } else if (obj.detachEvent){
      var r = obj.detachEvent("on"+evType, fn);
      return r;
   }
}


/* SCRIPT FROM A LIST APART'S STYLESHEET SWITCHER
*******************************************************************************

Source: http://www.alistapart.com/stories/alternate/
Modifications:
   The window.onload and window.onunload statements have been put 
   into functions called loadActiveStylesheet() and unloadActiveStylesheet() so
   that they play well with other loading and unloading logic. Loading and 
   unloading is handled in page-scripts.js.
*/

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function loadActiveStylesheet(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

function unloadActiveStylesheet(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

/*
*******************************************************************************
///
*/
