﻿//ticker
var ticker = {
	id: "",
	index: 1,
	timer: "",
	options: {
		speed: 1200,
		delay: 4000,
		max_text_length: 78
	},
	init:function(id){
		ticker.id = id;
		$(ticker.id).find("li").each(ticker.cropText).hide();
		$(ticker.id).show();
		ticker.prev = $(ticker.id).find("li").length -1;
		ticker.rotate();
		ticker.timer = window.setInterval("ticker.rotate()", ticker.options.delay);
		ticker.setHover();
	},
	rotate:function(){
		ticker.prev = ticker.index - 1;
		if(ticker.index >  $(ticker.id).find("li").length) ticker.index = 1;
		if(ticker.prev < 0) ticker.prev = $(ticker.id).find("li").length -1;
		$(ticker.id).find("li:nth-child("+ ticker.prev +")").hide();
		//$(ticker.id).find("li:nth-child("+ ticker.index +")").fadeIn(ticker.options.speed);
		$(ticker.id).find("li:nth-child("+ ticker.index +")").show("slide", {direction: "right"}, ticker.options.speed);
		ticker.index++;
	},
	setHover: function(){
		$(ticker.id).hover(
			function(){ticker.timer = clearInterval(ticker.timer);},
			function(){ticker.timer = window.setInterval("ticker.rotate()", ticker.options.delay);});
	},
	cropText: function(){
		var text = $(this).find(":not(span)").text();
		if(text.length > ticker.options.max_text_length) {
			var string1 = text.substr(0, ticker.options.max_text_length);
			var string2 = string1.substr(0, string1.lastIndexOf(" "));
			$(this).find(":not(span)").text(string2 + " ...");
		}
	}
}
