/*
  addEvent function from
  http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
*/
function pod_addEvent( obj, type, fn )
{
    if (obj.addEventListener)
	obj.addEventListener( type, fn, false );
    else if (obj.attachEvent) {
	obj["e"+type+fn] = fn;
	obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
	obj.attachEvent( "on"+type, obj[type+fn] );
    }
}

/*
  createElement function function
  http://simon.incutio.com/archive/2003/06/15/javascriptWithXML
*/
function pod_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;
}

/*
  pod_init function by Barry Jaspan (firstname at lastname dot org).
  
  Inspired by "Transparent custom corners and borders"
  http://www.456bereastreet.com/archive/200505/transparent_custom_corners_and_borders/
*/

/* this is global so pod_fix does not have to recompute it */
var pod_fix_list = [];

function pod_init()
{
    // utility function for later
    var append_div = function(named, to) {
	var d = pod_createElement('div');
	d.className = named;
	to.appendChild(d);
	return d;
    }

    // Find all div elements
    var divs = document.getElementsByTagName('div');

    // Find all div elements with pod-plain in their class attribute while
    // allowing for multiple class names
    var pods = [];
    for (var i = 0; i < divs.length; i++) {
	if (/\bpod-plain\b/.test(divs[i].className))
	    pods[pods.length] = divs[i];
    }

    // Convert pod-plains into pods by adding the sub-divs.
    // Loop through the found div elements
    var thediv, outer, d;
    for (var i = 0; i < pods.length; i++) {
	// Save the original pod outer div for later
	thediv = pods[i];
	
	// Create a new div, give it the original div's class attribute,
	// and replace 'pod-plain' with 'pod'
	outer = pod_createElement('div');
	outer.className = thediv.className;
	outer.className = thediv.className.replace('pod-plain', 'pod');
	
	// Add our new divs to the new outer div.
	append_div('pod-ul', outer);
	append_div('pod-ur', outer);
	pod_fix_list[pod_fix_list.length] = append_div('pod-left', outer);
	pod_fix_list[pod_fix_list.length] = append_div('pod-right', outer);
	append_div('pod-ll', outer);
	append_div('pod-lr', outer);

	// Replace the original pod with the new div.
	thediv.parentNode.replaceChild(outer, thediv);

	// Rename the original to pod-content and add it back in.
	thediv.className = 'pod-content';
	outer.appendChild(thediv);
    }

    // Now, find all pod-lefts and pod-rights and remember them for
    // later.  This is a separate pass so I support class="pod" or
    // class="pod-plain" in the HTML.
    divs = document.getElementsByTagName('div');
    for (var i = 0; i < divs.length; i++) {
	if (/\bpod-left\b/.test(divs[i].className) ||
	    /\bpod-right\b/.test(divs[i].className)) {
	    pod_fix_list[pod_fix_list.length] = divs[i];
	}
    }

    pod_fix();
}

function pod_fix() {
    var c;
    for (var i = 0; i < pod_fix_list.length; i++) {
	c = pod_fix_list[i];
	c.style.height = c.parentNode.offsetHeight - 336;
    }
}

if (document.getElementById)
{
    pod_addEvent(window, 'load', pod_init);
    pod_addEvent(window, 'resize', pod_fix);
}
