	window.onresize = resize;
	
	/**
	 * Function to initialize menu and windows on page load
	 * @return void
	 */
	function init()
	{
		AccordionMenu.init({collapseprev: true});
		resize();
		
		// -- reset country and language displays		
		try{
			$("#countries").hover(doNothing, reset);
			$("#languages").hover(doNothing, reset);
		} catch(e){}
	}
	
	function doNothing(){}
	/**
	 * Function for resetting search box, language and country displays
	 * @return void
	 */
	 function reset()
	 {
	 	try{
		$("#countries").css("display", "none");
	 	$("#languages").css("display", "none");
		} catch(e) {}
	 }
	
	/**
	 * 	Helper function to create XML request
	 *  @return object
	 */
	function createXMLHttpRequest() 
	{
		try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
		try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
		try { return new XMLHttpRequest(); } catch(e) {}
		alert("XMLHttpRequest not supported");
		return null;
	}
	
	/**
	 * Main navigation color on hover
	 * @return void
	 */
	function showbg(element)
	{
		if(element.className == "menuitem" || element.parentNode.className == "menuitem")
		{
			element.style.color = "#2b3782";
		}
	}
	
	/**
	 * Main navigation color normally
	 * @return void
	 */   
  	function hidebg(element)
  	{
		if(element.className == "menuitem"  || element.parentNode.className == "menuitem")
		{
			element.style.color = "#141d54";
		}
  	}
  	
 	/**
 	 * Select text color on hover
 	 * @return void
 	 */	
	function SelectLanguageHover(element)
	{
		element.style.backgroundColor = "#141D54";
		element.style.color = "White";
	}
	
	/**
	 * Returns text color to normal on hover out
	 * @return void
	 */
	function SelectLanguageHoverOut(element)
	{
		element.style.backgroundColor = "White";
		element.style.color = "#3B3C45";
	}
	
	/**
	 * Country selection function. Submits country selection
	 * @return void
	 */
	function SelectCountry(country_id, country_name)
	{
	 	$("#countries").css("display", "none");
	 	$("#country").attr("value", country_id);
	 	$("#selectedCountry").html(country_name+"<img src='/application/images/menu_selected.png' id='country_selection_arrow' onclick='elementVisibility(\"countries\");'>");
	 	$("#selection_testi").submit(); 	
	}
	
	/**
	 * Display / Hide element 
	 * @return void
	 */ 
	function elementVisibility(element)
	{		
		if($("#"+element).css("display") == "block")
			$("#"+element).hide();
		else
			$("#"+element).show();
	}
	
	/**
	 * Language selection function. Submits languages selection
	 * @return void
	 */
	function SelectLanguage(language_id, flag_path)
	{
		$("#languages").hide();
		$("#displayedFlag").attr("src", flag_path);
		$("#language").attr("value", language_id);
		$("#selection_testi").submit();
	}
	
	/**
	 *	Function for loading all available languages using Ajax technique.
	 *	@return void
	 */
	function LoadAvailableLanguages(element)
	{
		var http = createXMLHttpRequest();
		
		var url = "/application/scripts/getAvailableLanguages.php";
		var parameters = "country_id="+element.value;
		
		http.open("POST", url, true);
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		http.onreadystatechange = function()
		{
				$("#languages").html(http.responseText);
		}
		http.send(parameters);
	}
	
	/**
	 * Clear search text
	 * @return void
	 */
	function clearSearch()
	{
		$("#search").attr("value", "");
	}
	
	/**
	 * Resets search text
	 * @return void
	 */
	function resetSearch()
	{
		$("#search").attr("value", "search");
	}
	
	function submitSearch()
	{
		$("#search-form").submit();
		//document.getElementById("search-form").submit();
	}
	
	/**
	 * Get window height for different browsers (doesn't work on Opera)
	 * @return int
	 */
	function getHeight()
	{
		if( typeof(window.innerHeight) == 'number') return window.innerHeight;
		else if(document.documentElement.clientHeight) return document.documentElement.clientHeight;
		else if(document.body.clientHeight) return document.body.clientHeight;
	}

	/**
	 * Get window width for different browsers (doesn't work on Opera)
	 * @return int
	 */
	function getWidth()
	{
		if( typeof(window.innerWidth) == 'number') return window.innerWidth;
		else if(document.documentElement.clientWidth) return document.documentElement.clientWidth;
		else if(document.body.clientWidth) return document.body.clientWidth;
	}
	
	/**
	 *	Resizes contents and positions main container depending on viewport height and windowsize
	 *	@return void
	 */
	function resize() 
	{
		// -- resize content
		if(document.getElementById("content").offsetHeight < "500")
		{
			document.getElementById("content").style.height = "500px";
		}
		
		// -- resize left panel
		if(document.getElementById("leftbar").offsetHeight < "506")
		{
			document.getElementById("leftbar").style.height = "506px";
		}			
	}
	
	/**
	 * Gets data from flash for further javascript processing
	 * @return void
	 */
	function onBannerUpdate(clipIndex)
	{
		createCookie("clipIndex", clipIndex, 1);
	}
	
	/**
	 * Create javascript cookie ( http://www.quirksmode.org/js/cookies.html )
	 * @return void
	 */
	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=/";
	}
	
	/**
	 * Read javascript cookie ( http://www.quirksmode.org/js/cookies.html )
	 * @return string Returns string or null if cookie is not set
	 */
	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;
	}

 	/**
 	 * Erases cookie ( http://www.quirksmode.org/js/cookies.html )
 	 * @return void
 	 */
	function eraseCookie(name) 
	{
		createCookie(name,"",-1);
	}
	
	/**
	 * Function for handling flash url
	 * @return void
	 */
	function setFlash()
	{
		var movie_url = $("#movie_url").attr("value");

		if(readCookie("clipIndex") != null)
		{	
			movie_url += "&startIndex="+readCookie("clipIndex")+"&gotoEnd=1";
		}
		else
		{	
			movie_url += "&startIndex=0";
		}
		
		$("#movie").attr("value", movie_url);
		$("#movie_embed").attr("src", movie_url);
	
		$("#flash").flash({
			src: movie_url,
    		width: 690,
   			height: 227
		});
	}
	
	/**
	 * Display selected number of news
	 * @return void
	 */
	function displayNews()
	{
		var url = "/application/scripts/getNews.php";	

		$("#newslisting").load(url, { 
				timeperiod: $("input[@name=timeperiod]:checked").val(),
				language: $("#curlang").attr("value"),
				page: $("#page").attr("value") 
			}, function(){
				$("#content").height($("#newslisting").height());
			} 
		);
	}
	
	/**
	 * Get viewport top relative to the document
	 * @return int
	 */
	var topPos;
	function getTopPos()
	{
		topPos = 0;
		if(document.body.scrollTop) topPos = document.body.scrollTop;
		else if(window.pageYOffset) topPos = window.pageYOffset;
		else if(window.scrollY) 	topPos = window.scrollY;
		
		if(topPos == "NaN") topPos = 0;
		
		if(topPos < 285) 	topPos = topPos + 250 + "px";
		else				topPos = topPos + 50 + "px";
		
	}
	
	/**
	 * Get left position for news item
	 * @return int
	 */
	var leftPos;
	function getLeftPos()
	{
		leftPos = 0;
		var centerpoint = $("body").width() / 2;

		leftPos = centerpoint - (749 / 2);
		leftPos = parseInt(leftPos) + "px";
	}
	
	/**
	 * Display one news item
	 * @return void
	 */
	function displayNewsItem(newsID)
	{		
		var url = "/application/scripts/getNewsItem.php?newsID="+newsID;
		var popup = window.open(url, "TalentorNews", "toolbar=0,menubar=0,resizable=1,height=680,width=668");  
		return;	
	}
	
	function hideNewsItem(newsID)
	{
		if($.browser.msie)
		{
			$("#"+newsID).remove();
			$("#blackbox").remove();
			$("#newsitem-container").remove();
		}
		else
		{
			$("#"+newsID).fadeOut("slow", function(){
				$("#blackbox").fadeOut("slow", function(){
					$("#"+newsID).remove();
					$("#blackbox").remove();
					$("#newsitem-container").remove();
				});
			});
		}
		
	}
	
	// Eipä taho toimia tuo highlight plugin vaikka sitä muokattiin tähän jquery versioon toimivaksi, prkleee!
	function highlightSearchWords(wordlist)
	{
		var words = wordlist.split(" ");

		for(var i=0; i<words.length; i++)
		{
			$("#contentcontainer").highlight(words[i]);
		}
	}

	
	/**
	 * On page load execute this function
	 * @return void
	 */
	$(document).ready(function()
	{
		init();
		setFlash();
		$("#submenu").hide();
		
		$(".link").hover( function(){ 
				
				$(this).css("text-decoration", "underline");
				$(this).css("color", "#527ACF");
				
			}, function(){ 
				
				$(this).css("text-decoration", "none");
				$(this).css("color", "#0655AD");
			} );
	});