/* Desenvolvido por Guilherme Augusto Madaleno - zatec@ymail.com */
	/* Variáveis da página */

	var backButton = "div#back";
	var backImage = "http://www.zatecus.com/images/green/back.png";
	var backImageOn = "http://www.zatecus.com/images/back.png";
	var backButtonSpeed = 500;
	var forwardButton = "div#next";
	var forwardImage = "http://www.zatecus.com/images/green/next.png";
	var forwardImageOn = "http://www.zatecus.com/images/next.png";
	var loader = "div#loader";
	var monitorDisabled = false;
	var monitorSwitch = true;
	var screenshot = "div#screenshot";
	var star = "#star_";
	var starImage = "http://www.zatecus.com/images/green/star.png";
	var starImageOut = "http://www.zatecus.com/images/star.png";
	var stars = "div#stars img";
	var switchCount = 0;
	var switcher = false;
	var turnOff = "div#turnoff";
	var youtubeVideo = "http://www.youtube.com/v/a1Y73sPHKxw";
	var watchTimer = "";
	
	$(document).ready(function() 
	{
		
		/* Tooltip */
	
		$("#screenshot").tooltip
		({  
			track: true, 
			delay: 0, 
			showURL: false, 
			fade: 100 
		});
		
		/* Para ocultar, exibir e animar botões */
		
		if (id_back == '#') 
		{
			$(backButton).css('opacity', 0.5);
			$(backButton + ' img').attr('src', backImageOn);
			$(backButton + ' img').hover(function()
			{
				$(this).attr('src', backImageOn);
			}, function ()
			{
				$(this).attr('src', backImageOn);
			});
		}
		else
		{
			$(backButton + ' img').hover(function()
			{
				clearTimeout(watchTimer);
			}, function ()
			{
				watchTimer = setInterval(function()
				{
					$(backButton).animate
					({
						opacity: 0.2
					}, 100, function ()
					{
						$(backButton).animate
						({
							opacity: 1
						}, 300);
					});
				}, backButtonSpeed);
			});	
		}
		if (id_next == '#')
		{
			watchTimer = setInterval(function()
			{
				$(backButton).animate
				({
					opacity: 0.2
				}, 100, function ()
				{
					$(backButton).animate
					({
						opacity: 1
					}, 300);
				});
			}, backButtonSpeed);
			$(forwardButton).css('opacity', 0.5);
			$(forwardButton + ' img').attr('src', forwardImageOn);
			$(forwardButton + ' img').hover(function()
			{
				$(this).attr('src', forwardImageOn);
			}, function ()
			{
				$(this).attr('src', forwardImageOn);
			});
		}
			
		/* Para montar notas */			
		
		if (rate)
		{
			for (n = 0; n <= rate; n++)
			{
				$(star + n).attr('src', starImage);	
			}
		}
		
		/* Para efeito rollover nas estrelas de avaliação */
		
		$(stars).hover(function ()
		{
			if (!switcher)
			{
				var selectStar = this.id.replace('star_', '');
				for (n = 1; n <= selectStar; n++)
				{
					$(star + n).attr('src', starImage);	
				}
			}
		},
		function () 
		{
			if (!switcher)
			{
				for (n = 1; n <= 5; n++)
				{
					$(star + n).attr('src', starImageOut);	
				}
			}
		});	
		
		/* Para clique em uma das estrelas de avaliação */
		
		$(stars).click(function ()
		{
			if (!switcher)
			{
				var rateNumber = this.id.replace('star_', '');
				if (rateNumber)
				{
					$.ajax
					({
						type: "POST",
						url: "http://www.zatecus.com/ratings.json",
						data:
						{
							rate: rateNumber,
							type: 'www',
							id: id_source,
							rateid: rateID
						},
						dataType: "json",
						error: function (XMLHttpRequest, textStatus, errorThrown)
						{
							$.balloon('Ops :( Erro!');	
						},
						success: function (callBack)
						{
							switch (callBack.msg)
							{
								default:
									$.balloon('Ops :( Erro!');	
								break;
								case 'ok':
									switcher = true;
									$.balloon('Obrigado (:');
								break;
								case 'already_exists':
									switcher = true;
									$.balloon('Obrigado de novo (:');
								break;
							}
							if (callBack.rate)
							{
								for (n = 0; n <= callBack.rate; n++)
								{
									$(star + n).attr('src', starImage);	
								}
							}
						}
					});
				}						
			}
		});
		
		/* Para avançar e voltar entre páginas */
		
		$(document).keydown(function (e) 
		{
			if (!disableKeys)
			{
				if(!e) 
				{
					e = window.event;
				}
				switch(e.keyCode) 
				{
					case 37:
						if (id_back != '#')
						{
							
							$(backButton + ' img').attr('src', backImage);
							var shut = setInterval (function()
							{
								$(backButton + ' img').attr('src', backImageOn);
								clearTimeout(shut);
							}, 200);
							document.location = id_back;
						}
					break;
					case 39:
						if (id_next != '#')
						{
							
							$(forwardButton + ' img').attr('src', forwardImage);
							var shut = setInterval (function()
							{
								$(forwardButton + ' img').attr('src', forwardImageOn);
								clearTimeout(shut);
							}, 200);
							document.location = id_next;
						}
					break;
				}				
			}
		});
		
		/* Para clicar na screenshot e acessar o site */
		
		$(screenshot).click(function()
		{
			if (url)
				window.open(url);
			else
				document.location = 'http://www.zatecus.com/www.html';
		});
				
		/* Para desligar o monitor (incentivo à economizar energia?) */
		
		$(turnOff).click(function()
		{
			if (!monitorDisabled)
			{
				if (switchCount == 8)
				{
					$.message('Cuidado...<br />Assim voc&ecirc; pode queimar este monitor...');
					switchCount++;
				}
				else if (switchCount == 13)
				{
					$(screenshot).show(); 
					$(screenshot).html('<object width="482" height="295"><param name="movie" value="' + youtubeVideo + '?fs=0&amp;hl=en_US&amp;autoplay=1&amp;rel=0&amp;loop=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="wmode" value="transparent"></param><embed src="' + youtubeVideo + '?fs=0&amp;hl=en_US&amp;autoplay=1&amp;rel=0&amp;loop=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="482" height="295" wmode="transparent"></embed></object><div id="monitorGlass"></div>');
					monitorDisabled = true;
				}
				else
				{
					switchCount++;
				}
				if (monitorSwitch)
				{
					$(screenshot).fadeOut(50);
					$(turnOff + ' img').attr('src','http://www.zatecus.com/images/switch.gif');
					monitorSwitch = false;
				}
				else
				{
					$(screenshot).fadeIn(50);
					$(turnOff + ' img').attr('src','http://www.zatecus.com/images/blank.png');
					monitorSwitch = true;
				}
	
			}
		});
	});
									
