﻿var StyleDivFunfo;

//Objects declarations
function Page() {
    this.ProxyService = new ProxyService;
    this.isPartialWaiting = false;
}
function ProxyService() {
    this.format = "json"
    this.contentType = "application/json";
    this.timeout = 120000;
    this.waiting = true;
    this.method = "POST";
}
Page.prototype.Scroll = function (unbindAfter, functionBind, param1, param2, param3, param4) {
    $(window).scroll(function () {
        if ($(window).scrollTop() == 0) {
            //TOP
        }
        else if ($(window).height() + $(window).scrollTop() >= $(document).height() - 1207) {
            if (param1 == undefined) {
                functionBind();
            }
            else if (param2 == undefined) {
                functionBind(param1);
            }
            else if (param3 == undefined) {
                functionBind(param1, param2);
            }
            else if (param4 == undefined) {
                functionBind(param1, param2, param3);
            }
            else {
                functionBind(param1, param2, param3, param4);
            }

            if (unbindAfter) {
                Page.ScrollUnbind();
            }
        }
    });
}
Page.prototype.CloseAlerts = function () {
    $("#divAlert").hide();
    $("#divError").hide();
}
Page.prototype.AlertHtml = function (msg) {
    if (!$("#divError").is(":visible")) {
        $("#spanAlert").html(msg);
        $("#divAlert").show();
        Page.ScrollTo($("#divAlert"));
    }
}
Page.prototype.ScrollTo = function (obj) {
    if (typeof obj == "number") {
        $('html, body').scrollTop(obj);
    }
    else {
        if (obj != null && obj != undefined) {
            $('html, body').scrollTop(obj.offset().top);
        }
    }
}
Page.prototype.ErrorHtml = function (msg) {
    Page.CloseAlerts();
    $("#spanError").html(msg);
    $("#divError").show();
    Page.ScrollTo($("#divError"));
}
Page.prototype.ScrollUnbind = function () {
    $(window).unbind("scroll");
}
Page.prototype.PopupFromDiv = function (hiddenDiv) {
    var obj = (typeof hiddenDiv == "string") ? $("#" + hiddenDiv) : hiddenDiv;
    $.modal(obj.html());
}
//Prototypes
Page.prototype.isOldBrowser = function () {
    return (navigator.userAgent.indexOf("MSIE 6.") != -1) || (navigator.userAgent.indexOf("MSIE 5.") != -1) || (navigator.userAgent.indexOf("MSIE 4.") != -1);
}
Page.prototype.DropDownListClear = function (ddlObject, options) {
    if (options == undefined) { options = null; }

    if (typeof ddlObject == "string") {
        ddlObject = $("#" + ddlObject);
    }
    if (ddlObject != null) {
        ddlObject.children().remove().end();
    }

    if (options != null) {
        ddlObject.append("<option value='" + options.Value + "'>" + options.Text + "</option>");
    }

}
Page.prototype.DropDownListBind = function (ddlObject, jsonObject, options) {
    //options: { Text: "Selecione uma cidade", Value: -1 }
    if (typeof ddlObject == "string") {
        ddlObject = $("#" + ddlObject);
    }
    if (typeof jsonObject == "string") {
        jsonObject = JSON.parse(jsonObject);
    }
    ddlObject.children().remove().end();
    if (options != null) {
        ddlObject.append("<option value='" + options.Value + "'>" + options.Text + "</option>");
    }
    $.each(jsonObject, function (val, text) {
        ddlObject.append("<option value='" + text.Value + "'>" + text.Text + "</option>");
    });
}
Page.prototype.WaitingLiteMove = function () {
    var wait = $("#progresslite");
    var isShoppingCartVisible = false;
    try {
        isShoppingCartVisible = ShoppingCart.isVisible();
    } catch (e) { }

    if (Page.isPartialWaiting) {
        var px = "40px";
        if (isShoppingCartVisible) {
            if (wait.css("bottom") == "40px") {
                return;
            }
            wait.animate({ "bottom": "+=" + px }, "fast");
        }
        else {
            if (wait.css("bottom") == "0px") {
                return;
            }
            wait.animate({ "bottom": "-=" + px }, "fast");
        }
    }

}
Page.prototype.WaitingLite = function () {
    var wait = $("#progresslite");
    var isShoppingCartVisible = false;
    var close = Page.isPartialWaiting;

    try {
        isShoppingCartVisible = ShoppingCart.isVisible();
    } catch (e) { }

    var px = "";

    if (isShoppingCartVisible) {
        px = "40px";
    }
    else {
        px = "0px";
    }

    if (close) {
        wait.fadeTo("slow", 0, function () {
            wait.css("bottom", "-110px");
        });

    }
    else {
        wait.css("bottom", px);
        wait.fadeTo("slow", 1);
    }
    Page.isPartialWaiting = !Page.isPartialWaiting;
}
Page.prototype.Transfer = function (address) {
    window.location = address;
}
Page.prototype.Dialog = function (html) {
    $("#popupModal-all").empty();
    $("#popupModal-all").html(html);
    $("#popupModal-all").modal();
}

