// zoomi - A zoom for images ~ Sean Catchpole - Version 0.9
// modified by PMS 72 - Sean Jeronimus

(function($){

$.fn.zoomi = function() {
	$(this).filter("img").each(function(){
		$(this).wrap('<div class="zoomwrapper"/>');
		var hoogte1 = $(this).height();
		$(this).parent('.zoomwrapper').css('height',hoogte1+'px');
			if(!this.z) {
			$(this).zoom1()
				.click(function(){
					$(this)
						.stop()
						.css({'display':'none'})
						.zoom2().css({'display':'inline-block'})
						.parent('.zoomwrapper')
							.animate({'height':$(this).next('.zoom2').height()+'px'},300)
							.addClass('clicked');
						$('#side').stop().animate({'marginLeft':'-180px'},300);
						$('#main, #mainwrap').stop().animate({'width':'1000px'},300,
							function(){
								var windowR = $(window).height();
								var mainR = $('#mainwrap').height() + 10;
								if(windowR > mainR){
									$('.eraf_b').css({"opacity":"0"});
									$('#eraf_b').animate({"display":"block","paddingLeft":0,'width':'980px','marginLeft':'-500px'},150);
								};
								if(windowR < mainR){
									$('.eraf_b').css({"opacity":"1","padding-left":0,'width':'1000px','margin-left':'-500px'});
									$('#eraf_b').css({"display":"none"});
								};
							});
						if(($('div.clicked').length >= 2)&&($('#close_all').length != 1)){
							$('<div id="close_all"><p>zoom out all images</p></div>')
								.appendTo('body')
								.css('opacity',0)
								.animate({'opacity':0},800)
								.animate({'opacity':.7},200)
								.hover(
									function(){
										$(this).stop().animate({"opacity": 1}, 100);
									}, function(){
										$(this).stop().animate({"opacity": .7}, 200);
									}
								).click(function(){
									$('.clicked').each(
										function(){
											var zwHeight = $(this).find('img.zoomi').height();
											$(this).find('img.zoomi').css({'display':'inline-block'})
												.next('img').css({'display':'none'});
											$(this).removeClass('clicked').stop().animate({'height':zwHeight},300)
										}
									);
									$('#side').stop().animate({'marginLeft':'0'}, 150);
									$('#mainwrap').stop().animate({'width':'980px'}, 150);
									$('#main').stop().animate({'width':'800px'}, 100,
										function(){
											var windowR = $(window).height();
											var mainR = $('#mainwrap').height() + 10;
											if(windowR > mainR){
												$('.eraf_b').css({"opacity":"0","padding-left":0,'width':'800px','margin-left':'-400px'});
												$('#eraf_b').animate({"display":"block","paddingLeft":"180px",'width':'800px'},150);
											};
											if(windowR < mainR){
												$('.eraf_b').css({"opacity":"1","padding-left":0,'width':'800px','margin-left':'-400px'});
												$('#eraf_b').css({"display":"none"});
											};
										});
									$(this).stop()
										.animate({'opacity':0},200, function(){
											$(this).remove();
										});
								});
						};
				});
			$(this.z)
				.click(function(){
					$(this)
						.stop()
						.css({'display':'none'});
					$(this).prev('.zoomi')
						.css({'display':'inline-block'});
					$(this).parent('.zoomwrapper')
						.animate({'height':hoogte1+'px'},150)
						.removeClass('clicked');
					if ($('div.clicked').length == 0){
						$('#side').stop().animate({'marginLeft':'0'}, 150);
						$('#mainwrap').stop().animate({'width':'980px'}, 150);
						$('#main').stop().animate({'width':'800px'}, 100,
							function(){
								var windowR = $(window).height();
								var mainR = $('#mainwrap').height() + 10;
								if(windowR > mainR){
									$('.eraf_b').css({"opacity":"0","padding-left":0,'width':'800px','margin-left':'-400px'});
									$('#eraf_b').animate({"display":"block","paddingLeft":"180px",'width':'800px'},150);
								};
								if(windowR < mainR){
									$('.eraf_b').css({"opacity":"1","padding-left":0,'width':'800px','margin-left':'-400px'});
									$('#eraf_b').css({"display":"none"});
								};
							});
					};
					if($('div.clicked').length < 2){
						$('#close_all').stop()
							.animate({'opacity':0},200, function(){
								$(this).remove();
							});
					};
				});
		}
	});
 return this;
}
$.fn.zoom1 = function() {
  $(this).each(function(){
    var e = this;
    $(e).css({'position':'relative'}).after('<img class="'+e.className+'">');
    e.z = e.nextSibling;
    $(e.z).removeClass("zoomi").addClass("zoom2").attr("src",e.alt || e.src)
    .css({'position':'relative','display':'none'});
    //$(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.scrollWidth)/2+'px',
    //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;
  return e.offsetTop + $(e.offsetParent).offsetTop(); }
**/
$(function(){ $('img.zoomi').zoomi(); });

})(jQuery);
