// zoomi - A zoom for images ~ Sean Catchpole - Version 0.9
// Slightly modified by Fox for tourism.poltava.ua
(function($){

$.fn.zoomi = function(){
	$(this).filter("img").each(function(){
		if(!this.z){
			$(this).zoom1().mouseover(function(){$(this).zoom2().animate({opacity: "show"}, 200);});
		    $(this.z).mouseout(function(){$(this).animate({opacity: "hide"}, 200);}); }
			});
		return this;
	}

$.fn.zooomi = function(){
	$(this).filter("a").each(function(){
		//if(!$("#i"+$(this).attr("rel")).z){
			$(this).mouseover(function(){$("#i"+$(this).attr("rel")).zoom2().animate({opacity: "show"}, 200);});
		    $(this).mouseout(function(){
				$(".zoom2").animate({opacity: "hide"}, 200);
				});
			//}
			});
		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.alt || e.src)
    .css({'position':'absolute','z-index':'10'});
    $(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(); }
	var ww=xClientWidth()-$(e).offsetLeft()-(e.z.width-e.scrollWidth);
	//alert(ww);
	if (ww<40){
	    $(e.z).css({left:$(e).offsetLeft()-(e.z.width-e.scrollWidth)+(ww>0?ww*2-10:0)+'px'});
		}else{
	    $(e.z).css({left:$(e).offsetLeft()-(e.z.width-e.scrollWidth)/2+'px'});
		}
	$(e.z).css({top:$(e).offsetTop()-(e.z.height-e.scrollHeight)/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;
  if (!jQuery.browser.opera){
	  return e.offsetTop + $(e.offsetParent).offsetTop();
	}else{
	  return e.offsetTop + $(e.offsetParent).offsetTop()-60;
	}
}

$(function(){ $('img.zoomi').zoomi(); });
$(function(){ $('a.zooomi').zooomi(); });

})(jQuery);

