/*
General jQuery functions
Copyright (c) 2009 Ylab, www.ylab.nl
Author: Yohan Creemers
*/
var addHoverClass = function(){$(this).addClass('over');}
var remHoverClass = function(){$(this).removeClass('over');}

function dmY2Ymd(dmY){
	var a = dmY.split('-');
	return '' + a[2] + a[1] + a[0];
}

function Overlay(id){
	//styling properties
	this.overlayBgColor = '#000';
	this.overlayOpacity = 0.5;

	//public methods
	this.show = methodShow;
	this.hide = methodHide;
	this.resize = methodResize;
	this.hideOnClick = methodHideOnClick;

	//internal functions
	function methodShow(){
		//hide some elements to avoid these elements to appear above the overlay in IE
		this.$visible = $('embed:visible, object:visible, select:visible').css({ 'visibility' : 'hidden' });
		//apply styling and show overlay
		this.$overlay.css({
			backgroundColor: this.overlayBgColor,
			opacity: this.overlayOpacity
		});
		this.resize();
		this.$overlay.fadeIn();
	}

	function methodHide(){
		if(this.$visible){
			this.$visible.css({ 'visibility' : 'visible' });
			this.$visible = null;
			this.$overlay.fadeOut();
		}
	}

	function methodResize(){
		this.$overlay.css({
			width: $(window).width() + 'px',
			height: $(document).height() + 'px'
		});
	}

	function methodHideOnClick(){
		$(this.$overlay).click(function(){obj.hide();});
	}

	//init, create dom element, set initial styling
	var obj = this;
	this.$visible;
	$('body').append('<div id="' + id + '"></div>');
	this.$overlay = $('#' + id);
	this.$overlay.css({
		position: 'absolute',
		top: 0,
		left: 0,
		zIndex: 90
	});
	$(window).resize(function(){obj.resize();});
}

function LightForm(id, urlForm, caption){
	//styling properties
	this.initialTop = 0; //integer in pixels
	this.width = 536;    //integer in pixels, includes padding

	//public methods
	this.show = _methodShow;
	this.hide = _methodHide;

	//internal functions
	function _methodShow(fCallback){
		//apply styling and load html
		this.$dom.css({
			width: this.width + 'px',
			left: (($(window).width() - this.width) / 2) + 'px',
			top: this.initialTop + 'px'
		});
		this.$domBody[0].fCallback = fCallback;
		this.$domBody.load(urlForm, null, _displayOnStage);
	}

	function _methodHide(){
		this.$dom.hide();
	}

	function _displayOnStage(responseText, textStatus, XMLHttpRequest){
		//scroll to the center of the viewport
		var scrollTop = $(document).scrollTop();
		$(this).parent().css({top: scrollTop}).show().animate({
			top: scrollTop + (($(window).height() - $(this).height()) / 2) + 'px'
		});
		if(this.fCallback && $.isFunction(this.fCallback)){
			this.fCallback();
		}
	}

	var obj = this;
	if($('#' + id).length == 0){
		//init, create dom element, set initial styling
		$('body').append(
			'<div id="' + id + '">' +
				'<div id="' + id + '-header"></div>' +
				'<div id="' + id + '-body"></div>' +
			'</div>');
	}
	this.$dom = $('#' + id);
	this.$domHeader = $('#' + id + '-header');
	this.$domBody = $('#' + id + '-body');
	this.$dom.css({
		position:'absolute',
		zIndex: 91
	});
	if(caption){
		this.$domHeader.html(caption);
	}
}

var objOverlay,objForm;
function showEditForm(urlForm, sCaption, fCallback){
	if(!objOverlay){
		objOverlay = new Overlay('overlay-for-edit');
	}
	objOverlay.show();

	objForm = new LightForm('overlay-form', urlForm, sCaption);
	objForm.show(fCallback);
	return false;
}

jQuery(document).ready(function($){
	//wait cursor during ajax calls
	var ajaxOverlay;
	ajaxOverlay = new Overlay('overlay-ajax');
	ajaxOverlay.overlayOpacity = 0;
	ajaxOverlay.$overlay.css({cursor:'wait'}).hide();

	$('#pagecontent').ajaxStart(function(){
		ajaxOverlay.show();
	}).ajaxStop(function(){
		ajaxOverlay.hide();
	});
});
