/*
JMENU:

fade - czas zanikania obrazków z automatu
fast_fade - czas zanikania przy jezdzeniu myszka
timer - odstšp czasowy w automatycznej zmianie obrazków
images - iloµć obrazków w każdym dziale

JPANES:

opacity - przezroczystosc nieaktywnych obrazkow
fade - szybkosc animacji

JLAVA:

moving - szybkosc ruszania sie "hovera"
easing - styl animacji

	@JMENU, JPANES i JLAVA
	@autor: Radomir Rytych
	@www: http://rytych.pl
*/

(function( $ ){
$.fn.jmenu=function(options){
	var def={
		fade:500,
		fast_fade:250,
		timer:2500,
		images:[7,2,1,1,1]
		};
	
	var config=$.extend({},def,options);

	var $all=this;
	var $top=$("#topImg");
	$.each(this.children("li"),function(){
		
		var $a=$(this).children();
		var pos=$a.position();
		
		$b=$("<a/>",{
			href: $a.attr("href"),
			'class': $a.attr("class"),
			css:{
				position:'absolute',
				top:pos.top,
				left:pos.left,
				opacity:0,
				background:'url(/templates/novum/img/menu-top2.png)'
				}
			});
			$a.after($b);
		$b.mouseenter(function(){
				var i=$(this).parent().index();
				if (current_image==i) return;
				current_image=i;
				current_subimage=0;
				newImage(config.fast_fade);
				animator();
				light($(this),config.fast_fade);
			});
	
		});
	var timer=0;
	var deleteTimer=0;
	var current_image=0;
	var current_subimage=0;
	var zindex=0;
	var oldOne=null;
	function light(a,t){
		a.stop().fadeTo(t,1);
		if (oldOne!=null) oldOne.stop().fadeTo(t,0);
		oldOne=a;
		}
	function newImage(time){
		clearTimeout(deleteTimer);
		var a=['a','b','c','d','e'][current_image];
		var images=$top.children("img");
		var $new=$("<img/>",{src:"/user-files/slider/"+a+(current_subimage+1)+".jpg",css:{zIndex:(++zindex),opacity:0}});
		$top.prepend($new);
		$new.fadeTo(time,1);
		deleteTimer=setTimeout(function(){
			if (images.length>0) images.remove();
			$new.css({zIndex:0});
			zindex=0;
			},time+config.fade);
		}
	function animator(){
		clearTimeout(timer);
		timer=setTimeout(function(){nextStep();},config.timer);
		}
	function nextStep(){
		current_subimage++;
		if (current_subimage>config.images[current_image]-1){
			current_subimage=0;
			current_image++;
			if (current_image==config.images.length) current_image=0;
			light($all.children("li:eq("+current_image+")").children(),config.fade);
			}
			newImage(config.fade);
			animator();
		}
	//start
	newImage(1);
	light($all.children("li:first").children(),1);
	animator();
	};//end jmenu

$.fn.jpanes=function(options){
	var def={
		opacity:0.5,
		fade:200,
		timer:5000
		};
	
	var config=$.extend({},def,options);
	var $t=this;
	var $c=$t.children("li");
	$c.css({opacity:config.opacity});
	var timer;
	$c.hover(function(){
		clearTimeout(timer);
 		if (!$(this).hasClass("active")) 
			$(".active").jfade().removeClass("active");
			$(this).jshow().addClass("active");
		//	}
		},function(){anim();});
	$.fn.jshow=function(){
	this.stop().fadeTo(config.fade,1);return this;
		};
	$.fn.jfade=function(){
	this.stop().fadeTo(config.fade,config.opacity);return this;
	};
	function anim(){
		var $cur=$t.find(".active");
		$cur.jshow();
		timer=setTimeout(function(){
			$cur.jfade();
			$cur.removeClass("active");
			if ($cur.index()==$c.length-1) $t.children(":first").addClass("active");
			else $cur.next().addClass("active");
			anim();
			},config.timer);
	}
	$t.children(":first").addClass("active");
	anim();
}

$.fn.jlava=function(options){
	var def={
		moving:700
		};
	var config=$.extend({},def,options);
	var $all=this;
	var $hover=$("<span/>",{id:"hover",css:{left:$all.offset().left}});
	$('body').append($hover);
	function moveHover($i,forceTime){
		$hover.stop();
		var pos=$i.offset().top;
		var time=(typeof forceTime !="undefined")?forceTime:config.moving;
		$hover.animate({top:pos},time);
		}
	var first=$all.find(".current");
	if (first.length==1) moveHover(first,1);
	else moveHover($all.children("li a:first"),1);
	$all.children("li").children().mouseenter(function(){
		moveHover($(this));
		});
}
})( jQuery );



$(function(){
	$("#topMenu1").jlava();
	$("#promoMenu").jpanes();
	$("#topMenu2").jmenu();
});
