function init()
{
	//Initialize the searchboxes
	$('#SearchString1').click( function() { if (this.value == 'Leitarorð') { this.value = '';} } ).blur( function() { if (this.value == '') { this.value = 'Leitarorð';} } );
	
	InitEmployeeList();
	InitAgricultureList();

	$('#newsStartdate').datepicker({ dateFormat: 'yy-mm-dd' }); 
	$('#newsEnddate').datepicker({ dateFormat: 'yy-mm-dd' }); 
}

function InitEmployeeList()
{
	$('.employee-depts select').change(
		function()
		{
			$('.employee-list').load('/um-bi/starfsfolk?deild=' + this.value + '&templateid=5 .employee-list');
		}
	)
}

function InitAgricultureList()
{
	$('.agriculture-listing ul li a').click(
		function()
		{
			$('.agriculture-listing').load(this.href+'&templateid=5 .agriculture-listing', function() { InitAgricultureList() });
			return false;
		}
	)
}

$(init);

function enlargeFont()
{
	var size = getCookie("fontsize");
	if (size == null)
	{
		size = 11;
	}
	size++;
	if (size > 15) {size = 15}
	document.body.style.fontSize = getPercentSize(size)+'%';
	setCookie("fontsize",size);
}

function shrinkFont()
{
	var size = getCookie("fontsize");
	if (size == null) {
		size = 11;
	}
	size--;
	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) + ";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));
}

/* 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 if(elem.className.indexOf('timeval') > 0) 
				{
					isValid = isTimeValid(elem.value);					
				}
				else if(elem.className.indexOf('dateval') > 0) 
				{
					isValid = isDateValid(elem.value);					
				}
				else 
				{
					isValid = (elem.value != '');
				}
				
				if (!isValid) {
					alert(elem.title + ' ekki rétt útfyllt(ur)!');
					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 isTimeValid(value)
{
	var timeRegEx = /^(([0-9])|([0-1][0-9])|([2][0-3])):(([0-9])|([0-5][0-9]))$/;
	var time = value;
	if(time.match(timeRegEx))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function isDateValid(value)
{
	var dateRegEx = /^(([2][0][0-2][0-9])-(([1-9])|([0][1-9])|([1][0-2]))-(([1-9])|([0-2][1-9])|([3][0-1])))$/;	
	var date = value;
	if(date.match(dateRegEx))
	{
		return true;
	}
	else
	{
		return false;
	}
}

/*
 * Shadow
 * Created by Edgar Verle
 * Shadow is made for the jQuery library.
 * http://www.edgarverle.com/shadow
 */

/* 
 * @name shadow
 * @type jQuery
 * @param Map options Optional settings
 * @option Number cornerHeight The height in pixels of the corners of the shadow. Default: 8
 * @option Number width The width in pixels of the shadow. Default: 4
 * @option Number startOpacity This is the opacity at the closest point to the object. Default: 80
 * @option Number endOpacity The opacity at the furthest point in the shadow from the object. Default: 20
 * @option String color The color of the shadow. Default: black
 */
 
jQuery.fn.extend({
	shadow: function(options) {
		DropShadow(this, options);
	}
});

