/* ------------------------------------------------------------------------
	s3Slider
	
	Developped By: Boban Karišik -> http://www.serie3.info/
        CSS Help: Mészáros Róbert -> http://www.perspectived.com/
	Version: 1.0
	
	Copyright: Feel free to redistribute the script/modify it, as
			   long as you leave my infos at the top.
------------------------------------------------------------------------- */

(function($) {

	$.fn.startSlider = function(vars) {

		var timeOut = (vars.timeOut != undefined) ? vars.timeOut : 4000;
		var slideTime = (vars.slide != undefined) ? vars.slide : 1000;
		var current = -1;
		var mOver = false;
		var timeOutFn = null;
		var length = $(".slider").children().length;

		$(".nav-but").hover(function() {
			clearTimeout(timeOutFn);
			timeOutFn = null;
			slide($(".nav-but").index(this));
		});
		
		$(this).find('*').each(function(i) {
			$(this).mouseover(function() {
				mOver = true;
			});

			$(this).mouseout(function() {
				mOver = false;
				switchElement(true);
			});
		});

		var switchElement = function() {
			if (length > 1) {
				clearTimeout(timeOutFn);
				timeOutFn = setTimeout(function() {slide(current + 1)}, timeOut);
			}
		};

		var slide = function(index) {
			var calculatedIndex = index % length;
			if (current != calculatedIndex && !mOver) {
				$(".nav-but:nth-child(" + (current + 1) + ")").removeClass("current");
				current = calculatedIndex;
				$(".nav-but:nth-child(" + (current + 1) + ")").addClass("current");
				$(".slider").animate({top : -current * 291}, slideTime, switchElement);
			}
		}

		slide(0);

	};

})(jQuery);