(function($){
	$.fn.fadingThrough = function(options){
		
		var settings = {
			"time": 2000,
			"play_control": null,
			"dir": null,
			"left_arrow": null /* $("#news_navigation .left_arrow") */, 
			"right_arrow": null /* $("#news_navigation .right_arrow") */,
			"navigation_center": false
		}
		
		$.extend(settings, options);
		
		
		var current_index = 0;
		var length = this.length;
		var elements = $(this);
		
		elements.css("display", "none");
		elements.first().css("display", "block");
		
		generate_dots();
		add_events();
		
		if(settings["navigation_center"]){
			count_news_navigation_width();
		}
		count_width();
		count_height();
		//images_preload();
		
		var interval = setInterval(move_next, settings["time"]);
		is_playing = true;
		
		/*
		function images_preload(){
			
			if(settings["container"].find("img[loaded!=true]").first().length > 0){
				
				var src = settings["container"].find("img[loaded!=true]").first().attr("src");
				settings["container"].find("img[loaded!=true]").first().attr("src", settings["dir"]+src).load(function(){		
					settings["container"].find("img[loaded!=true]").first().attr("loaded", "true");
					images_preload();
				});
				
			}
			else {
				
				count_height();
			}
		}
		*/
		
		function generate_dots(){
			elements.each(function(index){
				var el = document.createElement('a');
				$("#dots").append(el);
			})
			
			$("#dots a").first().addClass("active");
		}
		
		function add_events(){
			if(settings["left_arrow"]){	
				settings["left_arrow"].click(function(event){
					event.preventDefault();
					refresh_interval()
					move_prev();
				});
			}
			
			if(settings["right_arrow"]){	
				settings["right_arrow"].click(function(event){
					event.preventDefault();
					refresh_interval()
					move_next();
				});
			}
			
			$("#dots a").each(function(index, el){
				$(el).click(function(){
					refresh_interval()
					move_to(index);
				});
			});
			
			if(settings["play_control"]){
				settings["play_control"].click(function(event){
					event.preventDefault();
					refresh_interval()
					toggle_play();
				});
			}
			
			$(window).resize(function() {
				count_width();
				count_height();
			});
		}
		
		function refresh_interval(){
			if(is_playing == true){
				clearInterval(interval);
				interval = setInterval(move_next, settings["time"]);
			}
		}
		
		function move_next(){
			var cur_el = current_index;
			if(current_index == length-1){
				current_index = 0;
			}
			else {
				current_index += 1;
			}
			
			animation(cur_el, current_index);
		}
		
		function move_prev(){
			var cur_el = current_index;
			if(current_index == 0){
				current_index = length-1;
			}
			else {
				current_index -= 1;
			}
			
			animation(cur_el, current_index);
		}
		
		function move_to(to){
			animation(current_index, to);
			current_index = to;
		}
		
		function toggle_play(){
			if(is_playing){
				is_playing = false;
				clearInterval(interval);
				settings["play_control"].addClass("play");
			}
			else {
				is_playing = true;
				interval = setInterval(move_next, settings["time"]);
				settings["play_control"].removeClass("play");
			}
		}
		
		function animation(prev, next){
			$("#dots a").removeClass("active");
			$($("#dots a").get(next)).addClass("active");
			$($(elements).get(prev)).fadeOut();
			$($(elements).get(next)).fadeIn();
		}
		
		function count_news_navigation_width(){
			var els = $("#news_navigation a");
			var width = 0;
			
			els.each(function(index, el){
				width += el.offsetWidth;
				width += parseInt($(el).css("margin-left"));
				width += parseInt($(el).css("margin-right"));
			});
			
			$("#news_navigation").css("width", width);
		}
		
		function count_width(){
			$(".news").width(settings["container"].width());
		}
		
		function count_height(){
			var heights = new Array();
			elements.each(function(index, el){
				heights.push($(el).height());
			});
			
			settings["container"].height(heights.max());
		}
	};
})(jQuery);


Array.prototype.max = function() {
	var max = this[0];
	var len = this.length;
	for (var i = 1; i < len; i++) if (this[i] > max) max = this[i];
	return max;
}

