function changeOrderQuery() {
	$(".product_orderby").change(function() {
		var currentUrl = document.location.href;
		currentUrl = currentUrl.replace(/orden=\w*/i, '');
		currentUrl = currentUrl.replace(/[?&]$/, '');
		currentUrl = currentUrl.replace(/(.*\?)&/, "$1");
		currentUrl += (currentUrl.search(/\?/) != -1 ? '&' : '?');
		currentUrl += 'orden=' + $(this).val();
		document.location.href = currentUrl;
	});
}

function changeViewNumItems() {
	$(".view_page").change(function() {
		var currentUrl = document.location.href;
		currentUrl = currentUrl.replace(/ver=\w*/i, '');
		currentUrl = currentUrl.replace(/[?&]$/, '');
		currentUrl = currentUrl.replace(/(.*\?)&/, "$1");
		currentUrl += (currentUrl.search(/\?/) != -1 ? '&' : '?');
		currentUrl += 'ver=' + $(this).val();
		document.location.href = currentUrl;
	});
}

function sendToFriend(id) {
	/* POPUP OBJECT */
	popupObj = new ManagePopup({
		type : 'iframe',
		nameFile : 'popup/enviar-amigo/' + id + '/',
		container : 'pop_message_f',
		position : {width : 626, height : 485, left : -10}
	});
	popupObj.launchPopup();
} 


function requestProduct(id) {
	/* POPUP OBJECT */
	popupObj = new ManagePopup({
		type : 'iframe',
		nameFile : 'popup/solicitar-producto/' + id + '/',
		container : 'pop_message_c',
		position : {width : 626, height : 485, left : -10}
	});
	popupObj.launchPopup();
} 

function askForOffers() {
	//	SEND BULLETIN DATA
	$("#send_bulletin_signup").click(function() {
		var form  = document.getElementById("form_bulletin");
		var send  = Spry.Widget.Form.validate(form);
		var data = '';
		var curr_icon = '';
		var currObj = this.currentInstance;
		
		if(send) {
			$.post("popup/bulletin-signup", $("#form_bulletin").serialize(), function(data){
				if(data.status)
					form.reset();
				$("#msg_bulletin_status").html(data.msg);
			}, "json");
		}
	});
}

function contacUs () {
	//	SEND CONTACT DATA
	$("#contact_send").click(function() {
		sendData({
			content : 'popup_env_msg',
			form_f : 'contact_form',
			filename : 'contactenos/popup-contactenos',
			clear : true,
			action : 'contactUs'
		});
	});
}

function sendData (_arg) {
	var form  = document.getElementById(_arg.form_f);
	var send  = Spry.Widget.Form.validate(form);
	var data = '';
	var curr_icon = '';
	var currObj = this.currentInstance;
	
	if(send) {
		$.post("contactenos/send", $("#" + _arg.form_f).serialize(),function(data){
			if(data.status)
				form.reset();

			/* POPUP OBJECT */
			popupObj = new ManagePopup({
				type : 'layer',
				nameFile : _arg.filename,
				container : 'pop_message_p',
				position : {width : 626, height : 485, left : -10},
				msg : {place : _arg.content, txt : data.msg}
			});
			popupObj.launchPopup();
		}, "json");
	}
}

//Main Process

var popupObj = ''; 

$(function() {
	changeOrderQuery();
	changeViewNumItems();
	contacUs();
	askForOffers();
});

