window.onload = init;

function init()
{
	//equalElementHeight('contentwrapper', 'leftpanel');
	var search = document.getElementById('SearchString1');
	if (search != null) {
		search.onclick = search_onclick;
		search.onblur = search_onblur;
	}
	
	var fontSize = "11";
	if ((getCookie("fontsize") != null) &&  (getCookie("fontsize") != '')) {
		fontSize =  getCookie("fontsize");
	}
	if (fontSize != 11) {
		document.body.style.fontSize = getPercentSize(fontSize) + "%";
	}	

	// FixContentBottom();
	FixFooter();
}


$(function() 
    { 
	$(window).resize(FixFooter);
	FixFooter();
    } 
); 

function FixFooter()
{
	var MinHeight = Math.max( $('#content').height(), $('#leftpanel').height(), $(window).height()-392 );
	$('#content').css( 'minHeight', MinHeight );
}


var lastEmpId=0;
function ShowEmployee(id, node)
{
	if(lastEmpId != 0) {$('#empline'+lastEmpId).show()}
	$('.displayEmp').hide();
	$('#employee'+id).fadeIn('slow');
	$('#empline'+id).hide();
	lastEmpId = id;
	return false;
}

function HideEmployee(id, node)
{	
	$('#employee'+id).hide();
	$('#empline'+id).show();
	return false;
}

function FixContentBottom()
{
	var detect = navigator.userAgent.toLowerCase();
	var rightpanel = document.getElementById('rightpanel');
	var contentbottom = document.getElementById('contentbottom');

	var topbox1 = document.getElementById('topbox1');
	var topbox2 = document.getElementById('topbox2');

	if(rightpanel != null && contentbottom != null && topbox1 != null)
	{
		var xheight = rightpanel.offsetHeight;
		var contentarea = contentbottom.offsetHeight;

		if(detect.indexOf('msie') != -1){
			contentarea = contentarea + topbox1.offsetHeight + 26;		
		}
		else
		{
			contentarea = contentarea + topbox1.offsetHeight + 14;
		}
	
		//alert(contenttop.offsetHeight + " " + contentbottom.offsetHeight + " " + testbox.offsetHeight);
 
		//alert(contentarea + " " + xheight);
		if(contentarea > xheight)
		{
			rightpanel.style.height = contentarea + "px";
		}
		else
		{
			var difference = xheight - contentarea;
			topbox1.style.height = difference + topbox1.offsetHeight;
			topbox2.style.height = difference + topbox2.offsetHeight;
		}
	
		xheight = rightpanel.offsetHeight;

		if(detect.indexOf('msie') != -1){
			xheight = xheight + 36;		
		}
		else
		{
			xheight = xheight + 24;
		}
	
		contentbottom.style.top = xheight + "px";
	}
}

function search_onclick() {
	var search = document.getElementById('SearchString1');
	if (search.value == 'Sláðu inn leitarorð') {
		search.value = '';
	}
}

function search_onblur() {
	var search = document.getElementById('SearchString1');
	if (search.value == '') {
		search.value = 'Sláðu inn leitarorð';
	}
}

function enlargeFont() {
	var size = getCookie("fontsize");

	if (size == null) {
		size = 11;
	}
	size = parseInt(size) + 2;
	
	if (size > 13) {size = 13}
	document.body.style.fontSize = getPercentSize(size)+'%';
	setCookie("fontsize",size);
}

function shrinkFont() {
	var size = getCookie("fontsize");
	if (size == null) {
		size = 11;
	}
	size = parseInt(size) - 2;
	if (size < 9) {size = 9}
	document.body.style.fontSize = getPercentSize(size)+'%';
	setCookie("fontsize",size);
}

function restoreSize() {
	size = "11";
	document.body.style.fontSize = getPercentSize(size)+'%';
	setCookie("fontsize",size);	
}

function getPercentSize(size) {
	return (size/16)*100;
}

function setCookie(cookieName,cookieValue) {
	var today = new Date();
	var expire = new Date();
	expire.setTime(today.getTime() + 3600000*24*1000);
	document.cookie = cookieName + "=" + escape(cookieValue) + "; path=/; expires="+expire.toGMTString();	
	//document.cookie = cookieName + "=" + escape(cookieValue) + ";expires="+expire.toGMTString();
}

function getCookie(cookieName) {
	oCookie = document.cookie;
	var index = oCookie.indexOf(cookieName + "=");
	if (index == -1) return null;
	index = oCookie.indexOf("=", index) + 1;
	var endstr = oCookie.indexOf(";", index);
	if (endstr == -1) endstr = oCookie.length;
	return unescape(oCookie.substring(index, endstr));
}
function popcorn(x,w,h,y)
{
	var f='scrollbars,resizable'+(w?',width='+w:'')+(h?',height='+h:'')
	return !window.open(x,y||'popcorn',f) 
}

/* General form-validation */
function validate(form) {
	var isValid = true;
	
	for (var i = 0; i < form.elements.length; i++) {
		var elem = form.elements[i];
		if (elem.className.indexOf('reqd') > 0) {
			
			/* input, select og textarea er höndlað á sama hátt .... */
			if ((elem.tagName == "INPUT") || (elem.tagName == "TEXTAREA") || (elem.tagName == "SELECT")) {			
				if (elem.className.indexOf('emailval') > 0) {
					isValid = isValidEmail(elem.value);
				} else {
					isValid = (elem.value != '');
				}
				
				if (!isValid) {
					alert(elem.title + ' er ekki rétt útfyllt!');
					elem.focus();
					elem.style.borderColor = '#FF4A4A';
					elem.style.backgroundColor = '#FDFAD0';
					return false;
				} else {
					elem.style.borderColor = '';
					elem.style.backgroundColor = '';
				}
			}			
		}
	}
	return true;
}

function isValidEmail(value) {
	return (value.indexOf(".") > 2) && (value.indexOf("@") > 0);
}

function sortNum(a,b) { return b-a }

function equalElementHeight(one,two)
{
	if(document.getElementById(one) && document.getElementById(two))
	{
		var obj = [];
		var option = [one,two];
		for(var i = 0; i < option.length; i++)
		{
			obj[i] = document.getElementById(option[i]).offsetHeight;
			nh = obj.sort(sortNum);
		}
		nh1 = nh.splice(1,2);
		var newHeight = parseInt(nh) + parseInt(10);
		for(var i = 0; i < option.length; i++)
		{
			document.getElementById(option[i]).style.minHeight = newHeight + "px";
		}
	}
}


function lisa_flash(s,w,h)
{
	var a=arguments
	with(document)
	{
		writeln('<object codeBase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" height="'+h+'" width="'+w+'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="asdf">')
		writeln('<param name="Movie" value="'+s+'">')
		for(var i=3;i<a.length;i+=2)writeln('<param name="'+a[i]+'" value="'+a[i+1]+'">')
		write('<embed src="'+s+'" quality="high" height="'+h+'" width="'+w+'" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" ')
		for(i=3;i<a.length;i+=2)write(a[i]+'="'+a[i+1]+'" ')
		writeln('/>')
		write('</object>')
	}

}