Page.prototype.WaitingClose = function (sender) {
    var oDiv = (typeof sender == "string") ? window.document.getElementById(sender) : sender;

    var $oDiv = $(oDiv);

    if ($oDiv.attr("boxActive") == true || oDiv[0].getAttribute("boxActive") == true)
        window.document.body.removeChild(oDiv);
    else
        $oDiv.css("display", "none").css("visibility", "hidden").hide();

    var createHintBoxyFundo = window.document.getElementById("boxBackground");
    if (createHintBoxyFundo != null) {
        window.document.body.removeChild(createHintBoxyFundo);
    }
}
Page.prototype.Waiting = function (message, sender, isCloseAutomatic, position, isToCenter) {
    var xPosition;
    var yPosition;
    var bolCenter = false;

    if (isCloseAutomatic == undefined)
        isCloseAutomatic = true;

    if (sender != undefined && $(sender).length > 0) {
        xPosition = $(sender).position().left;
        yPosition = $(sender).position().top;
        width = $(sender).outerWidth(true);
        height = $(sender).outerHeight(true);
        xPosition += width;
        //yPosition += height;
    }
    else {

        if (self.innerHeight) {
            xPosition = self.innerWidth;
            yPosition = self.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) {
            xPosition = document.documentElement.clientWidth;
            yPosition = document.documentElement.clientHeight;
        } else if (document.body) {
            xPosition = document.body.clientWidth;
            yPosition = document.body.clientHeight;
        }

        yPosition = yPosition <= 5 ? YMousePosition : yPosition / 2;

        xPosition = xPosition / 2;
    }

    if (position == undefined) position = "fixed";

    //FUNDO
    var oDivFundo = window.document.createElement("div");
    oDivFundo.id = "boxBackground";
    oDivFundo.style.filter = "Alpha(Opacity=40)";

    if (StyleDivFunfo == 1) {
        oDivFundo.style.opacity = "0.7";
        oDivFundo.style.backgroundColor = "#222";
    }
    else {
        oDivFundo.style.opacity = "0.4";
        oDivFundo.style.backgroundColor = "#CCC";
    }

    if (window.scrollMaxY == 0)
        oDivFundo.style.height = "100%";
    else
        oDivFundo.style.height = $(document).height() + 100 + "px";

    oDivFundo.style.width = "100%";
    oDivFundo.style.position = Page.isOldBrowser ? "absolute" : "fixed";
    oDivFundo.style.zIndex = "500";
    oDivFundo.style.top = "0px";
    oDivFundo.style.left = "0px";

    if ($("#boxBackground").length == 0)
        window.document.body.appendChild(oDivFundo);

    if (typeof message == "string") {

        var oDiv = window.document.createElement("div");

        if (isCloseAutomatic) {
            oDiv.style.cursor = "pointer";
            oDiv.onclick = function () { Page.WaitingClose(sender); };
            //oDiv.setAttribute("onClick", "CloseHintBoxy(this);"); Nao funciona no IE7
        }

        oDiv.style.backgroundColor = "#FFF";
        oDiv.style.position = position;

        oDiv.innerHTML = message;

        $(oDiv).attr("boxActive", true);

        bolCenter = true;
    }
    else {
        oDiv = message;
    }

    if (oDiv.jquery == undefined) {

        oDiv.style.display = "none";
        oDiv.style.visibility = "visible";
        window.document.body.appendChild(oDiv);

        //PRECISA SER DEPOIS DO APPEND PARA DESCOBRIR O TAMANHO DO ELEMENTO CRIADO
        var $oDiv = $(oDiv);
        if (bolCenter) {
            xPosition -= ($oDiv.width() / 2);
            yPosition -= ($oDiv.height() / 2);
        }

        if (($.browser.msie) && (parseInt($.browser.version, 10) < 8)) {
            oDiv.style.left = "550px";
            oDiv.style.top = "200px";
            oDiv.style.right = "620px";
        }
        else {
            oDiv.style.left = xPosition + "px";
            oDiv.style.top = yPosition + "px";
        }

        oDiv.style.zIndex = "1000";
        oDiv.style.position = position;
        $oDiv.fadeIn(200);

    }
    else {

        oDiv[0].style.display = "none";
        oDiv[0].style.visibility = "visible";
        window.document.body.appendChild(oDiv[0]);

        var $oDiv = $(oDiv);
        if (isToCenter) {
            xPosition -= ($oDiv.width() / 2);
            yPosition -= ($oDiv.height() / 2);
        }

        if (oDiv[0] != undefined) {
            oDiv[0].style.left = xPosition + "px";
            oDiv[0].style.top = yPosition + "px";
            oDiv[0].style.position = position;
            oDiv[0].style.zIndex = "1000";
            oDiv.fadeIn(200);
        }
    }
}
Page.prototype.Error = function (error) {
    var ret = false;
    try {
        var parsedError = JSON.parse(error);
        if (parsedError) {
            if (parsedError.Error) {
                ret = true;
                var msg = eval(parsedError.Code);

                if (parsedError.Values != null && parsedError.Values != undefined) {
                    $.each(parsedError.Values, function (index, val) {
                        msg = msg.replace("{" + index + "}", val);
                    });
                }
                if (parsedError.isErrorHtml) {
                    Page.ErrorHtml(msg);
                }
                else {
                    if (parsedError.isAlert) {
                        alert(msg);
                    }
                    else {
                        if (parsedError.isAlertHtml) {
                            Page.AlertHtml(msg);
                        }
                    }
                }

            }
        }
    } catch (e) {

    }
    return ret;
}
Page.prototype.QueryString = function (key) {
    var ret = "";
    index = 0;

    var element = "";
    var uri = window.location.search.substring(1).split("&");

    key = key.toLowerCase();

    for (var i = 0, leng = uri.length; i < leng; i++) {
        element = uri[i].split("=");
        if (element[0].toLowerCase() == key) {
            ret = element[1];
            break;
        }
    }
    return ret;
}
ProxyService.prototype.InvokeDDL = function (url, data, dropdownlistBing, firstObjectBind, callbackError, progressFunction) {
    if (firstObjectBind == undefined) firstObjectBind = null;
    if (callbackError == undefined) callbackError = null;
    if (progressFunction == undefined) progressFunction = null;
    var obj = (typeof dropdownlistBing == "string") ? $("#" + dropdownlistBing) : dropdownlistBing;
    Page.ProxyService.InvokeJSON(url, data, function (data) {
        Page.DropDownListBind(obj, data.Data, firstObjectBind);
    }, callbackError, progressFunction);
}

