$(document).ready(function()
{
	// show loginform.
	$('#kund').click(function()
	{
		$('#login').fadeIn('slow');
		$('#username').focus();
		return false;
	});

	// slideshow
	$('#slideshow img').show();
	$('#slideshow').cycle({
		fx: 'fade',
		timeout: 7500,
		speed: 2500
	});

	// recruitment containers
	var positions = {};
	var nav = 0;

	// init recruitment banner
	$.ajax({
		url: "/services.json",
		dataType: "json",
		success: function (data,status)
		{
			positions = data;
			$("#recruitment").empty();
			$("#recruitment").append('<a href="' + positions[nav]['url'] + '" class="showpopup">' + positions[nav]['position'] + ' (' + positions[nav]['city'] + ')</a>');

			if (nav == 0)
				$("#recruitment").append('<div id="nav">&lt; <a href="" id="next">&gt;</a></div>');
			else if (nav == (positions.length - 1))
				$("#recruitment").append('<div id="nav"><a href="" id="prev">&lt;</a> &gt;</div>');
			else
				$("#recruitment").append('<div id="nav"><a href="" id="prev">&lt;</a> <a href="" id="next">&gt;</a></div>');
		},
		error: function()
		{
			$("#recruitment").empty();
			$("#recruitment").append('<em>(Inga Tjänster Tillgängliga)</em>');
		}
	});

	// show previous item
	$('#prev').livequery('click',function()
	{
		if (nav <= 0)
			return false;
		else
			nav--;

		$("#recruitment").empty();
		$("#recruitment").append('<a href="' + positions[nav]['url'] + '" class="showpopup">' + positions[nav]['position'] + ' (' + positions[nav]['city'] + ')</a>');

		if (nav == 0)
			$("#recruitment").append('<div id="nav">&lt; <a href="" id="next">&gt;</a></div>');
		else
			$("#recruitment").append('<div id="nav"><a href="" id="prev">&lt;</a> <a href="" id="next">&gt;</a></div>');

		return false;
	});

	// show next item
	$('#next').livequery('click',function()
	{
		if (nav >= (positions.length - 1))
			return false;
		else
			nav++;

		$("#recruitment").empty();
		$("#recruitment").append('<a href="' + positions[nav]['url'] + '" class="showpopup">' + positions[nav]['position'] + ' (' + positions[nav]['city'] + ')</a>');

		if (nav == (positions.length - 1))
			$("#recruitment").append('<div id="nav"><a href="" id="prev">&lt;</a> &gt;</div>');
		else
			$("#recruitment").append('<div id="nav"><a href="" id="prev">&lt;</a> <a href="" id="next">&gt;</a></div>');

		return false;
	});

	// show the popup.
	$('.showpopup').livequery('click',function()
	{
		$('#overlay').height($(document).height());
		$('#overlay').fadeTo('fast',0.40,function(){
			$('#popup').fadeIn('fast');
			$('#close').fadeIn('fast');
		});

		$.ajax({
			url: this.href,
			dataType: "html",
			type: "GET",
			success: function (data,status)
			{
				$('#popup').html(data);
			},
			error: function()
			{
				$('#popup').html('<strong>Fel:</strong> Kunde lite ladda innehållet, var god försök igen senare.');
			}
		});

		return false;
	});

	// hide the popup.
	$('#close').livequery('click',function()
	{
		// fade out nicely.
		$('#close').fadeOut('fast');
		$('#popup').fadeOut('fast',function(){
			$('#overlay').fadeOut('fast');
		});
		$('#popup').empty();

		// return true to make anchors act properly.
		return true;
	});

	// make escape hide the popup.
	$(document).keyup(function(event)
	{
		if (event.keyCode == '27') {
			// fade out nicely.
			$('#close').fadeOut('fast');
			$('#popup').fadeOut('fast',function(){
				$('#overlay').fadeOut('fast');
			});
			$('#popup').empty();
	
			// return true to make anchors act properly.
			return true;
		}
	});
});

