/*
 * jQuery Light Dialog Plugin
 * Author (c) Miroslav Peterka - wiki@seznam.cz
 * 2009 (c) Xacti Group
 * @version 0.0.8
 */


;(function($) {

	$.lightDialog = function(option){

		var options={
			modal:false,
			opacity:0.9,
			centerOnScroll:true,
			excenter:{x:0,y:0},
			speed:400,
      className:'',
			onClose:null,
      onOpen:null,
      onAnimateEnd:null
		};

		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,_doOpenEvent){
    if(_doOpenEvent==null)_doOpenEvent=true;

		if(_IE6)
			$('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>').addClass(this.options.className).appendTo('body');
		$('#light-dialog-loader').css({position:'absolute',left:'-4000px',top:0});
    $('#light-dialog-overlay').hide().css({
      opacity:this.options.opacity,
      top:0,
      left:0,
      position: 'fixed',
      height: '100%',
      width: '100%'
    }).fadeIn(this.options.speed/2);

    if(_IE6 || _quirk){
      var d=(_quirk)?document.body:document.documentElement;
      $('#light-dialog-overlay').css({
        position: 'absolute',
        paddingLeft:$('body').get(0).currentStyle.paddingLeft,
        paddingRight:$('body').get(0).currentStyle.paddingRight,
        height: Math.max(d.scrollHeight,d.clientHeight) + 'px'
      });
    }

    $('#light-dialog-window').css('position','absolute');

		
		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(){
				placeDialogWin(that.options);
			}
		:
			function(){
			};

		this._resizeFce=function(){
        resizeDialogOverlay();
				placeDialogWin(that.options);
		}

		this._resizeFce();


		$(window).bind('resize',this._resizeFce);
		$(window).bind('scroll',this._scrollFce);

		if(_doOpenEvent && typeof(this.options.onOpen) == 'function')
      this.options.onOpen.apply(this,[el]);
	}
 

	lightDialogClass.prototype.close = function (){
		if( typeof(this.options.onClose) == 'function' )
			this.options.onClose.apply(this);
		
		$(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(_IE6)
			$('embed, object, select').css({ 'visibility' : 'visible' });
		return false;
	}

	lightDialogClass.prototype.animate = function(el,options){
		var first=false;
    var that=this;

		if( $('#light-dialog').length == 0) {
			this.open(null,false);
			$('#light-dialog-window').css({height:10,width:10,opacity:0});
			first=true;
		}

		var loader=$('#light-dialog-loader');
		el.appendTo(loader);
    
    //handle onOpen event
    if(first && typeof(this.options.onOpen) == 'function')
       this.options.onOpen.apply(this,[el]);

    this.options=$.extend(this.options,options)


		var w=loader.width();
		var h=loader.height();

		var img=el.get(0);
		var bg=$('#light-dialog-overlay').get(0);
    

		var middle=_getMiddle(this.options);

		var size={x:img.offsetWidth/2,y:img.offsetHeight/2};
		var x=middle.x-size.x;
		var y=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');

      if( typeof(that.options.onAnimateEnd) == 'function' )
        that.options.onAnimateEnd.apply(that,[el]);

		}).html('').append(el);

	}


/*helper functions */
  var _quirk = ($.browser.msie && document.compatMode=='BackCompat')?true:false;
  var _IE6 = ($.browser.msie && navigator.userAgent.match( /MSIE ([\d.]+)/ )[1] < 7 );

  function _getBrowserSize(){
		var browsersize={x:window.innerWidth,y:window.innerHeight};
    if(window.innerHeight==undefined){
      // IE6+ quirk or strict
      var d=(_quirk)?document.body:document.documentElement;
      browsersize={x:d.clientWidth,y:d.clientHeight};
    }
    return browsersize;
  }

  function _getMiddle(options){

    var d=(_quirk)?document.body:document.documentElement;

		var scrolled=(window.pageXOffset==undefined)?{x:d.scrollLeft,y:d.scrollTop}:{x:window.pageXOffset,y:window.pageYOffset};

    var browsersize=_getBrowserSize();
    return {x:scrolled.x+browsersize.x/2+options.excenter.x,y:scrolled.y+browsersize.y/2+options.excenter.y};
  }


  function resizeDialogOverlay(){
    var d=(_quirk)?document.body:document.documentElement;
    $('#light-dialog-overlay')
      .css({
          height:Math.max(d.scrollHeight,d.clientHeight)+'px'
        })
  }

	function placeDialogWin(options){
    var win=$('#light-dialog-window');
    var middle=_getMiddle(options);
		var size={x:win.width()/2,y:win.height()/2};
		var x=middle.x-size.x;
		var y=middle.y-size.y;
		if(x<0)x=0;
		if(y<0)y=0;
		win.css({top:y+"px",left:x+"px"});
	}


})(jQuery);
