$().ready(function() {
	tempo_cidade();
	tempo();
	cotacoes();
	noticias_agricolas();
	link_top();
	galeria();
	combobox();
});

var array_cidade = Array('Erechim-RS', 'Aurea-RS', 'Erebango-RS', 'Estacao-RS', 'Jacutinga-RS', 'PauloBento-RS', 'QuatroIrmaos-RS', 'TresArroios-RS', 'Viadutos-RS');
var array_cidade_nome = Array('Erechim/RS', '&Aacute;urea/RS', 'Erebango/RS', 'Esta&ccedil;&atilde;o/RS', 'Jacutinga/RS', 'Paulo Bento/RS', 'Quatro Irm&atilde;os/RS', 'Tr&ecirc;s Arroios/RS', 'Viadutos/RS');
var cid = 0;

function tempo_cidade() {
	$.each(array_cidade, function(i, item) {
		$('<a href="#" class="cid mais" rel="' + i + '"></a>').html('&raquo; ' + array_cidade_nome[i]).appendTo("#tempo_cidade .cidades").click(function() {
			cid = i;

			tempo();

			return false;
		});

		if(i == 4)
			$('<span class="clear space"></span>').appendTo("#tempo_cidade .cidades");
	});

	$('<span class="clear"></span>').appendTo("#tempo_cidade .cidades");

	$('#tempo_cidade').hover(function() {
		$('>.mais', this).fadeOut(250);
		$('.cidades', this).fadeIn(250);
	}, function() {
		$('>.mais', this).fadeIn(250);
		$('.cidades', this).fadeOut(250);
	});
}

function tempo() {
	if(cid == array_cidade.length) {
		cid = 0;
	}

	$('#tempo').stopTime('tempo');

	$.ajax({
		url : config_url + 'web/system/php/tempo.php?callback=?',
		data : {cidade : array_cidade[cid]},
		dataType : 'json',
		success : function(res) {
			$("#tempo .lista").fadeOut(250, function() {
				$(this).html('');

				$('<div class="dia"></div>').html('').appendTo("#tempo .lista");
				$('<div class="dias"></div>').html('').appendTo("#tempo .lista");
				$('<div class="clear"></div>').html('').appendTo("#tempo .lista");

				$.each(res[0].tempo, function(i, item) {
					if(i == 0) {
						$('<div class="ico ' + item.ico + '"></div>').html("<h6>" + res[0].cidade + "/" + res[0].estado + "</h6><p style=\"font-style:italic;\">" + item.data + "</p><div class=\"space_h\"></div><p style=\"font-style:italic;\">" + item.situacao + "</p><div class=\"space_h\"></div><div class=\"temp\"><div class=\"min\">" + item.min + "</div><div class=\"max\">" + item.max + "</div><div class=\"chuv\">" + item.chuv + "<span>mm</span></div></div><div class=\"clear\"></div>").appendTo("#tempo .lista .dia");
					} else {
						$('<div class="ico ' + item.ico + '"></div>').html("<p style=\"font-weight:bold;\">" + item.data + "</p><div class=\"temp\"><div class=\"min\">" + item.min + "</div><div class=\"max\">" + item.max + "</div><div class=\"chuv\">" + item.chuv + "<span>mm</span></div><div class=\"clear\"></div></div>").appendTo("#tempo .lista .dias");

						if(i == 4) {
							$("<div class=\"clear\"></div>").appendTo("#tempo .lista .dias");
						}
					}
				});

				$('<p class="alignright"></p>').html('Fonte: <a href="http://tempoagora.com.br/previsaodotempo.html/brasil/' + array_cidade[cid - 1] + '/" target="_blank">Tempo Agora</a>').appendTo("#tempo .lista");

				$(this).fadeIn(250, function() {
					$('#tempo').oneTime(30000, 'tempo', function() {
						tempo();
					});
				});
			});
		}
	});

	cid++;
}

function cotacoes() {
	$('#cotacoes').stopTime('cotacoes');

	$.ajax({
		url : config_url + 'web/system/php/cotacoes.php?callback=?',
		dataType : 'json',
		success : function(res) {
			$("#cotacoes .lista").fadeOut(250, function() {
				$(this).html('');

				$('<div class="des titulo"></div>').html('CONTRATO (US$/bushel)').appendTo("#cotacoes .lista");
				$('<div class="pre titulo"></div>').html('PRE&Ccedil;O').appendTo("#cotacoes .lista");
				$('<div class="var titulo"></div>').html('VARIA&Ccedil;&Atilde;O').appendTo("#cotacoes .lista");

				$("<div class=\"clear\"></div>").appendTo("#cotacoes .lista");
				$("<hr />").appendTo("#cotacoes .lista");

				$.each(res[0].cotacao, function(i, item) {
					$('<div class="des ' + item.ico + '"></div>').html(item.descricao).appendTo("#cotacoes .lista");
					$('<div class="pre"></div>').html(item.preco).appendTo("#cotacoes .lista");
					$('<div class="var ' + item.ico + '"></div>').html(item.variacao).appendTo("#cotacoes .lista");

					$("<div class=\"clear\"></div>").appendTo("#cotacoes .lista");
					$("<hr />").appendTo("#cotacoes .lista");
				});

				$('<p class="aligncenter"></p>').html('<strong>&Uacute;ltima atualiza&ccedil;&atilde;o &agrave;s ' + res[0].hora + ' de ' + res[0].data + '</strong>').appendTo("#cotacoes .lista");

				$('<p class="alignright"></p>').html('Fonte: <a href="http://www.noticiasagricolas.com.br" target="_blank">Not&iacute;cias Agr&iacute;colas</a>').appendTo("#cotacoes .lista");

				$(this).fadeIn(250, function() {
					$('#cotacoes').oneTime(30000, 'cotacoes', function() {
						cotacoes();
					});
				});
			});
		}
	});
}

