/**
 * Expand object
 * 
 * @param object
 */
function expand( object, e )
{
	if( jQuery( object ).is( ":visible" ) )
	{
		jQuery( object ).slideUp();
		jQuery( e ).empty().append( "Expandera <span class=\"meta-button\">+</span>" );
	}
	else
	{
		jQuery( object ).slideDown();
		jQuery( e ).empty().append( "Minimera <span class=\"meta-button\">-</span>" );
	}
}

function ajaxLoadBox( element, key1, key2, key3 )
{
	var height = jQuery( element ).innerHeight();

	jQuery( element ).attr( { style: "height: " + height + "px" } ).empty().append(
		jQuery( document.createElement("div") ).attr( { style: "text-align: center; padding-top: " + ( height / 2 - 10 ) + "px;" } ).append(
			jQuery( document.createElement("img") ).attr( { src: "/gfx/theme_fz/common/ajax-bar.gif" } )
		)
	);

	jQuery.ajax( {
		url: "/java/ajax/ajax_load_pagecache_item.php",
		data: ({ key1: key1, key2: key2, key3: key3 }),
		success: function( data ){
			jQuery( element ).attr( { style: "height: auto" } ).empty().append( data );
			InitializeSettingsBoxes();
		},
		onFailure: function(){
			jQuery( element ).empty().append( "Något gick fel. Kontakta support." );
		}
	} );
}

/**
 * Toggle menu-buttons in head-menu
 * 
 * @param menu_id
 */
function fz_toggleSubMenu( menu_id )
{
	clearSubMenu();

	if( !jQuery( "#" + menu_id ).hasClass( "active" ) )
	{
		jQuery( "#" + menu_id + " .submenu" ).each(
			function()
			{
				jQuery( this ).css( { display: "block" } );
				jQuery( "#" + menu_id ).addClass( "active" );
			}
		);
	}
	else
		jQuery( "#" + menu_id ).removeClass( "active" );
}
function clearSubMenu()
{
	// Hide all submenus
	jQuery( "#fz_framework_menu .submenu" ).each( function(){
		jQuery( this ).hide();
	});
}

/*
Example:

<a href="#" class="tab-controller" rel="tab-0">Show 1</a>
<a href="#" class="tab-controller" rel="tab-1">Show 2</a>

<div class="tabs-container">
	<div id="tab-0" class="tab"> Content </div>
	<div id="tab-1" class="tab" style="display: none;"> Content </div>
</div>

<script>
	//Add to each tab-controller-button
	var switcher = new TabSwitcher();
	switcher.Initialize( elementToDisplay );
<script>
*/

/**
 * Nice fade-out/fade-in transfer between tabs.
 */
function TabSwitcher()
{
	var activeElement;
	var parentElement;
	var fadeToElement;

	/**
	 * Set what element should be faded to.
	 * @param element
	 */
	this.FadeToElement = function( element )
	{
		fadeToElement = element;
	}

	/**
	 * Set the parent element of the fading elements. The container.
	 * @param element
	 */
	this.ParentElement = function( element )
	{
		if( !element )
			parentElement = jQuery( fadeToElement ).parent();
		else
			parentElement = jQuery( element );
	}

	this.SetParentHeight = function()
	{
		parentElement.children().each(
			function()
			{
				if( jQuery( this ).is( ":visible" ) )
					activeElement = jQuery( this );
			}
		);
	}

	this.Initialize = function( element )
	{
		// Set element to be faded to.
		if( !fadeToElement )
			this.FadeToElement( element );

		// Initialize parent element
		this.ParentElement();

		// Set the height of the parent to the height of the visible element
		this.SetParentHeight();

		// Fade out active element
		activeElement.fadeOut( "fast",
			function()
			{
				// Fade in new element
				jQuery( fadeToElement ).fadeIn( "fast" );
			}
		);
	}
}

/**
 * Creates grids of a list. Actually just adds class "grid" to each child-element to id="grids" and removes classname "news-zebra"
 * @param list (boolean)
 */
function gridify( list )
{
	var i = 0;

	jQuery( "ul#grids > li" ).each(
		function()
		{
			if( !list )
			{
				jQuery( this ).removeClass( "news-zebra" );
				jQuery( this ).addClass( "grid" );
			}
			else
			{
				jQuery( this ).removeClass( "grid" );

				if( i%2 )
					jQuery( this ).addClass( "news-zebra" );
			}

			i++;
		}
	);

	jQuery.ajax( {
		url: "/java/ajax/ajax_manage_grid.php",
		data: ( { show: ( list ? 0 : 1 ) } )
	} );
}

/**
 * Set button to active on click in a ul-list
 * @param e (Element)
 */
function setActiveButton( e )
{
	jQuery( e ).parent().siblings().each(
		function()
		{
			jQuery( this ).removeClass( "active" );
		}
	);

	jQuery( e ).parent().addClass( "active" );
}

