﻿Type.registerNamespace('casapadova.common');

casapadova.common.debug = true;

casapadova.common.showEffect = {
    effect: "slide",
    options: {},
    speed: 250
};

casapadova.common.hideEffect = {
    effect: "slide",
    options: {},
    speed: 250
};

casapadova.common.alpha = {
    effect: "slide",
    options: {},
    speed: 250
};

casapadova.common.resources = {
    map : {},
    load : function() {
        
    }
}

casapadova.common.toggleLoader = function(type) {
    if (type == "show") {
        $("#loading").show(
            casapadova.common.showEffect.effect,
            casapadova.common.showEffect.options,
            casapadova.common.showEffect.speed
            );
    }
    else if (type == "hide") {
        $("#loading").hide(
            casapadova.common.hideEffect.effect,
            casapadova.common.hideEffect.options,
            casapadova.common.hideEffect.speed
            );
    }
    else {
        $("#loading").toggle(
            casapadova.common.showEffect.effect,
            casapadova.common.showEffect.options,
            casapadova.common.showEffect.speed
            );
    }
};

casapadova.common.showError = function(errorResult) {
    if (casapadova.common.debug) {
        if (confirm("An error occured!\nContinue result with object dumping?")) {
            casapadova.common.dumpProps(errorResult);
        }
    }
};

casapadova.common.dumpProps = function(obj, parent) {
    // Go through all the properties of the passed-in object 
    for (var i in obj) {
        // if a parent (2nd parameter) was passed in, then use that to 
        // build the message. Message includes i (the object's property name) 
        // then the object's property value on a new line 
        if (parent) { var msg = parent + "." + i + "\n" + obj[i]; } else { var msg = i + "\n" + obj[i]; }
        // Display the message. If the user clicks "OK", then continue. If they 
        // click "CANCEL" then quit this level of recursion 
        if (!confirm(msg)) { return; }
        // If this property (i) is an object, then recursively process the object 
        if (typeof obj[i] == "object") {
            if (parent) { dumpProps(obj[i], parent + "." + i); } else { dumpProps(obj[i], i); }
        }
    }
};

casapadova.common.favourites = {
    favDetailsUrl: "",
    favTemplate: "",
    hideEffect: "puff",
    addToFavs: function(source, id, hideEffect) {
        $.ajaxSetup({
            cache: false
        });
        $.getJSON("/RealEstate/AddToFavourites", { realEstateId: id }, function(data) {
            if (hideEffect == null) hideEffect = casapadova.common.favourites.hideEffect;
            if (hideEffect != "")
                $(source).hide(hideEffect, 2000);
            else
                $(source).fadeOut(250);
            casapadova.common.favourites.update(data);
        });
    },
    remFromFavs: function(source, id, hideEffect) {
        $.ajaxSetup({
            cache: false
        });
        $.getJSON("/RealEstate/RemoveFromFavourites", { realEstateId: id }, function(data) {
            if (hideEffect == null) hideEffect = casapadova.common.favourites.hideEffect;
            if (hideEffect != "" && source)
                $(source).hide(hideEffect, 2000);
            else if (source)
                $(source).fadeOut(250);
            casapadova.common.favourites.update(data);
        });
    },
    update: function(data) {
        var number = $(data).length;
        $(".colpreferiti .preferiti .number").html(number + "");
        if (number == 0) {
            $(".colpreferiti .preferiti #spnShowFavs").hide();
            $(".colpreferiti .preferiti #spnNoFavs").show();
            casapadova.common.favourites.hideBox();
        } else if (number > 0) {
            $(".colpreferiti .preferiti #spnShowFavs").show();
            $(".colpreferiti .preferiti #spnNoFavs").hide();
            var content = "";
            $(data).each(function(index, item) {
                var id = item.RealEstateID;
                var favUrl = $.format(
                    casapadova.common.favourites.favDetailsUrl,
                    item.RealEstateID
                );
                content += $.format(
                    casapadova.common.favourites.favTemplate,
                    item.RealEstateID,
                    item.RealEstateType,
                    item.LocationName,
                    item.FormattedPrice,
                    item.LocationZone,
                    favUrl
                );
            });
            $(".prefContent").html(content);
        }
    },
    toggleBox: function() {
        $(".prefbox").toggle("slide", { direction: "up" }, 200);
        return false;
    },
    hideBox: function() {
        $(".prefbox").hide("slide", { direction: "up" }, 200);
        return false;
    },
    showBox: function() {
        $(".prefbox").show("slide", { direction: "up" }, 200);
        return false;
    }
};

casapadova.common.beginRequest = function() {
    $("#results").block({ message: null, overlayCSS: {
        backgroundColor: '#CCC',
        opacity: '0.5'
    }
    });
    casapadova.common.toggleLoader("show");
}

casapadova.common.endRequest = function() {
    $("#results").unblock();
    casapadova.common.toggleLoader("hide");
}