﻿
//<![CDATA[

// Common functions

// Used to add more than one handler to the load event
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}








// fixFooterNav removes the border on the last child of the footer nav
// This enables us to use borders as the seperator
function fixFooterNav()
{
	var n = document.getElementById('footermenu');
	if (n != null)
	{
		n = n.getElementsByTagName('li')
	
		var ref = n[Number(n.length -1)];
	
		ref.style.borderRight = 'none';
	}
}




function addLocationSeperators()
{
	var l, li, el, sep, tn;
	
	try
	{
		l = document.getElementById('location');
	}
	catch(e)
	{
		return false;
	}
	
	if(l)
	{
		li = l.getElementsByTagName('li');
		
		// use length -1, because we dont need a seperator after the last item :)
		for(var i=0; i<li.length -1; i++)
		{
			el = li[i];
			sep = document.createElement('span');
			tn = document.createTextNode('>');
			sep.appendChild(tn);
			
			el.appendChild(sep);
		}
		
		return true;
	}
	return false;
}





// No target attr in xhtml 1.0, this script allows you to use rel="external" instead
function externalLinks(){
	if (!document.getElementsByTagName){
		return;
	}
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && String(anchor.getAttribute("rel")).indexOf("external") != -1){
			anchor.target = "_blank";
		}else if(anchor.getAttribute("href") && String(anchor.getAttribute("rel")).indexOf("internal") != -1){
			anchor.target = "_top";
		}
	}
}


var MagicBox = {
	// vars
	defaultClassName:'magic-box',
	isVerbose: true,
	wrapperClass: null,
	
	// constructor
	// MagicBox.create()
	// MagicBox.create(className:String[, verbose:Boolean, wrapperClassName:String])
	
	create: function()
	{
		var theClass, targets, el;
		
		theClass = arguments.length ? arguments[0] : this.defaultClassName;
		this.isVerbose = arguments.length > 1 ? arguments[1] : true;
		this.wrapperClass = arguments.length > 2 ? arguments[2] : null;
		
		targets = MagicBox.getElementsByClassName(theClass);
		
		//create the box for each target
		for( var i=0; i<targets.length; i++ )
		{
			el = targets[i];
			
			if(document.all && !window.opera)
			{
				this.applyIEGrid(el);
			}else
			{
				this.applyGrid(el);
			}
		}
	
	},
	
	applyIEGrid: function(el)
	{
		var w, t, tb, r1, r2, r3, td, d, i;
		
		if(this.wrapperClass != null)
		{
			w = document.createElement('<div class="' + this.wrapperClass + '"></div>')
		}
		else
		{
			w = document.createElement('<div class="magic-box-wrapper"></div>');
		}
		
		t = document.createElement('<table border="0" cellpadding="0" cellspacing="0"></table>');
		tb = document.createElement('<tbody></tbody>');
		
		r1 = document.createElement('tr');
		r2 = document.createElement('tr');
		r3 = document.createElement('tr');
		
		
		// make 9 <td>'s for the grid
		for(var i=1; i<10; i++)
		{
			td = document.createElement('<td class="mb-' + i + '" ></td>');
			
			if(this.isVerbose)
			{
				d = document.createElement('<div class="mb"></div>');
			}
			
			// Copy the content into the center
			if(i==5)
			{
				if(this.isVerbose)
				{
					d.appendChild(el.cloneNode(true));
				}
				else
				{
					td.appendChild(el.cloneNode(true));
				}
			}
			
			if(this.isVerbose) { td.appendChild(d); }
			
			if(i<4)
			{
				r1.appendChild(td);
			}
			else if(i < 7)
			{
				r2.appendChild(td);
			}
			else
			{
				r3.appendChild(td);
			}
		}
		
		tb.appendChild(r1);
		tb.appendChild(r2);
		tb.appendChild(r3);
		
		t.appendChild(tb);
		
		w.appendChild(t);
		
		el.parentNode.insertBefore(w, el);
		
		// remove the seed element
		el.parentNode.removeChild(el);
		
	},
	
	// apply to a single instance
	applyGrid: function(el)
	{
		var td, d, w, t, r1, r2, r3;
		
		w = document.createElement('div');
		this.wrapperClass != null ? w.setAttribute('class', this.wrapperClass) : w.setAttribute('class', 'magic-box-wrapper');
		
		// create a table
		t = document.createElement('table');
		t.setAttribute('border', '0');
		t.setAttribute('cellpadding','0');
		t.setAttribute('cellspacing', '0');
		
		// Create 3 rows
		r1 = document.createElement('tr');
		r2 = document.createElement('tr');
		r3 = document.createElement('tr');
		
		// make 9 <td>'s for the grid
		for(var i=1; i<10; i++)
		{
			td = document.createElement('td');
			td.setAttribute('class', 'mb-' + i);
			
			if(this.isVerbose)
			{
				d = document.createElement('div');
				d.setAttribute('class', 'mb');
			}
			
			// Copy the content into the center
			if(i==5)
			{
				if(this.isVerbose)
				{
					d.appendChild(el.cloneNode(true));
				}
				else
				{
					td.appendChild(el.cloneNode(true));
				}
			}
			
			if(this.isVerbose)
			{ td.appendChild(d); }
			
			if(i<4)
			{
				r1.appendChild(td);
			}
			else if(i < 7)
			{
				r2.appendChild(td);
			}
			else
			{
				r3.appendChild(td);
			}
		}
		
		// Append the populated table rows to the table
		
		t.appendChild(r1);
		t.appendChild(r2);
		t.appendChild(r3);
	
		// Append the populated table to the wrapper div
		w.appendChild(t)
		
		el.parentNode.insertBefore(w, el);
		
		// remove the seed element
		el.parentNode.removeChild(el);
	},
	
	
	
	getElementsByClassName: function(className, tagArg, elmArg)
	{
		var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
		
		var tag, elm;
		
		tagArg ? tag = tagArg : tag = "*"; 
		elmArg ? elm = elmArg : elm = document;
		var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
		var returnElements = [];
		var current;
		var length = elements.length;
		for(var i=0; i<length; i++)
		{
			current = elements[i];
			if(testClass.test(current.className))
			{
				returnElements.push(current);
			}
		}
		return returnElements;
	}
}



