/*
 * Manage height of right column automatically
 */
function updateRightColumnHeight()
{
	var maindivh = jQuery( "#maincolumnheight" ).height();
	var sidecolumn = jQuery( "#sidecolumnheight" ); 
	if( sidecolumn.length > 0 )
		sidecolumn[0].style.minHeight = maindivh + "px";
}
jQuery( window ).load( updateRightColumnHeight );
jQuery( window ).scroll( updateRightColumnHeight );

/*
 * Easy up, easy down.
 */
function slideThisDown( sel )
{
	jQuery( sel ).slideDown();
}
function slideThisUp( sel )
{
	jQuery( sel ).slideUp();
}

/*
 * Placeholder helper, activate
 */
/*jQuery( document ).ready( function(){
	jQuery( "input[type=text][placeholder]" ).placeholder({
		blankSubmit: true
	});
});*/

/*
 * Slider button. To add interactivity, set jQuery().toggle() to the single element.
 */

jQuery( document ).ready( function(){
	jQuery(".onoff-controller").click(function() {
		if( jQuery('#hidden-fieldset').is(":visible") ) {
			jQuery('#hidden-fieldset').fadeOut(200);

			if( jQuery('#child-slug-hidden').length > 0 ) {
				jQuery('#child-slug-hidden').fadeOut(600);
			}
		} else {
			jQuery('#hidden-fieldset').fadeIn(600);

			if( jQuery('#child-slug-hidden').length > 0 ) {
				if( jQuery('#childTag option').size() > 1 ) {
					jQuery('#child-slug-hidden').fadeIn(600);
				}
			}
		}
		toggleSlider( this );
	});
});

function toggleSlider( element )
{
		// Get this slider (a child)
		var tmpSlider = jQuery( element ).children( ".slider" );

		// Calculate offset
		var offset;
		if( tmpSlider.position().left > 0 )
			offset = 0;
		else
			offset = ( jQuery( element ).width() - tmpSlider.width() ) + "px";

		// Animate
		tmpSlider.animate({ left: offset }, 300);
}

jQuery( document ).ready( function() {

	jQuery( "a[class=toggle-result button-disguised]" ).each( function( index, element ){
		var id = element.id;
		jQuery( element ).click( function() {

			// Find out if poll results are showing or not
			var display = jQuery( "div[class=poll-results][id=" + id + "]" ).css( "display" );

			// Slide the poll results up or down depending if its showing or not
			if( display == "none" )
			{
				slideThisDown( "div[class=poll-results][id=" + id + "]" );
				jQuery( element ).html( "Dölj resultat" );
			}
			else
			{
				slideThisUp( "div[class=poll-results][id=" + id + "]" );
				jQuery( element ).html( "Visa resultat" );
			}
		});
	});
});

/**
 * Perform AJAX login.
 *
 * @param string username
 * @param string password
 * @param bool autologin
 */

jQuery( document ).ready( function() {
	jQuery( "#login-form" ).submit( function( event ) {
			event.preventDefault();
			loginWithAjax( jQuery( "#username" ).val(), jQuery( "#password" ).val(), jQuery( "#remember" ).val() );
	});
});

