$(document).ready(function(){

	var IE6 = (navigator.userAgent.indexOf("MSIE 6")>=0) ? true : false;
	var IE7 = (navigator.userAgent.indexOf("MSIE 7")>=0) ? true : false;

	/****************************************/
	
	if(IE6 || IE7){}
	else{
		$('.menu > ul > li').hoverIntent({
			over: function(){
				$(this).children('ul').css('display', 'block');
			},
			out: function(){
				$(this).children('ul').css('display', 'none');
			},
			timeout: 100
		});		
	}

	/****************************************/

	//Mascara telefone
	
	$("#phone_comprar").mask("(999)9999-9999");
	$("#phone_parceiro").mask("(999)9999-9999");
	$("#phone_parceria").mask("(999)9999-9999");
	$("#phone_trabalhe").mask("(999)9999-9999");
	$("#phone_contato").mask("(999)9999-9999");
	$("#phone_fabrica").mask("(999)9999-9999");
	$("#phone_casos").mask("(999)9999-9999");
	/****************************************/

	// Logo de clientes na sidebar

	var numLogosSide = $(".logomarcas img").length;

	if(numLogosSide > 1){
		$(".logomarcas img").hide();
		$(".logomarcas img:eq(0)").show();

		setInterval(function(){
			$(".logomarcas img:visible").fadeOut('slow', function(){
				if($(this).next('img').length > 0)
					$(this).next('img').fadeIn('slow');
				else
					$(".logomarcas img:first").fadeIn('slow');
			});
		}, 5000);
	}

	/****************************************/
	
	$('.logo, .telefone, #fEsq, #fDir, .anythingSlider').supersleight();

	/****************************************/
	
	function formatText(index, panel) {
		return index + "";
	};
	
	$('.anythingSlider').anythingSlider({
        easing: "easeInOutExpo",        // Anything other than "linear" or "swing" requires the easing plugin
        autoPlay: true,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
        delay: 10000,                    // How long between slide transitions in AutoPlay mode
        startStopped: false,            // If autoPlay is on, this can force it to start stopped
        animationTime: 1000,             // How long the slide transition takes
        hashTags: true,                 // Should links change the hashtag in the URL?
        buildNavigation: true,          // If true, builds and list of anchor links to link to each slide
		pauseOnHover: true,             // If true, and autoPlay is enabled, the show will pause on hover
		startText: "Go",                // Start text
        stopText: "Stop",               // Stop text
        navigationFormatter: formatText() // Details at the top of the file on this use (advanced use)
    });
    
    $("#slide-jump").click(function(){
        $('.anythingSlider').anythingSlider(6);
    });

	/****************************************/
	
	$(".emailInput").focus(function(){if($(this).val() == 'email') $(this).val('');});
	$(".emailInput").blur(function(){if($(this).val() == '') $(this).val('email');});
	$(".loginInput").focus(function(){if($(this).val() == 'login') $(this).val('');});
	$(".loginInput").blur(function(){if($(this).val() == '') $(this).val('login');});
	$(".passInput").focus(function(){if($(this).val() == 'senha') $(this).val('');});
	$(".passInput").blur(function(){if($(this).val() == '') $(this).val('senha');});
	
	/****************************************/

	$('#tabs .tabMenu li a:eq(0)').click(function(){	
		$('#tabs .tabMenu li a:not(:eq(0))').removeClass('tabSelected');
		$(this).addClass('tabSelected');
		$('#caixa').show();
		$('#estoque, #financeiro').hide();
		return false;
	});
	$('#tabs .tabMenu li a:eq(1)').click(function(){	
		$('#tabs .tabMenu li a:not(:eq(1))').removeClass('tabSelected');
		$(this).addClass('tabSelected');
		$('#estoque').show();
		$('#caixa, #financeiro').hide();
		return false;
	});
	$('#tabs .tabMenu li a:eq(2)').click(function(){	
		$('#tabs .tabMenu li a:not(:eq(2))').removeClass('tabSelected');
		$(this).addClass('tabSelected');
		$('#financeiro').show();
		$('#caixa, #estoque').hide();
		return false;
	});

	/****************************************/
	
	$("a[rel='galeria']").colorbox();
	$("a[rel='video']").colorbox({iframe:true, innerWidth:425, innerHeight:350 });
	$("a[rel='animacao']").colorbox({iframe:true, innerWidth:800, innerHeight:600, scrolling: false });
	//$("a[rel='animacao']").colorbox({inline:true, href:"#videoApresentacaoWrapper"});
	$(".botaoComprar").colorbox({inline:true, href:"#formInline"});
	$(".botaoQueroSerParceiro").colorbox({inline:true, href:"#formInline2"});
	$(".botaoQueroOrcamento").colorbox({inline:true, href:"#formInline"});
	
	/****************************************/
	
	$(".logoItem img").each(function(){
		var innerH = $(this).innerHeight();
		var innerW = $(this).innerWidth();
		
		var link    = $(this).prev('a');
		var topLink = '100px';
		
		var bloco  = $(this).parent();
		var blocoH = $(bloco).innerHeight() - innerH;

		$(link).css('top', topLink);
	});
	
	/****************************************/
	
	//$(".logoLoop").cloop();
	
	/****************************************/
	
	$('#selectProdutos').selectbox();
	$('#selectUF, #selectUF2').selectbox({
		inputClass: 'selectbox2',
		containerClass: 'selectbox2-wrapper'
	});
	$("#selectCidade").selectbox({
		inputClass: 'selectbox3',
		containerClass: 'selectbox3-wrapper'
	});
	$("#selectAssunto").selectbox({
		inputClass: 'selectbox4',
		containerClass: 'selectbox4-wrapper'
	});
	
	/****************************************/
	
	$(this).find("input[type=file]").change(function(){
		$(this).parent().find(".fakeFile").val(this.value);
	});

	/****************************************/
	
	$("#comprarProduto, #queroParceiro, #queroOrcamento, #faleConosco, #trabalheConosco").submit(function(){
	
			
		var emptyFields = false;
		var errorEmail = false;
		var thisError = false;
		var error = false;

		$("#cboxLoadedContent").css({overflow: 'hidden'});
		
		console.log($(this).children().children().children().children().children("select, input, textarea"));
		
		
//		$(this).children("#comprarProduto input, #comprarProduto select, #queroParceiro input, #queroParceiro select, #queroOrcamento input, #queroOrcamento select, #faleConosco input, #faleConosco select, #faleConosco textarea, #trabalheConosco input")
		$(this).children().children().children().children().children("select, input, textarea").each(function(){
			var fieldName = $(this).attr('name');
			var fieldValue = $(this).val();

			if(fieldName != 'action[quero-comprar]' && fieldName != 'action[quero-ser-parceiro]' && fieldName != 'action[quero-um-orcamento]' && fieldName != 'action[cadastrar-mensagem]' && fieldName != 'action[cadastrar-curriculo]'){
				thisError = false;

				if($(this).val() == ''){
					emptyFields = true;
					thisError = true;
				}
				
				

				if(fieldName == 'fields[e-mail]'){	
					if(fieldValue.indexOf("@") == -1 || fieldValue.indexOf(".") < 4){
						errorEmail = true;
						thisError = true;
					}
				}
				
				
				if(fieldName == 'fields[estado]'){
					if(thisError == true){
						$("#selectUF_input, #selectUF2_input").addClass('errorBorder');
					}
					else {
						$("#selectUF_input, #selectUF2_input").removeClass('errorBorder');
					}
				}
				
				if(emptyFields == true || errorEmail == true){
					error = true;
					if(thisError == true){
						if(fieldName == 'fields[curriculo]')
							$(".fakeFile").addClass('errorBorder');
						else
							$(this).addClass('errorBorder');
					}
					else{
						if(fieldName == 'fields[curriculo]')
							$(".fakeFile").removeClass('errorBorder');
						else
							$(this).removeClass('errorBorder');
					}
				} else {
					$(this).removeClass('errorBorder');
				}
			}
		});

		if(error == true){
			return false;
		} else
			return true;
	});
	
	$("#formNewsletter").submit(function(){
		if($(".emailInput").val().indexOf("@") == -1 || $(".emailInput").val().indexOf(".") < 4){
			$(".emailInput").addClass('errorBorder');
			return false;
		} else
			$(".emailInput").removeClass('errorBorder');
	});
	
	/****************************************/
	
	if($("#formConfirma")){
		setTimeout(function(){
			$("#formConfirma").slideUp('slow');
		}, 5000);
	}
});
