Vcom = {
    init: function () {
        //console.log("Vcom.init");
        $(".link").hover(
            function () {
                $(this).addClass("link-hover");
            },
            function () {
                $(this).removeClass("link-hover");
            }
        );
    },

    closeMap: function () {


    },

    showMap: function (location) {
        console.log("showMap(%s)", location);

        Vcom._upLocation = location;

        Vcom._mapSize = {
            width: $(window).width() * 0.8,
            height: $(window).height() * 0.8
        };

        Shadowbox.open({
            content: '<div id="map_canvas" style="width:' + Vcom._mapSize.width + 'px;height:' + Vcom._mapSize.height + 'px"></div>',
            player: "html",
            title: "",
            width: Vcom._mapSize.width,
            height: Vcom._mapSize.height,
            options: {
                onFinish: Vcom._sbLoadedMap
            }
        });

    },

    _onMapInit: function () {
        //console.log("_onMapInit(%s)", Vcom._upLocation);
        $('#map_canvas').gmap('addMarker', { 'position': Vcom._upMarker });
    },

    _sbLoadedMap: function () {

        //console.log("_sbLoadedMap(%s)", Vcom._upLocation);

        var parts = Vcom._upLocation.split(",");
        var lat = parseFloat(parts[0]);
        var lng = parseFloat(parts[1]);
        Vcom._upMarker = new google.maps.LatLng(lat, lng);
        var zoom = parseInt(parts[2]);

        $('#map_canvas').gmap({
            zoom: Vcom.defaultMapZoom,
            center: Vcom.defaultMapCenter
        });

        $('#map_canvas').bind("init", Vcom._onMapInit);
    }
};

if (!(window.console && window.console.firebug)) {
    console = {
        log: function () { }
    }
}

Vcom.MoreLoader = new Class({

    initialize: function (config) {
        this.config = config;
    },

    config: null,

    init: function (config) {
        this.config.nextLink.bind("click", this.onClickNext.bind(this));
    },

    isLoading: false,

    onClickNext: function () {
        //console.log("Vcom.MoreLoader.onClickNext");
        if (this.isLoading) {
            return;
        }

        this.isLoading = true;
        $.ajax(this.config.url + this.config.nextIndex, {
            success: this.onLoaded.bind(this)
        });
    },

    onLoaded: function (data) {
        //console.log("onLoaded");
        this.isLoading = false;

        if (data.search("MARKER") < 0) {
            //assume this means no more data:
            this.config.nextLink.addClass("DisplayNone");
            this.config.noMoreText.removeClass("DisplayNone");
            return;
        }

        this.config.nextIndex += this.config.pageSize;
        this.config.target.append(data);
    }

});

Vcom.DirectoryDropdown = new Class({

    init: function () {
        $("#DirectoryDropdown").bind("click", this.onClickDropdown.bind(this));
        $("#DirectoryGoButton").bind("click", this.onClickGo.bind(this));
        $("body").bind("click", this.onClickOutside.bind(this));

        $("#DirectoryDropdown").bind("mouseenter", this.onMouseInside.bind(this));
        $("#DirectoryDropdown").bind("mouseleave", this.onMouseOutside.bind(this));
        $("#PopupDirectory").bind("mouseenter", this.onMouseInside.bind(this));
        $("#PopupDirectory").bind("mouseleave", this.onMouseOutside.bind(this));
    },

    mouseIsInside: false,

    onMouseInside: function () {
        //console.log("onMouseInside");
        this.mouseIsInside = true;
    },

    onMouseOutside: function () {
        //console.log("onMouseOutside");
        this.mouseIsInside = false;
    },

    onClickGo: function () {
        alert(Vcom.strings.DirectoryPleaseSelect);
    },

    isUp: false,
    hasPoppedUpBefore: false,

    onClickDropdown: function () {
        if (this.isUp) {
            $("#PopupDirectory").addClass("DisplayNone");
            this.isUp = false;
        } else {
            var dir = $("#PopupDirectory");
            if (!this.hasPoppedUpBefore) {
                //http://stackoverflow.com/questions/1952344/override-overflowhidden-with-z-index
                dir.appendTo("body");
                dir.css({
                    left: $("#DirectoryDropdown").offset().left,
                    top: $("#DirectoryDropdown").offset().top
                });
                this.hasPoppedUpBefore = true;
            }
            dir.removeClass("DisplayNone");
            this.isUp = true;
        }
    },

    onClickOutside: function () {

        //console.log("onClickOutside: mouseIsInside: %d, isUp: %d", this.mouseIsInside, this.isUp);

        if (this.mouseIsInside) {
            return;
        }


        if (this.isUp) {
            $("#PopupDirectory").addClass("DisplayNone");
            this.isUp = false;
        }
    }

});

Vcom.AccommSearchResults = new Class({
    sortFields: ["Price", "Field_Distance"],

    initSortLinks: function () {
        ///Sort by links - current one shouldn't be a link:
        this.currentSort = AlpSystems.Util.getUrlParam("SortBy");
        if (this.currentSort == "") {
            this.currentSort = "Price";
        }

        this.currentSortDesc = AlpSystems.Util.getUrlParam("SortDesc") != "";

        //make current link look like text, not clickable:
        for (var i = 0; i < this.sortFields.length; i++) {
            var sortField = this.sortFields[i];
            if (this.currentSort == sortField) {
                var isDesc = (AlpSystems.Util.getUrlParam("SortDesc") != "");
                $(".SortLink_" + sortField + "_" + (isDesc ? "desc" : "asc")).removeClass("link");
            }
        }

        $(".SortLink").bind("click", this.sortResults.bind(this));
    },

    sortResults: function (e) {
        for (var i = 0; i < this.sortFields.length; i++) {
            var sortField = this.sortFields[i];
            if ($(e.currentTarget).hasClass("SortLink_" + sortField + "_asc")) {
                this.changeSort(sortField, false);
                return;
            } else if ($(e.currentTarget).hasClass("SortLink_" + sortField + "_desc")) {
                this.changeSort(sortField, true);
                return;
            }
        }
    },

    changeSort: function (newField, isDesc) {

        if (newField == this.currentSort && isDesc == this.currentSortDesc) {
            return;
        }

        var url = AlpSystems.Util.replaceUrlParam("SortBy", newField);
        url = AlpSystems.Util.replaceUrlParam("SortDesc", isDesc ? "True" : "", url);
        document.location.href = url;
    }
});

Vcom.AccommSearch = new Class({

    init: function() {
        $(document).ready(function () {
            $(".accomm-search .date-input").datepicker({
                inline: true,
                showOn: "button",
                buttonImageOnly: true,
                dateFormat: "dd/mm/yy",
                constrainInput: false,
                buttonImage: "/uploads/Layout/calendar.jpg"
            });

            $(".accomm-search .CheckboxLine").bind("click", this.onClickCheckboxLine.bind(this));
        }.bind(this));
    },

    onClickCheckboxLine: function(event) {

        if(event.target.nodeName == "INPUT") {
            return;
        }

        for(var i=0; i < event.currentTarget.childNodes.length; i++) {
            var node = event.currentTarget.childNodes[i];
            if(node.nodeName == "INPUT") {
                node.click();
            }
        }
    }

});

