// Unobtrusive Onload
function addLoadEvent(func){
	var oldonload = window.onload;
	if(typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		};
	}
};

// Unobtrusive listeners
function attachEventListener(target, eventType, functionRef, capture)
{
    if (typeof target.addEventListener != "undefined"){
        target.addEventListener(eventType, functionRef, capture);
    } else if (typeof target.attachEvent != "undefined") {
        target.attachEvent("on" + eventType, functionRef);
    } else {
        return false;
    }
    return true;
};

// Highlighting Effect
function highlight(action, elem, trigger){
	if(document.getElementById('big-grid')){
		var projects = document.getElementById('big-grid').getElementsByTagName('UL');
		var categories = document.getElementById('categories').getElementsByTagName('LI');
	} else {
		var projects = document.getElementById('project-grid').getElementsByTagName('A');
		var shortcut = true;
	}

	switch (action){
	case 'on':
		var sel = (trigger == undefined ? elem.id : elem.className);
		
		for (var i=0; i < projects.length; i++) {
			if(projects[i].id != sel){
				var overlay = 'overlay_'+projects[i].id+'';
				document.getElementById(overlay).style.display = 'block';
			}
		};
		
		// No need to execute categories status code
		if(shortcut){return true;}
		
		var categories = document.getElementById('categories').getElementsByTagName('A');
		for (var i=0; i < categories.length; i++) {
			if(categories[i].className == sel){
				categories[i].parentNode.className = "selected";
			}
		};	  
	  break;

	case 'off':
		for (var i=0; i < projects.length; i++) {
			var overlay = document.getElementById(projects[i].id).lastChild;
			var overlay = 'overlay_'+projects[i].id+'';
			document.getElementById(overlay).style.display = 'none';
		};
		
		if(shortcut){return true;}
		
		for (var i=0; i < categories.length; i++) {
			categories[i].className = "";
		};	  
	  break;
	}
};

// Unobtrusive hover effects for portfolio and projects
function prepareGrid(){
	if(document.getElementById('project-grid')) {
		var projects = document.getElementById('project-grid').getElementsByTagName('A');	
		e = 'SPAN';
	} else if (document.getElementById('big-grid')) {
		var projects = document.getElementById('big-grid').getElementsByTagName('UL');
		e = 'LI';
	} else {
		return;	
	}

	for (var i=0; i < projects.length; i++) {
		// create overlay with JS to keep it out of the markup
		var overlay = document.createElement(e);
		overlay.id = 'overlay_'+projects[i].id;
		overlay.className = 'overlay';
		overlay.style.display = 'none';
		document.getElementById(projects[i].id).appendChild(overlay);
		
		projects[i].onmouseover = function(){
			highlight('on', this);
			rolloverCoverCaption(this.id);
			return false;
		};
		
		projects[i].onmouseout = function(){
			highlight('off', this);
			rolloverCoverCaption(this.id);
			return false;			
		};

		projects[i].onclick = function(){
			window.onunload = function(){
				rolloverCoverCaption(this.id);
			}			
		};		
		
	};		
};

function rolloverCoverCaption(elem){
	if(!document.getElementById('cover_caption')){return;}
	
	var caption = document.getElementById('cover_caption');
	var alt = document.getElementById(elem).getElementsByTagName('img')[0].alt;
		alt = alt.split('//');
		alt = "<h1>"+alt[0]+"</h1>"+"<h2>"+alt[1]+"</h2>";
	var defaultCaption = '';
	
	caption.innerHTML = (caption.innerHTML == defaultCaption ? alt : defaultCaption);
};

// Prepare the remote rollovers for the navigation elements
function prepareGridNav(){
	if(document.getElementById('categories') && !document.getElementById('big-grid')){return;}

	var links = document.getElementById('categories').getElementsByTagName('A');
	for (var i=0; i < links.length; i++) {
		links[i].onmouseover = function(){
			highlight('on', this, true);
			return false;
		};
		
		links[i].onmouseout = function(){
			highlight('off', this);
			return false;			
		};
	};
};

function moreInformation(){
	// if(document.getElementsByTagName('body')[0].id=='view'){return;}
	if(!document.getElementById('more_info')){return;}
	var info = document.getElementById('more_info');
	info.style.display = 'none';
	
	var defaultLinkText = (document.getElementById('view') ? 	'More about this project' : info.className );
	
	
	var link = document.createElement('a');
	link.id = 'more';
	link.href = '';
	link.innerHTML = defaultLinkText;
	document.getElementById('copy').appendChild(link);
		
	link.onclick = function(){
		info.style.display = (info.style.display == 'none' ? 'block' : 'none');
		// window.location = '#more-top';
		this.innerHTML = (this.innerHTML == defaultLinkText ? 'Back to the top' : defaultLinkText);	
		return false;
	};
};

// Generic show method
function show(elem){
	e = document.getElementById(elem);
	(e.style.display=='none'? e.style.display='block' : e.style.display='none');
};

// Change top margin for 800*600 users
function checkBrowserDimensions(){
	var pageClass = document.body.className.replace(/small/,'');
	var theHeight = getBrowserHeight();
	if (theHeight < 450){ 
		document.body.className = pageClass + ' small';
	} else {
		document.body.className = pageClass;
	}
	return true;
}

function getBrowserHeight(){
	if (window.innerHeight){
		return window.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight != 0){
		return document.documentElement.clientHeight;
	} else if (document.body) {
		return document.body.clientHeight;
	}	
	return 0;
};

attachEventListener(window, "resize", checkBrowserDimensions, false);

// DOM Loaded
function init() {
	if (arguments.callee.done) return;
	arguments.callee.done = true;

	if (_timer) clearInterval(_timer);
	
	moreInformation();
	prepareGrid();
	prepareGridNav();
	checkBrowserDimensions();
};

/* for Mozilla/Opera9 and Safari */
if (document.addEventListener) {
	/* for Safari */
	if (/WebKit/i.test(navigator.userAgent)){
		var _timer = setInterval(function(){if(/loaded|complete/.test(document.readyState)){ init(); }}, 0);
		window.onload(init);
	} else {
		document.addEventListener("DOMContentLoaded", init, false);
	}
} else {
	document.write("<script id=__ie_onload defer src=//:><\/script>");
	var script = document.getElementById("__ie_onload");
	script.onreadystatechange = function() {
		if (this.readyState == "complete") {
			this.onreadystatechange = null;
			init(); // call the onload handler
		}
	};
}

// Safari flakey with DomLoaded when hiding stuff random flash of unhidden stuff not good.
function s(){
	if(/WebKit/i.test(navigator.userAgent)){
		document.getElementById('more_info').style.display='none';
	}
};