// zoomi - A zoom for images ~ Sean Catchpole - Version 0.9

(function($){
$.fn.zoomi = function() {
  $(this).filter("img").each(function(){
    if(!this.z) {
      //$(this).zoom1().mouseover(function(){$(this).zoom2().fadeIn(1500,$(this).pokaz_opis());});
      $(this).zoom1().mouseover(function(){
          $(this).zoom2();
          $(this).pokaz_opis();
          if (!this.z.dst_width){
                this.z.dst_width = $(this.z).width();
          }
          if (!this.z.dst_height){
                this.z.dst_height = $(this.z).height();
          }
          width = $(this).width();
          height = $(this).height();
          if ((this.z.dst_height <= height) && (this.z.dst_width <= width)){
                return false;
            }
          $(this.z).width(width);
          $(this.z).height(height);
          if (!this.z.dst_left){
                this.z.dst_left = $(this.z).css('left');
          }
          if (!this.z.dst_top){
                this.z.dst_top = $(this.z).css('top');
          }
          if(this.z.dst_left.indexOf('-')==0){
            this.z.dst_left='0px';
          }
          
          zoomi_description = this.opis;
          zoomi_image = this.z
          offsets = $(this).offset();
          $(this.z).css('left', offsets.left);
          $(this.z).css('top', offsets.top);
          
          if (!this.opis.src_height){
            this.opis.src_height = $(this.opis).height();
          }
          
          if ((this.opis_2) && (!this.opis_2.src_height)){
            this.opis_2.src_height = $(this.opis_2).height();
          }
          
          $(this.opis).height(this.opis.src_height);
          if (this.opis_2){
            $(this.opis_2).slideUp('slow',
                     function(){ 
                     $(zoomi_image).animate({'left': zoomi_image.dst_left,
                         'top': zoomi_image.dst_top,
                         'width': zoomi_image.dst_width,
                         'height': zoomi_image.dst_height},
                         1000,
                         'linear',
                         function(){$(zoomi_description).slideDown('slow');}
                     );}
                     );
          }else{
            $(zoomi_image).animate({'left': zoomi_image.dst_left,
                         'top': zoomi_image.dst_top,
                         'width': zoomi_image.dst_width,
                         'height': zoomi_image.dst_height},
                         1000,
                         'linear'
                     );
          }
          
      });
      //$(this).zoom1().mouseover(function(){$(this).zoom2().fadeIn("slow");});
      $(this.z).mouseout(function(){
                                    $(this).stop();       
                                    $(this.nextSibling).stop();                   
                                    $(this.nextSibling).hide(); $(this).hide();
                                    if (this.opis_2){$(this.opis_2).slideDown('slow');}
                                    })
      $(this).mouseout(function(){
                                      if (this.z.opis_2){
                                          $(this.z.opis_2).stop();
                                          $(this.z.opis_2).height(this.opis_2.src_height);
                                      }
                                  //    $(this.z.opis).hide();
                                });
                                  
    ;}
  });
 return this;
}
$.fn.zoom1 = function() {
  $(this).each(function(){
    var e = this;
    if ($(e).next().length > 0) {
        e.opis_2 = $(e).next();
    }

    dst = $('#zoomi_dst');
    if (dst.length > 0){
    	$(dst).after('<img class="'+e.className+'">');
    	e.z = dst[0].nextSibling;
    } else {
    	$(e).after('<img class="'+e.className+'">');
    	e.z = e.nextSibling;
    }
    if (e.opis_2){
        e.z.opis_2 = e.opis_2;
    }
    $(e.z).removeClass("zoomi").addClass("zoom2").attr("src",$(e).attr('rel') || e.src)
    .css({'position':'absolute','z-index':'60'});    
    $(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(); }
    if(!e.z.height) { $(e.z).show(); e.z.height=e.z.height; $(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];
  var new_offset = e.offsetLeft
  if(e.offsetParent){// return e.offsetLeft;
     new_offset =  e.offsetLeft + $(e.offsetParent).offsetLeft(); 
  }
  if(new_offset<0){
    new_offset=0;
  }
  return new_offset
}

$.fn.offsetTop = function() {
  var e = this[0];
  if(!e.offsetParent) return e.offsetTop;
  return e.offsetTop + $(e.offsetParent).offsetTop(); }

$.fn.przygotuj_opis = function() {
	return '<div>przygotuj opis</div>';
}

$.fn.pokaz_opis = function() {
    var s = [];
	this.each(function(){
		var e = this;
		if (!e.opis){
			$(e.z).after('<div class="zoomi_opis"><p>' + e.alt + '</p></div>');
			e.opis = e.z.nextSibling;
            s.push(e.opis);
		}
        $(e.opis).hide();
		$(e.opis).css({'position':'absolute', 'z-index':'61',
					   'width': e.z.width,
					   'left': $(e.z).css('left'),
                       //'height': e.opis.src_height,
					   'background-color': '#000',
                       'color': '#fff',
					   'top': parseInt($(e.z).css('top')) + e.z.height - 24});
		//$(e.opis).slideDown("slow");
		//$(e.opis).fadeIn("slow");
	});
    return this.pushStack(s);
}


$(function(){ $('.zoomi').zoomi(); });

})(jQuery);
(function($){
    $.fn.show_description = function() {
        $(this).each(function(){
            if (($(this).next().length > 0) & ($(this).next().attr('class') == 'description')){
                var e = this;
                e.photo_description = $(e).next();
                e.photo_title = $(e).prev();
                if (e.photo_title[0].tagName == "P"){
                    e.photo_title = $(e.photo_title).prev();
                }
                a = e;
                e.photo_description.src_height = $(e.photo_description).height();
                $(e.photo_description).hide();
                $(e.photo_title).children().mouseover(function(){
                    $(e.photo_description).stop();
                    $(e.photo_description).height(e.photo_description.src_height);
                    $(e.photo_description).slideDown('slow');
                });
                $(e.photo_title).children().mouseout(function(){
                    $(e.photo_description).stop();
                    $(e.photo_description).slideUp('slow');
                });
                
                /*
                $(e).mouseover(function(){
                    $(this).next().stop();
                    $(this).next().height(e.photo_description.src_height);
                    $(this).next().slideDown('slow');
                });
                $(e).mouseout(function(){
                    $(this).next().stop();                     
                    $(this).next().slideUp('slow');
                    e.photo_description.is_over = true;
                });
                $(e.photo_description).mouseover(function(){
                    $(this).stop();
                    $(this).height(e.photo_description.src_height);
                    //$(this).show();
                });
                $(e.photo_description).mouseout(function(){
                    $(this).stop();
                    $(this).slideUp('slow');
                });
                */
                
            }
        });
    }
$(function(){$('.zajawka_foto').show_description();});
})(jQuery);

