var sectionURL = "";
var includeDir = "/includes";
var tickerSpeed = 7000;
var tickerCount = 0;
var tickerItems = null;
var tickerURL = "/news.php";
var tickerLoad = 0;
var tickerFiles = ["/javascript/timer/jquery.timers.js","/javascript/query/jquery.query-2.0.1.js"]; // ,"/css/ticker_css.php"];
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

$(document).ready(function(){
	loadTicker();
});

function startsWith(str, substr){
	if(str.substring(0,substr.length) == substr) return(true);
	return(false);
}

function loadTicker(){
	if(tickerLoad >= tickerFiles.length){
		$.ajax({
		   type: "GET",
		   data: "data=ticker",
		   success: function(xml){
				tickerItems = $(xml).find('row');
				ticker();
		   }
		 });
	}else{
		var file = tickerFiles[tickerLoad];
		if(startsWith(file,"/css")){
			$.getCSS(includeDir+file);
			tickerLoad++;
			loadTicker();
		}else{
			$.getScript(includeDir+file, loadTicker);
			tickerLoad++;
		}
	}
}

function ticker(xml){
	sectionURL = $.query(document.location.search).get('section_url');
	var title = $(tickerItems[tickerCount]).find('news_title').text();
	var id = $(tickerItems[tickerCount]).find('news_id').text();
	var date = $(tickerItems[tickerCount]).find('news_release_date').text();
	$("#tickerTable").hide();
	$("#tickerLeft").html(tickerLeft(title, id));
	$("#tickerRight").html(tickerRight(date));
	$("#tickerTable").fadeIn('slow');
	tickerCount++;
	$("#ticker").everyTime(tickerSpeed,function(i){
		if(tickerCount >= tickerItems.length) tickerCount = 0;
		$("#ticker table").fadeOut('slow', function(){
			var title = $(tickerItems[tickerCount]).find('news_title').text();
			var id = $(tickerItems[tickerCount]).find('news_id').text();
			var date = $(tickerItems[tickerCount]).find('news_release_date').text();
			$("#tickerLeft").html(tickerLeft(title, id));
			$("#tickerRight").html(tickerRight(date));
			$("#tickerTable").fadeIn('slow');
			tickerCount++;
		});
		
	});
}

function tickerLeft(title,id){
	var html = "<a href='"+tickerURL+"?section_url="+sectionURL+"&news_id="+id+"'>"+title.replace(/&amp;/gi,"&")+"</a>";
	return(html);
}

function tickerRight(date){
	date = date.split("-");
	var html = months[Number(date[1]-1)]+" "+date[2]+", "+date[0];
	return(html);
}
