/**
 * All code hand-crafted by Rahmin Pavlovic
 *
 * (aka:  Chilly Freeze Steak, Soulcracker, Lemon Meranguetang..)
 *
 * Who put this together?  Me!  Thas who!
 *
 * Feel free to reuse the refuse..
 *
 * Copyright (c) 2006
 *
 */


// create our own DOM object to centralize any DOM-based routines we may want to use here
var dom=new Object();
dom.getElementById=function(id) {
	if(document.getElementById) { return document.getElementById(id); }
	if(document.layers) { return document.layers[id]; }
	if(document.all) { return document.all[id]; }
	return null;
}
dom.getElementsByTagName=function(tag) {
	var el=(this.getElementsByTagName.arguments[1])?this.getElementsByTagName.arguments[1]:document;
	if(el.getElementsByTagName) { return el.getElementsByTagName(tag); }
	return null;
}
dom.getInnerWidth=function() {
	if(window.innerWidth) { return window.innerWidth; }
	if(document.body.offsetWidth) { return document.body.offsetWidth; }
	return null;
}
dom.getInnerHeight=function() {
	if(window.innerHeight) { return window.innerHeight; }
	if(document.body.offsetHeight) { return document.body.offsetHeight; }
	return null;
}


// create our own doc object to centralize document routines we may want
var doc=new Object();
doc.getRandomLeft=function() {
	var now=new Date();
	return (((now.getTime() * now.getSeconds()) / parseInt((now.getDay()+1) * 2)) % dom.getInnerWidth());
}
doc.getRandomTop=function() {
	var now=new Date();
	return (((now.getTime() * now.getSeconds()) / parseInt(now.getDay()+1)) % parseInt(dom.getInnerHeight() / 2));
}
doc.createElement=function(el) {
	if(document.createElement) { return document.createElement(el); }
	return null;
}

// routine to loop through navbar elements to add our own hand-rolled, DOM-based, DHTML event handlers
function registerNav() {
	var nav=dom.getElementById('navbar');
	var navItems=nav.getElementsByTagName('a');
	if(navItems) {
		for(var i=0; i<navItems.length; i++) {
			var navItem=navItems[i];
			if(navItem.id.indexOf('nav-')!=-1) {

				// add mouseover event handler to create a randomly placed image on-th-fly
				navItem.onmouseover=function() {
					var navImage=doc.createElement('img');
					var container=dom.getElementById('nav-icon');
					if(container && navImage) {
						navImage.setAttribute('src', 'images/'+this.id+'.gif');

						container.style.top=doc.getRandomTop() + 'px';
						container.style.left=(doc.getRandomLeft() - navImage.width) + 'px';
						container.appendChild(navImage);
						container.style.display='block';
					}
				}

				// add onmouseout event to clear & hide random image container
				navItem.onmouseout=function() {
					var container=dom.getElementById('nav-icon');
					if(container) {
						container.innerHTML='';
						container.style.display='none';
					}
				}
			}
		}
	}
}


// register links 
function registerMixes() {
	var main=dom.getElementById('main');
	var mixItems=dom.getElementsByTagName('a',main);
	if(mixItems) {
		for(var i=0; i<mixItems.length; i++) {
			var mix=mixItems[i];
			if(mix.id.indexOf('mix-')!=-1) {
				mix.href="javascript:openWin('"+mix.href+"',300,30)";
			}
		}
	}
}

// routine to disable all submit buttons onsubmit
function registerForms() {
	var forms=dom.getElementsByTagName('form');
	if(forms) {
		for(var i=0; i<forms.length; i++) {
			var form=forms[i];
			var inputs=dom.getElementsByTagName('input',form);
			if(inputs) {
				for(var n=0; n<inputs.length; n++) {
					var input=inputs[n];
					if(input.type=='submit') {
						form.onsubmit=function() {
							input.disabled=true;
							return true;
						}
					}
					if(input.type=='reset') {
						form.onreset=function() {
							return confirm('Are you sure you want to reset this form?');
						}
					}
				}
			}
		}
	}
}

// pop-up code
function openWin(file,w,h) {
	var McBoinBoingWin=window.open(file,'McBoinBoingWin','width='+w+',height='+h+',directories=no,menubar=no,toolbar=no,location=no');
	if(McBoinBoingWin.resizeTo)McBoinBoingWin.resizeTo(w,h);
	if(McBoinBoingWin.focus)McBoinBoingWin.focus();
}

