var valid_html = "http://validator.w3.org/check?uri="+location.href;
var valid_css = "http://jigsaw.w3.org/css-validator/validator?uri="+location.href;
var domain = 'lyte.com.uy';

$(document).ready(function(){
   
   $('a.mailto').each(function(){
       var texto = $(this).text();
       $(this).attr( 'href','mailto:'+texto+'@'+domain ).text(texto+'@'+domain)
   });
   
   
   $('a.valid_html').attr('href', valid_html);
   $('a.valid_css').attr('href', valid_css);
   $('.sidebar, .margin-right, .linea-extremo-left').css('height', $('.body-bg').height() );
   
   $('.menu, .images-slider').fadeIn();
   
   $('#slider').nivoSlider({
        effect:'fade', // Specify sets like: 'fold,fade,sliceDown'
        animSpeed:500, // Slide transition speed
        pauseTime:3000, // How long each slide will show
        startSlide:0, // Set starting Slide (0 index)
        directionNav:false, // Next & Prev navigation
        controlNav:true, // 1,2,3... navigation
        pauseOnHover:true // Stop animation while hovering
    });

    $('.sidebar ul li').hover(
        function(){
            $(this).children('a.parent').addClass('jquery');
        },
        function(){
            $(this).children('a.parent').removeClass('jquery');
        }
     );
         

     $('.sidebar').each(function(){
              
        $(this).find('a.current').each(function(){
           
           $(this).parents('li').each(function(){
               $(this).find('a:eq(0)').addClass('current');
           });
           
           $(this).parents('ul').removeClass('hide');

        });

        $(this).find('a[href="javascript:;"]').click(function(){
            $(this).find('a').removeClass('current');
            $(this).toggleClass('current').next('ul').slideToggle();
        });

     });
     
     $('.search input').keypress(function(event){
        if (event.keyCode) key = event.keyCode; 
        else if (event.which) key = event.which; 
        if( key == 13 ){ $('.search a').click() }    
     });
     
     $('.search a').click(function(){
         var keys = $('.search input').val();
         if( keys.length > 3 ){
           location.href = SITE_URL+'Busqueda/'+keys;  
         }
     });
     
     
     $('form').submit(function(event){
        
        var errorText = '';   
        var inputs = $(this).find('.required, .email, #recaptcha_response_field');
        var hasErrors = false;

        inputs.each(function() {
             var i = $(this);
             i.css('border-color','#787878');   

             if( i.hasClass('email') && !isValidMail(i.val()) ) {
                 i.css('border-color','#CC0000');
                 errorText +='* El Campo EMAIL contiene errores<br/>';
                 hasErrors = true;
             }
             else if( i.hasClass('required') && i.val().length < 1) {
                 i.css('border-color','#CC0000');
                 errorText +='* El Campo '+ i.attr('name').toUpperCase() +' es requerido<br/>';
                 hasErrors = true;
             }
             else if( i.attr('name') == "recaptcha_response_field" && i.val().length < 1 ){
                 i.css('border-color','#CC0000');
                 errorText +='* Escriba el c&oacute;digo de la im&aacute;gen.<br/>';
                 hasErrors = true;
             }

         });
         if(hasErrors){
             event.preventDefault();
             $('.error').html(errorText).fadeIn();
         }
         else{
            //$(this).submit();
         }

    });
     
     
     
     
});

function isValidMail(val) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   return reg.test(val);
}

function cartActions( action, id ){
    $.ajax({
		type	 : "POST",
        url      : SITE_URL+"ajax/cotizaciones.json.php",
        data 	 : 'action='+action+'&Id='+id,
        success	 : function(){
        	location.href = SITE_URL+'Mis_Cotizaciones/';
        }
	});
}

function removeProduct(id){
    if( confirm( 'Seguro que desea quitar este producto de la solicitud?' ) ){
        cartActions( 'remove', id );
    }
}


var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-18742838-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
