﻿Array.prototype.Clone = function() {
    var count = this.length;
    var list = new Array();

    for (var i = 0; i < count; i++) {
        list.push(this[i]);
    }

    return list;
};

var GameItem = function(sku, name, rank) {
    //Constructor section
    this.sku = sku;
    this.name = name;
    this.rank = rank;
    //Constructor section
}

var GameItemComparerByName = function(g1, g2) {
    //Constructor section
    var g1n = g1.name.toLowerCase();
    var g2n = g2.name.toLowerCase();
    //Constructor section

    return ((g1n < g2n) ? -1 : ((g1n > g2n) ? 1 : 0));
}


var GameItemComparerRank = function(g1, g2) {
    //Constructor section
    var g1n = g1.rank;
    var g2n = g2.rank;
    //Constructor section

    return ((g1n < g2n) ? -1 : ((g1n > g2n) ? 1 : 0));
}

var GamesListHash = function(guiChangerInst) {
    //Constructor section
    this.items = new Array();
    this.sortedByNameList = new Array();
    this.sortedByPopularityList = new Array();
    //Constructor section

    this.SortByName = function() {
        guiChangerInst.SetActiveByName();
        if (this.IsSortedByName() == false) {
            this.sortedByNameList = this.items.Clone();
            this.sortedByNameList.sort(GameItemComparerByName);            
        }
        this.ApplyWith(this.sortedByNameList);
    }

    this.SortByPopularity = function() {
        guiChangerInst.SetActiveByPopularity();
        if (this.IsSortedByPopularity() == false) {
            this.sortedByPopularityList = this.items.Clone();
            this.sortedByPopularityList.sort(GameItemComparerRank);            
        }
        this.ApplyWith(this.sortedByPopularityList);
    }

    this.IsSortedByName = function() {
        return this.sortedByNameList.length > 0;
    }

    this.IsSortedByPopularity = function() {
        return this.sortedByPopularityList.length > 0;
    }

    this.ApplyWith = function(elementsToReorder) {
        var limit = elementsToReorder.length;
        for (i = 0; i < limit; i++) {
            guiChangerInst.MoveItemBottom(elementsToReorder[i].sku);
        }
    }
}

var ListSortingState = function(listNode, byPopularityNode, byNameNode, activeClassName) {
    //Constructor section
    this.byPopularityNode = jQuery(byPopularityNode);
    this.byNameNode = jQuery(byNameNode);
    this.activeClassName = activeClassName;
    //Constructor section

    this.SetActiveByName = function() {
        this.SetInactiveByPopularity();
        if (!this.byNameNode.hasClass(this.activeClassName)) {
            this.byNameNode.addClass(this.activeClassName);
        }
    }

    this.SetInactiveByName = function() {
        if (this.byNameNode.hasClass(this.activeClassName)) {
            this.byNameNode.removeClass(this.activeClassName);
        }
    }

    this.SetActiveByPopularity = function() {
        this.SetInactiveByName();
        if (!this.byPopularityNode.hasClass(this.activeClassName)) {
            this.byPopularityNode.addClass(this.activeClassName);
        }
    }

    this.SetInactiveByPopularity = function() {
        if (this.byPopularityNode.hasClass(this.activeClassName)) {
            this.byPopularityNode.removeClass(this.activeClassName);
        }
    }

    this.MoveItemBottom = function(elementId) {
        listNode.appendChild(document.getElementById(elementId));
    }
}
