function LocationNavigation() {

    var self = {};
    self.currentLocationId = 0;
    self.parentIdOfCurrentLocation = 0;
    self.navigationDirectionIsUp = undefined;
    self.entryPoint = undefined;

    self.requestLocationData = function (DirectionIsUp) {
        self.navigationDirectionIsUp = DirectionIsUp;

        var UserId = $.cookie("user_id");
        var current_session_token = $.cookie("session_token");

        var requester = new AjaxRequest({
            data: { "locationid": self.currentLocationId, "userid": UserId, "session_token": $.cookie("session_token") }, // This is for the json request
            success: self.requestLocationDataSuccess,
            error: self.requestLocationDataFailure,
            url: "RequestChildLocationsOnLocationId"
        });
        requester.ExecuteRequest();

    };

    self.requestLocationDataSuccess = function (data, status, xhr) {
        var locationList = data.d.LocationListElements;

        $("#location_list_list").empty();

        var locationListElements = '<ul class="listview_default_class" id="location_list_current_level" data-role="listview" data-inset="true" data-divider-theme="b">'

        jQuery.each(locationList, function () {
            //locationListElements += '<li class="list-element-class" data-theme="c" data-locationid="';
            locationListElements += '<li class="list-element-class" data-locationid="';
            locationListElements += this.LocationID;
            locationListElements += '" data-parentid="';
            locationListElements += this.ParentID;
            locationListElements += '">' + this.LocationName + '</li>';

        });

        locationListElements += '</ul>';
        
        $("#location_list_list").html(locationListElements);
        $("#location_list_list").trigger("create");

        self.attachEventToLocationList();
        self.updateSelectedLocationInfo(data.d);
        self.updateButtonBarVisibility();


    };

    self.requestLocationDataFailure = function (data, status, xhr) {
        c1('locationselection row 54: Failed to fetch location information. Unable to reach web service.');
        DbCommunicationErrorHandler(WEBSERVICE_FAULT_JSONREQUEST);
    };

    self.updateButtonBarVisibility = function () {
        if (self.currentLocationId == 0 && $('#location_list_navbar').is(":visible")) {
            $('#location_list_navbar').hide();
        }

        if (self.currentLocationId > 0 && $('#location_list_navbar').is(":hidden")) {
            $('#location_list_navbar').show();
        }
    }

    self.updateSelectedLocationInfo = function (locationInfo) {

        self.renderCurrentLocationInfo(locationInfo);
        if (self.navigationDirectionIsUp == true) {
            self.parentIdOfCurrentLocation = locationInfo.ParentId;
        }
        else if (!defined(self.navigationDirectionIsUp)) {
            c1('locationselection row 75: Navigation direction undefined');
        }

        self.navigationDirectionIsUp = undefined;

        //if (self.navigationDirectionIsUp == false) {
        //    console.log('Going down');

        //    if (self.currentLocationId == 0) {
        //        var locationInfoText = ReturnPhraseOnKey('location_list_current_location_info') + '<br><b>' + ReturnPhraseOnKey('location_list_top_level_default_info' + '</b>');
        //        $("#current_location_info").html(locationInfoText);
        //    }
        //    else {
        //        var locationInfoText = ReturnPhraseOnKey('location_list_current_location_info') + '<br><b>' + locationInfo.ParentCompletePath + '</b>';
        //        $("#current_location_info").html(locationInfoText);
        //        console.log('Selected location id after update: ' + self.currentLocationId);
        //        console.log('Parent id after update: ' + self.parentIdOfCurrentLocation);
        //    }
        //}
        //else if (self.navigationDirectionIsUp == true) {
        //    if (self.currentLocationId == 0) {
        //        var locationInfoText = ReturnPhraseOnKey('location_list_current_location_info') + '<br><b>' + ReturnPhraseOnKey('location_list_top_level_default_info') + '</b>';
        //        $("#current_location_info").html(locationInfoText);
        //    }
        //    else {
        //        console.log('Going up');
        //        console.log(locationInfo);
        //        self.parentIdOfCurrentLocation = locationInfo.ParentId;
        //        var locationInfoText = ReturnPhraseOnKey('location_list_current_location_info') + '<br><b>' + locationInfo.ParentCompletePath + '</b>';
        //        $("#current_location_info").html(locationInfoText);
        //        console.log('Selected location id after update: ' + self.currentLocationId);
        //        console.log('Parent id after update: ' + self.parentIdOfCurrentLocation);
        //    }

        //}
        //else {
        //    console.log('Navigation direction undefined');
        //}

        //self.navigationDirection = undefined;


    }

    self.renderCurrentLocationInfo = function (incomingCurrentLocationData) {
        var locationInfoText = '';
        var locationPath = '';

        if (self.currentLocationId == 0) {
            locationPath = TOP_NODE_NAME;
            CURRENT_LOCATION_COMPLETE_PATH = TOP_NODE_NAME;
        }
        else {
            locationPath = incomingCurrentLocationData.ParentCompletePath;
            CURRENT_LOCATION_COMPLETE_PATH = incomingCurrentLocationData.ParentCompletePath;
        }

        locationInfoText = '<h3>' + ReturnPhraseOnKey('location_list_current_location_info') + '<br><b>' + locationPath + '</b></h3>';

        $("#current_location_info").html(locationInfoText);
    };

    self.returnCurrentLocationInfo = function (header_size) {
        var locationInfoText = '';
        var locationPath = '';

        (self.currentLocationId == 0) ? locationPath = TOP_NODE_NAME : locationPath = CURRENT_LOCATION_COMPLETE_PATH;

        locationInfoText = '<h' + header_size + ' style="margin-top:1px;margin-bottom:1px">' + ReturnPhraseOnKey('location_list_current_location_info') + '<br><a href="#page_location_list"><b>' + locationPath + '</b></a></h3>';
        return locationInfoText;

    };

    self.resetToTop = function () {
        self.currentLocationId = 0;
        self.parentIdOfCurrentLocation = 0;
        CURRENT_LOCATION_COMPLETE_PATH = TOP_NODE_NAME;

    }

    self.attachEventToLocationList = function () {


        $('#location_list_current_level').on('click', 'li', function (e) {
            e.preventDefault();
            e.stopPropagation();
            self.currentLocationId = $(this).data("locationid");
            self.parentIdOfCurrentLocation = $(this).data("parentid");
            if (self.currentLocationId !== undefined) {

                self.requestLocationData(false);

            }
        });

    }


    return self;
}

