/*
 * jQuery Light Dialog Plugin
 * Author (c) Miroslav Peterka - wiki@seznam.cz
 * 2008 (c) Xacti Group
 * @version 0.0.6
 */


;(function($) {

	$.lightDialog = function(option){

		var options={
			modal:false,
			onClose:null,
			opacity:0.9,
			centerOnScroll:true,
			excenter:{x:0,y:0},
			speed:400
		};

		var lightDialog = new lightDialogClass($.extend(options,option));
		return lightDialog;
	}

		
	function lightDialogClass(options){
		this.options=options;
		this.resizeFce = null;
		this.scrollFce = null;
		return this;
	}
	
	lightDialogClass.prototype.open = function (el){
		if($.browser.msie && $.browser.version < 7)
			$('embed, object, select').css({ 'visibility' : 'hidden' });
		$('<div id="light-dialog"><div id="light-dialog-window"></div><div id="light-dialog-overlay"></div><div id="light-dialog-loader"></div></div>').appendTo('body');
		$('#light-dialog-loader').css({position:'absolute',left:'-4000px',top:0});
		$('#light-dialog-overlay').hide().css('opacity',this.options.opacity).fadeIn(this.options.speed/2);
		
		if(!this.options.modal){
			var that=this;
			$('#light-dialog-overlay').click(function(){that.close()});
		}

		if(el)$('#light-dialog-window').append(el);
		
		var that=this;

		this._scrollFce=(this.options.centerOnScroll) ?
			function(){
				placeDialogBg();
				placeDialogWin(that.options);
			}
		:
			function(){
				placeDialogBg();
			};

		this._resizeFce=function(){
				placeDialogBg();
				placeDialogWin(that.options);
		}

		this._resizeFce();


		$(window).bind('resize',this._resizeFce);
		$(window).bind('scroll',this._scrollFce);

	}

	lightDialogClass.prototype.close = function (){
		if( typeof(this.options.onClose) == 'function' )
			this.options.onClose();
		
		$(window).unbind('resize',this._resizeFce);
		$(window).unbind('scroll',this._centerFce);

		$('#light-dialog-window').hide();
		$('#light-dialog-overlay').fadeOut(this.options.speed/2,function(){$('#light-dialog').remove()});
		if($.browser.msie && $.browser.version < 7)
			$('embed, object, select').css({ 'visibility' : 'visible' });
		return false;
	}

	lightDialogClass.prototype.animate = function(el,options){
		var first=false;

		if( $('#light-dialog').length == 0) {
			this.open();
			$('#light-dialog-window').css({height:10,width:10,opacity:0});
			first=true;
		}


		this.options=$.extend(this.options,options)
		
		var loader=$('#light-dialog-loader');
		el.appendTo(loader);

		var w=loader.width();
		var h=loader.height();

		var img=el.get(0);
		var bg=$('#light-dialog-overlay').get(0);
		var middle={x:bg.offsetWidth/2+this.options.excenter.x,y:bg.offsetHeight/2+this.options.excenter.y};
		var size={x:img.offsetWidth/2,y:img.offsetHeight/2};
		var x=bg.offsetLeft+middle.x-size.x;
		var y=bg.offsetTop+middle.y-size.y;
		if(x<0)x=0;
		if(y<0)y=0;

		el.hide();
		
		$('#light-dialog-window').animate({top: y+"px" ,left: x+"px",width:w+"px",height:h+"px",opacity:1},this.options.speed,function(){
			el.show();
      if(this.style.removeAttribute)this.style.removeAttribute('filter');
		}).html('').append(el);

	}




/*helper functions */

	function placeDialogBg(){
		var bg=$('#light-dialog-overlay').get(0);
		var scrolled=(window.pageXOffset==undefined)?{x:document.documentElement.scrollLeft,y:document.documentElement.scrollTop}:{x:window.pageXOffset,y:window.pageYOffset};
		bg.style.width=0;
		bg.style.height=0;
		var winsize=(window.innerHeight==undefined)?{x:document.documentElement.clientWidth+'px',y:document.documentElement.clientHeight+'px'}:{x:'100%',y:'100%'};
		bg.style.top=scrolled.y+'px';
		bg.style.left=scrolled.x+'px';
		bg.style.width=winsize.x;
		bg.style.height=winsize.y;
	}

	function placeDialogWin(options){
		var img=$('#light-dialog-window').get(0);
		var bg=$('#light-dialog-overlay').get(0);
		var middle={x:bg.offsetWidth/2+options.excenter.x,y:bg.offsetHeight/2+options.excenter.y};
		var size={x:img.offsetWidth/2,y:img.offsetHeight/2};
		var x=bg.offsetLeft+middle.x-size.x;
		var y=bg.offsetTop+middle.y-size.y;
		if(x<0)x=0;
		if(y<0)y=0;
		img.style.top=y+"px";
		img.style.left=x+"px"
	}


})(jQuery);