
//	-----
//	Common reference functions here
//	-----

//	Originally by Simon Willison, found in "DOM Scripting" by Jeremy Keith
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

//	Written by Jonathan Snook, http://www.snook.ca/jonathan
//	Add-ons by Robert Nyman, http://www.robertnyman.com
function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}

//	Pulled from "DOM Scripting" by Jeremy Keith
function addClass(element, value) {
	if (!element.className) {
		element.className = value;
	} else {
		newClassName = element.className;
		newClassName+= " ";
		newClassName+= value;
		element.className = newClassName;
	}
}

//	Returns the current height of the browser window
//	Slightly repurposed from the original alertSize() at http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
function getWindowHeight() {
	var myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
	//	Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	//	IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
//	window.alert( 'Width = ' + myWidth );
	return myHeight;
}

//	by Peter-Paul Koch & Alex Tingle
function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent)
		while(1) 
		{
		  curleft += obj.offsetLeft;
		  if(!obj.offsetParent)
			break;
		  obj = obj.offsetParent;
		}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent)
		while(1)
		{
		  curtop += obj.offsetTop;
		  if (!obj.offsetParent)
			break;
		  obj = obj.offsetParent;
		}
	else if(obj.y)
		curtop += obj.y;
	return curtop;
}

//	-----
//	End reference functions
//	-----
	
/*
	This is the script for "Transparent Custom Corners and Borders"
	by Roger Johansson of 456 Berea Street.  Big thanks to Roger for this.
	http://www.456bereastreet.com/archive/200505/transparent_custom_corners_and_borders/
	
	This has been modified a bit to suit my needs, so if you're going to use this,
	it would be best to grab it from his site.
*/

if (document.getElementById && document.createTextNode) {
	addLoadEvent(initCB);
}

/*
createElement function found at http://simon.incutio.com/archive/2003/06/15/javascriptWithXML
*/
function createElement(element) {
	if (typeof document.createElementNS != 'undefined') {
		return document.createElementNS('http://www.w3.org/1999/xhtml', element);
	}
	if (typeof document.createElement != 'undefined') {
		return document.createElement(element);
	}
	return false;
}

function insertTop(obj) {
	// Create the two div elements needed for the top of the box
	d=createElement("div");
	d.className="bt"; // The outer div needs a class name
    d2=createElement("div");
    d.appendChild(d2);
	obj.insertBefore(d,obj.firstChild);
}

function insertBottom(obj) {
	// Create the two div elements needed for the bottom of the box
	d=createElement("div");
	d.className="bb"; // The outer div needs a class name
    d2=createElement("div");
    d.appendChild(d2);
	obj.appendChild(d);
}

function initCB()
{
	// Find all elements with class of cbb to replace
	var divs = getElementsByClassName(document, "*", "cbb");
	var cbDivs = [];
	for (var i = 0; i < divs.length; i++) {
	// Find all div elements with cbb in their class attribute while allowing for multiple class names
		if (/\bcbb\b/.test(divs[i].className))
			cbDivs[cbDivs.length] = divs[i];
	}
	// Loop through the found div elements
	var thediv, outer, i1, i2;
	for (var i = 0; i < cbDivs.length; i++) {
	// Save the original outer div for later
		thediv = cbDivs[i];
	// 	Create a new div, give it the original div's class attribute, and replace 'cbb' with 'cb'
		outer = createElement('div');
		outer.className = thediv.className;
		outer.className = thediv.className.replace('cbb', 'cb');
	// Change the original div's class name and replace it with the new div
		thediv.className = 'i3';
		thediv.parentNode.replaceChild(outer, thediv);
	// Create two new div elements and insert them into the outermost div
		i1 = createElement('div');
		i1.className = 'i1';
		outer.appendChild(i1);
		i2 = createElement('div');
		i2.className = 'i2';
		i1.appendChild(i2);
	// Insert the original div
		i2.appendChild(thediv);
	// Insert the top and bottom divs
		insertTop(outer);
		insertBottom(outer);
	}
}

//	-----
//	End "Transparent Custom Corners and Borders" script
//	-----

//	-----
//	Begin version 3 specific functions
//	-----

addLoadEvent(placeOwl);
window.onresize = placeOwl;
addLoadEvent(fixLastPost);
addLoadEvent(placePointerBlockers);
addLoadEvent(locatePointerBlockers);
addLoadEvent(apologize);