function noticias_agricolas() {
	$('#noticias_agricolas').stopTime('noticias_agricolas');

	var i = 0;

	$.ajax({
		url : config_url + 'web/system/php/noticias_agricolas.php',
		dataType : 'xml',
		success : function(res) {
			$("#noticias_agricolas .lista").fadeOut(250, function() {
				$(this).html('');

				$(res).find('item').each(function () {
					i++;

					if(i <= 5) {

					$('<p></p>').html('<a href="' + $('link', this).text() + '" target="_blank"><strong>' + format_time($('pubDate', this).text()) + 'h</strong> - ' + $('title', this).text() + '</a>').appendTo("#noticias_agricolas .lista");

						if(i < 5) {
							$("<hr />").appendTo("#noticias_agricolas .lista");
						}
					}
				});

				$('<p class="alignright"></p>').html('Fonte: <a href="http://www.noticiasagricolas.com.br" target="_blank">Not&iacute;cias Agr&iacute;colas</a>').appendTo("#noticias_agricolas .lista");

				$(this).fadeIn(250, function() {
					$('#noticias_agricolas').oneTime(30000, 'noticias_agricolas', function() {
						noticias_agricolas();
					});
				});
			});
		}
	});
}

function format_time(time_value) {
	var values = time_value.split(" ");
	var time = values[4].split(":");

	return time[0] + ':' + time[1];
}

function link_top() {
	$('body').css('backgroundColor', '#EAEAEA');

	$('input, textarea, select').hover(function() {
		$(this).parent().addClass('hover');
	}, function() {
		$(this).parent().removeClass('hover');
	});

	$('input, textarea, select').focus(function() {
		$(this).parent().addClass('focus');
	});

	$('input, textarea, select').blur(function() {
		$(this).parent().removeClass('focus');
	});

	$('.pag .back').hover(function() {
		$(this).stop(false, true).animate({backgroundPosition : "0% 0%"}, 150);
	}, function() {
		$(this).stop(false, true).animate({backgroundPosition : "50% 0%"}, 150);
	});

	$('.pag .next').hover(function() {
		$(this).stop(false, true).animate({backgroundPosition : "100% 0%"}, 150);
	}, function() {
		$(this).stop(false, true).animate({backgroundPosition : "50% 0%"}, 150);
	});

	$('.button').each(function() {
		var c = $(this).attr('class');
		$(this).removeClass('button').addClass('out').wrap('<span class="' + c + '"></span>');
		$(this).parent().append('<span class="over">' + $(this).text() + '</span>');
	});

	$('.button').hover(function() {
		$('.out', this).stop().animate({opacity : 0}, 250);
	}, function() {
		$('.out', this).stop().animate({opacity : 1}, 250);
	});

	$('.link_top').click(function() {
		$('html, body').stop(false, true).animate({scrollTop : 0}, 500);

		return false;
	});
}

function galeria() {
	Galleria.loadTheme(config_url + 'system/js/galleria/themes/classic/galleria.classic.js');

	$('.galleria').galleria({
		transition: 'fadeslide'
	});

	$("a[rel^='prettyPhoto']").prettyPhoto({
		animation_speed: 'fast',
		slideshow: 5000,
		autoplay_slideshow: false,
		opacity: 0.50,
		show_title: false,
		allow_resize: true,
		default_width: 640,
		default_height: 480,
		counter_separator_label: ' de ',
		theme: 'facebook',
		horizontal_padding: 20,
		hideflash: true,
		wmode: 'transparent',
		autoplay: true,
		modal: false,
		deeplinking: false,
		overlay_gallery: true,
		keyboard_shortcuts: true,
		ie6_fallback: true,
		social_tools: false
	});
}

function combobox() {
	var count = 0;

	$('select').each(function() {
		count++;

		var sel = $(this).parent();

		$(this).css({'opacity' : '0', 'position' : 'absolute', 'z-index' : '20'});

		$(this).after('<span class="sel" style="width:' + (parseInt($(this).innerWidth()) || 0) + 'px"><span class="val"></span></span>');

		$('.val', sel).html($('option:selected', this).text());

		$(this).change(function() {
			$('.val', sel).html($('option:selected', this).text());
		});
	});

	$('select.sel_estado').change(function(){
		var sel_cid = $("#" + $(this).attr('rel'));
		var sel = $(sel_cid).parent();

		$('.val', sel).html('Carregando...');

		$.getJSON(config_url + 'system/php/cidade.php?callback=?', {id: $(this).val()}, function(res){
			var options = '';

			for (var i = 0; i < res.length; i++) {
				options += '<option value="' + res[i].optVal + '">' + res[i].optDis + '</option>';
			}

			$(sel_cid).html(options);
			$('option:first', sel_cid).attr('selected', 'selected');

			$('.val', sel).html('');

			$(sel_cid).change(function() {
				$('.val', sel).html($('option:selected', this).text());
			});
		});
	});
}

