// browser selectors 
var css_browser_selector = function() {
	var 
		ua = navigator.userAgent.toLowerCase(),
		is = function(t){ return ua.indexOf(t) != -1; },
		h = document.getElementsByTagName('html')[0],
		b = (!(/opera|webtv/i.test(ua)) && /msie (\d)/.test(ua)) ? ((is('mac') ? 'ieMac ' : '') + 'ie ie' + RegExp.$1)
			: is('gecko/') ? 'gecko' : is('opera') ? 'opera' : is('konqueror') ? 'konqueror' : is('applewebkit/') ? 'webkit safari' : is('mozilla/') ? 'gecko' : '',
		os = (is('x11') || is('linux')) ? ' linux' : is('mac') ? ' mac' : is('win') ? ' win' : '';
	var c = b+os+' js';
	h.className += h.className?' '+c:c;
}();

//	Input field with help 
function inputWithHelp(elem){
  defaultValue = elem.value
	elem.onfocus = function(){
	  if(elem.value == defaultValue)
			elem.value = ''
	}
	elem.onblur = function(){
	  if(elem.value == '')
			elem.value = defaultValue
	}
}


// Element constructor
Element = function(tagname, attributes){
	var elem = document.createElement(tagname);
	for(var a in attributes)
		elem[a] = attributes[a]
	return elem
}


//	Menu	
Menu = {};

Menu.init = function(elem){
	if(document.all)document.body.insertBefore(elem, document.body.firstChild);

	var node = elem.firstChild;
	while(node){
		if(node.tagName == 'LI'){
			if(node.className == 'selected') Menu.current = node;
			Menu.makehover(node);
		}
		node = node.nextSibling;
	}
}

Menu.makehover = function(elem){
	var timeout = null;
	var out = function(){elem.className = (elem == Menu.current) ? 'selected' : ''}
	var fin = function(){elem.className = 'open'}
	
	elem.onmouseover = function(){
		window.clearTimeout(timeout)
		timeout = window.setTimeout(fin, 50);
	}

	elem.onmouseout = function(){
		window.clearTimeout(timeout)
		timeout = window.setTimeout(out, 100);
	}
}


//	Imageviewer 
window.getInnerWidth = function(){
  return window.innerWidth ? window.innerWidth : document.documentElement.clientWidth;
}

window.getInnerHeight = function(){
  return window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;
}


ImageViewer = {};

ImageViewer.init = function(elem){
	this.imagediv = elem;
  this.bgfade = document.createElement('DIV');
	this.bgfade.id = "background_fade"
	this.bgfade.style.display = 'none'
	document.body.appendChild(this.bgfade)
	document.body.appendChild(elem)

	window.onresize = function(){
		elem.style.left = (getInnerWidth()-710)/2 + 'px'
		elem.style.top = (getInnerHeight()-613)/2 + 'px'
	}

	Behaviour.register({
		"div.images a.zoom"			: function(elem){elem.onclick = function(){ImageViewer.show()}},
		"#imageviewer a.close"	: function(elem){elem.onclick = function(){ImageViewer.hide()}}
	})

	window.onresize();
}

ImageViewer.show = function(){
	this.imagediv.style.display = 'block'
	this.bgfade.style.display = 'block'
}

ImageViewer.hide = function(){
	this.imagediv.style.display = 'none'
	this.bgfade.style.display = 'none'
}



/********
*
*   Stylize Methods
*
********/

markEvenOdd = function(table){
  var trs = table.getElementsByTagName("TR")
	for(var i=0; i<trs.length; i+=2){
	  trs[i].className = 'odd'
	}
}

markDlEvenOdd = function(dl){
  var dts = dl.getElementsByTagName("DT")
  var dds = dl.getElementsByTagName("DD")
	for(var i=0; i<dts.length; i+=2){
	  dts[i].className = dts[i].className + ' odd'
  	dds[i].className = dds[i].className + ' odd'
	}
}

roundCorner = function(elem){
	element.parentNode.insertBefore(new Element("DIV", {id:"maincontent_tail"}), elem)
}


/********
*
*   Behaviour
*
********/
var sheet = {
	"#mainmenu" : Menu.init,
	"#imageviewer" : function(elem){ImageViewer.init(elem)},
	"#search input" : inputWithHelp,

	"table" : markEvenOdd,
	"dl.properties"		: markDlEvenOdd,
	"div.footer" : roundCorner
}

Behaviour.register(sheet);
