(function($){
    
    $.fn.imagelook = function(options) {
        
        var options = $.extend({
            cloakColor: '#000000', 
	    cloakOp: '0.5', 
            Zindex: '1000',
	    inlargeButtonSrc: '/img/fancyZoom/zoom.png',
            inlargeButtonSrcHeight: '50',
            inlargeButtonSrcWidth: '50'            
	  },options);          
          
        var wrappedObject = this;    
        
        bigImageSrc = $(wrappedObject).children('img').attr('alt');        
        inlargeButton = '<a href="#" id="ILzoomButton" style="text-indent: -9999px; z-index:100;  display:none; position: absolute; width:'+options.inlargeButtonSrcWidth+'px;  height:'+options.inlargeButtonSrcHeight+'px; background-image: url('+options.inlargeButtonSrc+');">ZoomIn</a>';                        
        cloakBlock = '<div id="ILcloacBlock" style="z-index:'+options.Zindex+'; background-color: '+options.cloakColor+'; position:absolute;  top: 0px; display: none; left:0px; width:'+$(document).width()+'px; height:'+$(document).height()+'px; "></div>';        
        bigImage = '<a href="#" id="ILbigImage" style="position: absolute; z-index:'+(options.Zindex+1)+'; display:none; border:none;"><img src="'+bigImageSrc+'" style="border:none;"  id="ILbigImageInner"/></a>'; 
        
        $(this).children('img').mouseover(function(){
            if( !$('#ILzoomButton').length ) {
                $('body').append(inlargeButton);   
                
                $('#ILzoomButton').live('click' , function(){
                    $(wrappedObject).css('visibility' , 'hidden');
                    $(this).hide();
                    
                    if( !$('#ILcloacBlock').length  ) {
                        $('body').append(cloakBlock);
                    }
                    $('#ILcloacBlock').fadeTo('fast', options.cloakOp);  
                    
                    $('#ILcloacBlock').live('click', function(){
                        $('#ILcloacBlock').fadeOut('fast');
                        $(wrappedObject).css('visibility' , 'visible');  
                        $('#ILbigImage').fadeOut('fast');
                    });  
                    
                    if( !$('#ILbigImage').length  ) {
                        $('body').append(bigImage);
                         
                        $('#ILbigImageInner').load(function(){    
                            
                            if( ($(window).height()/2 + $('html,body').scrollTop() - $('#ILbigImage').height()/2) > $('html,body').scrollTop() + 20   ) {
                               bigImageTop = ($(window).height()/2 + $('html,body').scrollTop() - $('#ILbigImage').height()/2);
                            } else {
                               bigImageTop =  $('html,body').scrollTop() + 20
                            }

                            if( ($(window).width()/2 - $('#ILbigImage').width()/2) > 20   ) {
                                bigImageLeft = ($(window).width()/2 - $('#ILbigImage').width()/2);
                            } else {
                               bigImageLeft = 20;
                            }

                            $('#ILbigImage').css('top', bigImageTop  );
                            $('#ILbigImage').css('left', bigImageLeft  );
                            $('#ILbigImage').fadeIn('fast');   
                            $('#ILcloacBlock').css('width', $(document).width());
                        });
                        
                    } else {
                        if( ($(window).height()/2 + $('html,body').scrollTop() - $('#ILbigImage').height()/2) > $('html,body').scrollTop() + 20   ) {
                           bigImageTop = ($(window).height()/2 + $('html,body').scrollTop() - $('#ILbigImage').height()/2);
                        } else {
                           bigImageTop =  $('html,body').scrollTop() + 20
                        }

                        if( ($(window).width()/2 - $('#ILbigImage').width()/2) > 20   ) {
                            bigImageLeft = ($(window).width()/2 - $('#ILbigImage').width()/2);
                        } else {
                           bigImageLeft = 20;
                        }

                        $('#ILbigImage').css('top', bigImageTop  );
                        $('#ILbigImage').css('left', bigImageLeft  );
                        $('#ILbigImage').fadeIn('fast');  
                        $('#ILcloacBlock').css('width', $(document).width());
                    }
                    
                    
                    
                    $('#ILbigImage').live('click', function(){
                        $('#ILcloacBlock').fadeOut('fast');
                        $(wrappedObject).css('visibility' , 'visible');  
                        $('#ILbigImage').fadeOut('fast');
                        return false;                        
                    });   
                    
                    return false;
                });
            
                $('#ILzoomButton').live('mouseover', function(){
                    $(this).show();
                });                
            }           
            
            $('#ILzoomButton').css('top', $(this).offset().top + 10 );
            $('#ILzoomButton').css('left', $(this).offset().left + 10 );
            $('#ILzoomButton').show();    
        });
        
        $(this).children('img').mouseout(function(){
            $('#ILzoomButton').hide();            
        });          
        
        $(window).resize(function(){
            $('#ILcloacBlock').fadeOut('fast');
            $(wrappedObject).css('visibility' , 'visible');  
            $('#ILbigImage').fadeOut('fast');
        });        
    }
})(jQuery);
