/*************************************************************************************
 * This script provides the expanding buttons' effect.                               *
 *                                                                                   *
 * Author: Viktor Varadi                                                             *
 *                                                                                   *
 * All rights reserved!                                                              *
 *                                                                                   *
 *************************************************************************************/


var duration = 1000;
var is_open = {'search_btn': 0, 'join_btn': 0, 'login_btn': 0};
var id_stack = new Array();
var pending_open;
var enabled = {'search_btn': true, 'join_btn': true, 'login_btn': true};
var all_locked = false;

jQuery(document).ready(function(){
	if(jQuery(document).getUrlParam('w') != null && jQuery(jQuery(document).getUrlParam('w')+'_btn').css('display') != 'none')
	{
		SwitchWnd(jQuery(document).getUrlParam('w')+'_btn');
	}
});

function OpenWnd(button)
{
	jQuery('.home_txt').fadeOut(duration);
	is_open[button] = 1;
	switch (button)
	{
		case 'search_btn':
			jQuery('#search_btn').animateToClass('pull_out', duration);
			jQuery('.search_raiser').animateToClass('pull_up', duration);
			id_stack.push(setTimeout("id_stack.shift(); jQuery('#search_btn .btn_body > div').animateToClass('expand', duration,'',function(){PullContentIn('search_btn')});", duration/5));
		break;
		case 'join_btn':
			jQuery('#join_btn').animateToClass('pull_out', duration);
			id_stack.push(setTimeout("id_stack.shift(); jQuery('.join_raiser').animateToClass('pull_up', duration); jQuery('#join_btn .btn_body > div').animateToClass('expand', duration,'',function(){PullContentIn('join_btn')});", duration/6));
		break;
		case 'login_btn':
			jQuery('#login_btn').animateToClass('pull_out', duration);
			id_stack.push(setTimeout("id_stack.shift(); jQuery('.login_raiser').animateToClass('pull_up', duration); jQuery('#login_btn .btn_body > div').animateToClass('expand', duration,'',function(){PullContentIn('login_btn')});", duration/5));
		break;
	}
}

function StopTimeouts()
{
	while (id_stack.length > 0)
	{clearTimeout(id_stack.shift());}
}

function CloseWnd(button, wnd_open)
{
	
	function PostClose()
	{
		is_open[button] = 0;
		enabled[button] = true;
		all_locked = false;
		if(pending_open == wnd_open) OpenWnd(wnd_open);		//if clause needed as the user can click during 'duration'
	}
	
	switch (button)
	{
		case 'search_btn':
			PushContentOut(button, function(){
			jQuery('.home_txt').fadeIn(duration);
			jQuery('#search_btn .btn_body > div').animateToClass('srink_btn', duration,0,PostClose);
			setTimeout("jQuery('#search_btn').animateToClass('btn_sized', duration); jQuery('.search_raiser').animateToClass('search_raiser', duration);", duration/5);});
		break;
		case 'join_btn':
			PushContentOut(button, function(){
			jQuery('.home_txt').fadeIn(duration);
			jQuery('.join_raiser').animateToClass('join_raiser', duration,0,PostClose);
			jQuery('#join_btn .btn_body > div').animateToClass('srink_btn', duration);
			setTimeout("jQuery('#join_btn').animateToClass('btn_sized', duration);", duration/6);});
		break;
		case 'login_btn':
			PushContentOut(button, function(){
			jQuery('.home_txt').fadeIn(duration);
			jQuery('.login_raiser').animateToClass('login_raiser', duration,0,PostClose);
			jQuery('#login_btn .btn_body > div').animateToClass('srink_btn', duration);
			setTimeout("jQuery('#login_btn').animateToClass('btn_sized', duration);", duration/5);});
		break;
	}
}

function PullContentIn(button)
{
	var cont = '#'+button+' .btn_content:first';
	jQuery(cont).show().animate({left: "0px"},'',0,function(){all_locked = false;});
}

function PushContentOut(button, callback)
{
	var cont = '#'+button+' .btn_content:first';
	jQuery(cont).animate({left: "-450px"},'','',function(){jQuery(cont).hide(); callback.call();});
	
	if (button == 'join_btn')
	{
		cont = '#join_p2';
		jQuery(cont).animate({left: "450px"},'','',function(){jQuery(cont).hide(); callback.call();});
		page = 0;
	}
}

function SwitchWnd(button)
{
	if (!all_locked)
	{
		if (enabled[button])
		{
			all_locked = true;
			StopTimeouts();
			pending_open = button;
			switch (button)
			{
				case 'search_btn':
					if (is_open['join_btn']) {CloseWnd('join_btn', button);}
					else if (is_open['login_btn']) {CloseWnd('login_btn', button);}
					else OpenWnd(button);
				break;
				case 'join_btn':
					if (is_open['search_btn']) {CloseWnd('search_btn', button);}
					else if (is_open['login_btn']) {CloseWnd('login_btn', button);}
					else OpenWnd(button);
				break;
				case 'login_btn':
					if (is_open['search_btn']) {CloseWnd('search_btn', button);}
					else if (is_open['join_btn']) {CloseWnd('join_btn', button);}
					else OpenWnd(button);
				break;
			}
		}
		if (button != 'search_btn' && !is_open['search_btn']) enabled['search_btn'] = true;
		if (button != 'join_btn' && !is_open['join_btn']) enabled['join_btn'] = true;
		if (button != 'login_btn' && !is_open['login_btn']) enabled['login_btn'] = true;
		enabled[button] = false;
	}
}