//	Positions the owl accordingly based on window height
function placeOwl() {
	var windowHeight = getWindowHeight();
	var navNode = document.getElementById('nav');
	var owlNode = document.getElementById('ahhh-not-another');
	var pointerNode = document.getElementById('are-you-freaking-kidding-me');
	var owlHeight = 525; // must equal the height of the owl image
	var navTopMargin = 90; // must equal the top margin of the nav
	var navHeight = navNode.clientHeight + navTopMargin;

	owlNode.style.display = "block"; // summon owl
	if (windowHeight > (navHeight + owlHeight)) {
		//	Enough space for both owl and nav, align to bottom of window
		owlNode.style.top = "";
		owlNode.style.bottom = "-20px";
		pointerNode.style.top = "";
		pointerNode.style.bottom = "220px";
	} else {
		//	Some portion of the owl image will be cut off
		owlNode.style.top = navHeight + "px";
		owlNode.style.bottom = "";
		pointerNode.style.top = navHeight + 280 + "px";
		pointerNode.style.bottom = "";
	}
	pointerNode.style.display = "block";
	
	//	daisy-chaining onResize functions, sloppy I know, will be fixed
//	pinFooter();
}

//	Flags the last post on the page with the "last-post" class, so I can remove the unnecessary stem from its bottom
function fixLastPost() {
	var i3Divs = getElementsByClassName(document, "div", "i3");
	var lasti3Div = i3Divs[i3Divs.length - 1];
	var lasti3DivParent = lasti3Div.parentNode.parentNode.parentNode;
	addClass(lasti3DivParent, "last-post");
}

//	Adds pointer blocker divs so they're only added when js is enabled
function placePointerBlockers() {
	// This function (presumably) won't work unless it is fired after corners.js */
	var i3Divs = getElementsByClassName(document, "div", "i3");
	var i3DivsLength = i3Divs.length;
	var blockSideDivs = getElementsByClassName(document, "div", "block-side");
	var blockSideDivsLength = blockSideDivs.length;
	var blockers = new Array();
	var sideBlockers = new Array();

	//	Pointer blockers for all bubble divs
	for (var i = 0; i < i3DivsLength; i++) {
		blockers[i] = document.createElement("div");
		blockers[i].className = "pointer-blocker";
		i3Divs[i].appendChild(blockers[i]);
	}
	
	//	Pointer blockers for any divs that follow the bubbles, and are classed "block-side"
	for (var i = 0; i < blockSideDivsLength; i++) {
		sideBlockers[i] = document.createElement("div");
		sideBlockers[i].className = "pointer-blocker-side";
		blockSideDivs[i].appendChild(sideBlockers[i]);
	}
	
}

//	Now we need to locate the x/y position of these pointer blockers, so we can place them at the bottom 
//	of the body and still absolutely position them where they should be.
//	Step 1: Find the x/y position of these pointer blockers
//	Step 2: Remove them from the document
//	Step 3: Add new divs to the body (outside of their previous parent divs) with the same positions
function locatePointerBlockers() {
	var i3Divs = getElementsByClassName(document, "div", "i3");
	var oldBlockers = getElementsByClassName(document, "div", "pointer-blocker");
	var oldBlockersLength = oldBlockers.length;
	var newBlockers = new Array();
	var xPositions = new Array();
	var yPositions = new Array();
		
	for (var i = 0; i < oldBlockersLength; i++) {
		xPositions[i] = findPosX(oldBlockers[i]);
		yPositions[i] = findPosY(oldBlockers[i]);
		
		i3Divs[i].removeChild(oldBlockers[i]);
		
		newBlockers[i] = document.createElement("div");
		newBlockers[i].className = "pointer-blocker";
		newBlockers[i].style.left = xPositions[i] + "px";
		newBlockers[i].style.top = yPositions[i] + "px";
		document.body.appendChild(newBlockers[i]);
		
		if (i == (oldBlockersLength - 1)) {
			newBlockers[i].style.backgroundImage = "url(../v3/wp-content/themes/v3/images/pointer-blocker-last.png)";
		}
	}
}

//	An apology to IE6 users for not supporting them
function apologize() {
	if (!window.XMLHttpRequest) {
		var apology = document.createElement("div");
		apology.setAttribute("id", "apology");
		var apologyHeader = document.createElement("h2");
		var apologyText = document.createTextNode("I haven't gotten around to supporting IE6 yet, so for now you'll have to use IE7, Firefox or Opera. You should really upgrade anyway, they're way better.");
		var apologyHeaderText = document.createTextNode("Sorry");
		var wrapper = document.getElementById("wrapper");
		
		apologyHeader.appendChild(apologyHeaderText);
		apology.appendChild(apologyHeader);
		apology.appendChild(apologyText);
		wrapper.appendChild(apology);
	}
}