$(function(){

	$('.searchpanel input.button').val('');

	$('div#wrapper > div.content > div.rightcol > div.subcol').height( $('div#wrapper > div.content').height() - 161 );

	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;
		}
	}
}
