$(document).ready(function() {

	 $('#header').cycle({
                 fx: 'fade',
                 delay:  -5000,
                 timeout: 5000,
                 speed: 5000,
                 cleartype:  !$.support.opacity
            });

         //Banners Principais
             $('#rotator').cycle({
                 fx: 'fade',
                 delay:  -5000,
                 timeout: 5000,
                 speed: 5000,
                 pause: true,
                 pager: "#pager"
            });
            //PAtrocinios
            $('.patrocinador-box').cycle({
             fx: 'scrollDown',
             easeIn:  'bounceout',
             cleartype: !$.support.opacity,
             delay:  -5000,
             timeout: 5000,
             speed: 3000,
             pause: true
            });

             $("ul.menubar li").mouseover(function() { //When trigger is clicked...

			$(this).find("div.sub-menu").slideDown('fast').show(); //Drop down the subnav on click

			$(this).hover(function() {
			}, function(){
				$(this).find("div.sub-menu").slideUp('fast'); //When the mouse hovers out of the subnav, move it back up
			});

		/*
		$(this).parent().hover(function() {
		}, function(){
			$(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
		});
			*/
		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() {
			$(this).addClass("menuint"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$(this).removeClass("menuint"); //On hover out, remove class "subhover"
                });


                /*Noticias e Mais*/
                $('.noticia-box:even, .notice-wrapper:even, .comments-wrapper:even').css({background: "#555555"});
                $('.noticia-box:odd, .notice-wrapper:odd, .comments-wrapper:odd').css({background: "#343434"});
                $('.foto:odd').css({background: "#555555"});
                $('.foto:even').css({background: "#343434"});
                

               $(".tweet").tweet({
                    join_text: "auto",
                    username: "amilkartshow",
                    avatar_size: 48,
                    count: 5,
                    auto_join_text_default: "",
                    auto_join_text_ed: "",
                    auto_join_text_ing: "",
                    auto_join_text_reply: "",
                    auto_join_text_url: "",
                    loading_text: "Carregando nossas twitadas !!!"
                  });


                  //MODAL
                  $(".inline").fancybox({
                	'hideOnContentClick': false,
                        'autoDimensions': true
                   });

                  $("a.imgs").attr('rel', 'galeria-fotos').fancybox({
                            'showNavArrows'    : true,
                            'titlePosition'         : 'inside',
                            'transitionIn'	:	'elastic',
                            'transitionOut'	:	'elastic',
                            'speedIn'		:	600,
                            'speedOut'		:	200,
                            'overlayShow'	:	false,
                            'hideOnContentClick': true
                    });

                    //COMENTARIOS
                    $("#enviar-comentario-show-hide").click(function () {
                      $("#enviar-comentario").slideToggle("slow");
                    });

                     /*PILOTOS*/
                      $(".p-d-p-info").hide();
                      $("#perfil-dos-pilotos li a").click(function()
                      {
                       $(".p-d-p-info:visible").slideUp("slow");
                       $(this).next('table').slideDown("slow");
                        return false;
                      });

   
});

//Imagem load
function imgLoaddefault (x){
    var a = '<img src="images/load'+x+'.gif"/>';
    return a;
}

/*Mascara*/
function mascara(Field, m, e) {

    var Value = Field.value.replace( /\W/gi, "" ),i;

    for (i = 0; i < Value.length; i ++) {
        if (!/[#A9%]/.test(m.charAt(i))) {
            Value = Value.splice(i, 1, m.charAt(i) + Value.charAt(i));
        }
    }

    var ValueFormated = '';
    for (i = 0; i < Value.length; i ++) {
        if (
            (m.charAt(i) == "A" && /[a-z]/i.test(Value.charAt(i))) ||
            (m.charAt(i) == "9" && /[0-9]/.test(Value.charAt(i))) ||
            (m.charAt(i) == "%" && /[0-9a-fA-F]/.test(Value.charAt(i))) ||
            (m.charAt(i) == "#" && /\w/.test(Value.charAt(i)))
            ) {

            ValueFormated += Value.charAt(i);

        }else if (m.charAt(i) == Value.charAt(i)){
            ValueFormated += Value.charAt(i);
        }
    }

    if (ValueFormated.replace( /\W/gi, "" ) == ''){
        ValueFormated = '';
    }

    Field.value = ValueFormated;
}

String.prototype.splice = function (i, offset, str) {
    if (i < 0)
        i = this.length + i;

    return this.substr(0, i) + (str || "") + this.substr(i + offset, this.length)
}

/*///////// COMENTARIO ////////////*/
function enviarComentario(form) {
      var cont=0;
       $(form).find('input[type="text"], textarea').each(function(){

           if($(this).attr('id')=='email'){
                var vEmail = $(this).val();
                var eEmail = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
                    if($(this).val()  == ""){
                      $(this).next().html(' Obrigatório').fadeIn('slow');
                        cont = cont + 1;
                    }else if ( !(eEmail.test(vEmail))) {
                        $(this).next().html(' E-mail inválido').fadeIn('slow');
                        cont = cont + 1;
                    }else $(this).next().empty().fadeOut('slow');
           }else if($(this).attr('id')=='fone'){
               if( $(this).val() == '' ) {
                    $(this).next().html(' Obrigatório').fadeIn('slow');
                    cont = cont + 1;
                }else if ( $(this).val().length < 14) {
                    $(this).next().html(' Telefone inválido').fadeIn('slow');
                    cont = cont + 1;
                }else $(this).next().empty().fadeOut();
           }else{
               if( $(this).val() == '') {
                   $(this).next().html(' Obrigatório').fadeIn('slow');
                    cont = cont + 1;
                }else{
                    $(this).next().empty().fadeOut('slow');
                }
           }
       });

    if(cont <= 0) {
        //Envia Email
         var itemid,nome,email,msg;
         $(form).find('textarea, input').each(function()
        {
           id = $(this).attr('id');
           valor = $(this).val();
          /*Alimenta Vars*/
           if(id=='itemid') itemid = valor;
           if(id=='nome') nome = valor;
           if(id=='email') email = valor;
           if(id=='msg') msg = valor;
        });


         var imgload = imgLoaddefault('p');
         var url = '_adm/ajax/busca.php?opc=insert&itemid='+itemid+'&nome='+nome+'&email='+email+'&msg='+msg;
        $.ajax({
                url: url,
                dataType: 'html',
                type: 'GET',
                timeout: 10000,
                beforeSend: function(){
                    $('#comentario-retorno').fadeIn().empty().append(imgload+' Enviando...');
                },
                complete: function() {
                    $('#comentario-retorno').html(' Enviado com sucesso! <br /> Seu comentário será avaliado e liberado em instantes.');
                },
                success: function(data, textStatus) {
                     $(form).find('input[type="text"], textarea').each(function(){$(this).val('')});
                },
                error: function(xhr , er) {
                }
        });
        return false;

    }else return false;
}

/*///////// EMAIL ////////////*/
function enviarEmail(form) {
      var cont=0;
       $(form).find('input[type="text"], textarea').each(function(){
           if($(this).attr('id')=='email'){
                var vEmail = $(this).val();
                var eEmail = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
                    if($(this).val()  == ""){
                      $(this).parent().find('span').html(' Obrigatório').fadeIn('slow');
                        cont = cont + 1;
                    }else if ( !(eEmail.test(vEmail))) {
                        $(this).parent().find('span').html(' E-mail inválido').fadeIn('slow');
                        cont = cont + 1;
                    }else $(this).parent().find('span').empty().fadeOut('slow');
           }else if($(this).attr('id')=='fone'){
               if( $(this).val() == '' ) {
                    $(this).parent().find('span').html(' Obrigatório').fadeIn('slow');
                    cont = cont + 1;
                }else if ( $(this).val().length < 14) {
                    $(this).parent().find('span').html(' Telefone inválido').fadeIn('slow');
                    cont = cont + 1;
                }else $(this).parent().find('span').empty().fadeOut();
           }else{
               if( $(this).val() == '') {
                   $(this).parent().find('span').html(' Obrigatório').fadeIn('slow');
                    cont = cont + 1;
                }else{
                    $(this).parent().find('span').empty().fadeOut('slow');
                }
           }
       });
       
    if(cont <= 0) {
        //Envia Email
         var valor,id,nome,fone,email,assunto,msg;
         $(form).find('textarea, input').each(function()
        {
           id = $(this).attr('id');
           valor = $(this).val();
          /*Alimenta Vars*/
           if(id=='nome') nome = valor;
           if(id=='fone') fone = valor;
           if(id=='email') email = valor;
           if(id=='assunto') assunto = valor;
           if(id=='msg') msg = valor;
        });


         var imgload = imgLoaddefault('p');
         var url = 'paginas/contato-enviar.php?nome='+nome+'&assunto='+assunto+'&fone='+fone+'&email='+email+'&msg='+msg;
        
        $.ajax({
                url: url,
                dataType: 'html',
                type: 'GET',
                timeout: 10000,
                beforeSend: function(){
                    $(form).find('div.contato-retorno').fadeIn().empty().append(imgload+' Enviando...');
                },
                complete: function() {
                   $(form).find('div.contato-retorno').html(' Enviado com sucesso!');
                },
                success: function(data, textStatus) {
                   $(form).find('input[type="text"], textarea').each(function(){$(this).val('')});
                },
                error: function(xhr , er) {
                }
        });
        return false;

    }else return false;
}

/*///////// EMAIL ////////////*/

function validacao(form){
    var cont=0;
       $(form).find('input[type="text"]').each(function(){
           id = $(this).attr('id');
           valor = $(this).val();
           if(id=='email' || id == 'para' || id == 'de'){
                var vEmail = valor;
                var eEmail = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
                    if(valor  == ""){
                      $(this).css({background:'#f9f9f9'}).next('span').html('campo obrigatório').fadeIn('slow');
                      cont = cont + 1;
                    }else if ( !(eEmail.test(vEmail))) {
                        $(this).css({background:'#f9f9f9'}).next('span').html('e-mail inválido').fadeIn('slow');
                        cont = cont + 1;
                    }else $(this).css({background:'#f8faf0'}).next('span').html('').fadeOut('slow');
           }else if(id=='fone'){
               if( valor == '' ) {
                    $(this).css({background:'#f9f9f9'}).next('span').html('campo obrigatório').fadeIn('slow');
                    cont = cont + 1;
                }else if ( valor.length < 14) {
                    $(this).css({background:'#f9f9f9'}).next('span').html('telefone inválido').fadeIn('slow');
                    cont = cont + 1;
                }else $(this).css({background:'#f8faf0'}).next('span').html('').fadeOut('slow');
           }else{
               if( valor == '') {
                   $(this).css({background:'#f9f9f9'}).next('span').html('campo obrigatório').fadeIn('slow');
                    cont = cont + 1;
                }else $(this).css({background:'#f8faf0'}).next('span').html('').fadeOut('slow');
           }
       });

       return cont;
}


