$(function()
{
	var indexes_collection;
	var current_indexes = [-1,-1,-1];
	
	//onload
	initQuoteTimers();
	
	//function
	function initQuoteTimers()
	{
		$.get("spc_ajax.aspx?at=gfpqi", function(data)
		{
			var wfpqi = JSON.parse(data);
			indexes_collection = wfpqi.array;
			onQuoteSwapTimerWhat();
			onQuoteSwapTimerWho();
			onQuoteSwapTimerWhy();
		});
	}
	
	function getQuote(type)
	{
		var ri = getRandomNumber(indexes_collection[type].indexes.length);
		if (ri == current_indexes[type])
		{
			if ((ri + 1) >= indexes_collection[type].indexes.length) ri--;
			else ri++;
		}
		current_indexes[type] = ri;
		$.get("spc_ajax.aspx?at=gfpq&qid=" + indexes_collection[type].indexes[ri], function(data)
		{
			var wfpq = JSON.parse(data);
			setFrontQuote(type, wfpq);
		});
	}
	
	function setFrontQuote(type_id, data)
	{
		var tp = $("#spc_fq_qt" + type_id).parent().parent();
		tp.hide("fade", {}, 600, function()
		{
			$("#spc_fq_qt" + type_id).html("&#34;" + data.quote + "&#34;");
			$("#spc_fq_qt" + type_id + "c").text(data.customer_name + " | " + data.company_name);
			tp.show("fade", {}, 600);
		});
	}
	
	//timers
	function onQuoteSwapTimerWhat()
	{
		getQuote(0);
		setTimeout(onQuoteSwapTimerWhat, 14000);
	}
	
	function onQuoteSwapTimerWho()
	{
		getQuote(1);
		setTimeout(onQuoteSwapTimerWho, 23000);
	}
	
	function onQuoteSwapTimerWhy()
	{
		getQuote(2);
		setTimeout(onQuoteSwapTimerWhy, 18000);
	}
});