/*
function loginBox()
{
	var myGlassBox = new GlassBox();
	myGlassBox.init( 'login-box', '500px', '300px', 'Logga in' );
	myGlassBox.lbo( true, 0.25 );
	myGlassBox.appear();
}
*/

function thumb( element, type, target_id, show_count )
{
	jQuery.ajax( {
		url: "/java/ajax/ajax_thumb.php",
		data:
		{
			type: type,
			target_id: target_id,
			show_count: show_count
		}
	} );
}

function expandbox( elem )
{
	jQuery( elem ).css( { maxHeight: "100%" } );

	if( jQuery( elem ).attr( "id" ) == "moreinfo_fanin" )
	{
		jQuery( elem ).attr(
			{
				id: "moreinfo_fanout",
				title: "Klicka för att expandera"
			}
		)
		.css(
			{
				color: "#ccc"
			}
		)
		.animate( { height: "100px" }, 2000 );
	}
	else
	{
		jQuery( elem ).attr(
			{
				id: "moreinfo_fanin",
				title: "Klicka för att återställa"
			}
		)
		.css(
			{
				color: "#000"
			}
		)
		.animate( { height: "100%" }, 2000 );
	}
}

function set_gcol_grade( game_id, value )
{
	var curr = document.getElementById( "rating_" + game_id + "_form" ).value;

	if( curr == value )
	{
		document.getElementById( "rating_" + game_id + "_zero_show" ).style.width = "0%";
		document.getElementById( "rating_" + game_id + "_show" ).style.width = "0%";
		document.getElementById( "rating_" + game_id + "_form" ).value = -1;
	}
	else
	{
		if( value == 0 )
		{
			document.getElementById( "rating_" + game_id + "_zero_show" ).style.width = "100%";
			document.getElementById( "rating_" + game_id + "_show" ).style.width = "0%";
		}
		else
		{
			document.getElementById( "rating_" + game_id + "_zero_show" ).style.width = "0%";
			document.getElementById( "rating_" + game_id + "_show" ).style.width = value * 20 + "%";
		}
		document.getElementById( "rating_" + game_id + "_form" ).value = value;
	}
}

function redirectUrl( url ) {
	window.location = url;
}

function loginBox( requestUri )
{
	var content = 	'<form action="/login.fz" method="post">' +
						'<input type="hidden" name="q" value="' + requestUri + '">' +
						'<div class="pgrid two"><label for="uname">Användarnamn</label></div>' +
						'<div class="pgrid six last"><input type="text" class="std-input" id="uname" name="login_username" tabindex="1" /> </div>' +
						'<div class="pgrid two"><label for="pword">Lösenord</label></div>' +
						'<div class="pgrid six last"><input type="password" class="std-input" id="pword" name="login_password" tabindex="2" /> </div>' +
						'<div class="pgrid two"></div>' +
						'<div class="pgrid six last"><a href="/hem/control_restore_password.fz">Glömt lösenordet? Klicka här.</a> </div>' +
						'<input type="submit" name="login_auto" value="Logga in permanent" class="button selected" tabindex="3" />' +
						'<input type="submit" name="submit" value="Logga in" class="button selected" tabindex="4" />' +
						'<span class="button" onclick="Modalbox.Remove();">Avbryt</span>' +
					'</form>';

	Modalbox.Initialize();
	Modalbox.SetTitle( "Logga in" );
	Modalbox.SetIcon( "/core/gfx/modalbox/avatar-login.png" );
	Modalbox.SetContent( content );
	Modalbox.Render();
}

/**
 * Search field auto complete
 * Check if we have jQueryUI or prototype
 */
jQuery(document).ready(
	function()
	{
		jQuery(".sphinx").each(
			function(index, value)
			{
				jQuery(value).autocomplete(
				{
					serviceUrl: "/java/ajax/ajax_search_games.php",
					minChars: 2,
					width: 180,
					highlightFirstChild: true,
					onSelect:
						function(value, data)
						{
							if( data != "" )
							{
								window.location = data;
								return false;
							}
						}
				});
			}
		);

		jQuery( ".tab-controller" ).each(
			function()
			{
				jQuery( this ).bind( "click",
					function()
					{
						var switcher = new TabSwitcher();
						switcher.Initialize( "#" + jQuery( this ).attr( "rel" ) );

						setActiveButton( this );

						return false;
					}
				);
			}
		);

		jQuery( "input.click, textarea.click" ).each(
			function()
			{
				jQuery( this ).bind( "click",
					function()
					{
						if( jQuery( this ).val() == jQuery( this )[0].defaultValue )
							jQuery( this ).val( "" );
					}
				);

				jQuery( this ).bind( "blur",
					function()
					{
						if( jQuery( this ).val() == "" )
							jQuery( this ).val( jQuery( this )[0].defaultValue );
					}
				);
			}
		);
	}
);

