
//respuestas en los comentarios
function addToComments(text) {
    $('#comment').get(0).value = text + "\n" + $('#comment').get(0).value;
}
function addReference(id, name) {
    addToComments('<a href="#comment-' + id + '">@' + name + '</a>\n');
}

//citas en los comentarios
function citar(id) {
    addToComments('<blockquote>' + $('#mensaje-' + id).get(0).innerHTML + '</blockquote>');
}

jQuery(document).ready(function(){


    jQuery("#suscribete > input#nn").focus(function(){
        if(jQuery(this).attr("value") == "Nombre"){
            jQuery(this).attr("value", "");
        }
    });

    jQuery("#suscribete > input#nn").blur(function(){
        if(jQuery(this).attr("value") == ""){
            jQuery(this).attr("value", "Nombre");
        }
    });

    jQuery("#suscribete > input#ne").focus(function(){
        if(jQuery(this).attr("value") == "Email"){
            jQuery(this).attr("value", "");
        }
    });

    jQuery("#suscribete > input#ne").blur(function(){
        if(jQuery(this).attr("value") == ""){
            jQuery(this).attr("value", "Email");
        }
    });

    jQuery("#suscribete > input#np1").focus(function(){
        if(jQuery(this).attr("value") == "Localidad"){
            jQuery(this).attr("value", "");
        }
    });

    jQuery("#suscribete > input#np1").blur(function(){
        if(jQuery(this).attr("value") == ""){
            jQuery(this).attr("value", "Localidad");
        }
    });

    jQuery("#suscribete").submit(function(){
        var ok = true;

        if(jQuery("input#nn").attr("value") == "Nombre"
            || jQuery("input#nn").attr("value") == ""){
            ok = false;
        }

        if(jQuery("input#ne").attr("value") == "Email"
            || jQuery("input#nn").attr("value") == ""){
            ok = false;
        }

        if(jQuery("input#np1").attr("value") == "Localidad"
            || jQuery("input#nn").attr("value") == ""){
            ok = false;
        }

        if(ok == false){
            alert("Por favor, introduce todos los campos");
            return false;
        }

        return true;

    });
    
    
    jQuery(".your-city input").attr("value", mmjsCity);
    
    
    jQuery("a.camiseta").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none'
	});
    
    
    jQuery('form.tienda').html5form({
        
        async : false, // cancela el método de envío por defecto.
        responseDiv : '#respuesta', // define un contenedor para mostrar el resultado de la petición Ajax.
        allBrowsers: true
        
    });
    
    jQuery("a.comprar").click(function(e){
        
        var talla = '';
        var color = '';
        
        var linia = jQuery(this).parent().children('.ref').children('.span-ref').html()
        
        if(jQuery(this).parent().children('.tallas').children('select').children('option:selected').val() != undefined){
            talla = jQuery(this).parent().children('.tallas').children('select').children('option:selected').val();
            linia = linia + ' - ' + talla;
        }
        if(jQuery(this).parent().children('.color').children('select').children('option:selected').val() != undefined){
            color = jQuery(this).parent().children('.color').children('select').children('option:selected').val();
            linia = linia + ' - ' + color;
        }
        linia = linia + ' - ' + jQuery(this).parent().children('.precio').children('.span-precio').html();
        
        
        jQuery('#pedido').append('<input type="hidden" name="ids[' + i + ']" value="' + jQuery(this).parent().children('.id_prod').val() + '" />');
        jQuery('#pedido').append('<input type="hidden" name="colors[' + i + ']" value="' + color + '" />');
        jQuery('#pedido').append('<input type="hidden" name="tallas[' + i + ']" value="' + talla + '" />');
        jQuery('#pedido').append('<input type="hidden" name="precios[' + i + ']" value="' + parseFloat(jQuery(this).parent().children('.precio').children('.span-precio').html()) + '" />');
        jQuery('#pedido').append('<input checked="checked" class="productos" onclick="gastosf()" type="checkbox" name="prods[' + i + ']" value="' + linia + '" />' + linia + '<br />');
        prods++;
        i++;
        gastosf();
        jQuery('#content .post .entry form.tienda table tr.gastos').show();
        
    });
    

});
    
var prods = 0;
var gastos = 0;
var i = 0;

    
function gastosf(){
    prods = jQuery('input.productos:checked').length;
    
    if(prods == 1){
        gastos = 4;
    }else if(prods == 2){
        gastos = 6;
    }else if(prods == 3){
        gastos = 7;
    }else if(prods < 1){
        gastos = 0;
    }else{
        gastos = 7;
    }
    
    var total = 0;
    
    jQuery('input.productos:checked').each(function(index){
        
        var ii = parseInt(jQuery(this).attr('name').substr(6));
        total = total + parseFloat(jQuery('input[name="precios[' + ii + ']"]').val());
        
    });
    
    jQuery('#content .post .entry form.tienda table tr.gastos td span.g').html(gastos);
    total = total + gastos;
    jQuery('#content .post .entry form.tienda table tr.gastos td span.t').html(number_format(total, 2, '.', ''));
}

function number_format (number, decimals, dec_point, thousands_sep) {
    number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
    var n = !isFinite(+number) ? 0 : +number,
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;
        };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
}