ProxyService.prototype.InvokeJSON = function (url, data, callback, callbackError, progressFunction) {
    if (callback == undefined) callback = null;
    if (callbackError == undefined) callbackError = null;
    if (progressFunction == undefined) progressFunction = null;
    if (data == undefined) data = null;

    Page.ProxyService.Invoke(url, data, "json", callback, callbackError, false, progressFunction);
}
ProxyService.prototype.InvokePage = function (url, data, callback, callbackError, progressFunction) {
    if (callback == undefined) callback = null;
    if (callbackError == undefined) callbackError = null;
    if (progressFunction == undefined) progressFunction = null;
    if (data == undefined) data = null;

    Page.ProxyService.Invoke(url, data, "html", callback, callbackError, false, progressFunction);
}
ProxyService.prototype.InvokePopup = function (url, data, callback, callbackError, progressFunction) {
    if (callback == undefined) callback = null;
    if (callbackError == undefined) callbackError = null;
    if (progressFunction == undefined) progressFunction = null;
    if (data == undefined) data = null;

    Page.ProxyService.Invoke(url, data, "popup", callback, callbackError, false, progressFunction);
}
ProxyService.prototype.Invoke = function (url, data, returnType, callback, callbackError, isLog, progressFunction) {
    if (this.waiting) {
        if (progressFunction == null || progressFunction == undefined) {
            Page.Waiting($("#progress"));
        }
        else {
            if (typeof progressFunction == "string") {
                if (progressFunction == "lite") {
                    Page.WaitingLite();
                }
            }
            else {
                progressFunction(true);
            }
        }
    }

    if (!isLog) {
        var callerInformation = "";
        var pageName = "";

        try {
            callerInformation = arguments.callee.caller;
        } catch (e) { }
        try {
            pageName = window.location.pathname;
        } catch (e) { }
    }

    var jsonData = JSON.stringify(data);

    var ajax = $.ajax({
        url: "/atracoes" + url,
        data: jsonData,
        type: this.method,
        processData: false,
        contentType: this.contentType,
        timeout: this.timeout,
        dataType: "text",  // not "json" we'll parse
        success:
            function (data) {
                if (Page.ProxyService.waiting) {
                    if (progressFunction == null || progressFunction == undefined) {
                        Page.WaitingClose($("#progress"));
                    }
                    else {
                        if (typeof progressFunction == "string") {
                            if (progressFunction == "lite") {
                                Page.WaitingLite();
                            }
                        }
                        else {
                            progressFunction(false);
                        }
                    }
                }
                if (Page.Error(data)) {
                    if (callbackError != null && callbackError != undefined) {
                        callbackError();
                    }
                    return;
                }

                if (returnType == "json") {

                    var parsed = JSON.parse(data);
                    if (callback != null && callback != undefined) {
                        callback(parsed);
                    }
                    else {
                        alert("config:callback");
                    }
                }
                else {
                    if (returnType == "popup") {
                        Page.Dialog(data);
                        callback();
                    }
                    else {
                        if (callback != null && callback != undefined) {
                            callback(data);
                        }
                        else {
                            alert("config:callback");
                        }
                    }
                }
            },
        error:
            function (xhr, textStatus) {
                if (Page.ProxyService.waiting) {
                    if (progressFunction == null || progressFunction == undefined) {
                        Page.WaitingClose($("#progress"));
                    }
                    else {
                        if (typeof progressFunction == "string") {
                            if (progressFunction == "lite") {
                                Page.WaitingLite();
                            }
                        }
                        else {
                            progressFunction(false);
                        }
                    }
                }
                if (Page.Error(xhr.responseText)) {
                    return;
                }
                else {
                    Page.ErrorHtml("Problemas de processamento");
                }
                if (callbackError != null && callbackError != undefined) {
                    callbackError();
                }
            }
    });
}

var errorAux;

//Object Page
var Page = new Page;

$(document).ready(function () {
    CoreLoadJS();
});
function CoreLoadJS() {
    $("a[id='hlkDoubtAttraction']").click(function () {
        Page.PopupFromDiv($("#doubtPopup"));
    });
    $("a[id='hlkPoliciesConditions']").click(function () {
        Page.PopupFromDiv($("#divCondicoesGeraisAtracoes"));
    });
}




