var articulo;
var cesta;
var letras;
var estampacion;
var precio;


var PERSONALIZACION = {
	
	init : function() {
	    document.getElementsByTagName('form')[0].reset();
		var filsets = document.getElementsByTagName('fieldset');
		for (i=0;i<filsets.length;i++) {
			if (filsets[i].className == 'oculto') {
				articulo = FUNCIONES.fromAbsurdoToNormal(filsets[i].getElementsByTagName('input')[0].value);
				conversion = FUNCIONES.fromAbsurdoToNormal(filsets[i].getElementsByTagName('input')[1].value.substring (0,4) );
				articulo2 = FUNCIONES.fromAbsurdoToNormal(filsets[i].getElementsByTagName('input')[2].value);
				precio = articulo;
			}
		}
		document.getElementById ( 'price0' ) .innerHTML = FUNCIONES.fromNormalToAbsurdo(articulo) + ' &euro;'; 
	    document.getElementById ( 'price1' ) .innerHTML = FUNCIONES.fromNormalToAbsurdo(articulo) + ' &euro;' ; 
	    document.getElementById ( 'price2' ) .innerHTML = '$' + (eval(articulo.replace(',','.'))*eval(conversion)).toFixed(2) ; 
	    if (articulo != articulo2) {
	        document.getElementById ( 'price3' ) .innerHTML = FUNCIONES.fromNormalToAbsurdo(articulo2) + ' &euro;' ; 
	        document.getElementById ( 'price4' ) .innerHTML = FUNCIONES.fromNormalToAbsurdo(articulo2) + ' &euro;' ; 
	    }
	    else {
	        document.getElementById ( 'price3' ) .innerHTML = '' ; 
	        document.getElementById ( 'price4' ) .innerHTML = '' ; 
	    }
		
	    jQuery('input.pers1.none').each(function() {
		    this.onclick = function() {
			    jQuery('div.datos-producto').removeClass().addClass('datos-producto').addClass(this.parentNode.className);
			    estampacion = 0.00;
				PERSONALIZACION.recalcularPrecio();
	        };
	    });
		jQuery('input.pers1.player').each(function() { 
		    this.onclick = function() {
			    jQuery('div.datos-producto').removeClass().addClass('datos-producto').addClass(this.parentNode.className);
				estampacion = ESTAMPACION_CAMISETA;
				PERSONALIZACION.recalcularPrecio();
	        };
	    });
	    jQuery('input.pers1.custom').each(function() {
		    this.onclick = function() {
			    jQuery('div.datos-producto').removeClass().addClass('datos-producto').addClass(this.parentNode.className);
                PERSONALIZACION.recalcularEstampacion(this);
				PERSONALIZACION.recalcularPrecio();
	        };
	    });
	    jQuery('input.pers2').each(function() {
	        this.onkeyup = function() {
	            PERSONALIZACION.recalcularEstampacion(this);
	        };
	    });
	},
	
	recalcularEstampacion : function(elt) {
	    if (jQuery(elt).hasClass('number') && !FUNCIONES.validaNumero(elt.value))
	        alert('Debes elegir un número válido para poder estampar la camiseta');
	    else {
	        var newVal = jQuery('input.pers2.name')[0].value.replace(/[^a-z0-9çñáéíóú\ ]+/i,'').toUpperCase();
	        jQuery('input.pers2.name').val(newVal);
	        letras = eval(jQuery('input.pers2.name')[0].value.replace(/[\ ]+/ig,'').length) + eval(jQuery('input.pers2.number')[0].value.length);
			estampacion = (letras*LETRA_ADICIONAL);
			var newPrice = FUNCIONES.fromNormalToAbsurdo(estampacion.toFixed(2)) + ' &euro;';
            jQuery('dd.price4').html(newPrice);
	    }
		this.recalcularPrecio ();
	},
	
	recalcularPrecio : function() {
		precio = FUNCIONES.fromNormalToAbsurdo((eval(articulo) + eval(estampacion)).toFixed(2));
		precioOrig = FUNCIONES.fromNormalToAbsurdo((eval(articulo2) + eval(estampacion)).toFixed(2));
		jQuery('dd#printingPrice').html(FUNCIONES.fromNormalToAbsurdo(estampacion.toFixed(2)) + ' &euro;');
		jQuery('dd#price1').html(precio + ' &euro;');
		if (articulo != articulo2) {
    		jQuery('dd#price4').html(precioOrig + ' &euro;');
		} else {
    		jQuery('dd#price4').html('');
		}
		jQuery('#price2').html((eval(precio.replace(',','.'))*eval(conversion)).toFixed(2) + ' $');
		PERSONALIZACION.recalcularTotal();
	},
	
	recalcularTotal : function() {
		if (!FUNCIONES.validaNumero(jQuery('input.qty')[0].value)) {
			jQuery('input.qty').val('1');
		} else {
			var cantidad = jQuery('input.qty').val();
			var total = FUNCIONES.fromNormalToAbsurdo((cantidad * FUNCIONES.fromAbsurdoToNormal(precio)).toFixed(2));
		}
	}

}

addLoadEvent(function() {
	PERSONALIZACION.init();
});
