/* IE 5.0 does not support the push method, so here goes */
if(Array.prototype.push == null) {
	Array.prototype.push = function(item) {
		this[this.length] = item;
		return this.length;
	};
};
/* same for shift */
if (Array.prototype.shift == null) {
  Array.prototype.shift = function() {
    var response = this[0];
    for (var i=0; i < this.length-1; i++) {
      this[i] = this[i + 1];
    };
    this.length--;
    return response;
  };
};
/*	Implement function.apply for browsers which don't support it natively
	Courtesy of Aaron Boodman - http://youngpup.net */
if (!Function.prototype.apply) {
  Function.prototype.apply = function(oScope, args) {
    var sarg = [];
    var rtrn, call;
    if (!oScope) oScope = window;
    if (!args) args = [];
    for (var i = 0; i < args.length; i++) {
      sarg[i] = "args["+i+"]";
    };
    call = "oScope.__applyTemp__(" + sarg.join(",") + ");";
    oScope.__applyTemp__ = this;
    rtrn = eval(call);
    oScope.__applyTemp__ = null;
	return rtrn;
  };
};
// Get Element By Class Name Functionality
getElementsByClassName = function (c) {
    var ret = new Array();
    var j = 0;
    var objs = document.all ? document.all : document.getElementsByTagName("*");
    for(i = 0;i<objs.length;i++){
        if(objs[i].className == c){
            ret[j] = objs[i];
            j++;
        } // if
    } // for
    return ret;
}
// Simple Uppercase F'n
function ucFirst(text) {
  return text.substring(0,1).toUpperCase() + text.substring(1);
}
// Get Cookie Value
function getCookieValue(name) {
  var pos = allcookies.indexOf(name);
  if (pos != -1) {
    var start = pos + 8;
    var end = allcookies.indexOf(";",start);
    if (end == -1) end = allcookies.length;
    var value = allcookies.substring(start, end);
    value = unescape(value);
    return value;
  }
}

// onload will process an array we can push any functions to that we want
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 = [];

// sIFR replace statements (for the pretty fonts)
function runMyIFR() {
  if (sIFR != null && sIFR.replaceElement != null) {
    sIFR.replaceElement('h1', '/adchallenge/fonts/AGBookStencil.swf', 
                        '#c20904', '#000000', '#999999', 'transparent',
                        0, 0, 0, 0);
    sIFR.replaceElement('h2', '/adchallenge/fonts/AGBookStencil.swf', 
                        '#c20904', '#000000', '#999999', 'transparent',
                        0, 0, 0, 0);
    sIFR.replaceElement('caption', '/adchallenge/fonts/AGBookStencil.swf', 
                        '#c20904', '#000000', '#999999', 'transparent',
                        0, 0, 0, 0);
  };
};
window.onload.actions.push(runMyIFR);

sfTarget = function() {
  var sfEls = getElementsByClassName("sfHide"); 
  var aEls = document.getElementsByTagName("a");
  document.lastTarget = null;
  for (var i=0; i<sfEls.length; i++) {
    if (sfEls[i].id) {
      if (location.hash==("#" + sfEls[i].id)) {
        sfEls[i].className+=" " + "sfTarget";
        document.lastTarget=sfEls[i];
      }
      for (var j=0; j<aEls.length; j++) {
        if (aEls[j].hash==("#" + sfEls[i].id)) aEls[j].targetEl = sfEls[i]; aEls[j].onclick = function() {
          if (document.lastTarget) document.lastTarget.className = document.lastTarget.className.replace(new RegExp(" sfTarget\\b"), "");
          if (this.targetEl) this.targetEl.className+=" sfTarget"; document.lastTarget=this.targetEl;
          return true;
        }
      }
    }
  }
}
if (window.attachEvent) window.onload.actions.push(sfTarget);

function fixLinks()
{
  if (!document.getElementsByTagName) return null;
  var server = document.location.hostname;
  var anchors = document.getElementsByTagName("a");
  var i, href, title;
  for(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 is not relative (for Safari)
              anchors[i].setAttribute("target","_blank");
              anchors[i].setAttribute("title",title + " [This Link Will Open in a New Window]");
            }
          }
        }
      }
    }
    if(id.indexOf('enlarge') != -1) {
      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);

