$().ready(function(){
	
	if (!Array.prototype.forEach)
{
    Array.prototype.forEach = function (closure)
    {
        var context = arguments[1] || null;
        for (var i = 0; i < this.length; i ++)
        {
            arguments[0].call(context, this[i], i, this);
        }
    }
}
	Animate = {
	FrameTime: 13,
	Running: false,
	Speed: 100,
	Unit: 'px',
	
	elements: [],
	values: [],

	set: function(element, destination, speed) {
		speed = (typeof speed == 'undefined') ? this.Speed : speed;

		if (typeof element.attr("animateID") == 'undefined') {
			element.attr("animateID", this.elements.length);
			this.elements[this.elements.length] = element;
		}
		
		var steps = {};
		for (var key in destination) {
			var current = parseFloat(element.css(key));
			var distanse = parseFloat(destination[key]) - current;
			steps[key] = distanse / speed * Animate.FrameTime;
		}

		this.values[element.attr("animateID")] = {
				destination: destination,
				steps: steps
				};
		if (!this.Running) {	
			this.Running = setInterval("Animate.render();", this.FrameTime);
		}
	},

	render: function() {
		for (var idx in Animate.elements) {
			var element = Animate.elements[idx];
			var dst = Animate.values[idx].destination;
			var steps = Animate.values[idx].steps;
			
			for (var key in dst) {
				var current = parseFloat(element.css(key));
				if(current == 0){
					current = element.key;
				}
				var distanse = dst[key] - current;
				var step = steps[key];				
				if (Math.abs(distanse) > Math.abs(step)) {
					element.css(key, (current + step) + Animate.Unit);
					element.key = current + step;
					
				} else {
					element.css(key, dst[key] + Animate.Unit);
					element.key = dst[key];
				}				
			}
		}
	}
	}

	$("#logowanie label+input").each(function(){
		if($(this).attr("value")==''){
			$(this).attr("value", "Login...");
		}
	});
	$("#logowanie label+input").focus(function(){
		if(!$(this).attr("wartosc"))
		{
			$(this).attr("wartosc", $(this).attr("value"));
		}
		if($(this).attr("wartosc")==$(this).attr("value")){
			$(this).attr("value", "");
		}
	});
	$("#logowanie label+input").blur(function(){
		if(!$(this).attr("value")){
			$(this).attr("value", $(this).attr("wartosc"));
		}
	});
	
	$("#top ul.menu")
		.append('<span class="point"></span>')
		;
	
	$("#top ul.menu li")
		.append('<span class="hover"><span class="hover_inner"><span></span></span></span>')
		;
		
	$("#top ul.menu li")
		.each(function() {
			$(this).find('span.hover_inner span').css("width", $(this).width()-18);
		});
	var pointStartNum = $("#top ul.menu li").index($("#top ul.menu li."+$(".content").attr("id")));
	
	if(pointStartNum != -1){
	$("#top ul.menu li:nth-child("+(pointStartNum+1)+")")
		.addClass("active")
		.find('span.hover')
		.css({top: "70px"})
		;
		
	var pointStart = $("#top ul.menu li."+$(".content").attr("id")).width()/2-3;
	
	for (var i = 1; i<=pointStartNum; i++) {
		pointStart += $("#top ul.menu li:nth-child("+i+")").width();
	}
	pointStart = pointStart + 0.5;
	pointStart = parseInt(pointStart);
	if(navigator.userAgent.indexOf("IE")!=-1 && $(".content").attr("id")!="account" && $(".content").attr("id")!="frontpage"){
				pointStart++;
	}
	}
	else{
		pointStart = -10;
	}
	$("#top span.point").css("left", pointStart+"px");
	var li_active=false;
	
	$("#top ul.menu li").hover(
		function() {
			if ($(this).attr("class") == "active") {
				li_active=true;
			} else {
				li_active=false;
			}
			
			var pozycja = $("#top ul.menu li").index(this);
			var cel = $(this).width()/2-3;
			for (var i = 1; i<=pozycja; i++) {
				cel += $("#top ul.menu li:nth-child("+i+")").width();
			}
			cel = cel + 0.5;
			cel = parseInt(cel);
			if(navigator.userAgent.indexOf("IE")!=-1 && $(this).attr("class")!="account" && $(this).attr("class")!="frontpage"){
				cel++;
			}
			Animate.set($("#top span.point"), {left: cel}, 300);
			Animate.set($(this).find('span.hover'), {top: 70});
			
		}, function(e) { // wyjazd z pozycji menu
			Animate.set($(this).find('span.hover'), {top: 59});
			
		});
	
	$("#top ul.menu").hover(function() { // wjazd na menu
			// jesli nie na 1 pozycje to schowaj 1 pozycje
			if (li_active == false) {
				Animate.set($("#top ul.menu li.active").find('span.hover'), {top: 59});
			}
				
	}, function() { // wyjazd poza cale menu
			// rozwin 1 pozycje i cofnij kulke
			Animate.set($("#top ul.menu li.active").find('span.hover'), {top: 70});
			Animate.set($("#top span.point"), {left: pointStart}, 300);		
	});
	
	var partnerzyWidth=0;	
	$("#partnerzy ul a").each(function() { 
		partnerzyWidth += $(this).width()+10;
		$(this).css("height", ($(this).find("img").height()/2)+"px");
		$(this).css("margin-top", ((88-$(this).find("img").height()/2)/2)+"px");
	});
	if(partnerzyWidth>775){
		var partnerzyMargin = partnerzyWidth-775;
		var partnerzyTime = parseInt(partnerzyMargin/30)*1000;
		var backward = true;
		
		if (backward = !backward) {
				Animate.set($("#partnerzy ul"), {marginLeft: 0}, partnerzyTime);
			} else {
				Animate.set($("#partnerzy ul"), {marginLeft: -partnerzyMargin}, partnerzyTime);
			}
		
		setInterval(function(){
			if (backward = !backward) {
				Animate.set($("#partnerzy ul"), {marginLeft: 0}, partnerzyTime);
			} else {
				Animate.set($("#partnerzy ul"), {marginLeft: -partnerzyMargin}, partnerzyTime);
			}
		}, partnerzyTime+3000);
	}
	
	if ($("#slider").attr("id") == "slider") { // jesli strona glowna - ze sliderem
		$("#slider div:first-child").addClass("active");
		$("#slider li:first-child").addClass("active");
		if(/MSIE (5\.5|6)/.test(navigator.userAgent)){
			$("#slider div.active").get(0).runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+$("#slider div.active").css("background-image").replace('url(', '').replace(')', '').replace('"', '')+'", sizingMethod="crop")';
			$("#slider div.active").css("background-image", "none");
		}
		var slider;
		function run_slider() {
			slider = setInterval(function() {
				var id = parseInt($("#slider div.active").attr("id").substr(6))+1;
				
				$("#slider div.active")
					.fadeOut(400, function() {
						$("#slider div.active").removeClass("active");
					});
						
				$("#slider li.active")
					.removeClass("active")
					;
					
				if (id <= $("#slider").children("div").length) {
				
					$("#slider li.slide_"+id)
						.addClass("active")
						;
						
					$("#slider div#slide_"+id)
						.fadeIn(400, function() {
							$(this).addClass("active")
								;

						});
						
				} else {
				
					$("#slider li:first-child")
						.addClass("active")
						;
						
					$("#slider div:first-child")
						.fadeIn(400, function(){
							$(this).addClass("active")
								;
						});
				}
				
				
				setTimeout(function(){
				if(/MSIE (5\.5|6)/.test(navigator.userAgent) && $("#slider div.active").css("background-image")!='none'){
					$("#slider div.active").get(0).runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+$("#slider div.active").css("background-image").replace('url(', '').replace(')', '').replace('"', '')+'", sizingMethod="crop")';
					$("#slider div.active").css("background-image", "none");
				}
				}, 800);
				
				
			}, 5000);
		}
		
		run_slider();
		
		$("#slider ul li").click(function() {
			clearInterval(slider);
	
			$("#slider div.active")
				.fadeOut(400, function() {
					$("#slider div.active")
						.removeClass("active")
						;
				});
				
			$("#slider div#"+$(this).attr("class"))
				.fadeIn(400, function() { 
				$(this).addClass("active")
					;
					
					if(/MSIE (5\.5|6)/.test(navigator.userAgent) && $(this).css("background-image")!='none'){
						$(this).get(0).runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+$(this).css("background-image").replace('url(', '').replace(')', '').replace('"', '')+'", sizingMethod="crop")';
						$(this).css("background-image", "none");
					}
				});
				
			$("#slider li.active").
				removeClass("active")
				;
				
			$(this).addClass("active")
				;
				
			run_slider();
		});
	}
});
