﻿Sorpreza = {
	log: function(parametro){
		console.log(parametro);
	}	
}
$(document).ready(function() {
	
	calcularMonto = function() {
		var cantidad = $('#item_quantity_1').val();
		var precio_envio_seleccionado = $('#carrito input:radio:checked').val();
		var precio_envio =  precio_envio_seleccionado / cantidad;
		
		if (cantidad > 1) {
			$('#shipping_type_1').val(2);
		} else {
			$('#shipping_type_1').val(1);
		}
		
		$('#shipping_cost_1_1').val(parseFloat(precio_envio_seleccionado).toFixed(2));
		$('#shipping_cost_2_1').val(precio_envio.toFixed(2));
	}
	
	if ( $("#shipping").length > 0 ) {
		$('#carrito input:radio').click(function(){
			calcularMonto();
		});
	}
	
	
	if ( $("#codigo").length > 0 ) {
		
		$("#carrito_especial").submit(function(){
			var error = false;
			
			if ($('#codigo').val() === '') {
				error = true;
			}
			
			if ($('#item_ammount_1').val() === '') {
				error = true;
			}
			
			if (error) {
				alert('Por favor complete los campos');
			}
			
			var patron = /^[.0-9]*$/;
			var number = $('#item_ammount_1').val(); 
			
			if (!patron.test(number)) {
				alert("Por favor solo ingrese Números en el Monto.");
				$(this).val('');
				error = true;
			}
			
			return !error; 
		});
		
		
		$("#item_ammount_1").blur(function(){
			var number = $(this).val();
			var patron = /^[.0-9]*$/;
			if (!patron.test(number)) {
				alert("Por favor solo ingrese Números en el Monto.");
				$(this).val('');
				return false;
			}
			
			if (number === '') {
				alert("Por favor no deje vacio el Monto.");
				$(this).val();
				return false;
			}
			
			number = parseFloat(number);
			
			
			$(this).val(number.toFixed(2));
		});
	}
	
	if ( $("#item_quantity_1").length > 0 ) {
		var original = '';
		
		$("#item_quantity_1").focus(function(){
			original = $(this).val();
		});
		
		$("#item_quantity_1").blur(function(){
			var number = $(this).val();
			var patron = /^[0-9_]*$/;
			if (!patron.test(number)) {
				alert("Por favor solo ingrese Números en la cantidad.");
				$(this).val(original);
			}
			
			if (number === '') {
				alert("Por favor no deje vacia la cantidad.");
				$(this).val(original);
			}
			calcularMonto();
		});
	}
	
	$("#botonera ul li a img").each(function(){
		Preload.execute($(this).attr("src"));
		$(this).mouseover(function(){
			var source = $(this).attr("src").replace('.jpg','_over.jpg');
			$(this).attr("src", source);
		});
		$(this).mouseout(function(){
			var source = $(this).attr("src").replace('_over.jpg','.jpg');
			$(this).attr("src", source);
		});
	});
	Preload.clear();
	$('#txtNewsletter').click(function (){$(this).attr('value', '');})
	$('#txtNewsletter').blur(function (){
		if($(this).attr('value') == ''){
			$(this).attr('value', 'Escriba su mail aquí.');
			$('#errores').css("display","none");
		}
	});
	if ( $("#fromContacto").length > 0 ){
		$("#fromContacto").validate({
		   messages: {
		     txtNombre: "Ingrese su nombre",
		     txtApellido: "Ingrese su apellido",
		     txtTelefono: {
				required: "Ingrese su telefono",
				number: "Ingrese solo numeros para el telefono"
			 },
		     txtConsulta: "Ingrese su comentario",
		     txtEmail: {
		       required: "Ingrese un mail",
		       email: "ingrese un mail valido"
		     }
		   }
		});
	}
$("img").noContext();	
	if ( $("#frmBuscar").length > 0 ){
		$("#frmBuscar").validate({
			errorLabelContainer: "#errores",
			errorElement: "p",
			rules: {
				txtNewsletter: {
					required: true,
					email: true
				}			
			},
			messages:{
				txtNewsletter: {
					required: "Ingrese un mail",
					email: "ingrese un mail valido"
				}
			},
			submitHandler:function(form){
				$.post("newsletter.php", { email: $('#txtNewsletter').val()},function(data){
					if(data == '0'){
						jAlert("Bienvenido al newsletter de Sorprezas.com.ar");
						$('#txtNewsletter').attr("disabled", true)
					}else{
						jAlert("Ya se encuentra registrado este email, por favor intentelo con otro.");
					}
				});
				return false;
			}
		});
	}
	
	
});
Preload = {
	contador:0,
	execute: function(src){
		$("#container").append("<div id='preload'>");
		$("#preload").append("<img>");
		var imagen = $("#container #preload img").get(Preload.contador);
		$(imagen).attr("src",src.replace('.jpg','_over.jpg'));
		Preload.contador++;
	},
	clear: function(){
		$("#container #preload").remove();	
	}
};
