var stateObject = new Object();

function writeCookie()
{
	var str = "";
	var key;
	var date = new Date();
	
	date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000));
	
	for (key in stateObject) 
	{ 
		if (stateObject[key] == "c")
		{
			str += key + ':' + stateObject[key] + ';'; 
		}
	}
	$.cookie('hall9000_state', str, { path: '/', expires: date });
}

function restoreState()
{
	var cookieValue = $.cookie('hall9000_state');
	
	if (cookieValue)
	{
		var stateEntryArray = cookieValue.split(';');

		for (i = 0; i < stateEntryArray.length; i++)
		{
			var state = stateEntryArray[i].split(':');
			
			stateObject[state[0]] = state[1];
			
			if (state[1] == 'c')
			{		
				$(state[0]).hide();
				
				if (state[0].indexOf(" ") > 0)
				{
					var pathPartArray = state[0].split(' ');
					
					if (pathPartArray[1] == ".detail")
					{
						hallBox = $(pathPartArray[0]).parents('.hall_box');
						hallBox.find('.detail_aus').hide();
						hallBox.find('.detail_ein').show();
					}
				}
				else 
				{		
					hallBox = $(state[0]).parents('.hall_box');
					hallBox.find('.detail_toggle').hide();
					hallBox.find('.box_aus').hide();
					hallBox.find('.box_ein').show();
				}
			}
		}
	}
}

$(document).ready(function() {

	
	restoreState();
	
	$('div.hall_box .main_toggle').click(function() 
	{	
		var hallBox = $(this).parents('.hall_box');
		var mainContent = hallBox.find('.main_content');
		
		var id = '#' + mainContent.attr("id");
		
		if (mainContent.is(':visible'))
		{
			hallBox.find('.box_aus').hide();
			hallBox.find('.box_ein').show();
			hallBox.find('.detail_toggle').hide();
			mainContent.hide();
			stateObject[id] = 'c';
		}
		else
		{
			hallBox.find('.box_aus').show();
			hallBox.find('.box_ein').hide();
			hallBox.find('.detail_toggle').show();
			mainContent.show();
			stateObject[id] = 'e';
		}
						
		writeCookie();
		
	});
	
	
	$('div.hall_box .detail_toggle').click(function() 
	{
		var hallBox = $(this).parents('.hall_box');
		var mainContent = hallBox.find(".main_content");	
		
		var id = '#' + mainContent.attr("id") + ' .detail';

		var detailElement = mainContent.find('.detail');
		
		if (detailElement.is(':visible'))
		{
			hallBox.find('.detail_aus').hide();
			hallBox.find('.detail_ein').show();
			detailElement.hide();
			stateObject[id] = 'c';
		}
		else
		{
			hallBox.find('.detail_aus').show();
			hallBox.find('.detail_ein').hide();
			detailElement.show();
			stateObject[id] = 'e';
		}
		
		writeCookie();
		
	});
	
	$('#navigation li.mainmenu').mouseover(function()
	{
		$(this).oneTime(300, function()
		{
			currentSelected = $('#navigation li.mainmenu.selected');
			elementId = currentSelected.attr('id') + '_sub';
			$('#' + elementId).hide();
			currentSelected.removeClass('selected');
			
			$(this).addClass('selected');
			elementId = $(this).attr('id') + '_sub';
			$('#' + elementId).show();
		});
	});
	$('#navigation li.mainmenu').mouseout(function()
	{
		$(this).stopTime();
	});
	
	$("#games_auto_complete").autocomplete("/html/search.php", {
		delay: 200,
		width: 300,
		selectFirst: false,
		minChars: 2,
		matchSubset: false,
		max: 100,
		scrollHeight: 300,
		formatItem: function(row, i, max) {
			return i + ". " + row[1];
		},
		formatResult: function(row, i, max) {
			return row[1];
		}
	}).result(function(event, row) {
		location.href = "/html/spiel/" + row[0];
	});	
	
	$("#games_auto_complete").hint("blurred_hint");
		
	
	$("#author_auto_complete").autocomplete("/html/search_author.php", {
		delay: 200,
		width: 300,
		selectFirst: false,
		minChars: 2,
		matchSubset: false,
		max: 100,
		scrollHeight: 300,
		formatItem: function(row, i, max) {
			return i + ". " + row[1];
		},
		formatResult: function(row, i, max) {
			return row[1];
		}
	}).result(function(event, row) {
		$("#author_auto_complete_id").val(row[0]);
	});
	
	
	$("#publisher_auto_complete").autocomplete("/html/search_publisher.php", {
		delay: 200,
		width: 300,
		selectFirst: false,
		minChars: 2,
		matchSubset: false,
		max: 100,
		scrollHeight: 300,
		formatItem: function(row, i, max) {
			return i + ". " + row[1];
		},
		formatResult: function(row, i, max) {
			return row[1];
		}
	}).result(function(event, row) {
		$("#publisher_auto_complete_id").val(row[0]);
	});
	
		
	$("#rezensent_auto_complete").autocomplete("/html/search_rezensent.php", {
		delay: 200,
		width: 300,
		selectFirst: false,
		minChars: 2,
		matchSubset: false,
		max: 100,
		scrollHeight: 300,
		formatItem: function(row, i, max) {
			return i + ". " + row[0];
		},
		formatResult: function(row, i, max) {
			return row[0];
		}
	}).result(function(event, row) {
		$("#rezensent_auto_complete_id").val(row[0]);
	});
	
	
	$(".icon_aufmachung").tooltip({ 
	    delay: 0, 
		fixPNG: true,
		track: true,
		top: 20,
		left: -20,
	    showURL: false, 
	    bodyHandler: function() { 
	        return $("#tt_aufmachung").html();
	    } 
	});
	
	$(".icon_spielbarkeit").tooltip({ 
	    delay: 0, 
		fixPNG: true,
		track: true,
		top: 20,
		left: -20,
	    showURL: false, 
	    bodyHandler: function() { 
	        return $("#tt_spielbarkeit").html();
	    } 
	});
	
	$(".icon_interaktion").tooltip({ 
	    delay: 0, 
		fixPNG: true,
		track: true,
		top: 20,
		left: -20, 
	    showURL: false, 
	    bodyHandler: function() { 
	        return $("#tt_interaktion").html();
	    } 
	});
	
	$(".icon_einfluss").tooltip({ 
	    delay: 0, 
		fixPNG: true,
		track: true,
		top: 20,
		left: -20, 
	    showURL: false, 
	    bodyHandler: function() { 
	        return $("#tt_einfluss").html();
	    } 
	});
	
	$(".icon_spielreiz").tooltip({ 
	    delay: 0, 
		fixPNG: true,
		track: true,
		top: 20,
		left: -20,
	    showURL: false, 
	    bodyHandler: function() { 
	        return $("#tt_spielreiz").html();
	    } 
	});
	
});
