
$(document).ready(function () {
	var behaviours = [
		startListBehaviour, 
		leftMenuBehaviour,
		topMenuBehaviour,
		jsGraphicsBehaviour
	];
	for (var i=0; i<behaviours.length; i++) {
		behaviours[i]();
	}
});

function startListBehaviour () {
	if (document.all&&document.getElementById) {
	navRoot = document.getElementById("nav");
	for (i=0; i<navRoot.childNodes.length; i++) {
	node = navRoot.childNodes[i];
	if (node.nodeName=="LI") {
	node.onmouseover=function() {
	this.className+=" over";
	  }
	  node.onmouseout=function() {
	  this.className=this.className.replace(" over", "");
	   }
	   }
	  }
	 }
}


function leftMenuBehaviour () {
	$('#nav .menu').each(function (i, menu) {
		var menuQ = $(menu);
		menuQ.parent().hover(show, planHide);
		menuQ.hover(cancelHide, planHide);
	});
	
	
	function show () {
		var ownerQ = $(this);
		var menuQ = $('.menu', ownerQ);
		//if (activeItem == this) {
			cancelHide()
		//} else if (activeItem) {
			hide.call(activeItem);
		//}
		
		var p = ownerQ.position();
		menuQ.css({
			//left: px(p.left+ownerQ.outerWidth()),
			//top: px(p.top)
			left: px(p.left+ownerQ.parent().outerWidth()),
			top: px(p.top + 7)
				
				
		});
		menuQ.show();
		activeItem = this;
	}
	
	var delayId, activeItem;
	function planHide () {
		delayId = setTimeout(delegate(hide, this), 300);
	}
	function cancelHide () {
		clearInterval(delayId);
	}

	
	function hide () {
		var elQ = $(this).is('.menu') ? $(this) : $('.menu', this);
		elQ.hide();
		//activeItem = null;
	}
}


function topMenuBehaviour () {
	$('#menu .menu').each(function (i, menu) {
		var menuQ = $(menu);
		menuQ.parent().hover(show, planHide);
		menuQ.hover(cancelHide, planHide);
	});
	
	
	function show () {
		var ownerQ = $(this);
		var menuQ = $('.menu', ownerQ);
		//if (activeItem == this) {
			cancelHide()
		//} else if (activeItem) {
			hide.call(activeItem);
		//}
		
		var p = ownerQ.position();
		//console.log(p);
		//console.log(ownerQ.outerHeight());
		menuQ.css({
			//left: px(p.left+ownerQ.outerWidth()),
			//top: px(p.top)
			left: px(p.left),
			top: px(p.top + ownerQ.outerHeight()),
			'margin-left': '0px',
			'margin-top': '9px',
			'z-index': 200
		});
		$('li', menuQ).css({
			'float': 'none'
		});
		menuQ.show();
		activeItem = this;
	}
	
	var delayId, activeItem;
	function planHide () {
		delayId = setTimeout(delegate(hide, this), 300);
	}
	function cancelHide () {
		clearInterval(delayId);
	}

	
	function hide () {
		var elQ = $(this).is('.menu') ? $(this) : $('.menu', this);
		elQ.hide();
		//activeItem = null;
	}
}

function delegate (fn, scope) {
	return function () {
		fn.apply(scope, arguments);
	}
}

function px (v) {
	return v + 'px';
}

function jsGraphicsBehaviour () {
	g = new jsGraphics("head-page");
	g.setColor("#0167CC");
	g.drawLine(190, 35, 190, 235);
	g.drawLine(191, 35, 191, 235);
	g.drawLine(182, 60, 975, 60);
	g.drawLine(182, 61, 975, 61);
	g.drawLine(965, 15, 965, 85);
	g.drawLine(966, 15, 966, 85);
	g.paint();
	
	g = new jsGraphics('footer');
	g.setColor("#0167CC");
	g.drawLine(40, 2, 975, 2);
	g.drawLine(40, 3, 975, 3);
	g.drawLine(50, -23, 50, 100);
	g.drawLine(51, -23, 51, 100);
	g.drawLine(965, 27, 965, -208);
	g.drawLine(966, 27, 966, -208);
	g.paint();
}
