﻿Type.registerNamespace('casapadova.realestate');

casapadova.realestate.sendToFriend = {
    send: function(form) {
        var data = $(form).serialize();
        $.ajax({
            type: "POST",
            url: "/RealEstate/SendToFriend",
            data: data,
            success: function(result) {
                if (result.success) {
                    casapadova.realestate.sendToFriend.hide();
                    $.growlUI('Messaggio Inviato', 'Il messaggio è stato spedito correttamente');
                } else {
                    casapadova.realestate.sendToFriend.hide();
                    $.growlUI('Errore', 'E\' avvenuto un errore nell\'invio del messaggio (' + result.reason + ')');
                }
            },
            error: function(req, status, error) {
                casapadova.realestate.sendToFriend.hide();
                $.growlUI('Errore', 'E\' avvenuto un errore nell\'invio del messaggio (' + error + ')');
            },
            dataType: "json"
        });
    },
    show: function() {
        $.blockUI({ message: $('#sendToFriend') });
    },
    hide: function() {
        $.unblockUI();
    }
}

casapadova.realestate.infoRequest = {
    blockingUI: {
        css: {
            width: "250px",
            border: '2px solid #FF8D34',
            padding: "5px"
        },
        overlayCSS: {
            backgroundColor: '#F0F0F0',
            opacity: '0.9'
        }
    },
    errorMessages: {
        "InfoRequest.FirstName": "Nome obbligatorio",
        "InfoRequest.LastName": "Cognome obbligatorio",
        "InfoRequest.PhoneNumber": "Telefono obbligatorio",
        "InfoRequest.Email": {
            required: "Email obbligatoria",
            email: "Email non valida"
        },
        "InfoRequest.PrivacyCheck": "Devi accettare l\'informativa sulla privacy"
    },
    send: function(submittedForm) {
        var validatedForm = $(submittedForm).validate({
            onkeyup: false,
            errorLabelContainer: "#errorList",
            wrapper: "li",
            showErrors: function(errorMap, errorList) {
                $("#serverErrorList").html();
                $("#messageBox").html("");
                var nums = this.numberOfInvalids()
                if (nums > 0) {
                    $(errorList).each(function(index, obj) {
                        $(obj.element).effect("pulsate", { times: 1 }, 300);
                    });
                    $("<a href='javascript:' onclick='casapadova.realestate.infoRequest.showErrors();' >" + "Ci sono "
                                       + this.numberOfInvalids()
                                       + " errori di compilazione." + "</a>").appendTo("#messageBox")
                    $("#messageBox").show();
                } else {
                    $("#messageBox").hide();
                }
                this.defaultShowErrors();
            },
            messages: casapadova.realestate.infoRequest.errorMessages
        });
        if ($(submittedForm).valid()) {
            $("#infoRequestBox").block({
                message: "<h4>Inviando...</h4>",
                css: casapadova.realestate.infoRequest.blockingUI.css,
                overlayCSS: casapadova.realestate.infoRequest.blockingUI.overlayCSS
            });
            var data = $(submittedForm).serialize();
            $.ajax({
                type: "GET",
                url: "/RealEstate/InfoRequest",
                data: data,
                success: function(result) {
                    if (result.success) {
                        validatedForm.resetForm();
                        $("#infoRequestBox").unblock();
                        $.growlUI('Messaggio Inviato', 'Il messaggio è stato spedito correttamente');
                    } else {
                        if (result.reason == "ValidationError") {
                            $("#serverErrorList").html("");
                            $(result.errors).each(function(index, error) {
                                var errorMessage = error.Message;
                                $("<li></li>").html(errorMessage).appendTo("#serverErrorList");
                            });
                            $("#serverErrorList").show();
                            $("#infoRequestBox").block({
                                message: $('#errorsBox'),
                                css: casapadova.realestate.infoRequest.blockingUI.css,
                                overlayCSS: casapadova.realestate.infoRequest.blockingUI.overlayCSS,
                                onUnblock: function() {
                                    $("#serverErrorList").html();
                                }
                            });
                        }
                        else {
                            $("#infoRequestBox").unblock();
                            $.growlUI('Errore', 'E\' avvenuto un errore nell\'invio del messaggio (' + result.reason + ')');
                        }
                    }

                },
                error: function(req, status, error) {
                    $("#infoRequestBox").unblock();
                    $.growlUI('Errore', 'E\' avvenuto un errore nell\'invio del messaggio (' + error + ')');
                },
                dataType: "json"
            });
        }
    },
    showErrors: function() {
        $("#infoRequestBox").block({
            message: $('#errorsBox'),
            css: casapadova.realestate.infoRequest.blockingUI.css,
            overlayCSS: casapadova.realestate.infoRequest.blockingUI.overlayCSS
        });
    },
    hideErrors: function() {
        $("#infoRequestBox").unblock();
    }
}

casapadova.realestate.showPrivacy = function() {
    $.blockUI({ message: $('#privacyStatement'), css: { width: "550px", centerY: false, top: "30px", focusInput: false} });
}

