
$(function() {
	/* Expand any flash messages */
	expandFlash();
	
	/* Append an ajax waiting overlay should we need it */
	$('<div id="ajax-overlay"></div>').appendTo('body').hide();
	
	/* Show any non-JS hidden stuff! */
	$('.hidden').show();
	/* Remove any js-hidden class stuff! */
	$('.js-hidden').remove();
	/* Add functionality ro remove link on login_as */
	$('#last_admin .close', this).click(function() {
		$('#last_admin').fadeOut();
	});

	/* avoid double click on submit */
	$('input[type=submit]').click(function(){
		$(this).val('loading');
		$(this).css({'background': 'url('+basePath+'webroot/img/spinner-fb.gif)', 'background-repeat': 'no-repeat', 'background-position': '75px 5px'});
		$(this).attr("READONLY", true);
		return true;
	});

	/* alert on empty calendar*/
	$('.dateInput').click(function(){
		var date = $(this).val();
		var id = $(this).attr('id');
		setTimeout("checkDate('"+id+"', '"+date+"')", 500);
	});
});

function checkDate(id, date){
	var exist = ($('#ui-datepicker-div').length == 0);
	var empty = ($('#ui-datepicker-div').text().length != 0);
	var hidden = ($('#ui-datepicker-div').css('display') == 'none' || $('#ui-datepicker-div').css('display') == 'hidden');
	if(hidden){
		if(date == $('#'+id).val()){
			var time = new Date();
			time = time.getTime();
			$('#'+id).after("<p id='"+time+"' class='inline notice' style='margin-left: 25px;'>No date has been changed</p>");
			setTimeout("$('#"+time+"').fadeOut(500)",1000);
		}
		
	}else{
		setTimeout("checkDate('"+id+"', '"+date+"')", 500);
	}
}

function expandFlash() {
	$('#flash').each(function() {
		var height = $(this).height();
		$(this).css({top: '-'+height+'px', opacity: 0});
		setTimeout(displayMessage, 500);
		var closeTag = setTimeout(hideMessage, 3500);
		$(this).hover(function(){
			clearTimeout(closeTag);
		}, function(){
			setTimeout(hideMessage, 500);
		});
		$('.close', this).click(function() {
			$('#flash').fadeOut();
		});
	});
}

function ajaxPost(url, data, update) {
	var displayingOverlay = window.setTimeout(displayOverlay, 500);
	if (url.indexOf(basePath) == -1) {
		url = basePath + url;
	}
	$.post(
		url,
		data,
		function(result){

			$(update).html(result);
			window.clearTimeout(displayingOverlay);
			window.setTimeout(hideOverlay, 500);
			expandFlash();
		}
	);
}

function ajaxGet(url, data, update) {
	var displayingOverlay = window.setTimeout(displayOverlay, 500);
	if (url.indexOf(basePath) == -1) {
		url = basePath + url;
	}
	$.get(
		url,
		data,
		function(result){
			$(update).html(result);
			window.clearTimeout(displayingOverlay);
			window.setTimeout(hideOverlay, 500);
			expandFlash();
		}
	);
}

/*
 * @param	string	url		the url to be called
 * @param	object	element	the element to be removed on success
 * @author Dan walton , Francesco Frison
 */

function ajaxDelete(url, element) {
	var displayingOverlay = window.setTimeout(displayOverlay, 500);
	if (url.indexOf(basePath) == -1) {
		url = basePath + url;
	}
	$.get(
		url,
		null,
		function(result){
			$(element).remove();
			$(result).appendTo('body');
			window.clearTimeout(displayingOverlay);
			window.setTimeout(hideOverlay, 500);
			expandFlash();
		}
	);
}

function displayMessage() {
	$('#flash').animate({top: 0, opacity: 1}, 500);
}

function hideMessage() {
	$('#flash').animate({top: -1*$('#flash').height(), opacity: 0}, 500);
	window.setTimeout(deleteMessage, 500);
}

function deleteMessage() {
	$('#flash').remove();
}

function displayOverlay() {
	$('#ajax-overlay').fadeIn();
}

function hideOverlay() {
	$('#ajax-overlay').fadeOut();
}
