var str = {
	required: {
		fi: "Et syöttänyt kaikkia vaadittuja arvoja",
		en: "You did not fill all the required fields"
	},
	spam: {
		fi: "Viestisi tulkittiin spämmiksi, eikä sitä lähetetty",
		en: "Your message was considered as spam and was not delivered."
	},
	ok: {
		fi: "Kiitos viestistäsi!",
		en: "Thank you for your message!"
	}
};

var first = 0;

$(document).ready(function(){
	// hilighting valittu
	$(".valittu").css("background-position", "0 -" + $(".valittu").height() + "px");

	// image hovering
	$(".hilight:not(.valittu)").hover(
		function(){
			$(this).css("background-position", "0 -" + $(this).height() + "px");
		},
		function(){
			$(this).css("background-position", "0 0");
		}
		);

	$("#nav").lavaLamp({
		speed: 700
	});

	selectSlide(window.location.hash ? window.location.hash : "#slide1");
//	initializeTicker();

	$('#contact_form').submit(function(){
		var lng = $('#contact_form').attr('name');
		$.post("mailer.php", {
			data: $('#contact_form').serializeArray()
			}, function(data){
			if(data.error == 1)
				alert(str.required[lng]);
			if(data.error == 9)
				alert(str.spam[lng]);
			if(data.error == 0) // ok
			{
				$('<div />').addClass("notice").text(str.ok[lng]).appendTo("body").fadeIn('slow').delay(5300).fadeOut('slow', function(){
					$(this).remove()
					});
				$('#form_reset').click();
			}

		}, "json");
		return false;
	});

	$('#form_reset').click(function(){
		$("#contact_form")[0].reset();
		return false;
	});

	$('div.uutinen a').live('click', function(){
//		clearInterval(interval);
		var e = $(this).parent().parent().find(".notice");
		$("<div />").addClass("notice_close").html("<a href='#' class='close_link'><img src='kuvat/x.png' width='15' height='15' border='0' /></a>").appendTo(e);
		e.fadeIn('slow');
		return false;
	});

	$('a.close_link').live('click', function(){
		$(".notice").fadeOut('slow');
//		initializeTicker();
		return false;
	});

});

function selectSlide(hash)
{
	if(!hash)
		hash = "#slide1";
	var elm = $('.slide:visible');
	if(elm.length)
	{
		elm.hide();
		$(hash).show();
//		elm.fadeOut('fast', function(){
//			$(hash).fadeIn('slow');
//		});
	}
	else
	{
		$(hash).show();
//		$(hash).fadeIn('slow');
	}

}

function initializeTicker()
{
	var speed = 700;
	var pause = 4500;

	function removeFirst(){
		first = $('ul#ticker li:first').html();
		$('ul#ticker li:first')
		.animate({
			opacity: 0
		}, speed)
		.slideUp('slow', function() {
			$(this).remove();
		});
		addLast(first);
	}

	function addLast(first){
		last = '<li style="display:none">'+first+'</li>';
		$('ul#ticker').append(last)
		$('ul#ticker li:last')
		.animate({
			opacity: 1
		}, speed)
		.fadeIn('slow')
	}

	interval = setInterval(removeFirst, pause);
}

