/************************************** pk_accordion_gallery  **********************************************/	
(function($){ 
	var opts; 
	var objekt;
	var szerokosc;
	var elem_width;
	var elem_show_width;
	var ilosc;
	var trzymacz_width=0;
	$.fn.pk_accordion_gallery = function(options) {  
		opts = $.extend({}, $.fn.pk_accordion_gallery.defaults, options);
		objekt=$(this);
		nb_el=$(this).length;
		szerokosc=$(this).width();
		return this.each(function(i) {			
			$(this).pk_accordion_gallery_init();
			$(this).pk_accordion_gallery_click();
		});   
	}; 


	$.fn.pk_accordion_gallery_init = function(){
		ilosc = $(this).find("."+opts.elem_class).length;
		elem_width=szerokosc/ilosc;
		elem_show_width=szerokosc-((ilosc-1)*opts.min_width);
		$(this).wrapInner("<div class='trzymacz' style='overflow:hidden;position:absolute;top:0;left:0;background:"+opts.bg_trzymacz+";'></div>");
		
		$(this).find("."+opts.elem_class).css({"background":opts.bg_trzymacz});
		$(this).find("."+opts.elem_class).wrapInner("<div style='overflow:hidden;opacity:"+opts.elem_opacity+";'></div>");
		
		$(this).find("."+opts.elem_class).css({"position":"absolute"});
		if ( !$.browser.msie )
		{
			$(this).find("."+opts.elem_class+" .tytul h2").css({"width":0,"height":0});
			//$("b").clone().prependTo("p");
			//$(this).find("."+opts.elem_class+" .tytul").prependTo($(this).parent());
			//$(this).find("."+opts.elem_class+" .tytul").unwrap();
		}
		$(this).find("."+opts.elem_class).each(function(i) {	
			trzymacz_width+=$(this).width();
			//objekt.find(".trzymacz").width(trzymacz_width);
			$(this).css({"left":i*elem_width});
			if ( !$.browser.msie )
			{
				//$(this).find(".tytul").prependTo($(this));
				$(this).find(".tytul").appendTo($(this));
			}
		});

		//$(this).prepend("<div style='position:absolute;top:0;left:0;background:#000;width:"+trzymacz_width+"px;height:"+$(this).height()+";'></div>");
		$(this).find(".trzymacz").width(trzymacz_width);
		$(this).find(".trzymacz").height($(this).height());
		//alert(trzymacz_width);
		//alert("szerokosc: "+szerokosc+",elem_width: "+elem_width+", elem_show_width: "+elem_show_width+", ilosc: "+ilosc);
		//$(this).find("."+opts.elem_class).width(elem_width);
		//$(this).find("."+opts.elem_class).css({"opacity":opts.elem_opacity});	
	}
 

    $.fn.pk_accordion_gallery_click = function(){ 
	
		$(this).find("."+opts.elem_class).hover(
			function () {
				//$(this).stop().animate({width: elem_show_width,"opacity":1});
				//$(this).parent().find("."+opts.elem_class).not($(this)).stop().animate({width: opts.min_width,"opacity":opts.elem_opacity});
				$(this).children("div").not(".tytul").stop().animate({"opacity":1});
				$(this).parent().find("."+opts.elem_class).not($(this)).children("div").not(".tytul").stop().animate({"opacity":opts.elem_opacity});
				
				var index = $(this).index();
				var lewa;
				//alert(index);
				objekt.find("."+opts.elem_class).each(function(i) {	
					if(i<index)
					{
						lewa=i*opts.min_width;
					}
					else if(i>index)
					{
						lewa=(i-1)*opts.min_width+elem_show_width;
					}
					else
					{
						lewa=i*opts.min_width;
					}
					$(this).stop().animate({"left":lewa},500,opts.easing);
				});
				$(this).toggleClass(opts.hover_class);
				/*object.find("."+opts.elem_class).each(function(i) {	
					
				});*/				
				
			}, 
			function () {
				//$(this).parent().find("."+opts.elem_class).stop().animate({width: elem_width,"opacity":opts.elem_opacity});
				$(this).parent().find("."+opts.elem_class).children("div").not(".tytul").stop().animate({"opacity":opts.elem_opacity});
				
				objekt.find("."+opts.elem_class).each(function(i) {	
					$(this).stop().animate({"left":i*elem_width},500,opts.easing);
					//$(this).css({"left":i*elem_width});
				});
				$(this).toggleClass(opts.hover_class);
			}
		);
		
		$(this).find("."+opts.elem_class).click(function(event) {
			//alert("clicked: " + event.currentTarget);
			var index = $(this).index();
			var lewa;
			//alert(index);
			objekt.find("."+opts.elem_class).each(function(i) {	
				if(i<index)
				{
					lewa=i*opts.min_width;
				}
				else if(i>index)
				{
					lewa=(i-1)*opts.min_width+elem_show_width;
				}
				else
				{
					lewa=i*opts.min_width;
				}
				$(this).animate({"left":lewa},500,opts.easing);
			});
		});
	};
	

 	$.fn.pk_accordion_gallery.defaults = {
	   //nie uzywane
	   elem_class: "slide" 
	   ,min_width: 44
	   ,bg_trzymacz: "#000"
	   ,elem_opacity: 0.5
	   ,easing: "easeOutCubic"
	   ,hover_class: "hover"
	};
 
})(jQuery); 
/************************************** pk_accordion_gallery end *******************************************/