// Functions for setting CSS by width
var styleCookie = new Cookie(document, "styleCookie", 24, '/', 'drink-drive-lose.com');
if (!styleCookie.load() || !styleCookie.activeStyle) {
  styleCookie.activeStyle = 'Full';
}
var largeStyle = 'Full';
var smallStyle = 'Compact';
function checkBrowserWidth() {
  docheck = true;
  var agt = navigator.userAgent.toLowerCase();
  var ismac = (agt.indexOf("mac")>-1);
  if (ismac) {
  	if (navigator.appName == "Microsoft Internet Explorer") docheck = false;
  }
  if (docheck) {
    var width  = getBrowserWidth();
    var height = getBrowserHeight();
    if ((width > 995) && (height > 519)) {
      if (styleCookie.activeStyle != largeStyle) {
        setStylesheet(largeStyle);
        styleCookie.activeStyle = largeStyle;
      }
    } else {
      if (styleCookie.activeStyle != smallStyle) {
        setStylesheet(smallStyle);
        styleCookie.activeStyle = smallStyle;
      }
    }
  }
  var tmpStyle = styleCookie.activeStyle;
  styleCookie.remove();
  styleCookie.activeStyle = tmpStyle;
  styleCookie.store();
  return true;
}
function getBrowserWidth() {
  if (window.innerWidth) {
    return window.innerWidth;
  } else if (document.documentElement && document.documentElement.clientWidth != 0) {
    return document.documentElement.clientWidth;
  } else if (document.body) {
    return document.body.clientWidth;
  }
  return 0;
}
function getBrowserHeight() {
  if (window.innerHeight) {
    return window.innerHeight;
  } else if (document.documentElement && document.documentElement.clientWidth != 0) {
    return document.documentElement.clientHeight;
  } else if (document.body) {
    return document.body.clientHeight;
  }
  return 0;
}
function setStylesheet(styleTitle) {
  var currTag;
  if (document.getElementsByTagName) {
    for (var i = 0; (currTag = document.getElementsByTagName("link")[i]); i++) {
      if (currTag.getAttribute("rel").indexOf("style") != -1 && currTag.getAttribute("title")) {
        currTag.disabled = true;
        if(currTag.getAttribute("title") == styleTitle) {
          defaultStatus = 'Style: ' + styleTitle;
          currTag.disabled = false;
        }
      }
    }
  }
  return true;
}
window.onload.actions.push(checkBrowserWidth);
window.onresize = checkBrowserWidth;

// Silly stuff when people are idle
var timeoutID;
function beFunny() {
  var funnyStatements = new Array();
  funnyStatements[0] = 'Those things on the left are links... click one';
  funnyStatements[1] = 'I\'ll just wait for you then.';
  funnyStatements[2] = 'Whenever you\'re ready, I\'d be happy to show you more stuff';
  funnyStatements[3] = 'Hello? Anyone there?';
  funnyStatements[4] = 'Please click something, I\'m feeling neglected.';
  funnyStatements[5] = 'What\'s a website have to do to get a little attention around here?';
  var randNumber = Math.floor(Math.random() * (funnyStatements.length + 1));
  // make sure it is a valid entry
  while (randNumber > (funnyStatements.length - 1)) {
    randNumber = Math.floor(Math.random() * (funnyStatements.length + 1));
  };
  custAlert(funnyStatements[randNumber]);
};
function resetTimer() {
  remCustAlert();
  clearTimeout(timeoutID);
  timeoutID = setTimeout('beFunny()', 1200000);
};
function custAlert(say) {
  var myAlert = document.createElement("div");
  myAlert.className='alert';
  myAlert.setAttribute('id','myAlert');
  var text = document.createTextNode(say);
  myAlert.appendChild(text);
  document.getElementsByTagName("body")[0].appendChild(myAlert);
}
function remCustAlert() {
  var myAlert = document.getElementById('myAlert');
  if (myAlert) {
    myAlert.parentNode.removeChild(myAlert);
  }
}
window.onmousemove = resetTimer;

// AutoBlink
// Puts Google's Autolink on the Blink :)
// (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);
