/*
* 
* wlightbox 1.5
* 
* http://wdevblog.net.ru
*
*/

(function($) {

$.fn.wlightbox = function(options){
  
  var opt = $.extend({'spinner_img':'spinner.gif','frame_padding':10,'anim_duration':500},options);
  $(document.body).append('<div id="wl_overlay"></div><div id="wl_loading"></div><div id="wl_popup"></div>');
  
  $('#wl_popup').css({
    'background-color': '#fff',
    'border': '1px solid #888',
    'padding': opt.frame_padding+'px',
    'display': 'none',
    'position': 'absolute',
    'top': '50%',
    'left': '50%',
    'z-index': '100'
  });
  
  $('#wl_overlay').css({
    'background-color': '#000',
    'position': 'absolute',
    'top': '0',
    'left': '0',
    'z-index': '50',
    'display': 'none',
    'overflow': 'hidden'
  });
  
  $('#wl_loading').css({
    'background': 'url('+opt.spinner_img+') no-repeat center center #fff',
    'border': '1px solid #888',
    'width': '50px',
    'height': '50px',
    'position': 'absolute',
    'top': '50%',
    'left': '50%',
    'z-index': '100',
    'margin': '-25px 0 0 -25px',
    'display': 'none'
  });
  
  function removePopup(){
    $('#wl_overlay').fadeOut(300,function(){
      $('#wl_popup').fadeOut(300);
    });
    $(window).unbind('scroll');
  }
  
  function ShowImage(url){
    var yScroll = $.browser.msie ? document.documentElement.scrollTop : self.pageYOffset;
    var windowHeight = $.browser.msie ? document.documentElement.clientHeight : window.innerHeight;
    var posTop = Math.round((windowHeight/2) + yScroll);
    
    $('#wl_loading').css({display:'block',top:posTop+'px'});
    $('#wl_popup').hide();
    var img = new Image();
    $(img)
    .load(function(){
      $('#wl_loading').hide();
      $('#wl_popup')
      .html(this)
      .css({
        width: 0,
        overflow: 'hidden',
        top:posTop+'px',
        marginLeft: img.width/2+'px',
        marginTop:'-'+(img.height/2+opt.frame_padding)+'px'
      });
      $('#wl_overlay')
      .css({
  			'width':		0,
  			'height':		$(document).height()+'px',
  			'opacity':  0.5
  		})
      .animate({
        width: $(window).width()+'px'
      },opt.anim_duration,function(){
        $('img:first','#wl_popup')
        .css({'margin-left': -(img.width+opt.frame_padding)+'px'})
        .animate({'margin-left':'0'},opt.anim_duration);
        $('#wl_popup').animate({
          'width': img.width+'px',
          'margin-left': -(img.width/2+opt.frame_padding)+'px'
        },opt.anim_duration,function(){
          popupFit();
          $(window).bind('scroll',popupFit);
        });
      })
      .click(function(){
        removePopup();
        $('#wl_overlay').unbind('click');
      })
      .attr("title","Закрыть");
    })
    .attr('src', url);
  }

  function overlayFit(){
    $('#wl_overlay').css({
      width: $(window).width(),
      height: $(document).height()
    });
  }
  
  function popupFit(){
    var yScroll = $.browser.msie ? document.documentElement.scrollTop : self.pageYOffset;
    var windowHeight = $.browser.msie ? document.documentElement.clientHeight : window.innerHeight;
    var posTop = Math.round((windowHeight/2) + yScroll);
    $('#wl_loading').stop().animate({top: posTop+'px'},500);
    $('#wl_popup').stop().animate({top: posTop+'px'},500);
  }
  
  $(this).click(function(){
    ShowImage($(this).attr('href'));
    return false;
  });
  
  $(window).bind('resize',overlayFit);
  
   $(document).bind("keydown", function (e) {
    if (e.keyCode === 27) {
      removePopup();
    }
  });
  
}

})(jQuery);

