(function($)
{
   $.fn.fullSizeImg = function()
   {
      var full_img_width = 0;
      var full_img_height = 0;
      $(this).click(function(e)
      {
//         e.preventDefault();
         var href_full_img = $(this).parents('li').eq(0).find('.image-link').val();
//       var href_full_img = $(this).parent('a').attr('href');

         $(this).parents('a').eq(0).removeAttr('href');
 
         $(".full_img").remove();
         $('<div class="full_img"><div class="shadow"></div><img src="/img/main/round_preloader.gif" class="preloader"/><div class="img_block"><img src="' + href_full_img + '" class="img"/></div></div>')
         .appendTo('body')
         .css({'display' : 'none', 'width' : ($(window).width() + 20) + 'px'})
         .fadeIn('fast')
         ;
         $('.img_block img').load(function(){
             $('.preloader').remove();
             full_img_width = $(this).width();
             full_img_height = $(this).height();

             if($(this).width() > $(window).width())
             {
                var new_width = $(window).width() - 100;
                $(this).width(new_width);
                 
                $('.enlarge').remove();
                $('<a class="enlarge"></a>').appendTo('.img_block');
 
             }
             if($(this).height() > $(window).height())              
             {
                var new_height = $(window).height() - 50;
                $(this).height(new_height).css('width','auto');

                $('.enlarge').remove();
                $('<a class="enlarge"></a>').appendTo('.img_block');
             } 
             var top_margin = ($(window).height() - $('.full_img img').height()) / 2;
             var left_margin = ($(window).width() - $('.full_img img').width()) / 2;

            $('.img_block')
            .width($(this).width() + 12)
            .height($(this).height() + 12) 
            .css({'top': top_margin + 'px', 'left': left_margin + 'px', 'visibility' : 'visible'});
//            $(this).css({'margin-top': top_margin + 'px', 'margin-left': left_margin + 'px', 'visibility' : 'visible'});
         });
         if($.browser.msie) 
            $('html').css({'overflow-y': 'hidden'});
         else
            $('body').css({'overflow': 'hidden'});

      });

      $('.enlarge').live('click', function(){
         $('.full_img img').width(full_img_width);
         $('.full_img img').height(full_img_height);
         $('.img_block').width(full_img_width + 12);
         $('.img_block').height(full_img_height + 12);
         var top_margin = ($(window).height() - $('.full_img img').height()) / 2;
         if(top_margin < 10)
            top_margin = 10;

         var left_margin = ($(window).width() - $('.full_img img').width()) / 2;
         if(left_margin < 10)
            left_margin = 10;

         if($('.full_img img').height() > $('.full_img .shadow').height()) 
            $('.full_img .shadow').height($('.full_img img').height() + 30);

         $('.img_block').css({'top': top_margin + 'px', 'left': left_margin + 'px'});

         $('.full_img').unbind('click');
         $(this).remove();
      });

      $('.full_img .shadow, .full_img img').live('click', function(){
         $('.full_img').fadeOut('fast', function()
         {
            $('.full_img').remove();
            if($.browser.msie) 
               $('html').css({'overflow-y': 'visible'});
            else
               $('body').css({'overflow': 'visible'});

         });
         $('.full_img').remove();
      });
      $(window).resize(function(){ 
         $('.full_img').width($(window).width()).height($(window).height());
         $('.full_img .shadow').height($('.full_img').height());

      });
 
   }
 
}
)(jQuery);
