window.onload = init;

jQuery(document).ready(function()
{
	if($.datepicker)
	{
		$(".datepicker").datepicker($.extend({},
			{ 
				showAnim: "slideDown",
				dateFormat: "d.m.yy"
			}
		));
	}

	// Image Loop
	ImageLoopStart('#banner li');

	$('form select.tourname').unbind('change').change(function()
	{
		$('.tournamevalue').attr('value', $(this).find(':selected').text());
	});	
});

function init()
{
	SetHover();
}

function SetHover()
{
	var nav = document.getElementById("nav");

	if(!nav) { return; }

	var children;

	for(var i = 0, n = nav.childNodes.length; i<n; i++)
	{
		if(nav.childNodes[i].tagName && nav.childNodes[i].tagName.toLowerCase() == "ul")
		{
			children = nav.childNodes[i];
			break;
		}
	}

	if(!children) { return; }

	for(var i = 0, n = children.childNodes.length; i<n; i++)
	{
		var child = children.childNodes[i];

		if(!child) { continue; }

		if(!child.tagName || child.tagName.toLowerCase() != "li") { continue; }

		child.onmouseover = (function(child) { return function()
		{
			child.className += " hover";
		} })(child);

		child.onmouseout = (function(child) { return function()
		{
			child.className = child.className.replace("hover", "");
		} })(child);
	}
}

