/*
* Copyright (c) 2011 http://www.hotpages.co.il/
* "Jquery Horizontal Slide Ticker by types"
* Date: 2011-08-30
* Author: Nachliel Shilo
* Ver 1.0
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
var ticker; //The timer holds here
var inx = 0; //the index of the type of the news
var tickTxt;//The div of the text that moves
var newsType = new Array("YNET","ONE","NEWS");  //The types of the news
var ticSpeed = 10;
var dime = new Date();
var speed = 3;
var zero = dime.getTime();
var FPS = 30;
$(document).ready(starter);
//Set the default and start running
function starter()
{
	tickTxt = $('#ticker-' + newsType[inx]);
	//Check if there is text in the news fields
	if (tickTxt.html().length==null)
	{
		inx++;
		if (inx>newsType.length)
			return 0;
		starter();
		return 0;
	}
	//Set the style of the tic_Buttons
	$("#tic_Buttons A").each(function(i){
		$(this).css("background","url('../images/papa.png') -" + i*200 +"px 0px");
	});
	//Stop the text
	$(".tic").hover(function(){clearTimeout(ticker)},function(){dime = new Date();zero = dime.getTime();slideText()});
	
	loadNews();
}

//Load the next type of news or load specific type if num inserted - not needed if you're going to the next one.. 
function loadNews(num){
	clearTimeout(ticker);
	if (num==null)
		num=inx;
	
	tickTxt.css('top',50);
	if (num>newsType.length-1)
		num = 0;
	//Get the next news
	tickTxt = $('#ticker-' + newsType[num]);
	inx = num+1;
	//set the starting position
	tickTxt.css('top',0);
	tickTxt.css('left',-1*tickTxt.width());
	//Change the style of the tic_Buttons
	$("#tic_Buttons A").each(function(i){
		if (i==num)
			$(this).css("background","url('../images/papa.png') -" + i*70 +"px 50px");
		else
			$(this).css("background","url('../images/papa.png') -" + i*70 +"px 0px");
	});
	slideText();
}

//Slide the text. 25FPS 
function slideText() {
	//Move the news
	dime = new Date();
	ticSpeed = (speed*(FPS/(dime.getTime()-zero)));
	zero = dime.getTime();
	tickTxt.css('left', tickTxt.position().left + ticSpeed);
	//when the news copleted his cycle hidden:
	if (tickTxt.position().left>$('#mainTicker').width())
		loadNews();
	else
		ticker = setTimeout('slideText()', FPS);
}
