/* Desenvolvido por Guilherme Augusto Madaleno - zatec@ymail.com */
	/* jQuery liScroll - Gian Carlo Mingati */

	jQuery.fn.liScroll = function(settings) 
	{
		settings = jQuery.extend
		({
			travelocity: 0.07
		}, settings);
		return this.each(function()
		{
			var $strip = jQuery(this);
			$strip.addClass("newsticker");
			var stripWidth = 0;
			var $mask = $strip.wrap("<div class='mask'></div>");
			var $tickercontainer = $strip.parent().wrap("<div class='tickercontainer'></div>");								
			var containerWidth = $strip.parent().parent().width();
			$strip.find("li").each(function(i)
			{
				stripWidth += jQuery(this, i).outerWidth(true);
			});
			$strip.width(stripWidth);			
			var totalTravel = stripWidth+containerWidth;
			var defTiming = totalTravel/settings.travelocity;
			function scrollnews(spazio, tempo)
			{
				$strip.animate({left: '-='+ spazio}, tempo, "linear", function(){$strip.css("left", containerWidth); scrollnews(totalTravel, defTiming);});
			}
			scrollnews(totalTravel, defTiming);				
			$strip.hover(function()
			{
				jQuery(this).stop();
			},
			function()
			{
				var offset = jQuery(this).offset();
				var residualSpace = offset.left + stripWidth;
				var residualTime = residualSpace/settings.travelocity;
				scrollnews(residualSpace, residualTime);
			});	
		});			
	};
	
	/* Carrega Tweets */
	
	$.loadTweets = function (speedScroll)
	{
		$.ajax
		({
			url: "http://www.zatecus.com/index.json",
			type: "POST",
			data: 'tool=tweets',
			dataType: "json",
			success: function(callback)
			{
				switch (callback.status)
				{
					default:
					break;
					case 'ok':
						var tweets = callback.tweets;
						var tweetsList = "";
						if (tweets.length)
						{
							for (var i in tweets)
							{
								if (tweets[i].tweet)
									tweetsList += "<li><a href=\"http://twitter.com/zatecus/status/" + tweets[i].id + "\" target=\"_blank\" title=\"" + tweets[i].tweetFull + "\">" + tweets[i].tweet + "</a> <span>(" + tweets[i].date + ")</span></li><li id=\"bird\"><img src=\"http://www.zatecus.com/images/bird.png\" border=\"0\" alt=\"Tweet!\" /></li>";
							}
							if (tweetsList)
							{
								$("ul#tweets").slideDown(200);	
								$('ul#tweets').html('<li style="margin-right: 6px;"><strong>&Uacute;ltimos tweets:</strong></li>' + tweetsList); 
								setTimeout(function()
								{
									$("ul#tweets li").show();
									$("ul#tweets").liScroll
									({
										travelocity: speedScroll
									});
								}, 1000);
							}
						}
					break;
				}
			}
		});	
	};

	/* Carrega slideshow inicial */

	$(window).load(function() 
	{
		$('#slider').nivoSlider
		({
			effect:'fold',
			slices: 20,
			animSpeed: 500,
			pauseTime: 6000,
			controlNav: false,
			directionNav: false
		});
	});

 	$(document).ready(function() 
	{
		
		/* Carrega o Jingle inicial se permitido */
		
		if (enableJingle == 'on')
		{
			if ($.isNotIETrash())
			{
				var audioElement = document.createElement('audio');
				audioElement.setAttribute('src', "http://www.zatecus.com/sounds//jingle4.ogg");				
				audioElement.play();	
			}
		}
		
		/* Busca Tweets e inicia script de scroll */
		
		$.loadTweets(0.05);
		
		
	});

