$(document).ready(function(){
	/**
	 * Mostrando el ...searching! antes de ejecutar una busqueda
	 */
	$("#searching_fancy").ajaxStart(function(){
		$(this).show();
	});
	
	/**
	 * Ocultando el ...searching! antes de ejecutar una busqueda
	 */
	$("#searching_fancy").ajaxStop(function(){
		$(this).hide();
	});
        
        function mostrar_menu() {
	    $("#menu-iframe").attr('src', $("#url_fancy").attr("href"));
	    $("#searching_fancy, #postal-code-modal").hide();
        }
        
	$(".modal-menu").click(function(e){
            $("#menu-iframe").attr("src", "about:blank");
            $("#searching_fancy").show()
            $("#menu-iframe").empty()
            e.preventDefault();
            pcs = $("#postal_code_selected").val()
			skp = $("#skip_postal").val()
            $("#url_fancy").attr('href', $(this).attr('href'));
            if (pcs) {
                mostrar_menu();
                show_modal("modal-menu");
            } else if (skp){
                mostrar_menu();
                show_modal("modal-menu");
			} else {
                show_modal("postal-code-modal");
            }
	});
    $("#postal-code-label").click(function () {
        $(this).css({'display':'none'});
        $("#postal-code-input").css({'display':'block'});
        $("#postal-code-input").focus()
    })


    $("#postal-code-input").blur(function () {
        if ($("#postal-code-input").val() == '') {
            $("#postal-code-input").css({'display':'none'});
            $("#postal-code-label").css({'display':'block'});
        } else {
            $("#postal-code-input").focus()
        }
    })
    
    function check_postal_code(elemento) {
        var url = $(elemento).attr('href')
        if ($("#postal-code-input").val() != '') {
            $("#postal-code-done").parent("div").fadeOut("200")
            regX = /[a-zA-Z]{1}[0-9]{1}[a-zA-Z]{1}$/
            if(!(regX).test($("#postal-code-input").val())) {
                if ($("#hiddenSelectedLanguage").val() == 'french' || $("#hiddenSelectedLanguage").val() == '_fr') {
                    alert('Invalid code postal')
                } else {
                    alert('Invalid postal code')
                }
		    $("#mask").fadeOut("400")
            } else {
                $.ajax({
                    url: url,
                    data: {'p': $("#postal-code-input").val()},
                    async: true,
                    success: function(data) {
                        if (data == 'TRUE') {
                            $("#postal_code_selected").val($("#postal-code-input").val())
                            mostrar_menu();
                            show_modal("modal-menu");
                        } else if (data == 'FALSE'){
                            if ($("#hiddenSelectedLanguage").val() == 'french' || $("#hiddenSelectedLanguage").val() == '_fr') {
                                alert('Nous sommes désolés, nous ne proposons pas de livraison services dans votre emplacement actuel')
                            } else {
                                alert('**We are sorry, we don’t offer delivery services in your current location')
                            }
                            $("#mask").fadeOut("400")
                        } else {
							var op = false;
                            if ($("#hiddenSelectedLanguage").val() == 'french' || $("#hiddenSelectedLanguage").val() == '_fr') {
                                if (confirm('Cette succursale ne livre pas votre code postal, mais nous avons un qui ne, voulez-vous le visiter?')) {
									location.href = data;
								} else {
									$("#mask").fadeOut("400")
								}
                            } else {
                                if(confirm('This store does not deliver in your postal code, but we have one that does, would you like visit it?')) {
									location.href = data;
								} else {
									$("#mask").fadeOut("400")
								}
                            }
						}
                    }
                })
            }
        } else {
            if ($("#hiddenSelectedLanguage").val() == 'french'||$("#hiddenSelectedLanguage").val() == '_fr') {
                alert("S'il vous plaît sélectionner un code postal")
            } else {
                alert('Please select a postal code')
            }
        }
        return false;
    }

    $("#postal-code-done").click(function (e) {
        e.preventDefault()
        check_postal_code(this)
    })
    
    $("#postal-code-input").keypress(function (e) {
        if ( e.which == 13 ) {
            e.preventDefault()
            $("#postal-code-done").trigger("click");
        }
    })
	
	$("#skip-postal").click(function (e) {
		e.preventDefault();
		var url = $(this).attr("href");
		$.ajax({
			url: url,
			async: true,
			success: function(data) {
				console.log(data);
			}
		});
		mostrar_menu();
		show_modal("modal-menu");
	})
})
