// Tassa napataan mahdolliset virheet
function errorHandler(){
	window.status=""
	return true;
}
//window.onerror = errorHandler;

var iDelay = 200; // milliseconds
var iTimer = -1; // timer id
var objElement; // object handle
var iemac = navigator.userAgent.indexOf("Mac")>0 && document.all ? true:false;
var ns6StartPos = new String(); //= 143px sivuilla; = 221px etusivulla;
var d=document;
var IE4, IE5, NS4, NS6;
var sForms, dForms
// Huom! jos sivulla on lomake joka halutaan piilottaa kun valikko avataan
//		 niin laita sivun body-tagiin: onload="checkForms();"

init();

function init(){
	IE4 = d.all && !document.getElementById ? true:false;
	IE5 = d.all && document.getElementById ? true:false;
	NS4 = d.layers ? true:false;
	NS6 = d.getElementById && !document.all ? true:false;
}

function show( div, pos )
{
	if ( objElement ) hide(); hideForms();
	if ( NS4 ) {
		d.layers[div].visibility = "show";
		d.layers[div].onmouseover = function(){ if (iTimer > -1) { clearTimeout(iTimer); iTimer = -1; } };
		d.layers[div].onmouseout = function(){ spawnHide(div); };
		// check available space & set dropdown menu opening position left/right.-----------
		w=window.innerWidth;h=window.innerHeight;
		mnuWidth=d.layers[div].clip.width;
		if(w<document.layers[div+'_NSpointer'+pos].pageX+mnuWidth){
			d.layers[div].left=document.layers[div+'_NSpointer'+pos].pageX-((document.layers[div+'_NSpointer'+pos].pageX+mnuWidth-w)+16);
		}
		else {d.layers[div].left=document.layers[div+'_NSpointer'+pos].pageX;}
		d.layers[div].top=document.layers[div+'_NSpointer'+pos].pageY+22;
		//alert('NS4');
		// ---------------------------------------------------------------------------------
	}
	else if ( NS6 ) {
		//Tarkistetaan QueryStringista(path) ollaanko etu- vai muilla sivulla.
		var intPath = new Number(QueryStringValue('path').indexOf(";"));

		d.getElementById(div).style.top = ns6StartPos;
		d.getElementById(div).onmouseover = function(){ if (iTimer > -1) { clearTimeout(iTimer); iTimer = -1; } };
		d.getElementById(div).onmouseout = function(){ spawnHide(div); };
		// check available space & set dropdown menu opening position left/right.-----------
		w = window.innerWidth; h = window.innerHeight;
		menuRPos=(d.getElementById(div+'_pointer'+pos).offsetLeft+d.getElementById(div).offsetWidth)+15;
		if(window.scrollX > 0){
			if(w < menuRPos){ d.getElementById(div).style.left=(d.getElementById(div+'_pointer'+pos).offsetLeft-(menuRPos-w));}
			else {d.getElementById(div).style.left = d.getElementById(div+'_pointer'+pos).offsetLeft;}
		}
		else {
			if(w < menuRPos){ d.getElementById(div).style.left=(d.getElementById(div+'_pointer'+pos).offsetLeft-(menuRPos-w));}
			else {d.getElementById(div).style.left = d.getElementById(div+'_pointer'+pos).offsetLeft + 'px';}
		}
		d.getElementById(div).style.top = d.getElementById(div+"_pointer"+pos).offsetTop + 15 + "px";
		// ---------------------------------------------------------------------------------
		d.getElementById(div).style.visibility = "visible";
		//alert('NS6');
	}
	else if ( IE4 || IE5 ) {
		// check available space & set dropdown menu opening position left/right.-----------
		w = d.body.clientWidth; h = d.body.clientHeight;
		menuRPos=(d.all[div+'_pointer'+pos].offsetLeft+160);
		if(d.body.scrollLeft>0 || d.body.scrollTop>0){
			if(w < menuRPos){ d.all[div].style.left=(d.all[div+'_pointer'+pos].offsetLeft-(menuRPos-w));}
			else d.all[div].style.left=d.all[div+'_pointer'+pos].offsetLeft;}
		else {
			if(w < menuRPos){ d.all[div].style.left=(d.all[div+'_pointer'+pos].offsetLeft-(menuRPos-w));}
			else d.all[div].style.left=d.all[div+'_pointer'+pos].offsetLeft;}
		d.all[div].style.top=d.all[div+'_pointer'+pos].offsetTop+15;
		// ---------------------------------------------------------------------------------
		d.all[div].style.visibility = "visible";
		d.all[div].onmouseover = function(){ if (iTimer > -1) { clearTimeout(iTimer); iTimer = -1; } };
		d.all[div].onmouseout = function(){ spawnHide(div); };
		//alert('IE4 || IE5');
	}
}

function spawnHide( div,n)
{
	if ( NS4 ) objElement = document.layers[div];
		else if ( NS6 ) objElement = document.getElementById(div);
		else if ( IE4 || IE5 ) objElement = document.all[div];
		iTimer = setTimeout( "hide()", iDelay );
}

function hide()
{
	showForms();
	
	if ( iTimer > -1 ) {
		clearTimeout(iTimer);
		iTimer = -1;
	}
	if ( NS4 ) objElement.visibility = "hidden";
		else if ( NS6 ) objElement.style.visibility = "hidden";
		else if ( IE4 || IE5 ) objElement.style.visibility = "hidden";
}

