$(document).ready(function() {
	/*
	* Links externos
	*/
	$("a[rel=external]").attr("target","_blank");
	$("a[rel=external]").addClass ("external");	
	
	/*
	* Hover nos botoes IE6
	*/	
	if ($.browser.msie && $.browser.version<7){
		alert("teste");
		$("label.button input[type=submit]").mouseover(function () {
			if ($(this).attr("disabled") == ""){
				$(this).css("background-position","0 -37px");
			}
		});
		$("label.button input[type=submit]").mouseout(function () {
			if ($(this).attr("disabled") == ""){
				$(this).css("background-position","0 0");
			}
		});
	}

	/*
	* noticias destaque do header
	*/	
	$("div.list-news").easySlider({
		controlsShow: false, //Por padrão definido para true, false ele não irá adicionar qualquer tipo de controle.
		vertical: true, //true transição vertical. false transição horizontal
		speed: 500, //velocidade de transição
		pause: 5000, //milissegundos = 1000 -> 1s
		auto: true,
		continuous: true		
	});
	
	/*
	* Abas da HOME
	*/	
	$("#flowpanes").scrollable({size: 1, loop: false, clickable: false}).autoscroll({autoplay: false, interval: 5000 }).navigator({
		navi: "#flowtabs",// select #flowtabs to be used as navigator
		naviItem: 'a',// select A tags inside the navigator to work as items (not direct children)
		activeClass: 'current'// assign "current" class name for the active A tag inside navigator
	});
	
	/*
	* Image Zoom, Iframe
	*/
 	$("a.zoom").fancybox({ 
		'callbackOnShow' : function() {	
			$('ul#fancy_thumbs').html($('ul#thumbs').html());
			$('ul#fancy_thumbs li a').fancybox();
			$('a.zoom').fancybox({'overlayShow':true, 'zoomSpeedIn':600, 'zoomSpeedOut':500, 'easingIn':'easeOutBack', 'easingOut':'easeInBack'});
		},
		'overlayShow'	: true,
		'zoomSpeedIn'	: 600,
		'zoomSpeedOut'	: 500,
		'easingIn'		: 'easeOutBack',
		'easingOut'		: 'easeInBack'
	});	
	$("a.iframe").fancybox({
		'hideOnContentClick': false,
		'frameWidth': 800,
		'frameHeight': 600,
		'centerOnScroll': false
	});

	/*
	* Filtro por cidade
	*/
	$("form#form-filter select").change(function(){
		var name = $(this).attr('name');
		var url = $(this).attr('class');
		var value = $(this).val();
		if (value!=""){
			window.location.href = url+"?"+name+"="+value;
		} else {
			window.location.href = url;
		}
	});


	/*
	* Imagem de segurança
	*/
	$("a#reload-image").click(function(){
		$("img#secure-image").attr("src", $(this).attr("rel")+"/secure-image?sid=" + Math.random() );
	});	
	
	/*
	* Estado / Cidade
	*/
	var cityPos = 0;
	$("label.state select").change(function(){
		var pos = $("label.state select").index(this);
		if ($(this).val()==""){
			$( $("label.city").get(pos) ).children("input[type*=text]").attr("disabled", "disabled");
			$( $("label.city").get(pos) ).children("input[type*=text]").val("Selecione o Estado");
			$( $("label.city").get(pos) ).children("input[type*=hidden]").val("");
		} else {
			$( $("label.city").get(pos) ).children("input[type*=text]").attr("disabled", "");
			$( $("label.city").get(pos) ).children("input[type*=text]").val("");
			$( $("label.city").get(pos) ).children("input[type*=hidden]").val("");
		}
	});
	$("label.city input[type*=text]").focus(function(){
		cityPos = $("label.city input[type*=text]").index(this);
	});
	$("label.city input[type*=text]").keyup(function(){
		var pos = $("label.city input").index(this);
		$( $("label.city").get(pos) ).children("input[type*=hidden]").val("");
	});
	for (var i=0; i<$("label.state").length;i++){
		var selectState = $( $("label.state").get(i) ).children("select").get(0);
		var cityName = $( $("label.city").get(i) ).children("input[type*=text]").get(0);
		var cityId = $( $("label.city").get(i) ).children("input[type*=hidden]").get(0);
		if ($(selectState).val()==""){
			$(cityName).attr("disabled", "disabled");
			$(cityName).val("Selecione o Estado");
			$(cityId).val("");
		}
		$(cityName).autocomplete("fale-conosco", {
			extraParams: {
				stateId: function() {
					return $( $( $("label.state").get(cityPos) ).children("select").get(0) ).val(); 
				}
			},
			minChars: 0,
			max: 10,
			scrollHeight: 300,
			formatResult: function(data, value) {
				return value;
			}
		});	
		$(cityName).result(
			function(event, data, formatted) {
				var cityData = data.toString().split(",");
				$( $( $("label.city").get(cityPos) ).children("input[type*=hidden]").get(0) ).val(cityData[1]);
			}
		);	
	}

	/*
	* Google página não encontrada
	*/
	$("input#goog-wm-sb").attr("value","");
	$("input#goog-wm-sb").attr("title","Pesquisa Google");
	
	/*
	* Scrool de imagens
	*/
	$('ul#thumbs').jcarousel({ horizontal: true, scroll:3 });
	
	/*
	* Validações do formulário
	*/	
	$("form#form-contactus").validate();
	$("form#form-login").validate();
	$("form#form-remember-pass").validate();
	$("form#form-pesquisa").validate();
	$("form").submit(function () {
		if($(this).valid()){
			$($(this).find("label.button input[type=submit]")).attr("disabled","disabled");
			$($(this).find("label.button input[type=submit]")).addClass("disabled");
			return true;
		} else {
			return false;
		}
	});
	
	/*
	* Mensagens de erro ou sucesso
	*/	
	function displayMessage($alert) {
		var alerttimer = window.setTimeout(function () {
			$alert.trigger('click');
		}, 8000);
		$alert.animate({height:'83px'}, 800)
		.click(function () {
			window.clearTimeout(alerttimer);
			$alert.animate({height: '0'}, 800);
		});
	}
	if ($('div#exception').length){
		displayMessage($('div#exception'));
	}
	if ($('div#sucess').length){
		displayMessage($('div#sucess'));
	}
	
	/*
	* Mascáras nos campos
	*/
	setMask();
});

function unMask(){
	$(".txt-date").unmask();
	$(".txt-date-my").unmask();
	$(".txt-time").unmask();
	$(".txt-phone").unmask();
	$(".txt-cnpj").unmask();
	$(".txt-cpf").unmask();
	$(".txt-zipcode").unmask();	
}

function setMask(){
	$(".txt-date").mask("99/99/9999");
	$(".txt-date-my").mask("99/9999");
	$(".txt-time").mask("99:99");
	$(".txt-phone").mask("(99) 9999-9999");
	$(".txt-cnpj").mask("99.999.999/9999-99");
	$(".txt-cpf").mask("999.999.999-99");
	$(".txt-zipcode").mask("99999-999");
	$(".creditcard").mask("9999 9999 9999 9999");
	$(".valid-date").mask("99/99");
}