casapadova.realestate.hidePrivacy = function() {
    $.unblockUI();
}

casapadova.realestate.toggleOpenHouse = function(id) {
    $("#" + id).toggle("slide", { direction: "up" }, 250);
}

casapadova.realestate.tabify = function() {
    $('.roundedCorners').corners();
    $('#featureTabsContainer').show();

    $('#featureTabsContainer .tab').click(function() {
        $('#featureTabsContainer .tab').removeClass('tab_selected');
        $(this).addClass('tab_selected');
        $('#featureElementsContainer .feature').hide();
        $($('#featureElementsContainer .feature')).filter(this.hash).show();
        if (this.hash == "#map" && casapadova.realestate.vemap.isLoaded && casapadova.realestate.vemap.vecontrol != null) {
            if (!casapadova.realestate.vemap.showed) {
                casapadova.realestate.vemap.showed = true;
                casapadova.realestate.vemap.vecontrol.SetMapView(new VEMapViewSpecification(
					new VELatLong(casapadova.realestate.vemap.latitude, casapadova.realestate.vemap.longitude), casapadova.realestate.vemap.startingZoomLevel
				));
                casapadova.realestate.vemap.vecontrol.Resize();
                if (casapadova.realestate.vemap.pin) casapadova.realestate.vemap.pin.ShowIcon();
            }
        }
        return false;
    }).filter(":first").click();
}


casapadova.realestate.buyInfoRequest = {
    blockingUI: {
        css: {
            width: "250px",
            border: '2px solid #FF8D34',
            padding: "5px"
        },
        overlayCSS: {
            backgroundColor: '#F0F0F0',
            opacity: '0.9'
        }
    },
    errorMessages: {
        "InfoRequest.FirstName": "Nome obbligatorio",
        "InfoRequest.LastName": "Cognome obbligatorio",
        "InfoRequest.PhoneNumber": "Telefono obbligatorio",
        "InfoRequest.LocationZone": "Zona obbligatorio",
        "InfoRequest.PriceMin": { required: "Prezzo da obbligatorio", number: "Prezzo da non valido" },
        "InfoRequest.PriceMax": { required: "Prezzo a obbligatorio", number: "Prezzo a non valido" },
        "InfoRequest.RoomsMin": { required: "Camere / Locali obbligatorio", number: "Camere / Locali non valido" },
        "InfoRequest.Email": {
            required: "Email obbligatoria",
            email: "Email non valida"
        },
        "InfoRequest.PrivacyCheck": "Devi accettare l\'informativa sulla privacy"
    },
    send: function(submittedForm) {
        var validatedForm = $(submittedForm).validate({
            submitHandler: function(form) {
                form.submit();
            },
            onkeyup: false,
            errorLabelContainer: "#errorList",
            wrapper: "li",
            showErrors: function(errorMap, errorList) {
                $("#serverErrorList").html();
                $("#messageBox").html("");
                var nums = this.numberOfInvalids()
                if (nums > 0) {
                    $(errorList).each(function(index, obj) {
                        $(obj.element).effect("pulsate", { times: 1 }, 300);
                    });
                    $("<a href='javascript:' onclick='casapadova.realestate.buyInfoRequest.showErrors();' >" + "Ci sono "
                                       + this.numberOfInvalids()
                                       + " errori di compilazione." + "</a>").appendTo("#messageBox")
                    $("#messageBox").show();
                } else {
                    $("#messageBox").hide();
                }
                this.defaultShowErrors();
            },
            messages: casapadova.realestate.buyInfoRequest.errorMessages
        });
        if ($(submittedForm).valid()) {
            $("#infoRequestBox").block({
                message: "<h4>Inviando...</h4>",
                css: casapadova.realestate.buyInfoRequest.blockingUI.css,
                overlayCSS: casapadova.realestate.buyInfoRequest.blockingUI.overlayCSS
            });
            var data = $(submittedForm).serialize();
            $.ajax({
                type: "POST",
                url: "/InfoRequest/CreateBuy",
                data: data,
                success: function(result) {
                    if (result.success) {
                        validatedForm.resetForm();
                        $("#infoRequestBox").unblock();
                        $.growlUI('Messaggio Inviato', 'Il messaggio è stato spedito correttamente');
                        $(".infook").show();
                        $(".info").hide();
                    } else {
                        if (result.reason == "ValidationError") {
                            $("#serverErrorList").html("");
                            $(result.errors).each(function(index, error) {
                                var errorMessage = error.Message;
                                $("<li></li>").html(errorMessage).appendTo("#serverErrorList");
                            });
                            $("#serverErrorList").show();
                            $("#infoRequestBox").block({
                                message: $('#errorsBox'),
                                css: casapadova.realestate.buyInfoRequest.blockingUI.css,
                                overlayCSS: casapadova.realestate.buyInfoRequest.blockingUI.overlayCSS,
                                onUnblock: function() {
                                    $("#serverErrorList").html();
                                }
                            });
                        }
                        else {
                            $("#infoRequestBox").unblock();
                            $.growlUI('Errore', 'E\' avvenuto un errore nell\'invio del messaggio (' + result.reason + ')');
                        }
                    }
                },
                error: function(req, status, error) {
                    $("#infoRequestBox").unblock();
                    $.growlUI('Errore', 'E\' avvenuto un errore nell\'invio del messaggio (' + error + ')');
                },
                dataType: "json"
            });
        }
    },
    showErrors: function() {
        $("#infoRequestBox").block({
            message: $('#errorsBox'),
            css: casapadova.realestate.buyInfoRequest.blockingUI.css,
            overlayCSS: casapadova.realestate.buyInfoRequest.blockingUI.overlayCSS
        });
    },
    hideErrors: function() {
        $("#infoRequestBox").unblock();
    }
}


