/*
 * Handling of tabs
 */
(function($) {
	$.tabhandling = function(element,switch_type,image_switcher_ul,fade_type) {
		
		// Setting image switcher ul as standard if nothing else has been specified
		if(!image_switcher_ul)
		{
			image_switcher_ul = "image_switcher";
		}
		
		// Setting no switcher arrows or numbers as standard
		if(!switch_type)
		{
			switch_type = "none";
		}
		
		// Setting no switcher arrows or numbers as standard
		if(!fade_type)
		{
			fade_type = "fade";
		}
		
		
		var tabhandling_interval = setInterval(function(){ switcher_timer(element,image_switcher_ul,fade_type); }, 10000);
		
		// Function for image switcher
		function switcher_timer(element,image_switcher_ul,fade_type)
		{
			// Adding 1 for next li
			next_li = parseInt($("ul." + image_switcher_ul + " li.active span").html())+1;
			
			// If next li not exists go to li 1
			if(next_li > $("ul." + image_switcher_ul+" li").size())
			{
				next_li = 1;
			}
			
			// Switching to correct tab
			switch_to_tab(element,$("ul."+image_switcher_ul+" li:nth-child(" + next_li + ") span"),fade_type);
			
			// Creating new timer
			clearInterval(tabhandling_interval);
			tabhandling_interval = setInterval(function(){ switcher_timer(element,image_switcher_ul,fade_type); }, 10000);
		}
		
		// Function for switching to the correct tab
		function switch_to_tab(element,switcher_li,fade_type)
		{
			if(fade_type == "fade")
			{
				// Show correct element
				$(element + " li").hide();
				$(element + " li:nth-child(" + switcher_li.html() + ")").fadeIn();
			}
			// If slider
			else if(fade_type == "slide")
			{
				new_leftmargin = -((parseInt(switcher_li.html())-1) * $(element + " li:first").outerWidth(true)) + "px";
				
				$(element).animate({
						    marginLeft: new_leftmargin
						  }, 700, function() {
						    	
						  });
			}
			
			// Setting active class on correct li
			$(switcher_li).parent().parent().find("li").removeClass("active");
			$(switcher_li).parent().addClass("active");
		}
		
		// Only show first element
		if(fade_type == "fade")
		{
			$(element + " li").hide();
			$(element + " li:first").show();
		}
		
		// Correct displaying
		if(fade_type == "slide")
		{
			$(element).css("width",($(element + " li").size() * $(element + " li").outerWidth(true))  + "px");
			$(element).parent().css("width",($(element + " li:first").outerWidth(true))  + "px");
		}
		
		// Dont display number switcher if number isnt set as switch type
		if(switch_type != "numbers")
		{
			extra_code = "style=\"display:none\"";	
		}
		else
		{
			extra_code = "";
		}
		
		// Creating menu for switching image
		after_content = "<ul class=\"" + image_switcher_ul + "\" " + extra_code + ">";
		for(i = 1; i <= $(element + " li").size(); i++)
		{
			after_content += "<li><span>" + i + "</span></li>";	
		}
		after_content += "</ul>";
		
		// Inserting menu
		$(after_content).insertAfter(element);
		
		// First li is active
		$("ul." + image_switcher_ul + " li:first").addClass("active");
		
		// Binding switcher buttons
		$("ul." + image_switcher_ul + " li").bind("click", function()
		{
			switch_to_tab(element,$(this).find("span"),fade_type);
		});
		
		// Inserting arrows if switcher type is arrows
		if(switch_type == "arrows")
		{
			$("<a class=\"link-prev\"><</a>").insertBefore(element);
			$("<a class=\"link-next\"><</a>").insertAfter(element);
			
			// Button listening for next and previous buttons
			$(element).parent().find("a.link-prev").bind("click",function()
			{
				// Subtracting 1 for prev li
				prev_li = parseInt($("ul." + image_switcher_ul + " li.active").html())-1;
				
				// If next li not exists go to li 1
				if(prev_li < 1)
				{
					prev_li = $("ul." + image_switcher_ul+" li").size();
				}
			
				// Switching to correct tab
				switch_to_tab(element,$("ul."+image_switcher_ul+" li:nth-child(" + prev_li + ")"));	
				
				// Creating new timer
				clearInterval(tabhandling_interval);
				tabhandling_interval = setInterval(function(){ switcher_timer(element,image_switcher_ul,fade_type); }, 15000);
			});
			
			$(element).parent().find("a.link-next").bind("click",function()
			{
				// Switching to next image
				switcher_timer(element,image_switcher_ul);
			});
		}
	}
})(jQuery);
