$(document).ready(
	function(){
		$("#news1").advice();
		window.setInterval("$('#news1').animate()", 7000);	
	}	
);
(function($){
	
	jQuery.fn.extend({
		advice: function(options){
			return this.each(function(){					
					
				var container = $(this);
					html = '<div class="bg"></div>\
   							<div class="sep"></div>';
					container.append(html);
					
					var advices = container.find(".advice");
					
					//Jesli jest wiecej nic jedna reklama
					if(advices.length > 1){
						
						var class_active = "class='nav_active'";
						html = '<div id="nav_step">';
						jQuery.each(advices, function() {
							html += '<a href="#" '+class_active+'></a>';
							class_active = "class='nav'";
						})
						html += '</div>';
						container.append(html);
						
						html = '<a href="#" class="nav_l"></a>\
    							<a href="#" class="nav_r"></a>';
						container.append(html);
					
						container.find("#nav_step a").click(function(){container.setAdvice($(this));return false;})
						container.find("a.nav_l").click(function(){container.nextAdvice();return false;})
						container.find("a.nav_r").click(function(){container.prevAdvice();return false})
						
						
					}		 				
				})
		},
		animate: function(){
			$(this).nextAdvice();
		},
		nextAdvice: function(){
			
			var step_active = $(this).find("a.nav_active");
			var step_active_index = $(this).find("#nav_step a").index(step_active);
			var advice = $(this).find(".advice:eq("+step_active_index+")");
			
			step_active.attr("class","nav");
			advice.hide();
			
			if(step_active.next().is("a.nav")){
				step_active.next().attr("class","nav_active");
				var el = advice.next().show();
			}
			else{
				$(this).find("a.nav:first").attr("class","nav_active");
				$(this).find(".advice:first").show();
			}
		},
		prevAdvice: function(){
			
			var step_active = $(this).find("a.nav_active");
			var step_active_index = $(this).find("#nav_step a").index(step_active);
			var advice = $(this).find(".advice:eq("+step_active_index+")");
			
			step_active.attr("class","nav");
			advice.hide();
			
			if(step_active.prev().is("a.nav")){
				step_active.prev().attr("class","nav_active");
				advice.prev().show();
			}
			else{
				$(this).find("a.nav:last").attr("class","nav_active");
				$(this).find(".advice:last").show();
			}	
		},
		setAdvice: function(obj){
			
			var old_step_active = $(this).find("a.nav_active");
			var old_step_active_index = $(this).find("#nav_step a").index(old_step_active);
			var old_advice = $(this).find(".advice:eq("+old_step_active_index+")");
			var new_step_active_index = $(this).find("#nav_step a").index(obj);
			old_step_active.attr("class","nav");
			old_advice.hide();
			
			$(this).find("#nav_step a:eq("+new_step_active_index+")").attr("class","nav_active");
			$(this).find(".advice:eq("+new_step_active_index+")").show();	
		}
	})
})(jQuery);