window.onload = init;

function init()
{
	paging();
	newGallery();
	var search = document.getElementById('SearchString1');
		if (search != null) {
			search.onclick = search_onclick;
			search.onblur = search_onblur;
		}

}

$(function() {
	gallery();
	dropDown();
});

function search_onclick() {
	var search = document.getElementById('SearchString1');
	if (search.value == 'Type keyword') {
		search.value = '';
	}
}

function search_onblur() {
	var search = document.getElementById('SearchString1');
	if (search.value == '') {
		search.value = 'Type keyword';
	}
}




// 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/' ) );

		// <viktor>
		$('.paging > .nav > ul.pages > li > a:first').click();
		// </viktor>

		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;
		}
	}
}

function dropDown()
{
	$('.dropdown > .heading > a').click(function() {
		if ($('.dropdown > .items:hidden').length > 0)
			$(this).parent().parent().children('ul:hidden').slideDown('fast');
		else
			$(this).parent().parent().children('ul:hidden').slideUp('fast');
		return false;
	});
	$('.dropdown .items').css('display', 'none').hover(function() {
		$(this).slideDown();
	}, function() {
		$(this).slideUp('slow');
	});
}

function paging()
{
	$('.paging > .hole').css('overflow','hidden').parent().each(function()
	{
		var holeHeight = $(this).find('.hole').height();
		var contentHeight = $(this).find('.hole > .content').height();
		if ( contentHeight > holeHeight + 1)
		{
			$(this).children('.nav').prepend('<ul><li><a href="#" class="back"></a></li><br /><li><a href="#" class="next"></a></li></ul>');

			var paging = '';
			for ( i=0; i < contentHeight; i+=holeHeight )
			{
				paging += '<li><a href="#'+(i)+'" id="'+(i)+'">'+((i / holeHeight )+1)+'</a></li>';
			}
			$(this).find('.nav > ul.pages').append(paging);
		}
		
		$(this).find('.nav > ul.pages > li > a').click(function(){
			$(this).parent().parent().children('li').removeClass('selected');
			$(this).parent().parent().parent().parent().find('.hole > .content').stop().animate({top: '-'+$(this).attr('id')+'px'}, 750);
			$(this).parent().addClass('selected');
			return false;
		}).filter(':first').parent().addClass('selected');
	
		$(this).find('.nav > ul > li > .back').click(function(){
			$(this).parents('.nav').find('ul.pages > li.selected').prev().children('a').click();
			return false;
		});
		
		$(this).find('.nav > ul > li > .next').click(function(){
			$(this).parents('.nav').find('ul.pages > li.selected').next().children('a').click();
			return false;
		});
		
	});
}

function newGallery()
{
	$('.gallery').each(function() {
		$(this).find('ul.thumbnails a').click(function() {
			$(this).parents('ul').hide().parents('.thumbs').append('<img src="' + $(this).find('img').attr('src').replace('/proc/3/','/proc/4/') + '" />').animate({'height':'286px'}).find('img').not('.thumb').click(function() {
				location = $(this).attr('src').replace('/proc/4','');
			}).hover(function() {
				$(this).parents('.thumbs').find('.close').fadeIn();
			}, function() {
				$(this).parents('.thumbs').find('.close').hide();
			});
			return false;
		});
		$(this).find('.close').click(function() {
			$(this).hide().parents('.thumbs').find('img').not('.thumb').remove();
			$(this).parents('.thumbs').css('height','auto').find('ul').show();
			return false;
		}).hover(function() {
			$(this).show();
		}, function() {
			$(this).hide();
		});
	});
}

var formValidationPostfix = ' is incorrect!';
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 || elem.className.indexOf('email') > 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 + formValidationPostfix);
					elem.focus();
					elem.style.borderColor = '#FF4A4A';
					elem.style.backgroundColor = '#FDFAD0';
					return false;
				} else {
					elem.style.borderColor = '';
					elem.style.backgroundColor = '';
				}
			}			
		}
	}
	return true;
}

function isValidEmail(value) {
	var regex = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i;
	return regex.test(value);
}

function WriteFlash(movie, width, height)
{
	var flash = '<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">';
	flash += '<param name="wmode" value="transparent" />';
	flash += '<param name="bgcolor" value="#ffffff" />';
	flash += '<param name="Movie" value="' + movie + '">';
	flash += '<embed src="' + movie + '" quality="high" bgcolor="" height="' + height + '" width="' + width + '" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">';
	flash += '</embed>';
	flash += '</object>';
	document.write(flash);
}

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">')
		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>')
	}
}