function display_login_widget()
{
	$('#account_login_box').css({display: 'block'});
	
	// Display the register form
	$('.account_become_member').click(function()
	{
		hide_login_widget();
		
		display_register_widget();
	});
}

function hide_login_widget()
{
	$('#account_login_box').css({display: 'none'});
}

function display_register_widget()
{
	$('#account_register_box').css({display: 'block'});
}

function hide_register_widget()
{
	$('#account_register_box').css({display: 'none'});
}

function display_register_ok_widget()
{
	$('#account_register_ok_box').css({display: 'block'});
}

function hide_register_ok_widget()
{
	$('#account_register_ok_box').css({display: 'none'});
}

function display_getkey_widget()
{
	$('#account_newkey_box').css({display: 'block'});
}

function display_keys_widget()
{	
	$('#account_keys_box').css({display: 'block'});
}

function display_cgu_validation_widget()
{	
	$('#account_cgu_validation_box').css({display: 'block'});
}

function display_upgrade_widget()
{									
	// Effects	
	var overlay = $('<div id="overlay" style="display: block; background-color: black; position: absolute; top: 0px; left: 0px;"></div>');

	$('#page').prepend(overlay);
	$(overlay).css({height:$(document).height(), 
					width: $(document).width(), 
					opacity: '0.8', 
					filter: 'alpha(opacity = 80)', 
					zIndex: 2});

	// Display the box
	$('#account_upgrade_box').css({display: 'block'});
	
	var popup_width = $('#account_upgrade_box').width();
	var popup_height = $('#account_upgrade_box').height();
	

	var scroll_top = 0;
	if (window.pageYOffset != null)
	{
		scroll_top = window.pageYOffset;
	}
	else if (document.documentElement != null)
	{
		scroll_top = document.documentElement.scrollTop;
	}
	
	$('#account_upgrade_box').css({	position: 'absolute',
									top: (($(window).height() - popup_height) / 2 ) + scroll_top,
									left: ($(document).width() - popup_width) / 2,
									'z-index':  1001});
		
	$(document).click(function(event)
	{
	    // No bind if the user click into the overlay box
		if(!$(event.target).parents('#account_upgrade_box').size())
		{
			$(document).unbind('click');
			
			// Revert changes
			hide_upgrade_widget();
		}
	});
	
	// Fix resize
	$(window).resize(function()
	{
		// If the overlay do not exist; unbind
		if (!$('#overlay').attr('id'))
		{
			$(window).unbind('resize');
		}
		
		$(overlay).css({width: $(document).width() + 'px',
						height: $(document).height() + 'px'});
	});

}

function populate_upgrade_widget(element)
{
	var tr = $(element).parent().parent();
	
	// Set the current key
	var key = $('.key_column', tr).text();
	$('#account_upgrade_box .key').text(key);
	$('#account_upgrade_box input[name="key"]').val(key);
}

function hide_upgrade_widget()
{
	$('#account_upgrade_box').fadeOut('slow', function()
	{
		$('#account_upgrade_box').css({display: 'none'});
	});
		
	// Effects
	$('#overlay').fadeOut('slow', function()
	{
		// Revert changes
		$('#overlay').remove();
		$('#account_upgrade_box').css('z-index', '');
	});
}

function widget_switcher()
{
/*	$('li.dh_menu2_on').click(function()
	{
		alert('!');
		widget_switcher();
	});*/
	
	var fragment = get_url_fragment();
	switch (fragment)
	{
		case 'login':
		default:
			display_login_widget();	
			hide_register_widget();	
			break;
			
		case 'register':
			display_register_widget();
			hide_login_widget();
			break;
	}
}

$(document).ready(function()
{
	// Fix for IE 6
	$('#developers_footer').css('bottom', $('#developers_footer').css('bottom'));
	
	if (typeof(jsConfig['widgets']) == 'undefined')
	{
		if (typeof(jsConfig['developer_id']) != 'undefined' && jsConfig['developer_id'] > 0)
		{
			display_getkey_widget();
			display_keys_widget();	
		}
		else
		{
			widget_switcher();
		}
	}
	else
	{
		// Force to use ONLY this widget list
		for (var i in jsConfig['widgets'])
		{
			eval('display_' + jsConfig['widgets'][i] + '_widget();');
		}
	}
	
	// Free API upgrade links
	$('#account_keys_box .upgrade').click(function()
	{
		display_upgrade_widget();
		
		populate_upgrade_widget($(this));
		
		return false;
	});
	
	// Close the API upgrade
	$('#account_upgrade_box .close').click(function()
	{
		hide_upgrade_widget();
		
		return false;
	});
	
	// Remove null form errors messages
	$('.form_error').each(function()
	{
		if (!$(this).html())
		{
			$(this).remove();
		}
	});
	
	$('.need_confirm').click(function()
	{
		return window.confirm('Are you sure to deleting this key ?');
	});
});