function getObject(theObjectID)
{
	if (document.all && !document.getElementById){
		return(document.all(theObjectID));
	} else {
		return(document.getElementById(theObjectID));
	}
}

if(parent.location.toString().indexOf(".asp")==-1){ // disable only for categories and projects pages
	document.oncontextmenu = function(){ return(false); }
}

var mouseOnMenu = "";
var fixedSubmenus = "";

function openMenu(menu){
	mouseOnMenu = menu;
	var obj = getObject(menu);
	obj.style.display = "block";
	if(fixedSubmenus.indexOf(menu)==-1){
		fixedSubmenus = fixedSubmenus + menu + ",";
		if(navigator.appName.indexOf("Explorer")==-1){
			if(navigator.userAgent.indexOf("Chrome")>-1){ // if chrome
				if(menu=="portfolio"){
					var leftInc = 30;
				}else{
					var leftInc = 40;
				}
				obj.style.left=(findPos(obj)[0]-leftInc)+"px";
			}
			obj.style.top=(findPos(obj)[1]+3)+"px";
		}else{
			if(navigator.userAgent.indexOf("MSIE 8")>-1){
				obj.style.top=(findPos(obj)[1]+3)+"px";
			}else{
				if(menu=="portfolio"){
					var leftInc = 57;
				}else{
					var leftInc = 37;
				}
				obj.style.left=(findPos(obj)[0]+leftInc)+"px";
				obj.style.top=(findPos(obj)[1]+18)+"px";
				obj.style.display = "none";
				obj.style.display = "block";
			}
		}
	}
}

function timeout(menu,time){ // create timeout but still check every 10 millisecords if another submenu was opened.
	if(time<=0||(mouseOnMenu!=menu&&mouseOnMenu!="")){
		getObject(menu).style.display = "none";
	}else if(mouseOnMenu!=menu){ // cut "close" if opened again
		window.setTimeout(function(){
			timeout(menu,time-10);
	   },10);
	}
}

function closeMenu(menu){
	mouseOnMenu = "";
	timeout(menu,1000);
//	window.setTimeout(function(){
//		if(mouseOnMenu!=menu){
//			getObject(menu).style.display = "none";
//		}
//	},1000);
}

function findPos(obj) 
// finds te posotion of an object in the page 
	{
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

var notWhitespace = /\S/;
function cleanWhitespace(node){
	for (var x = 0; x < node.childNodes.length; x++){
		var childNode = node.childNodes[x];
		if ((childNode.nodeType == 3)&&(!notWhitespace.test(childNode.nodeValue))){
			// that is, if it's a whitespace text node
			node.removeChild(node.childNodes[x]);
			x--;
		}
		if (childNode.nodeType == 1){
			// elements can have text child nodes of their own
			cleanWhitespace(childNode);
		}
	}
}

var scrollActive,scrollObj,scrollInt;
function startScroll(obj,amount){
	scrollObj = obj
	scrollActive = true;
	scrollInt = window.setInterval("scrollNow('"+obj+"',"+amount+");",10);
}

function scrollNow(obj,amount){
	var obj = getObject(obj);
	var cScrollTop = obj.scrollTop;
	obj.scrollTop = cScrollTop + amount;
	if(cScrollTop == obj.scrollTop){
		window.clearInterval(scrollInt);
	}
}

function stopScroll(){
	window.clearInterval(scrollInt);
	scrollActive = false;
	scrollObj = "";
}

function sidemenu(id){
	var height = 0;
	if(navigator.appName!="Microsoft Internet Explorer"){
		var intervalSpeed = 20;
		var speed = 2;
	}else{
		var intervalSpeed = 1;
		var speed = 8;
	}
	var category = getObject("category"+id);
	cleanWhitespace(category);
	var linksCount = category.childNodes[0].childNodes.length;
	if(linksCount>6){ linksCount = 6; }
	var counter = 0;
	var categoryArrow = getObject("categoryArrow"+id);
	var maxHeight = linksCount * 22 + 6;
	if (maxHeight < 7) {
	    document.location = ResolveUrl("/Category.aspx?CategoryID=") + id;
	}
	if(id=="selected"){
		maxHeight = 234;
	}
	category.style.overflow = "hidden";
	if(category.style.display=="none"){
			height = 0
			categoryArrow.src = categoryArrow.src.replace("_right","_down");
			category.style.height = height + "px";
			var slideInt = window.setInterval(function(){
				if(height<maxHeight){ // while height smalls than all links height (20px per link)
					if(counter==2){ // Increase speed evety 10px
						speed++;
						counter=0;
					}else{ counter++; }
					height = height + speed;
					if(height>maxHeight){ height = maxHeight; }
					category.style.height = height + "px";
					category.style.display = "block";
				}else{
					category.style.height = maxHeight + "px";
					window.clearInterval(slideInt);
				}
			},intervalSpeed);
	}else{
			if(height==0){ category.style.height = maxHeight+"px"; } // if cube is already open
			categoryArrow.src = categoryArrow.src.replace("_down","_right");
			height = category.style.height.replace("px","")*1;
			var slideInt = window.setInterval(function(){
			if(height>0){
					if(counter==4){ // decrease speed every 4px
						speed=speed+5;
						counter = 0;
					}else{ counter++; }
					height = height - speed;
					if(height<0){ height = 0; }
					category.style.height = height + "px";
				}else{
					category.style.display = "none";
					window.clearInterval(slideInt);
				}
			},intervalSpeed);
	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function ResolveUrl(url) {
    if (url.indexOf("~/") == 0) {
        url = baseUrl + url.substring(2);
    }
    return url;
}