function getElementsByClassName(className, tagArg, elmArg)
{
	var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
	var tag, elm;
	tagArg ? tag = tagArg : tag = "*"; 
	elmArg ? elm = elmArg : elm = document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++)
	{
		current = elements[i];
		if(testClass.test(current.className))
		{
			returnElements.push(current);
		}
	}
	return returnElements;
}

addLoadEvent(function()
{
	fixFooterNav();
	addLocationSeperators();
	MagicBox.create('login-box', false, 'login-mb');
	
	Nifty('div#sidebar', 'med transparent left');
	
	ZebraStripe.create();
	
	/* TODO
	initFavourites(); // Add favourites dhtml functionality
	*/
});


// Finds elements with the classname of 'zebra-stripe'
// Adds ' alt' to the classname of every other table row.
var ZebraStripe = {
	create: function()
	{
		var t = getElementsByClassName('zebra-stripe');
		for(var i=0; i<t.length; i++)
		{
			this.apply(t[i]);
		}
	},
	
	apply: function(target)
	{
		if(!target)
		{
			return false;
		}
		else
		{
			var rows = target.getElementsByTagName('tr');
			for(var i=0; i<rows.length; i++)
			{
				if(i % 2)
				{
					rows[i].className += ' alt';
                }
            }
			return true;
		}
	}
};



/* TODO
// Add onclick events to favourites links
function initFavourites()
{
	var f = getElementsByClassName('fav');
	
	for(var i=0; i<f.length; i++)
	{
		f[i].onclick = toggleFav;
	}
}

function toggleFav()
{
	alert(this.getElementsByTagName('input').length);
}
*/


//]]>