casapadova.realestate.sellInfoRequest = {
    blockingUI: {
        css: {
            width: "250px",
            border: '2px solid #FF8D34',
            padding: "5px"
        },
        overlayCSS: {
            backgroundColor: '#F0F0F0',
            opacity: '0.9'
        }
    },
    errorMessages: {
        "InfoRequest.FirstName": "Nome obbligatorio",
        "InfoRequest.LastName": "Cognome obbligatorio",
        "InfoRequest.PhoneNumber": "Telefono obbligatorio",
        "InfoRequest.LocationZone": "Zona obbligatorio",
        "InfoRequest.PriceMin": { required: "Prezzo da obbligatorio", number: "Prezzo da non valido" },
        "InfoRequest.PriceMax": { required: "Prezzo a obbligatorio", number: "Prezzo a non valido" },
        "InfoRequest.RoomsMin": { required: "Camere / Locali obbligatorio", number: "Camere / Locali non valido" },
        "InfoRequest.Email": {
            required: "Email obbligatoria",
            email: "Email non valida"
        },
        "InfoRequest.PrivacyCheck": "Devi accettare l\'informativa sulla privacy"
    },
    send: function(submittedForm) {
        var validatedForm = $(submittedForm).validate({
            submitHandler: function(form) {
                form.submit();
            },
            onkeyup: false,
            errorLabelContainer: "#errorList",
            wrapper: "li",
            showErrors: function(errorMap, errorList) {
                $("#serverErrorList").html();
                $("#messageBox").html("");
                var nums = this.numberOfInvalids()
                if (nums > 0) {
                    $(errorList).each(function(index, obj) {
                        $(obj.element).effect("pulsate", { times: 1 }, 300);
                    });
                    $("<a href='javascript:' onclick='casapadova.realestate.sellInfoRequest.showErrors();' >" + "Ci sono "
                                       + this.numberOfInvalids()
                                       + " errori di compilazione." + "</a>").appendTo("#messageBox")
                    $("#messageBox").show();
                } else {
                    $("#messageBox").hide();
                }
                this.defaultShowErrors();
            },
            messages: casapadova.realestate.sellInfoRequest.errorMessages
        });
        if ($(submittedForm).valid()) {
            $("#infoRequestBox").block({
                message: "<h4>Inviando...</h4>",
                css: casapadova.realestate.sellInfoRequest.blockingUI.css,
                overlayCSS: casapadova.realestate.sellInfoRequest.blockingUI.overlayCSS
            });
            var data = $(submittedForm).serialize();
            $.ajax({
                type: "POST",
                url: "/InfoRequest/CreateSell",
                data: data,
                success: function(result) {
                    if (result.success) {
                        validatedForm.resetForm();
                        $("#infoRequestBox").unblock();
                        $.growlUI('Messaggio Inviato', 'Il messaggio è stato spedito correttamente');
                        $(".infook").show();
                        $(".info").hide();
                    } else {
                        if (result.reason == "ValidationError") {
                            $("#serverErrorList").html("");
                            $(result.errors).each(function(index, error) {
                                var errorMessage = error.Message;
                                $("<li></li>").html(errorMessage).appendTo("#serverErrorList");
                            });
                            $("#serverErrorList").show();
                            $("#infoRequestBox").block({
                                message: $('#errorsBox'),
                                css: casapadova.realestate.sellInfoRequest.blockingUI.css,
                                overlayCSS: casapadova.realestate.sellInfoRequest.blockingUI.overlayCSS,
                                onUnblock: function() {
                                    $("#serverErrorList").html();
                                }
                            });
                        }
                        else {
                            $("#infoRequestBox").unblock();
                            $.growlUI('Errore', 'E\' avvenuto un errore nell\'invio del messaggio (' + result.reason + ')');
                        }
                    }
                },
                error: function(req, status, error) {
                    $("#infoRequestBox").unblock();
                    $.growlUI('Errore', 'E\' avvenuto un errore nell\'invio del messaggio (' + error + ')');
                },
                dataType: "json"
            });
        }
    },
    showErrors: function() {
        $("#infoRequestBox").block({
            message: $('#errorsBox'),
            css: casapadova.realestate.sellInfoRequest.blockingUI.css,
            overlayCSS: casapadova.realestate.sellInfoRequest.blockingUI.overlayCSS
        });
    },
    hideErrors: function() {
        $("#infoRequestBox").unblock();
    }
}