// Change menuitems background color
function BG(id,sw){
	if(sw=='1'){ id.style.backgroundColor='#FFCEB0';}
	if(sw=='2' || sw=='0'){ id.style.backgroundColor='#FFE3D2';}
	if(sw=='3'){ id.style.backgroundColor='#ffffff';}
}



// Lomakkeet piiloon kun valikko avataan.
//------------------------------------
function checkForms()
//------------------------------------
// Funktiota kutsutaan body-tagissa, jos sivulla on lomake.
{
	if (NS4) {
		sForms = d.layers['formIlayer'] ? true:false;
		dForms = d.layers['formIlayer0'] ? true:false;
	}
	else if (IE5 || IE4) {
		sForms = d.all['formDiv'] ? true:false;
		dForms = d.all['formDiv0'] ? true:false;
	}
}

//------------------------------------
function hideForms()
//------------------------------------
{
	if (NS4) {
		if (sForms){
			d.layers['formIlayer'].visibility = "hide";
		}
		if (dForms){
			for (var i=0; i < (document.forms.length); i++) {
				if(d.layers['formIlayer'+i]){
					d.layers['formIlayer'+i].visibility = "hide";
				}
			}
		}
	}
	else if (IE5 || IE4) {
		if (sForms){
			d.all['formDiv'].style.visibility = "hidden";
		}
		if (dForms){
			for (var i=0; i < (document.forms.length); i++) {
				if(d.all['formDiv'+i]){
					d.all['formDiv'+i].style.visibility = "hidden";
				}
			}
		}
	}
}

//------------------------------------
function showForms(){
//------------------------------------
	if (NS4) {
		if (sForms){
			d.layers['formIlayer'].visibility = "show";
		}
		if (dForms){
			for (var i=0; i < (document.forms.length); i++) {
				if(d.layers['formIlayer'+i]){
					d.layers['formIlayer'+i].visibility = "show";
				}
			}
		}
	}
	else if (IE5 || IE4) {
		if (sForms){
			d.all['formDiv'].style.visibility = "visible";
		}
		if (dForms){
			for (var i=0; i < (document.forms.length); i++) {
				if(d.all['formDiv'+i]){
					d.all['formDiv'+i].style.visibility = "visible";
				}
			}
		}
	}
}



//Pilkotaan QueryString ja palautetaan halutun muuttujan arvo.
//Kaytetaan NS6/7 ja MOZ, jotta tiedetaan ollaanko etusivulla vai muilla sivuilla.
//---------------------------------------------------------------------------------------------
function PageQuery(q) {
	if(q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;
	this.keyValuePairs = new Array();
	if(q) {
		for(var i=0; i < this.q.split("&").length; i++) {
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	
		this.getKeyValuePairs = function() { return this.keyValuePairs; }

		this.getValue = function(s) {
			for(var j=0; j < this.keyValuePairs.length; j++) {
				if(this.keyValuePairs[j].split("=")[0].toUpperCase().replace(/([^0-9A-Z])/g,"") == s.toUpperCase().replace(/([^0-9A-Z])/g,""))
				return this.keyValuePairs[j].split("=")[1];
			}
			return false;
		}
		
		this.getParameters = function() {
			var a = new Array(this.getLength());
			for(var j=0; j < this.keyValuePairs.length; j++) {
				a[j] = this.keyValuePairs[j].split("=")[0];
			}
			return a;
		}
		
		this.getLength = function() { return this.keyValuePairs.length; } 

	}
}

//-------------------------------------
function queryString(key){
	if(window.location.search.length > 1){
		//Page has querysting, get the key value from PageQuery -function.
		var page = new PageQuery(window.location.search);
		return unescape(page.getValue(key));
	}
	else{
		//Page has not querysting, return false.
		return false;
	}
}
//-------------------------------------
function QueryStringValue(key){
// Return:
//			'false' if path parameter not exist
//			value of key parameter if it exist in querystring
	if(queryString(key) == false){
		return 'false';
	}
	else{
		return queryString(key);
	}
}
//---------------------------------------------------------------------------------------------


// jQuery functions
$(function() {

/*-------------------------------------------------------------------- 
 * JQuery Plugin: "EqualHeights"
 * by:	Scott Jehl, Todd Parker, Maggie Costello Wachs (http://www.filamentgroup.com)
 *
 * Copyright (c) 2008 Filament Group
 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
 *
 * Description: Compares the heights or widths of the top-level children of a provided element 
 		and sets their min-height to the tallest height (or width to widest width). Sets in em units 
 		by default if pxToEm() method is available.
 * Dependencies: jQuery library, pxToEm method	(article: 
		http://www.filamentgroup.com/lab/retaining_scalable_interfaces_with_pixel_to_em_conversion/)							  
 * Usage Example: $(element).equalHeights();
  		Optional: to set min-height in px, pass a true argument: $(element).equalHeights(true);
 * Version: 2.0, 08.01.2008
--------------------------------------------------------------------*/

$.fn.equalHeights = function(px) {
	$(this).each(function(){
		var currentTallest = 0;
		$(this).children().each(function(i){
			if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
		});
		//if (!px || !Number.prototype.pxToEm) currentTallest = currentTallest.pxToEm(); //use ems unless px is specified
		// for ie6, set height since min-height isn't supported
		if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({'height': currentTallest}); }
		$(this).children().css({'min-height': currentTallest}); 
	});
	return this;
};


// elements which want to be equalized

/*
if ($("img").length) {
	$("img").load(function() {
		$("div.content-top").equalHeights(true);
	});
} else {
	$("div.content-top").equalHeights(true);
}
*/
//end jQuery
});
