
/**********************************************************/
function alertSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  //window.alert( 'Width = ' + myWidth );
  window.alert( 'Height = ' + myHeight );
}
/**********************************************************/
/**
* Add a function to an object event handler
* @param {Object} oObj Reference to object
* @param {String} evType Event handler to be used
* @param {Function} fn Function reference
* @param {Boolean} useCapture Uses Capture
*/
function addEvent(oObj, evType, fn, useCapture)
{
	if (oObj.addEventListener){
		oObj.addEventListener(evType, fn, useCapture);
		return true;
	} else if (oObj.attachEvent){
		var r = oObj.attachEvent("on"+evType, fn);
		return r;
	} else {
		alert("Handler could not be attached");
	}
}

/**
* Shorthand for getElementById
* @return Object with corresponding id
* @param {String} id Id of object
*/
function $(id)
{
	return document.getElementById(id);
}

/**
* Prepares the page and attaches
* various functions to objects.
*/
function init()
{
	//attachTopMenuHover();
	//attachTopMenuHover($("main-nav"));
	//attachTopMenuHover($("folk1-nav"));
	btnHover();
	
	attachMenuFunctions($("left-nav"));
	if($("box-report") != undefined) attachBoxHover($("box-report"));
	
	var i=0;
	while ($("box" + i) || i < 10) {
		attachBoxHover($("box" + i));
		i++;
	}
}

/**
* Attach hover functions to topmenu
*/
function attachTopMenuHover(nav)
{
	//var menu = $("main-nav");
	var menu = nav;
	if(menu == undefined)
	{
		return;
	}
	
	var items = menu.getElementsByTagName("img");
	
	for(var i=0;i<items.length;i++)
	{
		items[i].onmouseover =
			function()
			{
				if(this.src.indexOf("selected") == -1)
				{
					this.src = this.src.replace(".gif", "_hover.gif");
				}
			}
		items[i].onmouseout =
			function()
			{
				if(this.src.indexOf("selected") == -1)
				{
					this.src = this.src.substr(0, this.src.lastIndexOf(".")-6) + ".gif";
				}
			}

	}
}

/**
* Attach hover functions to topmenu
*/
function attachMenuFunctions(obj)
{
	//alertSize();
	window.rootMenuItems = new Array();
	if(obj == undefined) return;
	//var h = $("content").offsetHeight;
	//alert(h);
	//$("left-nav").style.height = h.toString() + "px";
	//$("column-left").style.height = h.toString() + "px";
	//$("left-nav").style.height = h.toString() + "px";
	
	//$("extra-length").style.height= h; //= "<img src='file:///C|/projects/Folksam/source/src/web/dispatcher/img_tre/top_nav_streck.gif' height='" + h + "' />";
	//$("left-column").style.height = $("main").offsetHeight; /*$("main").offsetHeight;*/
	//alert($("left-nav").offsetHeight);
	//alert($("content").offsetHeight);
	
	var items = obj.getElementsByTagName("li");
	for(var i=0;i<items.length;i++)
	{
		if(items[i].className.indexOf("has-children") > -1)
		{
			var link = items[i].getElementsByTagName("a")[0];
			if(!link) return;

			// Root menu item
			if(items[i].parentNode.parentNode.parentNode.id == "left-nav")
			{
				window.rootMenuItems.push(items[i]);
				items[i].closeItem =
					function()
					{
						var child = this.getElementsByTagName("ul")[0];
						if(child != undefined)
						{
							if(child.style.display == "block")
							{
								this.className = "has-children";
								child.style.display = "none";
							}
						}
						this.blur();
					}
			}

			if(items[i].className.indexOf("open") == -1 && items[i].className.indexOf("selected") == -1)
			{
				link.onclick =
					function()
					{
						var _parent = this.parentNode;
						var child = _parent.getElementsByTagName("ul")[0];
						if(child != undefined)
						{
							if(child.style.display == "block")
							{
							
								_parent.className = "has-children";
								child.style.display = "none";
							}
							else
							{
								_parent.className = "has-children open";
								child.style.display = "block";
								//closeRootMenuItems(_parent);
							}
						}
						this.blur();
					}
			}
			
			var child = items[i].getElementsByTagName("ul")[0];
			if(child != undefined)
			{
				if(items[i].className.indexOf("selected") > -1)
				{
					child.style.display = "block";
				}
			}
		}
	}
}

function closeRootMenuItems(obj)
{
	for(var i=0;i<window.rootMenuItems.length;i++)
	{
		if(window.rootMenuItems[i] == obj)
		{
			for(var j=0;j<window.rootMenuItems.length;j++)
			{
				if(window.rootMenuItems[j] != obj)
				{
					window.rootMenuItems[j].closeItem();
				}
			}
		}
	}
}


/**
* Attach hover functions to box
* @param {Object} box Box object containing a link
*/
function attachBoxHover(box)
{
	if(box == undefined) return;
	box.onmouseover =
		function()
		{
			var item = this.getElementsByTagName("a")[0];
			item.style.textDecoration = "underline";
			window.status = item.href;
		}
	box.onmouseout =
		function()
		{
			var item = this.getElementsByTagName("a")[0];
			item.style.textDecoration = "";
			window.status = "";
		}
	box.onclick =
		function()
		{
			var item = this.getElementsByTagName("a")[0];
			location.href = item.href;
		}
	
	box.style.cursor = "pointer";
}

