/**
* 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"));
	attachMenuFunctions($("left-nav"));
	attachMenuFunctions($("gen-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)
{
	window.rootMenuItems = new Array();
	if(obj == undefined) return;
	
	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" || items[i].parentNode.parentNode.parentNode.id == "gen-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(" ", "");
			}
			
		}
	}
	
}

addEvent(window, "load", init);

var cookieName = "folk1Favoriter";

/**
* Adds a favorite
*
*/
function addFavorite(favorite) {
	// Hämta nuvarande cookieinnehåll
	var codedFavorites = getCookie(cookieName);
	if (codedFavorites.length>0) {
		favorites = codedFavorites.split("@");
		var foundFavorite = false;
		// Finns denna sida som favorit?
		for (i=0; i<favorites.length; i++) {
			if (favorites[i] == favorite) {
				foundFavorite = true;	
			}
		}
		if (foundFavorite == false) {
			newFavorites = codedFavorites + "@" + favorite;
			setCookie(cookieName, newFavorites, 365);
		}	
	} else {
		setCookie(cookieName, favorite, 365);
	}
}

/**
* Sets cookie information
*
*/
function setCookie(c_name,value,expiredays)
{
	var exdate=new Date()
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString() + ";path=/");
}

/**
* Gets cookie information
*
*/
function getCookie(c_name)
{
	if (document.cookie.length>0)
  	{
  		c_start=document.cookie.indexOf(c_name + "=")
  		if (c_start!=-1)
    	{ 
    		c_start=c_start + c_name.length+1;
    		c_end=document.cookie.indexOf(";",c_start)
    		if (c_end==-1) c_end=document.cookie.length
    			return unescape(document.cookie.substring(c_start,c_end));
    		} 
  		}
	return "";
}

function getFavoriteName(index) {
	var cookieInfo = getCookie(cookieName);
	var favorites = cookieInfo.split("@");
	var favorite = favorites[index];
	return favorite.split("&&")[1];
}

function getFavoriteUrl(index) {
	var cookieInfo = getCookie(cookieName);
	var favorites = cookieInfo.split("@");
	var favorite = favorites[index];
	return favorite.split("&&")[0];
}

function getNumberOfFavorites() {
	var cookieInfo = getCookie(cookieName);
	if (cookieInfo.length>0) {
		var favorites = cookieInfo.split("@");
		return favorites.length;
	}
	return 0;
}

/**
* Removes a favorite
*
*/
function removeFavorite(index) {
	// Hämta nuvarande cookieinnehåll
	var codedFavorites = getCookie(cookieName);
	if (codedFavorites.length>0) {
		favorites = codedFavorites.split("@");
		if (index >= 0) {
			favorites.splice(index,1);
			setCookie(cookieName, favorites.join("@"), 365);
		}	
	}
}
/**
* Removes a favorite by name
*
*/
function removeFavoriteByName(name) {
	// Hämta nuvarande cookieinnehåll
	var codedFavorites = getCookie(cookieName);
	if (codedFavorites.length>0) {
		favorites = codedFavorites.split("@");
		for (i=0; i<favorites.length; i++) {
			if (favorites[i] == name) {
				favorites.splice(i,1);
				setCookie(cookieName, favorites.join("@"), 365);				
			}
		}	
	}
}

/**
* Returns true if the user has current page as favorite
*
*/
function hasFavorite(name) {
	// Hämta nuvarande cookieinnehåll
	var codedFavorites = getCookie(cookieName);
	if (codedFavorites.length>0) {
		favorites = codedFavorites.split("@");
		for (i=0; i<favorites.length; i++) {
			if (favorites[i] == name) {
				return true;				
			}
		}	
	}
	return false;
}

function showCookie() {
 	var cookieString = getCookie(cookieName);
 	alert(cookieString);
}
/**
Used to open a new window wtih internal phonebook
**/
function launchInternPhone() {
		 var newWindow = window.open("http://nice.intern.folksam.se/netwiseofficefolksam","Telefonkatalogen","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,height=440,width=720,top=40,left=10");
}