function myCities() {
    //properties
    this.CityList = new Array();        //associative
    this.Cookies = new cookieHandler();
    this.MyCityList = new Array();      //linknames
    this.Selected = null;               //linkname
    this.Current = null;                //linkname

    //methods
    this.Add = add;
    this.AddAndSave = addAndSave;
    this.DisplayAdd = displayAdd;
    this.DisplayClear = displayClear;
    this.DisplayDrop = displayDrop;
    this.DropAndSave = dropAndSave;
    this.Go = go;
    this.HideEmptyMessage = hideEmptyMessage;
    this.Init = init;
    this.Save = save;
    this.Select = select;
    this.ShowEmptyMessage = showEmptyMessage;

    function add(strLinkname) {
        if (this.CityList.searchProperty("linkname", strLinkname) > -1 && this.MyCityList.search(strLinkname) == -1) {
            if (this.MyCityList.length == 0) {
                this.HideEmptyMessage();
            }
            this.MyCityList.push(strLinkname);
            this.MyCityList.sort();
            this.DisplayAdd(strLinkname);
            return true;
        }
        return false;
    }

    function addAndSave(strLinkname) {
        if (this.Add(strLinkname)) {
            this.Save();
        }
    }

    function displayAdd(strLinkname) {
        var displayName = this.CityList.getPropertyValue("linkname", strLinkname, "location");
        var displayIndex = this.MyCityList.search(strLinkname);
        var displayElement = '<li id="myCitiesListItem_' + strLinkname + '" class="myCitiesListItem">' + displayName + '</li>';
        if (displayIndex == 0) {
            $("ul#myCitiesList").prepend(displayElement);
        } else if (displayIndex == this.MyCityList.length - 1) {
            $("ul#myCitiesList").append(displayElement);
        } else {
            $("li#myCitiesListItem_" + this.MyCityList[displayIndex -1]).after(displayElement);
        }
        $("li#myCitiesListItem_" + strLinkname).click(function() {
            MyCities.Select(this.id.replace("myCitiesListItem_", ""));
        });
    }

    function displayClear() {
        $("ul#myCitiesList").empty();
    }

    function displayDrop(strLinkname) {
        $("li#myCitiesListItem_" + strLinkname).remove();
        if (this.MyCityList.length == 0) {
            this.ShowEmptyMessage();
        }
    }

    function dropAndSave() {
        if (this.Selected) {
            var newCityList = new Array();
            for (var i=0; i<this.MyCityList.length; i++) {
                if (this.MyCityList[i] !== this.Selected) {
                    newCityList.push(this.MyCityList[i]);
                }
            }
            this.MyCityList = newCityList;
            this.Save();
            this.DisplayDrop(this.Selected);
            this.Selected = null;
        }
    }

    function go() {
        var locationRe = new RegExp("/go/\\w+/[A-Z]{3,3}/");
        var isAirport = locationRe.test(window.location.href);
        var newGuide = (this.Selected) ? this.Selected : this.MyCityList[0];
	if (isAirport) {
            newGuide += "/" + this.CityList.getPropertyValue("linkname", newGuide, "airport");
        }
        window.location.href = "/go/" + newGuide + "/index.html";
    }

    function hideEmptyMessage() {
        $("li#myCitiesListItem_empty").css("display", "none");
        $("li#myCitiesActionsGo").css("display", "block");
        $("li#myCitiesActionsRemove").css("display", "block");
        $("p#myCitiesPromptReady").css("display", "block");

    }

    function init() {
        //import master city list
        for (var i=0; i<cityListingImport.length; i++) {
            this.CityList.push(cityListingImport[i]);
        }

        //import user city list from browser cookie
        var storedCities = this.Cookies.Read("myCities");
        if (storedCities) {
            this.HideEmptyMessage();
            storedCities = storedCities.split(",");
            for (var j=0; j<storedCities.length; j++) {
                this.Add(storedCities[j]);
            }
        } else {
            this.ShowEmptyMessage();
        }

        if (citySelectionImport) {
            this.Current = citySelectionImport;
            if (this.MyCityList.search(this.Current) == -1) {
                this.AddAndSave(this.Current);
            }
            this.Select(this.Current);
        }
    }

    function save() {
        this.Cookies.Create("myCities", this.MyCityList.toString(), 30);
    }

    function select(strLinkname) {
        if (this.Selected) {
            $("li#myCitiesListItem_" + this.Selected).toggleClass("selected");
        }
        $("li#myCitiesListItem_" + strLinkname).toggleClass("selected");
        this.Selected = strLinkname;
    }

    function showEmptyMessage() {
        $("li#myCitiesListItem_empty").css("display", "block");
        $("li#myCitiesActionsGo").css("display", "none");
        $("li#myCitiesActionsRemove").css("display", "none");
        $("p#myCitiesPromptReady").css("display", "none");
    }
}
