/*
 * Útils per botigues virtuals - Javascript 
 *
 * Copyright (c) 2008 Perception (perception.cat)
 * 
 *  Requereix llibreria jQuery!
 * 
 */

var afegint = false;
var al_vol = true; // Actualització en viu (true|false) -> sinó, envia a una URL 

var ms_entrar_caixa = 200;
var ms_mostrar_caixa = 1800;
var ms_sortir_caixa = 1000;

jQuery(document).ready(function(){
    
    // Per la llista del carro, incrementar i decrementar les unitats amb els botons
    jQuery(".unitats_menys").click( function( event ){
        event.preventDefault();
        var caixa = jQuery(this).parent("td").children("input.capsa");
        var valor = caixa.val();
        if ( valor > 0 )
            valor--;
        caixa.val(valor);  
    });
    
    jQuery(".unitats_mes").click( function( event ){
        event.preventDefault();
        var caixa = jQuery(this).parent("td").children("input.capsa");
        var valor = caixa.val();
        valor++;
        caixa.val(valor);    
    });
    
});

function afegeix_producte ( id_producte  ) {
    var quantitat = document.getElementById( 'ipt_unitats_' + id_producte ).value;
    if ( IsNumeric( quantitat ) )
    {
    
        
        if ( quantitat < 1 )
        {
            document.getElementById( 'ipt_unitats_' + id_producte ).value = 1;
            quantitat = 1;
        }
        
        // Si es vol actualitzar-ho al vol sense canviar de pàgina: (jQuery)
        
        if ( al_vol ) 
        {
            if ( afegint == false )
            {
                afegint = true;
                
                jQuery.get( "carro-afegir.php" , { ajax: 1, idioma: IDIOMA, producte: id_producte, unitats: quantitat } , function( data ){
                    data = data.split("|");
                    afegint = false;
                    // Es pot saber si ha anat bé o no a través de data[0] == 'ok' o 'ko'
                    if ( data[0] == 'ok' )
                    {
                         jQuery("#import-bossa").text(data[1]+" €").addClass("resaltat");
                         setTimeout( function(){ jQuery("#import-bossa").removeClass("resaltat") }, 500 );
                        
                        jQuery("#caixa_missatge").hide().slideDown( 500 );
                        
                    }
                    else
                        window.location = BASE_URL_IDIOMA + "/cabas/afegir/" + id_producte + "/quantitat/" + quantitat;
                });
            }
        }
        else
        {
            // Si no es vol l'actualització en viu, fer servir això
            window.location = BASE_URL_IDIOMA + "/cabas/afegir/" + id_producte + "/quantitat/" + quantitat;
            
        }
    }
    else
    {
        jQuery("#error_unitats").fadeIn(300);
        
    }

    
    return false;
}


jQuery(document).ready(function(){
    
    jQuery(".formulari_afegir").submit(function(event){
        event.preventDefault();
        
        var id_producte = jQuery(this).children("input.hidden_producte_id").val();
        
        afegeix_producte( id_producte );
        
        return false;
    });
    
});

function canviaComa( numero ) {
    out = ","; // canvia ,
    add = "."; // per .
    temp = "" + numero;
    while (temp.indexOf(out)>-1) {
        pos= temp.indexOf(out);
        temp = "" + (temp.substring(0, pos) + add + temp.substring((pos + out.length), temp.length));
    }
    return temp;
}
function descanviaComa( numero ) {
    out = "."; // canvia ,
    add = ","; // per .
    temp = "" + numero;
    while (temp.indexOf(out)>-1) {
        pos= temp.indexOf(out);
        temp = "" + (temp.substring(0, pos) + add + 
        temp.substring((pos + out.length), temp.length));
    }
    return temp;
}

function tallaDecimals ( numero , decimals ) {
    // Millor que arrodoneixi! 
    
    var multiplicador = 10;
    for ( i = 1; i < decimals ; i ++ )
        multiplicador *=10;
    
    
    return Math.round(numero*multiplicador)/multiplicador;
    
//     coma = ",";
//     temp = "" + numero;
//     if ( temp.indexOf(coma) > 0 )
//         return temp.substring( 0, ( temp.indexOf(coma) + 1 + decimals ) );
//     else
//         return numero;
}


function IsNumeric(sText)
{
    var ValidChars = "0123456789.";
    var IsNumber=true;
    var Char;
    
    
    for (i = 0; i < sText.length && IsNumber == true; i++) 
    { 
        Char = sText.charAt(i); 
        if (ValidChars.indexOf(Char) == -1) 
        {
            IsNumber = false;
        }
    }
    return IsNumber;
    
}

jQuery(document).ready(function(){
	
	jQuery("ul#productes-relacionats span.foto-producte").click(function(event){
		window.location = jQuery(this).parent().attr("href");
	});	
	
	jQuery("ul.productos-destacados span.foto-producte").click(function(event){
		window.location = jQuery(this).parent().attr("href");
	});		

});