function lisa_flash(source, width, height, params)
{
	document.writeln('<object codeBase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" height="' + height + '" width="' + width + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">');
	document.writeln('<param name="Movie" value="' + source + '">');

	for(var param in params)
	{
		document.writeln('<param name="' + param + '" value="' + params[param] + '">');
	}

	document.write('<embed src="' + source + '" quality="high" height="' + height + '" width="' + width + '" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" ')

	for(var param in params)
	{
		document.write(param + '="' + params[param] + '" ');
	}

	document.write('/>');
	document.writeln('</object>');
}

function play_video_old(id)
{
	var width = 480;
	var height = 360;

	var player = document.createElement("div");
	player.id = "video-player";
	var close = document.createElement("a");
	var that = this;
	close.onclick = function()
	{
		stop_video();
		return false;
	}
	close.href = "#";
	close.appendChild(document.createTextNode("Close"));

	var flash = document.createElement("div");

	flash.innerHTML = ''+
		'<object id="IVP" codeBase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" height="' + height + '" width="' + width + '" align="middle" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000">' +
		'	<param name="_cx" VALUE="13229" />' +
		'	<param name="_cy" VALUE="7144" />' +
		'	<param name="FlashVars" VALUE="showTrace=false&video=' + id + '&xml=e73e9c3f-7099-45b5-a772-0578aa58ad4d" />' +
		'	<param name="Movie" VALUE="/lisalib/getfile.aspx?itemid=3988" />' +
		'	<param name="Src" VALUE="/lisalib/getfile.aspx?itemid=3988" />' +
		'	<param name="WMode" VALUE="Transparent" />' +
		'	<param name="Play" VALUE="-1" />' +
		'	<param name="Loop" VALUE="-1" />' +
		'	<param name="Quality" VALUE="High" />' +
		'	<param name="SAlign" VALUE="LT" />' +
		'	<param name="Menu" VALUE="0" />' +
		'	<param name="Base" VALUE="" />' +
		'	<param name="AllowScriptAccess" VALUE="sameDomain" />' +
		'	<param name="Scale" VALUE="ShowAll" />' +
		'	<param name="DeviceFont" VALUE="0" />' +
		'	<param name="EmbedMovie" VALUE="0" />' +
		'	<param name="BGColor" VALUE="FFFFFF" />' +
		'	<param name="SWRemote" VALUE="" />' +
		'	<param name="MovieData" VALUE="" />' +
		'	<param name="SeamlessTabbing" VALUE="1" />' +
		'	<param name="Profile" VALUE="0" />' +
		'	<param name="ProfileAddress" VALUE="" />' +
		'	<param name="ProfilePort" VALUE="0" />' +
		'	<param name="AllowNetworking" VALUE="all" />' +
		'	<param name="AllowFullScreen" VALUE="false" />' +
		'	<embed src="/lisalib/getfile.aspx?itemid=3988" menu="false" quality="high" salign="lt" wmode="transparent" bgcolor="#ffffff" width="' + width + '" height="' + height + '" name="IVP" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" FlashVars="showTrace=false&video=' + id + '&xml=e73e9c3f-7099-45b5-a772-0578aa58ad4d" />' +
		'</object>';

	player.appendChild(close);
	player.appendChild(flash);

	document.body.appendChild(player);

	/* opacity overlay */	
	var overlay = document.createElement("div");
	overlay.id = "wrapper-overlay";

	var heights =
	[
		self.innerHeight ? self.innerHeight : 0,
		document.body.scrollHeight ? document.body.scrollHeight : 0,
		document.documentElement.clientHeight ? document.documentElement.clientHeight : 0,
		document.body.clientHeight ? document.body.clientHeight : 0,
		document.body.offsetHeight ? document.body.offsetHeight : 0
	]

	overlayHeight = Math.max.apply(this, heights)

	overlay.style.width = document.body.clientWidth + "px";
	overlay.style.height = overlayHeight + "px";

	document.body.appendChild(overlay);
}

function play_video(id)
{
	var width = 480;
	var height = 360;

	var player = document.createElement("div");
	player.id = "video-player";
	var close = document.createElement("a");
	var that = this;
	close.onclick = function()
	{
		stop_video();
		return false;
	}
	close.href = "#";
	close.appendChild(document.createTextNode("Close"));

	var flash = document.createElement("div");

	flash.innerHTML = '<embed src="/library/5347/filename/player.swf" width="480" height="360" bgcolor="#000000" allowscriptaccess="always" allowfullscreen="true" flashvars="file=/library/' + id + '/filename/video.flv&autostart=true" />';

	player.appendChild(close);
	player.appendChild(flash);

	document.body.appendChild(player);

	/* opacity overlay */	
	var overlay = document.createElement("div");
	overlay.id = "wrapper-overlay";

	var heights =
	[
		self.innerHeight ? self.innerHeight : 0,
		document.body.scrollHeight ? document.body.scrollHeight : 0,
		document.documentElement.clientHeight ? document.documentElement.clientHeight : 0,
		document.body.clientHeight ? document.body.clientHeight : 0,
		document.body.offsetHeight ? document.body.offsetHeight : 0
	]

	overlayHeight = Math.max.apply(this, heights)

	overlay.style.width = document.body.clientWidth + "px";
	overlay.style.height = overlayHeight + "px";

	document.body.appendChild(overlay);
}


function stop_video()
{
	var player = document.getElementById("video-player");
	document.body.removeChild(player);
	document.body.removeChild(document.getElementById('wrapper-overlay'));
}

function ShowDropMenu(element)
{
	doc = document.getElementById(element);
	if(doc.style.display == 'block')
	{
		doc.style.display = 'none';
 	}
 	else
 	{
 		doc.style.display = 'block';
 	}
}

function HideElements(elementname)
{
	document.getElementById(elementname).style.display = 'none';
	return false;
}

function replaceChar(inputString,charIndex,replaseString)
{
	return inputString.substr(0,charIndex) + replaseString + inputString.substr(charIndex + 1);
}

function fixEncoding(theString)
{
	var intLength = theString.length;
	
	//var strWindowsLatin=' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~€‚ƒ…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷úûýþÿ';
	var strISO88591 = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~, ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷úûýþÿ';
	
	strISO88591 += String.fromCharCode(10) + String.fromCharCode(13) //New line chars
	strISO88591 += String.fromCharCode(9) //Tab char
	var arrISO88591 = new Array();
	var intChar = 0;

	for (i=0;i<strISO88591.length;i++)
	{
		arrISO88591[strISO88591.charCodeAt(i)] = 1;
	}	
	if (intLength)
	{
		for (i = intLength; i != 0; i--)
		{
				intChar = theString.charCodeAt(i - 1);
				if(!arrISO88591[intChar])
				{
					theString = replaceChar(theString,i - 1,'&#' + intChar + ';');
				}
		}
	}
	arrISO88591 = null;
	return theString;
}

function email(address1, address2)
{
	location.href = 'mailto:' + address1 + '@' + address2;
}

function validateAllSpamBlocker(form) 
{
	var isAllValid = true;
	var isValid = true;
	var errorMsg = "The following input areas are not properly filled out:\n" + '<br /><br />';
	

	for (var i = 0; i < form.elements.length; i++) 
    	{
        		var elem = form.elements[i];
	        	if (elem.className.indexOf('reqd') > 0) 
	        	{
			isValid = true;
			/* 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('ssnoval') > 0) 
	                		{
	                    		isValid = (elem.value.length == 11);
	                		} 
	                		else if (elem.className.indexOf('dropdownval') > 0) 
	                		{
	                    		isValid = (elem.value != '-- Select --' && elem.value.length != 0);
	                		} 
	                		else 
	                		{
	                    		isValid = (elem.value != '');
	                		}
		                	if (!isValid) 
		                	{		
					errorMsg += '<label for="' + elem.id + '"><strong>' + elem.title + ' is missing</strong></label><br/>'
					elem.focus();
					elem.style.borderColor = '#FF4A4A';
					elem.style.backgroundColor = '#FDFAD0';
					isValid = false;
		                	} 
		                	else 
		                	{
					elem.style.borderColor = '';
					elem.style.backgroundColor = '';
		
					if( document.getElementById('spamAction') )
					{
						document.getElementById('spamAction').name = 'action';
						document.getElementById('spamAction').value = 'process';
					};
		                	}
			}
         			if(!isValid)
         				isAllValid = false;   
		}
	}
	if(!isAllValid)
	{
		document.getElementById('notice').innerHTML = '<h3>Registration error</h3><br/>' + errorMsg;
		document.getElementById('notice').style.visibility = "visible";
	}
	return isAllValid;
}


function isValidEmail(value) {
	return (value.indexOf(".") > 2) && (value.indexOf("@") > 0);
}

function isValidNumber(value, number) {
	var elemIsValid = true;
	var stripped = value.replace(/[\(\)\.\-\ ]/g, '');
	
	if (!(stripped.length == number)) {
		elemIsValid = false;
	}

	if(isNaN(stripped)) {
	     elemIsValid = false;
   	}

	if (stripped.length == 0) {
		elemIsValid = true;
	}
	
	return elemIsValid;
}

function ImageLoopStart( CSS )
{
	var elements = $(CSS);
	if( elements.length > 1 )
	{
		setInterval(  "ImageLoop('"+CSS+"')", 8000 );
	};
};

function ImageLoop( CSS )
{
	var Now = $(CSS+':visible').fadeOut( 2000 );
	var Next = ( Now.next().is(CSS) && Now.next() ) || $(CSS+':first' );
	Next.fadeIn( 2000 );
};