preloadImages();

/*Global variables*/

var ieVersion = 0;

function inputFocusIn(field,defaultText,password) {
	if($(field).attr("value") != defaultText)
		return;
		
	$(field).attr("value","");
	
	if(password) {
		document.getElementById("password2").style.display = "none";
		document.getElementById("password").style.display = "block";
		document.getElementById("password").value = "";
		document.getElementById("password").focus();
	}
}

function inputFocusOut(field,defaultText,password) {
	if($(field).attr("value") != "" )
		return;
	
	$(field).attr("value",defaultText);
	
	if(password) {
		document.getElementById("password2").style.display = "block";
		document.getElementById("password").style.display = "none";
		document.getElementById("password2").value = defaultText;
	}
}

function naviOver(item,lang,name) {
	$(item).attr("src","/images/navi/" + lang + "/" + name + "2.png");
}

function naviOut(item,lang,name,currentSection) {
	if(!currentSection)
		$(item).attr("src","/images/navi/" + lang + "/" + name + ".png");
}

function getOptionIndexByValue(list,value) {
	for(var i = 0; i < list.length; i++) {
		if(list.options[i].value == value)
			return i;
	}
		
	return -1;
}

/* Layer */

function openLayer(ID, infotext) {
	var width = getWidth();
	var height = getHeight();
	
	var pageHeight = parseInt(xDocSize().h);
	
	$("#" + ID).css("display","block");
	
	var layerHeight = parseInt($("#" + ID).css("height")) ? $("#" + ID).css("height") : 150;
	
	// Position layer
	$("#" + ID).css("top",height / 2 - parseInt(layerHeight) / 2 + getScrollY());
	
	$("#" + ID).css("left",width / 2 - parseInt($("#" + ID).css("width")) / 2 + getScrollX());
	
	$("#shader").css("width",getWidth());
	$("#shader").css("height", pageHeight);
	$("#shader").css("opacity",0.7);
	$("#shader").css("display","block");
	
	//Set infotext if info box
	if(infotext != 'undefined'){
		$("#finishedbox_infotext").html("<h2>"+infotext+"</h2>");
	}
}

function closeLayer(ID) {
	$("#" + ID).css("display","none");
	$("#shader").css("opacity",0);
	$("#shader").css("display","none");
}

/* Shader tools */

function xDocSize()
{
  var b=document.body, e=document.documentElement;
  var esw=0, eow=0, bsw=0, bow=0, esh=0, eoh=0, bsh=0, boh=0;
  if (e) {
    esw = e.scrollWidth;
    eow = e.offsetWidth;
    esh = e.scrollHeight;
    eoh = e.offsetHeight;
  }
  if (b) {
    bsw = b.scrollWidth;
    bow = b.offsetWidth;
    bsh = b.scrollHeight;
    boh = b.offsetHeight;
  }
//  alert('compatMode: ' + document.compatMode + '\n\ndocumentElement.scrollHeight: ' + esh + '\ndocumentElement.offsetHeight: ' + eoh + '\nbody.scrollHeight: ' + bsh + '\nbody.offsetHeight: ' + boh + '\n\ndocumentElement.scrollWidth: ' + esw + '\ndocumentElement.offsetWidth: ' + eow + '\nbody.scrollWidth: ' + bsw + '\nbody.offsetWidth: ' + bow);
  return {w:Math.max(esw,eow,bsw,bow),h:Math.max(esh,eoh,bsh,boh)};
}

function getScrollX() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function getScrollY() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function getWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function getHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}


function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

/**
 * Preloads images
 */
function preloadImages() {
	if (document.images) {
		
		var images = new Array();
		
		// Button
		images[0] = "/images/navi/fi/apuja2.png";
		images[1] = "/images/navi/fi/etusivu2.png";
		images[2] = "/images/navi/fi/hommaraha2.png";
		images[3] = "/images/navi/fi/kilpailut2.png";
		images[4] = "/images/navi/fi/palvelusta2.png";
		images[5] = "/images/navi/fi/projektit2.png";
		images[6] = "/images/navi/fi/ryhmat2.png";
		images[7] = "/images/navi/fi/yhteistyossa2.png";
			
		for(var i = 0; i < images.length; i++) {
			var preload_image_object = new Image(25,25);
			preload_image_object.src = images[i];
		}
	}
}

function showTopNaviLayer() {
	if($("#topNaviLayer").css("display") == "block") {
		$("#topNaviLayerButton").removeClass("selected");
		$("#topNaviLayer").css("display","none");
		// if(ieVersion == 6 || ieVersion == 7)
			// $("#mainNavi .right").css("visibility","visible");
	}
	else {
		$("#topNaviLayerButton").addClass("selected");
		$("#topNaviLayer").css("display","block");
		$("#topNaviLayer div.wrapper").html("<img src='/images/ajax-loader.gif' style='margin-left: 23px;' /><div class=\"bottom\"></div>");
		$("#topNaviLayer div.wrapper").load("/notifications/ajaxindex");
		// if(ieVersion == 6 || ieVersion == 7)
			// $("#mainNavi .right").css("visibility","hidden");
	}
}


function markNotifications() {
	var ids = "";
	$("input[type='checkbox'][name^='notifications']:checked").each(function() {
		ids += $(this).val() + ",";
		$(this).parent("div").parent("div").slideUp();
	});
	$.get("/notifications/ajaxindex", {mark: ids});
	updateNotificationCount();
	return false;
}

function updateNotificationCount() {
	$("#notificationCount").load("/notifications/ajaxindex/count/all");
}

$(document).ready(updateNotificationCount);

setInterval("updateNotificationCount()", 30000);
