﻿var Landor = Landor || {};

Landor.LocationNav = function(canvas, container, itemsList, locationsContainer)
{
    this._canvas = canvas;
    this._container = container;
    this._itemsList = itemsList;
    this._locationsContainer = locationsContainer;

    this._locationUrl = null;

    this._Init();
}

Landor.LocationNav._childUrls = new Array();

Landor.LocationNav.RegisterChildUrls = function(childUrls)
{
    Landor.LocationNav._childUrls = childUrls;
}

Landor.LocationNav.prototype =
{
    HashChangedCallback: function (e) {
        if (null != this._locationUrl) {
            var startsWith = '/#' + e.fragment.substring(0, this._locationUrl.length - 2);

            if (startsWith != this._locationUrl) {
                if (null != Landor.LocationNav._childUrls) {
                    for (var i = 0; i < Landor.LocationNav._childUrls.length; ++i) {
                        if (Landor.LocationNav._childUrls[i] == '/#' + e.fragment) {
                            return;
                        }
                    }
                }

                this._ClearLocation();
            }
        }
    },

    SetLocation: function (locationName, locationHref) {
        this._ClearLocation();
        this._container.css('visibility', 'visible');
        var locationLink = $('#locationLink', this._container);
        locationLink.css('display', 'block');
        locationLink.html('<span class="locationName">' + locationName + '</span>');
        locationLink.attr('href', locationHref);
        locationLink.append('<span class="sf-sub-indicator"> »</span>');


        this._locationUrl = locationHref;
        this._GetLocationItems(locationHref);

        this._canvas.SetLocation(locationName, locationHref);
    },

    _ClearLocation: function () {
        this._canvas.ClearLocation();

        var locationLink = $('#locationLink', this._container);
        locationLink.text('');
        locationLink.attr('href', '#');
        locationLink.css('display', 'none');

        this._locationUrl = null;

        this._itemsList.empty();
        this._container.css('visible', 'hidden');
    },

    _Init: function () {
        var locations = $('a', $("#locations", this._locationsContainer));

        for (var i = 0; i < locations.length; ++i) {
            $(locations[i]).click($.proxy(function (event) {
                var target = $(event.target);

                this.SetLocation(target.text(), target.attr('href'));
            }, this));
        }

        var location = ReadCookie(this._canvas._cookieName);

        if (null != location) {
            var delim = location.indexOf(':');

            if (-1 == delim) {
                return;
            }

            var name = location.substring(0, delim);
            var href = location.substring(delim + 1);

            this.SetLocation(name, href);
        }
    },

    _GetLocationItems: function (url) {
        $.ajax({
            'url': '/data-services/clientservice.svc/getlocationitems',
            'context': this,
            'data': { "url": url },
            'success': function (content) {
                if (null == content || null == content.d) {
                    return;
                }

                this._PopulateItemsList(content.d);
            },
            'error': function (jqXHR, textStatus, errorThrown) {
                alert(textStatus);
                alert(errorThrown);
            }
        });
    },

    _PopulateItemsList: function (items) {
        for (var i = 0; i < items.length; ++i) {
            this._itemsList.append($('<li><a href="' + items[i].Href + '">' + items[i].Title + '</a></li>'));
        }
    }
}