/**
* Attach tab functions to a tab system
* @param {Object} ul Reference to <ul> object
*/
function attachTabSystem(ul)
{
	if(ul == undefined) return;
	//alert(ul.offsetHeight);
	
	
	
	var items = ul.getElementsByTagName("li");
	var liHeight = items[0].offsetHeight;
	//alert(items[0].offsetHeight + "  :  " + ul.offsetHeight);
	
	if (ul.offsetHeight > liHeight*2) {
		ul.className = "tabs_2";	
	}
	
	for(var i=0;i<items.length;i++)
	{
		var li = items[i];
		
		li.onclick =
			function()
			{
				/*
				 * This should be used if we want an AJAX call. Do we? 
				 *
				if(this.className.indexOf("selected") == -1)
				{
					var siblings = ul.getElementsByTagName("li");
					for(var i=0;i<siblings.length;i++)
					{
						var cName = siblings[i].className.replace("selected", "");
						if(cName.length == 1)
						{
							siblings[i].className = "";
						}
						else
						{
							siblings[i].className = cName;
						}
					}
					var space = (this.className.length)?" ":"";
					this.className = this.className + space + "selected";
					this.blur();
				}
				*/
				location.href = this.getElementsByTagName("a")[0].href;
			}
		
		li.style.cursor = "pointer";
	}

}

/**
* Attach hover functions to tables 
* @param {Object} obj Table object
*/
function attachTableHover(obj)
{	
	if(obj == undefined) return;
	
	var items = obj.getElementsByTagName("th");

	
	for(var i=0;i<items.length;i++)
	{
		var th = items[i];
		var link = th.getElementsByTagName("a")[0];
		if(!link) continue;
		
		th.onmouseover = 
			function()
			{
				
				var space = (this.className.length)?" ":"";
				this.className = this.className + space + "mouse-over";
			}
			
		th.onmouseout = 
			function()
			{
				var space = (this.className=="mouse-over")? "":" ";
				this.className = this.className.replace(space + "mouse-over", "");
			}
			
		th.onclick = 
			function()
			{
				if(this.className.indexOf("active-up") != -1)
				{
					this.className = this.className.replace("active-up", "active-down")
				} 
				else if (this.className.indexOf("active-down") != -1)
				{ 
					this.className = this.className.replace("active-down", "active-up")
				}
				else 
				{
					var siblings = obj.getElementsByTagName("th");
					for(var j=0;j<siblings.length;j++)
					{
						var repl = (siblings[j].className.indexOf("active-up"))?"active-down":"active-up";
						var cName = siblings[j].className.replace(repl, "");
						
						if(cName.length == 1)
						{
							siblings[j].className = "";
						}
						else
						{
							siblings[j].className = cName;
						}

					}
					var space = (this.className.length)?" ":"";
					this.className = "active-up" + space + this.className;

				}
				this.blur();
				
				var aItem = this.getElementsByTagName("a")[0];
				location.href = aItem.href;
			}
						
		items[i].style.cursor = "pointer";
	}
	
	/* For all rows in table body */
	var tbodyItem = obj.getElementsByTagName("tbody");
	//for (var i in tbodyItem){
	for (var i=0; i<tbodyItem.length; i++){
		var trItems = tbodyItem[i].getElementsByTagName("tr");
		for (var j=0; j<trItems.length; j++){
			var thisItem = trItems[j];
			
			thisItem.onmouseover =
			function()
			{
				var space = (this.className.length)?" ":"";
				this.className = this.className + space + "tr-mouse-over";
			}
			
			thisItem.onmouseout =
			function()
			{
				this.className = this.className.replace("tr-mouse-over", "");
				//if(this.className.length) this.className.replace(" ", "");
			}
			
		}
	}
	
}


/**
* Attach hover functions to form-input-buttons 
*/
btnHover = function() {
	
	var btns = document.getElementsByTagName("input");
	
	for (var i=0; i<btns.length; i++) {
		if(btns[i].type == "submit"){// && (btns[i].className == "submit-email" || btns[i].className == "submit-link") ){
		
			if ( btns[i].className == "submit-email" || btns[i].className == "submit-link" )
			{
				btns[i].onmouseover=function() {
					this.className+=" btn_hover";
				}
				btns[i].onmouseout=function() {
					this.className=this.className.replace(new RegExp(" btn_hover\\b"), "");
				}
			} 
			else if ( btns[i].className == "button") 
			{
				btns[i].onmouseover=function() {
					this.className+=" button_hover";
				}
				btns[i].onmouseout=function() {
					this.className=this.className.replace(new RegExp(" button_hover\\b"), "");
				}
			}
			else if ( btns[i].className == "button-back") 
			{
				btns[i].onmouseover=function() {
					this.className+=" buttonBack_hover";
				}
				btns[i].onmouseout=function() {
					this.className=this.className.replace(new RegExp(" buttonBack_hover\\b"), "");
				}
			}
		}
	}
}
//if (window.attachEvent) window.attachEvent("onload", sfHover);


addEvent(window, "load", init);
//addEvent(window, "resize", alertSize);