$(function() {
	$('.ajaxLoginError').hide();
	$('#logIn').click(function(e) {
		e.preventDefault();
		$('#ajaxLogin').dialog();
	});
	$('#loginForm').submit(function(e) {
		e.preventDefault();
		$('.ajaxLoginError, #ajaxLoginLoading').hide();
		var mail = $('input[name=ajaxLoginEmail]');  
		var pass = $('input[name=ajaxLoginPassword]');   
		
		if( mail.val() == '' ) {
			$('#ajaxLoginNameError').show();
			return false;
		}
		if( pass.val() == '' ) {
			$('#ajaxLoginPwError').show();
			return false;
		}
		
		$('#ajaxLoginLoading').show();
		$('.ui-widget-overlay').css({'z-index':1100});
		$.post(pageUrl+'User/ajaxLogin',{loginEmail: mail.val(),loginPassword: pass.val()},function (data) {	
			$('.ui-widget-overlay').css({'z-index':1001});
			$('#ajaxLoginLoading').hide();
			if (data == 'success') {
				$('#ajaxLogin').html('You have successfully logged in!');
				setTimeout("$('#ajaxLogin').dialog('close')",1000);
				$.get(rootUrl+'Index/prepareUserArea/true',function(data) {
					$('#topMenuRight').html(data);
				});
				$.get(rootUrl+'Index/prepareBottomMenu/true',function(data) {
					$('#bottomMenuRight').html(data);
				});
			} else {
				$('#ajaxLoginGenericError').text(data).show();
			}
		});
	});
});

function openSubMenu( id ) {
	$('#'+id+'Container').css({'display' : ''});
}

function closeSubMenu( id ) {
	$('#'+id+'Container').css({'display' : 'none'});
}

function setCalendarType( name, id ) {
	$('#calendarTypeSelect').html(name);
	$('#typeId').attr('value', id);
}

function makeHomePage() {
	$('#header').css({'behavior' : 'url(#default#homepage)'}).setHomePage('http://i-marbella.com/');
} 

var clicks = 0;

function dateSelect( date ) {
	if( clicks == 0) {
		$('#calendarStartDate').attr('value',date);
		$('#calendarEndDate').attr('value','')
		clicks += 1;
	} else if( clicks == 1 ) {
		$('#calendarEndDate').attr('value',date);
		clicks += 1;
	} else {
		clicks = 0;
		dateSelect( date );
	}
}

function setSearchDates( start, end ) {
	$('#calendarStartDate').attr('value',start);
	$('#calendarEndDate').attr('value',end);
	clicks = 0;
}