function loginWithAjax( username, password, autologin )
{
	var usr = username;
	var pass = password;
	var auto = autologin;

	jQuery( "#sidebar-progress" ).css( "position", "relative" );
	jQuery( "#sidebar-login" ).css( "position", "absolute" );
	jQuery( "#sidebar-progress" ).css( "display", "block" );
	jQuery( "span[id=login_text]" ).html( "" );
	jQuery( "a[id=ajax_error_button]" ).css( "display", "inline-block" );
	jQuery( "#ajax_error_div" ).css( "display", "none" );
	jQuery( "img[id=ajax_bar_load]" ).css( "display", "block" );
	// Try to authenticate the user
	jQuery.ajax({
	  	type: "POST",
	  	url: "/core/login.php",
	  	data: { login_username: usr, login_password: pass, login_auto: auto, jsonResponse: 1 },

		// If authentication was succesful
		success: function( msg )
		{
			// Display message if user logged in correctly
			setTimeout( function(){
				jQuery( "span[id=login_text]" ).html( msg );
			}, 1000 );
			setTimeout( function(){
				jQuery( "#sidebar-progress" ).fadeOut( 300 );
				jQuery( "#sidebar-login" ).fadeOut( 300 );
				jQuery( "#logged-out-box" ).fadeOut( 300 );

				// Verify login details
				jQuery.ajax({
					url: "/master/includes/boxes/box_login.php",
					data: { logged_in_by_ajax: 1, login_username: usr, login_password: pass },
					success: function( data ){
						jQuery( "#ajax-login-container" ).html( data );
						jQuery( "#sidebar-login" ).fadeOut( 50 );
					}
				});}, 1000 );
		},
		// If authentication failed, the error-message is displayed
		error: function( msg )
		{
			setTimeout( function(){
				jQuery( "#sidebar-progress" ).css( "position", "relative" );
				jQuery( "span[id=login_text]" ).html( jQuery.parseJSON( msg.responseText ) );
				jQuery( "#sidebar-progress p" ).css( "margin-top", "35px" );
				jQuery( "img[id=ajax_bar_load]" ).css( "display", "none" );
				jQuery( "#ajax_error_div" ).css( "display", "block" );
			}, 1000 )
		}
	});
}

jQuery( document ).ready( function(){
	jQuery( "a[id=ajax_error_button]" ).click( function(){
		jQuery( "#sidebar-login" ).css( "position", "relative" );
		jQuery( "#sidebar-progress" ).fadeOut( 100 );
	})
} );

/*
Example:

<a href="#" class="tab-controller" rel="unique-ID-0">Show 1</a>
<a href="#" class="tab-controller" rel="unique-ID-1">Show 2</a>

<div class="tabs-container">
	<div id="unique-ID-0" class="tab"> Content </div>
	<div id="unique-ID-1" class="tab" style="display: none;"> Content </div>
</div>

<script type="text/javascript">
	jQuery( document ).ready( function()
	{
		jQuery( ".tab-controller" ).each( function()
		{
			jQuery( this ).bind( "click", function()
			{
				var switcher = new TabSwitcher();
				switcher.Initialize( jQuery( this ).attr( "rel" ) );
				return false;
			} );
		} );
	} );
<script>
*/

var TabsContainer;
var DisplayTab;

/**
 * Nice fade-out/fade-in transfer between tabs.
 */
function TabSwitcher()
{
	this.Initialize = function( display )
	{
		DisplayTab = jQuery( "#" + display );
		TabsContainer = DisplayTab.parent( ".tabs-container" );

		var activeTab = this.GetActiveTab();

		this.SetHeight( activeTab.height() );

		this.Fade( activeTab, true );
		this.Fade( DisplayTab, false );
	};

	this.GetActiveTab = function()
	{
		var visibleTabElement;
		jQuery( TabsContainer ).children( ".tab" ).each(
			function()
			{
				if( jQuery( this ).is( ":visible" ) )
					visibleTabElement = jQuery( this );
			}
		);

		return visibleTabElement;
	};

	this.SetHeight = function( height )
	{
		jQuery( TabsContainer ).css( { height: height + "px" } );
	};

	this.Fade = function( e, out )
	{
		if( out )
		{
			jQuery( e ).fadeOut( 200, function(){
				jQuery( TabsContainer ).css( { height: jQuery( DisplayTab ).height() + "px" } );
				jQuery( e ).hide();
			} );
		}
		else
			jQuery( e ).delay( 250 ).fadeIn( 200 );
	}
}

function setActiveButton( e )
{
	jQuery( e ).parent( "li" ).siblings().each(
		function(){
			jQuery( this ).removeClass( "active" );
		}
	);

	jQuery( e ).parent( "li" ).addClass( "active" );
}

function ToggleSphinxAdvancedSearch()
{
	jQuery( "#expandable-options" ).slideToggle( 200 );
}

jQuery( document ).ready( function()
{
	//Activate all tabs
	jQuery( ".tab-controller" ).each( function()
	{
		jQuery( this ).bind( "click", function()
		{
			var switcher = new TabSwitcher();
			switcher.Initialize( jQuery( this ).attr( "rel" ) );

			setActiveButton( jQuery( this ) );

			return false;
		} );
	} );
} );

/*jQuery( "#link-clicks" ).click( function() {
	
});*/
