/* PAGE INITIALIZATION FUNCTIONS */
var addEvent = function(obj, type, fn) { //http://www.ilfilosofo.com/blog/2008/04/14/addevent-preserving-this/
  if (obj.addEventListener) {
    obj.addEventListener(type, fn, false);
  } else if (obj.attachEvent) {
    obj.attachEvent('on' + type, function() { return fn.apply(obj, new Array(window.event));});
  }
}
addEvent(window, 'load', preProcessPage);
addEvent(window, 'resize', preProcessPage);

function preProcessPage() {
	if (!document.getElementsByTagName || (!document.getElementById || (!document.createElement || !document.createTextNode))) return false;
	processPage();
}

function processPage() {
  processDivs();
  processLinks();
  setSize();
  createTheDJ();
}

function createTheDJ() {
	if($('flash_container')) {
		swfobject.embedSWF("images/be_the_dj.swf", "flash_container", "300", "350", "9.0.0", null, null, {wmode: 'transparent', quality: 'high', bgcolor: '#ffffff', align: 'middle', allowScriptAccess:'sameDomain',allFullScreen: 'false'});
		$('flash_container').setStyle({marginLeft: '-210px'});
	} else if ($('homepage_flash_container')) {
    	swfobject.embedSWF("images/index_flash.swf", "homepage_flash_container", "100%", "100%", "9.0.0", null, null, {wmode: 'transparent', quality: 'high', bgcolor: '#ffffff', align: 'middle', allowScriptAccess:'sameDomain',allFullScreen: 'false'});
	}
}

/* INDIVIDUAL ELEMENT INITIALIZATIONS */
function setSize() {
  var theHeight = document.body.offsetHeight;
  var windowHeight = document.documentElement.clientHeight;
  if (windowHeight > theHeight) {
    theHeight = windowHeight;
  }
  var heightSet = new Array();
  //html, body, #container_left, #container_right, #container_inner
  heightSet.push(document.getElementsByTagName("html")[0]);
  heightSet.push(document.getElementsByTagName("body")[0]);
  heightSet.push(document.getElementById("container_left"));
  heightSet.push(document.getElementById("container_right"));
  heightSet.push(document.getElementById("container_left"));
  var setHeight = function(theElement) {
    if (theElement) {
      theElement.style.height = theHeight + "px"; 
    }
  }
  forEach(heightSet,setHeight);
}

function processDivs() {
  var theDivs = document.getElementsByTagName("div");
  var fixDiv = function(theDiv) {
    if (hasClassHook(theDiv)) {
      if (/js_showLogo/.test(theDiv.className)) {
        showLogo(theDiv);
      }
      if (/js_showGirl/.test(theDiv.className)) {
        showGirl(theDiv);
      }
    }
  } 
  forEach(theDivs,fixDiv);
}

function processLinks() {
  var theLinks = document.getElementsByTagName("a");
  var fixLink = function(theLink) {
    if (hasClassHook(theLink)) {
      if (/js_toggleDiv/.test(theLink.className)) {
        theLink.onclick = function() {
          toggleDiv(this);
          return false;
        }
      }
      if (/js_newWindow/.test(theLink.className)) {
        theLink.onclick = function() {
          window.open(this.href);
          return false;
        }
      }
      if (/js_doPopup/.test(theLink.className)) {
        theLink.onclick = function() {
          doPopup(this);
          return false;
        }
      }
    }
    if (theLink.href && (theLink.href == document.location.href)) {
      addClass(theLink,"currentPage");
    }
  }
  forEach(theLinks,fixLink);
}

function doPopup(theLink) {
  var newUrl = getExtensionFromClass(theLink,"js_popupTarget_");
  if (newUrl) {
    var newWidth = '500';
    var newHeight = '400';
    var classWidth = getExtensionFromClass(theLink,"js_popupWidth_");
    var classHeight = getExtensionFromClass(theLink,"js_popupHeight_");
    if (classWidth) {
      newWidth = classWidth;
    }
    if (classHeight) {
      newHeight = classHeight;
    }
    window.open(newUrl,'popup','height='+newHeight+',width='+newWidth+',resizable=yes,menubars=no,location=no,scrollbars=yes');
  }
}

function toggleDiv(theLink) {
  var divId = getExtensionFromClass(theLink,"js_divTarget_");
  if (document.getElementById(divId)) {
    Effect.toggle(divId,'appear');
  }
}

function showLogo(theDiv) {
  var theContainer = document.getElementById("container_left");
  var container_pos = findPos(theContainer)[0];
  var container_width = theContainer.offsetWidth;
  removeClass(theDiv,"noshow");
  theDiv.style.left = container_pos + ((container_width / 2) - (theDiv.offsetWidth / 2)) + "px";
  //theDiv.style.left = ((document.body.offsetWidth / 2) - (theDiv.offsetWidth / 2)) + "px";
}

function showGirl(theDiv) {
  removeClass(theDiv,"noshow");
  theDiv.style.left = ((document.body.offsetWidth / 2) + 230) + "px";
}

/* UTILITY FUNCTIONS */
function forEach(array, action) {
  for (var i=0; i < array.length; i++) {
    action(array[i]);
  }
}

function removeChildNodes(theElement) {
  while (theElement.hasChildNodes()) {
    theElement.removeChild(theElement.firstChild);
  }
}

function getParentElement(theElement, targetNodeName) {
  var bodyNode = document.getElementsByTagName("body")[0];
  var currentParent = theElement;
  var targetNodeName = targetNodeName.toLowerCase();
  while ((currentParent.nodeName.toLowerCase() != targetNodeName) && (currentParent != bodyNode)) {
    currentParent = currentParent.parentNode;
  }
  return currentParent;
}

function stripWhitespace(theString) {
	return theString.replace(/^\s*|\s*$/g,'');
}

function removeWhitespace(theString) {
  return theString.replace(/\s+/g,'')
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
    do {
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    } while (obj = obj.offsetParent);
  }
  return [curleft,curtop];
}

function addClass(theElement,theClass) {
	if (!theElement.className) {
		theElement.className = theClass;
	} else if (theElement.className.indexOf(theClass) == -1) {
		theElement.className += (" " + theClass);
	}
}

function removeClass(theElement,theClass) {
  if (theElement.className && theElement.className.indexOf(theClass) != -1) {
    theElement.className = stripWhitespace(theElement.className.replace(theClass,""))
  }
}

function hasClassHook(theElement) {
  return (theElement.className && /js_/.test(theElement.className));
}

function getExtensionFromClass(theElement,theKey) {
	var theExtension = false;
	if (theElement.className) {
    var findExtension = function(thisClass) {
      if (thisClass.indexOf(theKey) == 0) {
        var tempName = thisClass.substring(theKey.length);
        if (tempName.length > 0) {
          theExtension = tempName;
        }
      }
    }
    forEach(theElement.className.split(' '),findExtension);
  }
	return theExtension;
}