var Search = {
	initialize: function () {
		this.inputs = $('search').getChildren().associate(['words', 'send']);
		this.inputs.send.addEvent('click', this.submit.bind(this));

		this.inputs.words.addEvent('keyup', function (e) {
			var ev = new Event(e);
			if (ev.key == 'enter') {
				this.submit();
			}
		}.bind(this));
	},

	submit: function () {
		var words = this.inputs.words.value = this.inputs.words.value.trim();
		if (words.length > 2) {
			redirect(BASE.web + '?lng=' + lng.code + '&s=' + words.replace(/\s+/g, '+') + '&i=0/0/0');
		} else {
			alert('No menos de 3 letras');
		}
	}
};

var fShipping = {

	instantiate: false,

	initialize: function () {
		this.ref = $('fshipping');

		if (this.ref) {
			this.instantiate = true;

			this.els = this.ref.getElements('input,select,i,strong').associate(['ciudad', 'zipcode', 'estado', 'go', 'ups', 'usps']);
			this.els.go.addEvent('click', this.getShippingCost.pass([false], this));

			['ciudad', 'zipcode'].each(function (el) {
				this.els[el].addEvents({
					keyup: function (e) {
						var e = new Event(e);
						if (e.key == 'enter') {
							this.getShippingCost(false);
						}
					}.bind(this),
					focus: this.checkDefaultValue.pass([el, 'focus', this.els[el].value], this),
					blur: this.checkDefaultValue.pass([el, 'blur', this.els[el].value], this)
				});
			}, this);

			this.request = new Request({
				url: BASE.ajax + 'carrito.ajax',
				onSuccess: this.onGetShippingCost.bind(this),
				onFailure: onError
			});
		}
	},

	checkDefaultValue: function (el, event_type, default_value) {
		var current_value = this.els[el].value.clean();

		if (event_type === 'focus' && current_value === default_value) {
			this.els[el].value = '';
		}

		if (event_type === 'blur' && current_value === '') {
			this.els[el].value = default_value;
		}
	},

	validate: function (silent) {
		if (this.els.ciudad.value.clean() == '') {
			if (!silent) {
				alert(_lng.required);
				this.els.ciudad.focus();
			}

			return false;
		}

		if (this.els.estado.value == '') {
			if (!silent) {
				alert(_lng.required);
				this.els.estado.focus();
			}

			return false;
		}

		if (this.els.zipcode.value.clean() == '' || !this.els.zipcode.value.clean().test(/^[\d]{5}$/)) {
			if (!silent) {
				alert(_lng.required);
				this.els.zipcode.focus();
			}

			return false;
		}

		return true;
	},

	getShippingCost: function (silent) {
		if (this.validate(silent)) {
			var data = {
				monto: Carrito.total,
				ciudad: this.els.ciudad.value,
				codigoPostal: this.els.zipcode.value,
				estado: this.els.estado.value,
				country: this.els.estado.options[this.els.estado.selectedIndex].className
			};

			this.ref.addClass('loading');

			this.request.send({data:'action=GetShippingCost&type=header&lng=' + lng.code + '&data=' + encodeURIComponent(JSON.encode(data))});
		}
	},

	onGetShippingCost: function (response) {
		if (response === 'free') {
			this.els.ups.innerHTML = '0.00';
			this.els.usps.innerHTML = '0.00';

		} else if (!['false', 'null'].contains(response)) {
			var data;

			try {
				data = JSON.decode(response);
			} catch (e) {
				data = false;
			}

			if (data) {
				this.els.ups.innerHTML = moneda(data.ups, false);
				this.els.usps.innerHTML = moneda(data.usps, false);
			}
		}

		this.ref.removeClass('loading');
	}
};

window.addEvent('domready', function () {
	var navCats = $('navCats');

	if (navCats) {
		var activeMenu = navCats.getElement('.active');

		if (activeMenu) {
			var slideBox = activeMenu.getElement('ul');

			if (!slideBox && activeMenu.getParent().getPrevious()) {
				activeMenu.getParent().getParent().addClass('active');
				slideBox = activeMenu.getParent();
			}

			if (slideBox) {
				slideBox.setStyle('display', 'block');
			}
		}
	}

	// options disabled
	$$('option').each(function (el) {
		if (el.disabled && el.innerHTML == 'seleccione') {
			el.dispose();
		}
	});

	//busqueda
	Search.initialize();

	//shipping
	fShipping.initialize();
});
