/*\------------------------------------------------------------------------

	>> pdd.co.uk
	>> 05/09/07				
	>> Kostis Sakkas				
		
------------------------------------------------------------------------\*/


window.onload	= function() {setEqual(); emailCloak();externalLinks();}
window.onresize	= function() {setEqual();}






/*\------------------------------------------------------------------------
	
	Opens new windows via rel="external"
	url : http://www.brucelawson.co.uk/index.php/2005/opening-links-in-new-windows-in-xhtml-strict/
	
------------------------------------------------------------------------\*/

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") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank";
			anchor.title = (anchor.title != "") ? anchor.title+" (opens in new window)" : "opens in new window";
			anchor.className = (anchor.className != '') ? anchor.className+' external' : 'external';
		}
	}
}


/*\------------------------------------------------------------------------
	
	Position footer div at the bottom of the page using DOM
	
	Source from the precious AListApart.com
	Article : Exploring Footers
	URL : http://www.alistapart.com/articles/footers/
	[changed the setFooter() a bit]

------------------------------------------------------------------------\*/

function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}
		else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}
		
function setFooter() {
	var add = (arguments[0]) ? arguments[0] : 0;
	
	if (document.getElementById) {
		var windowHeight = getWindowHeight();
		if (windowHeight > 0) {
			var headerHeight  	= document.getElementById('header').offsetHeight;
			var footerHeight  	= document.getElementById('footer').offsetHeight;
			
			var containerEl		= document.getElementById('container');
			var containerHeight = containerEl.offsetHeight + add;
			
			var sbElement		= document.getElementById('sidebar');
			var sbHeight  		= sbElement.offsetHeight;
			
			var mainElement		= document.getElementById('main');
			var mainHeight  	= mainElement.offsetHeight;
			
		
			//alert("window: " + windowHeight + ",\r\nheader: " +headerHeight + ",\r\ncontainer: " +containerHeight+ ",\r\nsidebar: " +sbHeight + ",\r\nmain: " +mainHeight + ",\r\nfooter:" + footerHeight);
			
			if (headerHeight + containerHeight + footerHeight < windowHeight) {				
				var h = windowHeight - (headerHeight + footerHeight)  + 'px';
				containerEl.style.height = sbElement.style.height = mainElement.style.height =  h;
			}
			else
				sbElement.style.height = mainElement.style.height = containerHeight +  'px';
		}
	}
}

function setEqual() {
	if (document.getElementById) {
		var sbContent  	= document.getElementById('content');
		var sbSidebar  = document.getElementById('sidebar');
		if(sbContent.offsetHeight > sbSidebar.offsetHeight){
			sbSidebar.style.height = sbContent.offsetHeight + 'px';
		}
		else{
			sbContent.style.height = sbSidebar.offsetHeight + 'px';
		}
	}
};


/*\------------------------------------------------------------------------
	
	Replaces email addresses with the title attr.
	credits : initial idea from http://www.badboy.ro/articles/2005-01-25/index.php
	
------------------------------------------------------------------------\*/

emailCloak = function() {
	if (document.getElementById) {
		var alltags = document.all? document.all : document.getElementsByTagName("*");
		for (i=0; i < alltags.length; i++) {
			if (alltags[i].className == "emailCloak") {
				var oldText = alltags[i].firstChild;
				var emailAddress = alltags[i].firstChild.nodeValue;
				var user = emailAddress.substring(0, emailAddress.indexOf("("));
				var website = emailAddress.substring(emailAddress.indexOf(")")+1, emailAddress.length);
				var newText = user+"@"+website;
			  	var a = document.createElement("a");
			  	a.href = "mailto:"+newText;
				var address = document.createTextNode(newText);
				a.appendChild(address);
				alltags[i].replaceChild(a,oldText);
			}
		}
	}
};



$(document).ready(function () {

	$('#nav').accordion({ 
		selectedClass: 'selected', 
		header: '.head', 
		navigation: true, 
		autoHeight: false 
	});
	
/*\------------------------------------------------------------------------
	png fix for IE6
------------------------------------------------------------------------\*/

	$('img[@src$=.png]').ifixpng(); 
	
	
});