function DropShadow(element, options){
	var cornerHeight = 8;
	var width = 4;
	var startOpacity = 80;
	var endOpacity = 20;
	var color = "black";
	
	if(options){
		if(options["cornerHeight"] != null)
			cornerHeight = parseInt(options["cornerHeight"]);
		if(options["width"] != null)
			width = parseInt(options["width"]);
		if(options["startOpacity"] != null)
			startOpacity = parseFloat(options["startOpacity"]);
		if(options["endOpacity"] != null)
			endOpacity = parseFloat(options["endOpacity"]);
		if(options["color"] != null)
			color = options["color"];
			
		if(startOpacity < 1)
			startOpacity = Math.round(startOpacity*100);
		
		if(endOpacity < 1)
			endOpacity = Math.round(endOpacity * 100);
	}
	
	if($(element.id+"_shadow").length > 0)
		$(element.id+"_shadow").remove();
	
	element = $(element);
	
	var id = element[0].id + "_shadow";
	var html = "<div id='"+id+"' style='";
	
	if(element.css("position") == "absolute" || element.css("position") == "relative"){
		html += "position: " + element.css("position")+"; ";
		element.css("position", "");
	}
	
	if(element.css("top")){
		html += "top:"+element.css("top")+"; ";
		element.css("top", "");
	}

	if(element.css("left")){
		html += "left:"+element.css("left")+"; ";
		element.css("left", "");		
	}
	
	html += "'><table style='float:left;' cellpadding='0px' cellspacing='0px'><tr class='shadowFirstRow'>"+
		"<td colspan='2' rowspan='2'></td></tr></table></div>";
	
	element.wrap(html);
		
	html = "<td colspan='"+width+"' style='width:"+width+"px; height:"+cornerHeight+"px;'><table cellpadding='0px' cellspacing='0px'>";
	
	var opacity = startOpacity;
	
	for(var i = 0; i < cornerHeight; i++){
		html += "<tr>";
		
		for(var j = 0; j < width; j++){
			opacity = Math.round((i - j * (cornerHeight / width)) * 10);
			
			if(opacity < 0)
				opacity = 0;
			else if(opacity < 10)
				opacity = "0"+opacity;
				
			html += "<td style='height:1px; width:1px; background-color: "+color+"; opacity:0."+opacity+"; filter:Alpha(opacity="+opacity+");'></td>"
		}
		
		html += "</tr>";
	}
	
	html += "</table></td>";
	
	$("#"+id).children().children().children(".shadowFirstRow").append(html);
	
	html = "<tr>";
	
	for(var i = startOpacity; i >= endOpacity; i -= (startOpacity - endOpacity) / (width - 1)){
		opacity = Math.round(i);
		
		if(opacity < 10)
			opacity = "0" + opacity;
			
		html += "<td style='background-color:"+color+"; padding:0px; margin:0px; height:"+(element.height()- cornerHeight + 12)+"px; opacity:0."+opacity+"; filter:Alpha(opacity="+opacity+");'></td>";
	}
	
	html += "</tr>";
	
	html += "<tr><td rowspan='"+width+"' style='width:"+cornerHeight+"px; height:"+width+"px;'><table cellpadding='0px' cellspacing='0px'>";
	for(var i = 0; i < width; i++){
		html += "<tr>";
		
		for(var j = 0; j < cornerHeight; j++){
			opacity = Math.round((0 - i * (cornerHeight / width) + j)*10);
			
			if(opacity < 0)
				opacity = 0;
			else if(opacity < 10)
				opacity = "0"+opacity;
				
			html += "<td style='width:1px; height:1px; background-color:"+color+"; opacity:0."+opacity+"; filter:Alpha(opacity="+opacity+");'></td>";
		}
		
		html += "</tr>";
	}
	
	html += "</table></td>";
	
	var temp = element.width() + 4;
	
	if($.browser.msie)
		temp -= cornerHeight + 4;
	
	for(var i = startOpacity; i >= endOpacity; i -= (startOpacity - endOpacity) / (width - 1)){
		if(i != startOpacity)
			html += "<tr>";
		
		opacity = Math.round(i);
		
		if(opacity < 10)
			opacity = "0"+opacity;
			
		html += "<td style='height:1px; width:"+temp+"px; background-color:"+color+"; opacity:0."+opacity+"; filter:Alpha(opacity="+opacity+");'></td>";
					
		for(var j = startOpacity; j >= endOpacity; j -= (startOpacity - endOpacity) / (width - 1)){
			opacity = Math.round(j);
			
			if(opacity > i)
				opacity = Math.round(i);
			
			
			if(opacity < 10)
				opacity = "0"+opacity;
				
			html += "<td style='width:1px; height:1px; background-color:"+color+"; opacity:0."+opacity+"; filter:Alpha(opacity="+opacity+");'></td>";
		}
		
		html += "</tr>";
	}
	
	$("#"+id).children("table").append(html);
}
