/* Function to addEventListener to onload
 * @param func - a function which should be executed once the page has loaded
 * http://simon.incutio.com/archive/2004/05/26/addLoadEvent
 * it will work even if something has previously been assigned to window.onload
 * without using addLoadEvent itself. 
 */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

var gaPageName = "";

function globalOnLoad() {
	GetAnchors();
}

/*
 * Function to clear a text field
 * @param thefield
 *     the field to clear
 */
function cleartext(thefield) {
	if (thefield.defaultValue == thefield.value) {
		thefield.value = "";
	}
}

function GetAnchors() {
  if (document.getElementById) {
    var elements = new Array('a', 'area');
    for (var j=0; j < elements.length; j++) {
      var x = document.getElementsByTagName(elements[j]);
      for (var i=0;i<x.length;i++) {
        if (x[i].className.indexOf('newWindow') != -1) {
          x[i].onkeypress = openNewWindow;
          x[i].onclick = openNewWindow;
          x[i].setAttribute("title", "New Window");
        } else if (x[i].className.indexOf('popup') != -1) {
          x[i].onkeypress = openNewWindow;
          x[i].onclick = openNewWindow;
          x[i].setAttribute("title", "Pop-up Window");
        } else if (x[i].className.indexOf('catchCode') != -1) {
          //Will not add if one of the above is used,
          //so opneNewWindow also checks
          x[i].onkeypress = gaCatchCode;
          x[i].onclick = gaCatchCode;
        } else if (isAssetDoc(x[i])) {
          var fileExt = getFileExt(x[i]);
          x[i].onkeypress = openNewWindow;
          x[i].onclick = openNewWindow;
          x[i].setAttribute("title", fileExt.toUpperCase()+" file");
        }
      }
    }
  }
}

function gaCatchCode(e) {
  urchinTracker(GetGAPath(this));
}

function GetGAPath(obj) {
  return "/catchcode/" + getValueFromClass(obj, 'ga');
}

function openNewWindow(e) {
  if (this.className.indexOf('catchCode') != -1) {
    urchinTracker(GetGAPath(this));
  } else if (isAssetDoc(this)) {
    var url = this.href;
    var parentHost = window.location.protocol+"//"+window.location.host;
    if (url.substring(0,parentHost.length)==parentHost) {
      url = url.substr(parentHost.length);
    } else if (url.substring(0,23)=="http://www.texaco.com/") {
      url = url.substr(22);  
    }
    urchinTracker(url);
  }
  var features = '';
  var start;
  var end;
  var width;
  var height;
  var thisurl = this.href ? this.href : "";
  width = parseInt(getValueFromClass(this, 'w'))>0 ? getValueFromClass(this, 'w') : '';
  height = parseInt(getValueFromClass(this, 'h'))>0 ? getValueFromClass(this, 'h') : '';
  if (height.length>0 || width.length>0) {
    features += height.length>0 ? 'height='+height+',' : '';
    features += width.length>0 ? 'width='+width+',' : '';
  }
  features += getFeatures(this);
   
  if (features.length>0) {
    if (features.substr(features.length-1,1) == ",")
      features = features.substr(0, features.length-1);
    window.open(this.href, '_new', features);
  } else {
    window.open(this.href);
  }
  return false;
}

function getValueFromClass(obj, attrib) {
  if (obj.className.indexOf(" "+attrib) != -1) {
    start = obj.className.indexOf(" "+attrib) + 1
    end = obj.className.indexOf(" ", start);
    end = (end == -1) ? obj.className.length - start : end - start;
    var aLength = attrib.length;
    return obj.className.substr(start+aLength, end-aLength);
  } else {
    return "";
  }
}

function isAssetDoc(obj) {
  var types = new Array("pdf", "doc", "xls");
  var fileExt = getFileExt(obj);
  
  for(i=0; i<types.length;i++) {
    if (types[i] == fileExt) {
      return true;
    }
  }
  return false;
}

function getFileExt(obj) {
  var url = obj.href;
  return url.substr(url.lastIndexOf(".")+1);
}

function getFeatures(obj) {
  var features = "";
  if (obj.className.indexOf(" scroll") != -1) {
    features += "scrollbars=yes,";
  }
  return features;
}

addLoadEvent(GetAnchors);
