$(function(){

	$('.searchpanel input.button').val('');

	$('div#wrapper > div.content > div.rightcol > div.subcol').height( $('div#wrapper > div.content').height() - 161 );

	/*$('a, form').each(function(){
		if( /kogun\.is/i.test( this.href ) )
		{
			if( this.pathname === '' || this.pathname === '/' || /pageid=766/i.test( this.pathname ) || /pages\/766/i.test( this.pathname ) || /pageid=1/i.test( this.pathname ) || /pages\/1/i.test( this.pathname ) )
			{
				this.href = '/new';
			}
			else
			{
				var ps = /\?/.test( this.href )?'&':'?';
				this.href += ps+'templateid=24';
			}
		}
		else if( this.action )
		{
			this.action += '/~/templateid/24'
		}
	});*/

	var fontSizeCookie = cookie('fontSize');
	if( fontSizeCookie ) fontSize(fontSizeCookie);
	$('.fontSize ul li.smallerFont a').click(function(){
		fontSize(-1);
	});
	$('.fontSize ul li.biggerFont a').click(function(){
		fontSize(1);
	});

	gallery();

});

if( !window.console )
{
	console = { trace:'Logger:\n', log:function(input){ console.trace += input+'\n'; } };
	window.onhelp = function(){ alert(console.trace); return false; }; // ATH! comment-a ut!
};

function fontSize( input )
{
	$('div#wrapper > div.content > div.rightcol > div.subcol').height( $('div#wrapper > div.content').height() - 161 );
	var fontSizeNow = $('body').css('fontSize').replace('px','')*1;
	if( !fontSize.base ) fontSize.base = fontSizeNow;

	if( isNaN(input) )
	{
		fontSizeNow = input.replace('px','')*1;
	}
	else
	{
		fontSizeNow += input;
	}

	if( fontSize.base <= fontSizeNow && fontSizeNow <= fontSize.base+3 )
	{
		$('body').css( 'fontSize', fontSizeNow );
		cookie( 'fontSize', fontSizeNow+'px', 999 );
	}
}

function cookie(name,value,days)
{
	if(value)
	{
		var expires = ""; 
		if (days) 
		{
			var date = new Date();
			date.setTime(date.getTime()+(days*86400000));
			expires = "expires="+date.toGMTString();
		}
		document.cookie = name + "=" + value + "; " + expires + "; path=/";
	}
	else
	{
		var re = new RegExp('(^|.*; )'+name+'=','i');
		if( !re.test(document.cookie) ) return '';
		return document.cookie.replace(re,'').replace(/;.*/i,'');
	}

}

// v 1.0.0 Lakio er hér
function gallery()
{
	// bind click and preload images and add selected on first item ( eq(0) )
	$('.picturealbum > .thumbs > ul > li > a').click( tumbClick ).each( preload ).eq(0).parent().addClass('selected');

	// activate prev and next links
	$('.picturealbum > .links > a').click( prevNextClick );


	function tumbClick()
	{
		$('.picturealbum > .thumbs > ul > li.selected').removeClass( 'selected' );
		$(this).parent().addClass( 'selected' );

		var bigSrc = $(this).attr( 'href' );
		var bigAlt = $(this).find( 'img' ).attr( 'alt' );
		var bigCaption = $(this).find( 'img' ).attr( 'caption' );

		// set the src and alt on the big image
		$('.picturealbum > .bigpicture img').attr( 'src', bigSrc ).attr( 'alt', bigAlt );

		// set the caption
		$('.picturealbum > .caption').html( '' ).html( bigCaption );

		// change the link on the big picture
		$('.picturealbum > .bigpicture a').attr( 'href' , this.href.replace( /proc\/[^/]*\// , 'proc/14/' ) );

		return false;
	}
	function prevNextClick()
	{
		var selectedThumb = $('.picturealbum > .thumbs li.selected');
		var prevNextThumb = $(this).hasClass('prev') ? selectedThumb.prev().find('a') : selectedThumb.next().find('a');
		if( prevNextThumb.length ) prevNextThumb.click();
		return false;
	}
	function preload(i)
	{
		if( i <= 10 )
		{
			(new Image()).src = this.href;
		}
	}
}


/* General form-validation */
function validate(form) {
	var isValid = true;
	var ifCount = 0;

	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('ssnoval') > 0) {
					isValid = (elem.value.length == 11);
				} else if (elem.className.indexOf('dropdownval') > 0) {
					isValid = (elem.value != 0);
				} else if (elem.className.indexOf('numberval') > 0) {
					isValid = IsNumeric(elem.value);
				} else {
					isValid = (elem.value != '');
				}
				
				if (!isValid) {
					alert(elem.title + ' er ekki rétt út fyllt!');
					elem.focus();
					elem.style.borderColor = '#FF4A4A';
					elem.style.backgroundColor = '#FDFAD0';
					return false;
				} else {
					elem.style.borderColor = '';
					elem.style.backgroundColor = '';
				}
			}			
		}
		else if (elem.className.indexOf('reeqdif') > 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 && elem.value != '') {
					ifCount = ifCount + 1;
					isValid = isValidEmail(elem.value);
				} 
				else if ( elem.className.indexOf('fileUpload') > 0  && elem.value != '' )
				{
					isValid = !( /\.exe$/.test( elem.value ) );
				}
				else if (elem.className.indexOf('ssnoval') > 0  && elem.value != '') {
					ifCount = ifCount + 1;
					isValid = (elem.value.length == 11);
				} else if (elem.className.indexOf('fileUpload') > 0  && elem.value != '') {
					console.log( elem.value );
				} else if (elem.className.indexOf('dropdownval') > 0) {
					ifCount = ifCount + 1;
					isValid = (elem.value != 0);
				} else if (elem.className.indexOf('numberval') > 0  && elem.value != '') {
					ifCount = ifCount + 1;
					isValid = IsNumeric(elem.value);
				} else if (elem.value != ''){
					ifCount = ifCount + 1;
				} 
				
				if (!isValid) {
					alert(elem.title + ' er ekki rétt út fyllt!');
					elem.focus();
					elem.style.borderColor = '#FF4A4A';
					elem.style.backgroundColor = '#FDFAD0';
					return false;
				} else {
					elem.style.borderColor = '';
					elem.style.backgroundColor = '';
				}
			}
		}
	}

	if(ifCount > 0){
		for (var i = 0; i < form.elements.length; i++) {
			var elemif = form.elements[i];
			if (elemif.className.indexOf('reeqdif') > 0) {
				if (elemif.value == '') {
					alert(elemif.title + ' er ekki rétt út fyllt!');
					elemif.focus();
					elemif.style.borderColor = '#FF4A4A';
					elemif.style.backgroundColor = '#FDFAD0';
					return false;
				} else {
					elemif.style.borderColor = '';
					elemif.style.backgroundColor = '';
				}
			}
		}	
	}


	return true;
}

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;
}



