/*
jQuery Overlay
Copyright (c) 2009 Ylab, www.ylab.nl
Author: Yohan Creemers
*/
var objOverlay,objForm;
var img_x = '/site_layout/x.gif';

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.backgroundColor = '#ff0';
	this.initialTop = 0; //integer in pixels
	this.width = 566;    //integer in pixels, includes padding
	this.height = 566;    //integer in pixels, includes padding
	this.padding = 10;   //integer in pixels

	//public methods
	this.show = _methodShow;
	this.hide = _methodHide;
	this.center = _displayOnStage;

	//internal functions
	function _methodShow(fCallback){
		//apply styling and load html
		this.$dom.css({
			display: 'none',
			width: this.width + 'px',
			left: (($(window).width() - this.width) / 2) + 'px',
			background: this.backgroundColor,
			top: this.initialTop + 'px'
		});
		this.$domHeader.css({
			padding: '1px ' + this.padding + 'px',
			color: this.backgroundColor,
			fontWeight: 'bold'
		});
		this.$domBody.css({
			overflow: 'auto',
			maxHeight: this.height + 'px',
			padding: this.padding + 'px'
		});
		this.$domBody[0].fCallback = fCallback;
		this.$domBody.load(urlForm, null, _displayOnStage);
	}

	function _methodHide(){
		this.$dom.hide();
	}

	function _displayOnStage(responseText, textStatus, XMLHttpRequest){
		if(this.fCallback && $.isFunction(this.fCallback)){
			this.fCallback();
			this.fCallback = null;
		}
		//scroll to the center of the viewport
		var scrollTop = $(document).scrollTop();
		//20091218:make sure titlebar is visible, no negative offset
		$(this).parent().css({top: $(document).scrollTop() + Math.max(0, ( $(window).height() - $(this).parent().height() ) / 2) + 'px'}).show();
	}

	var obj = this;
	if($('#' + id).length == 0){
		//init, create dom element, set initial styling
		$('body').append(
			'<div id="' + id + '">' +
				'<img id="' + id + '-close" src="' + img_x + '" />' +
				'<div id="' + id + '-header"></div>' +
				'<div id="' + id + '-body"></div>' +
			'</div>');
	}
	this.$dom = $('#' + id);
	this.$domClose = $('#' + id + '-close').css({
		position:'absolute',right:0,top:2,cursor:'pointer'
	}).click(function(){
		$('#' + id).hide();
		objOverlay.hide();
	});
	this.$domHeader = $('#' + id + '-header');
	this.$domBody = $('#' + id + '-body');
	this.$dom.css({
		position:'absolute',
		zIndex: 91
	});
	if(caption){
		this.$domHeader.html(caption);
	}
}

function showEditForm(urlForm, sCaption, fCallback){
	if(!objOverlay){
		objOverlay = new Overlay('overlay-for-edit');
	}
	objOverlay.show();

	objForm = new LightForm('overlay-form', urlForm, sCaption);
	objForm.backgroundColor = '#fff';
	objForm.show(fCallback);

	return false;
}
