/*
 * jQuery Light Dialog Plugin
 * Author (c) Miroslav Peterka - wiki@seznam.cz
 * 2011 (c) Xacti Group
 * @version 0.0.12
 */


;(function($) {

	$.lightDialog = function(option){

		var options={
			modal:false,
			opacity:0.9,
			centerOnScroll:true,
			excenter:{x:0,y:0},
      startposition:null,
      startelement:null,
      fixed:{},
			speed:400,
      className:'',
			onClose:null,
      onCloseEnd: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;
    this.border = {x:0,y:0};
		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-wrap"><div id="light-dialog-window"></div></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);
    
    var win=$('#light-dialog-window');
    this.border.x=win.outerWidth()-win.innerWidth();
    this.border.y=win.outerHeight()-win.innerHeight();
    
    if(this.options.startelement!=null){
      var pos=$(this.options.startelement).offset();
      this.options.startposition={top:pos.top+$(this.options.startelement).height()/2,left:pos.left+$(this.options.startelement).width()/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-wrap').css('position','absolute');
    $('#light-dialog-window').css('position','relative');

		
		if(!this.options.modal){
			var that=this;
			$('#light-dialog-overlay').click(function(){that.close()});
		}

		if(el){
      win.append(el);
    }
    
    $('#light-dialog-wrap').css({height:win.height()+this.border.y,width:win.width()+this.border.x});


    var that=this;

		this._scrollFce=(this.options.centerOnScroll) ?
			function(){
				placeDialogWin(that.options);
			}
		:
			function(){
			};

		this._resizeFce=function(){
        resizeDialogOverlay();
				placeDialogWin(that.options);
		}

		this._resizeFce();

    that.options.startposition=null;


		$(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-wrap').hide();
    var self=this;
		$('#light-dialog-overlay').fadeOut(this.options.speed/2,function(){
      $('#light-dialog').remove();
  		if( typeof(self.options.onCloseEnd) == 'function' )
	  		self.options.onCloseEnd.apply(self);
    });
		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-wrap').css({height:10+this.border.y,width:10+this.border.x});
      $('#light-dialog-window').css({opacity:0,height:10,width:10});
			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()+this.border.x;
		var h=loader.height()+this.border.y;

		var bg=$('#light-dialog-overlay').get(0);
    

		var middle=_getMiddle(this.options);
    
    pos={};
		pos.left=middle.x-w/2;
		pos.top=middle.y-h/2;
		if(pos.left<0)pos.left=0;
		if(pos.top<0)pos.top=0;

    pos=_positionFixed(pos,{width:w,height:h},this.options);


		el.hide();

    $('#light-dialog-window').animate({opacity:1,width:w-this.border.x,height:h-this.border.y},this.options.speed,function(){
      if(this.style.removeAttribute)this.style.removeAttribute('filter');    
    }).html('').append(el);
		$('#light-dialog-wrap').show().animate({top: pos.top ,left: pos.left,width:w,height:h},this.options.speed,function(){
			el.show();
      if( typeof(that.options.onAnimateEnd) == 'function' )
        that.options.onAnimateEnd.apply(that,[el]);

		}).css({overflow:'visible'});

	}


/*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 middle;    
    if(options.startposition!=null){
      middle={x:options.startposition.left,y:options.startposition.top}
    }else{
      var scrolled=_getScrolled();
      var browsersize=_getBrowserSize();
      middle={x:scrolled.x+browsersize.x/2+options.excenter.x,y:scrolled.y+browsersize.y/2+options.excenter.y}
    }
    return middle;
  }

  function _getScrolled(){
    var d=(_quirk)?document.body:document.documentElement;
    var scrolled=(window.pageXOffset==undefined)?{x:d.scrollLeft,y:d.scrollTop}:{x:window.pageXOffset,y:window.pageYOffset};
    return scrolled;
  }

  function _positionFixed(pos,size,options){
    var browsersize=_getBrowserSize();
    var scrolled=_getScrolled();

    
    if(options.fixed.top!=null){
      pos.top=scrolled.y+options.fixed.top;
    }
    else if(options.fixed.bottom!=null){
      pos.top=scrolled.y+browsersize.y-size.height-options.fixed.bottom;
    }

    if(options.fixed.left!=null){
      pos.left=scrolled.x+options.fixed.left;
    }
    else if(options.fixed.right!=null){
      pos.left=scrolled.x+browsersize.x-size.width-options.fixed.right;
    }

    return pos;

  }


  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-wrap');
    var middle=_getMiddle(options);
		var size={width:win.width(),height:win.height()};
    var pos={top:middle.y-size.height/2,left:middle.x-size.width/2};
		if(pos.top<0)pos.top=0;
		if(pos.left<0)pos.left=0;
    pos=_positionFixed(pos,size,options);
		win.css(pos);
	}


})(jQuery);
