// zoomi - A zoom for images ~ Sean Catchpole - Version 0.9
(function($){

$.fn.zoomi = function() {
  $(this).filter("img").each(function(){
    if(!this.z) {
    	var self = this
    	
    	$(this).parent('a').hover(
    			function(){$('.zoom2').hide(); $(self).zoom2().show();},
    			function(){$(self).zoom2().hide();})
      //$(this).zoom1().mouseover(function(){$('.zoom2').hide(); $(this).zoom2().show();});
      //$(this.z).mouseout(function(){$(this).hide();});  
    }
  }); 
 return this;
}

$.fn.zoom1 = function() {
  $(this).each(function(){
    var e = this;
    $(e).css({'position':'relative','z-index':'8'}).after('<img class="'+e.className+'">');
    e.z = e.nextSibling; 
    $(e.z).removeClass("zoomi").addClass("zoom2").attr("src", e.src || e.alt) 
    .css({'position':'absolute','z-index':'10', 'width': $(e).width() + $(e).width()/10 + width_img + "px", 'height': $(e).height() + width_img + $(e).height()/10 + "px"});
    
    $(e.z).hide();
  }); 
  return this;
} 
 
$.fn.zoom2 = function() {
  var s = [];
  this.each(function(){
    var e = this;
    if(!e.z) e = $(e).zoom1()[0]; s.push(e.z);
    if(!e.z.complete) return;
    if(!e.z.width) { $(e.z).show(); e.z.width=e.z.width; $(e.z).hide(); }
    $(e.z).css({left:$(e).offsetLeft()-( $(e.z).width() - $(e).width())/2+'px',
    top:$(e).offsetTop()-top_img-($(e.z).height()-$(e).height())/2+'px'});
  });
  return this.pushStack(s);
}

$.fn.offsetLeft = function() {
  var e = this[0];
  if(!e.offsetParent) return e.offsetLeft;
  return e.offsetLeft + $(e.offsetParent).offsetLeft(); }

$.fn.offsetTop = function() {
  var e = this[0];
  if(!e.offsetParent) return e.offsetTop;
  return e.offsetTop + $(e.offsetParent).offsetTop(); }

$(function(){ $('img.zoomi').zoomi(); });

})(jQuery);

