if(typeof String.prototype.trim !== 'function') {
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, '');
  }
}

function idafter_(mystr) {return mystr.split('_')[mystr.split('_').length-1];}
function getLangFromUrl() {return window.location.pathname.split('/')[1];}
function build_catalogue() {
    $.getJSON('/'+getLangFromUrl()+'/catalogo/getAllCategorias', function(data) {
           var msg = 'Hay '+ data.length +' categorias';
           alert(msg);
    });
}
var elems;
$(document).ready(function() {
 $('#main-fade').moodular({effects:'fade',controls:'',auto:true,speed:4000});

/* Menú desplegable */
 $('.parent_cat a.menu').click(function(e) {
    e.preventDefault();
    var show = false;
    if($(this).parent().children('ul').is(':hidden'))
        show = true;
    $('ul.sc1').hide('fast');
    if(show)
        $(this).parent().children('ul').show('fast');
    return false;

 });

 $('ul.sc1 li a').click(function(e) {
     
    if($(this).parent().children('ul').children('li').size() == 0) return true;
    e.preventDefault();
    var show = false;
    if($(this).parent().children('ul').is(':hidden'))
        show = true;
    $('ul.sc2').hide('fast');
    if(show)
        $(this).parent().children('ul').show('fast');
    return false;
});
 /* Form contacto */
 var $frm_contacto = $('#frm_contacto');

 $frm_contacto.submit(function(e) {
    e.preventDefault();
    var errors = false;
    $('#frm_contacto :input').each(function() {
        var $elem = $(this);
        if($elem.attr('type') == 'submit' || $elem.attr('type') == 'hidden') return false;
        $elem.val($elem.val().trim());
        if($elem.val() == '') {
            alert($elem.attr('original')+' '+msgs.blank_err);
            $elem.focus();
            errors = true;
            return false;
        }
    });
    if(errors == true) return false;
    var regexpmail = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    var $cnt_email = $('#cnt_email');
    if(!regexpmail.test($cnt_email.val())) {
        alert(msgs.email_err);
        $cnt_email.focus();
        return false;
    }    
    $.post($frm_contacto.attr('action'),$frm_contacto.serialize(), function(data) {
        alert(data);
    });
    return false;
 });
 
 /* Distribuidores */
 $('#distcombocat').change(function(){     
     if($(this).val() != 0) {
         $.getJSON('/' + getLangFromUrl() + '/distribuidores/getpaises?categoria=' + $(this).val(), function(data) {           
            $('#distcombopais').html('<option value=""></option>'); 
            for(var i = 0; i < data.length; i++) {                
                $('<option value="' + data[i].key +'">'+data[i].value + '</option>').appendTo('#distcombopais');
            }
            $('#distpais').show();
         });
     }
 });
 
 $('#distcombopais').change(function() {
     if($(this).val() != '') {
         $.getJSON('/' + getLangFromUrl() + '/distribuidores/getdists?categoria='+$('#distcombocat').val()+'&pais=' + $(this).val(), function(data) {
            // Asumo que siempre hay 1 y solo 1
            if(data.length > 0) {
                var found = data[0];            
                $('#foundnombre').html('<b>' + found.nombre + '</b>');
                $('#foundcategoria').html(found.categoria);
                $('#foundpais').html(found.pais);
                $('#foundciudad').html(found.ciudad);
                $('#foundemail').html(found.email);
                $('#foundtelefono').html(found.telefono);
                if(found.foto != '') $('#foundfoto').html('<img src="/thumbs/distribuidores/'+found.foto+'/235-0-C"/>');
                else $('#foundfoto').html('');
            } else {
                $('#foundnombre').html('');
                $('#foundcategoria').html('');
                $('#foundpais').html('');
                $('#foundciudad').html('');
                $('#foundemail').html('');
                $('#foundtelefono').html('');
                $('#foundfoto').html('');
            }
            
            $('#distfound, #distformall').show();
            $('#formpais').val($('#distcombopais').val());
            $('#formcategoria').val($('#distcombocat').val());
         });
     }
 });
 
});