var locationNavigation = LocationNavigation();

$(document).one("pageshow", "#page_location_list", function (e) {

    $("#location_list_up_top").click(function () {
        e.preventDefault();
        e.stopPropagation();
        if (locationNavigation.currentLocationId !== 0) {
            locationNavigation.currentLocationId = 0;
            locationNavigation.parentLocationId = 0;
            locationNavigation.requestLocationData();
        }
        else {
            c1('locationselection row 189: Already at top level');
        }
        e2('locationselection row 191');
    })

    $("#location_list_up_level").click(function () {
        e.preventDefault();
        e.stopPropagation();
        if (locationNavigation.currentLocationId !== 0) {
            locationNavigation.currentLocationId = locationNavigation.parentIdOfCurrentLocation;
            locationNavigation.requestLocationData(true);
        }
        else {
            c1('locationselection row 201: Already at top level');
        }
        e2('locationselection row 204');
    })

});

$(document).one("pageshow", "#page_location_list", function () {
    $("#location_list_back_button").unbind().click(function (e) {

        e.preventDefault();
        e.stopPropagation();

        if (locationNavigation.entryPoint == 'page_mainmenu') {
            $.mobile.navigate("#page_mainmenu", { role: "page" });
            e2('locationselection row 217');
        }
        else if (locationNavigation.entryPoint == 'page_machineselect') {
            $.mobile.navigate("#page_machineselect", { role: "page" });
            e2('locationselection row 221');
        }
        else if (locationNavigation.entryPoint == 'page_mainpersonallist') {
            $.mobile.navigate("#page_mainpersonallist", { role: "page" });
            e2('locationselection row 225');

        }
        //final option, if application cannot figure out which page to return to, go back to main menu
        else {
            $.mobile.navigate("#page_mainmenu", { role: "page" });
            e2('locationselection row 231');
        }
    });

});

$(document).on("pageshow", "#page_location_list", function (e) {
    initScanner(undefined);
    //if (SessionTokenStatus == true) {
    //    locationNavigation.requestLocationData();
    //}
    //else { helper.ForcedLogout(); }


});

$(document).on("pagebeforeshow", "#page_location_list", function (e, data) {
    $('#location_list_navbar').hide();

    if (SessionTokenStatus == true) {
        locationNavigation.requestLocationData();
    }
    else { helper.ForcedLogout(); }


    locationNavigation.entryPoint = data.prevPage.attr('id');

    if (locationNavigation.entryPoint == 'page_mainmenu') {
        c1('locationselection row 253: Coming from main menu');
    }
    else if (locationNavigation.entryPoint == 'page_machineselect'){
        c1('locationselection row 256: Coming from machine search input field');
    }
    else if (locationNavigation.entryPoint == 'page_mainpersonallist') {
        c1('locationselection row 259: Coming from personal job list');

    }

    
});