$(function(){
	//Menyn
	$('#head li').bind('mouseover', function(){
		$(this).siblings('li').children('ul').hide();
		$(this).children('ul').show();
	});

	$('#head li ul').bind('mouseout', function(){
		$(this).hide();
	});

	//Galleri
	var cat_id = 1;
	var book_count = 1;

	display_category(cat_id);

	function display_category(id, count)
	{
		if(count == undefined)
		{
			count = 1;
		}

		var id_temp = $('#' + id);
		var bk_temp = $('#' + id + '-' + count);

		if(id_temp.length == 0 || bk_temp.length == 0)
		{
			$('#debug').append('1: ' + id + '-' + count + '<br/>');
		}

		else
		{
			/*$('#debug').append('2: ' + id + '-' + count + '<br/>');*/
			
			cat_id = parseInt(id);
			book_count = parseInt(count);

			//Behöver nollställas för att inte id ska krocka
			$('#gallery-img .controls').html('');

			$('#' + id + '-' + count).addClass('selected').siblings('li').removeClass('selected');

			var book_list = '<ul>';

				if(count > 1)
				{
					book_list += "<li><a href='#' class='book_prev'><img src='/images/arrow_left.gif' alt=''\/><\/a><\/li>";
				}
				
				book_list += id_temp.children('a').siblings('ul').html();

				if(count < id_temp.children('a').siblings('ul').children('li').length)
				{
					book_list += "<li><a href='#' class='book_next'><img src='/images/arrow_right.gif' alt=''\/><\/a><\/li>";
				}
			
			book_list += '<\/ul>';

			id_temp.children('.selected').show().parent('span').siblings('span').children('.selected').hide();

			if(count == 1)
			{
				$('#gallery-info').show().children('.info').html(bk_temp.children('div').html());
			}
			
			$('#gallery-img .controls').html(book_list);

			$('#gallery-img').show().children('.info').html("<img src='" + bk_temp.children('a').attr('href') + "' alt=''/>");
		}
	}

	$('#gallery-list .scrollWrapper a').live('click', function(){
		display_category($(this).parent('span').attr('id'));

		return false;
	});

	$('#cat_prev').live('click', function(){
		display_category(cat_id - 1);

		return false;
	});

	$('#cat_next').live('click', function(){
		display_category(cat_id + 1);

		return false;
	});

	$('.book_prev').live('click', function(){
		display_category(cat_id, book_count - 1);

		return false;
	});

	$('.book_next').live('click', function(){
		display_category(cat_id, book_count + 1);

		return false;
	});

	$('#gallery-img .controls a').live('click', function(){
		display_category(cat_id, $(this).parent('li').attr('class'));

		return false;
	});

	$(document).keyup(function(e){
		switch(e.keyCode){
			case 37:
				display_category(cat_id, book_count - 1);
				break;
			case 38:
				display_category(cat_id + 1);
				break;
			case 39:
				display_category(cat_id, book_count + 1);
				break;
			case 40:
				display_category(cat_id - 1);
				break;
		};
	});
});
