/**
 * @copyright paravista media - Michael Hitzler 2011
 */

$(document).ready(function() {
        
		// init ticker
		load_ticker();
		
		$('.pwaliveticker-refreshbutton').click(function(){
			clearInterval(showtime);
            load_ticker();
            showtime = setInterval("load_ticker()", 5*60000);
		});
		
		showtime = setInterval("load_ticker()", 5*60000);

});


function load_ticker(){
        $.ajax({
			    url: "typo3temp/tx_pwaliveticker/" + tickerxml,
			    dataType: "xml",
			    cache: "false",
			    data: "stamp=" + new Date().getTime(),
				error: function(){
			        // Reload whole ticker in background if need to generate xml with a 10 sec delay!{
					setTimeout ( "re_generate_xml()", 10000 );
			    },
			    success: function(data){
					//alert ("reload" + window.location);
                    $(data).find('liveticker').each(function(){
			
                        var headline = $(this).find("headline").text();
						var tickertext = $(this).find("tickertext").text();
						var tickerstatus = $(this).find("status").text();
						var discipline = $(this).find("discipline").text();
						$('.pwaliveticker-headline').text(headline);
						$('.pwaliveticker-text').html(tickertext);
						$('.pwaliveticker-discipline').text("Current discipline: " + discipline);
						if (tickerstatus == 0) {
							clearInterval(showtime);
							$('.pwaliveticker-refreshbutton').css('background-color', '#999999');
						}
						if ($tickerscroller) {
							$tickerscroller.tinyscrollbar_update();
						}
									            
			        });
				
				}
		 });
}


function re_generate_xml() {

        $.ajax({
			    url: window.location,
			    cache: "false",
			    success: function(){
						load_ticker();
				}
		 });

}
