(function($) { $.fn["Taxonomy"] = function(shops) { this.shops = shops; this.nestResult = new Array(); this.numberingResult = new Array(); this.keySort = function(target) { var sortKey = new Array(); for (var compkey in target) { sortKey.push(compkey); } sortKey.sort(function (b1, b2) {return b1 > b2 ? 1 : -1;}); var result = new Array(); for (var resetkey in sortKey) { var key = sortKey[resetkey]; result[key] = target[key]; } return result; } this.classifyLines = function() { for (var i = 0; i < this.shops.length; i++) { var shop = this.shops[i]; if (this.nestResult[shop.lineName] == undefined) { this.nestResult[shop.lineName] = new Array(); } this.nestResult[shop.lineName].push(shop); } var tmp = this.nestResult; this.nestResult = this.keySort(tmp); } this.classifyStations = function() { for (lineID in this.nestResult) { var stations = this.nestResult[lineID]; var shops = new Array(); for (var i = 0; i < stations.length; i++) { var station = stations[i]; if (shops[station.stationName] == undefined) { shops[station.stationName] = new Array(); } shops[station.stationName].push(station); } var tmp = shops; shops = this.keySort(tmp); this.nestResult[lineID] = shops; } }, this.numbering = function() { var l = new Array(); for (lineID in this.nestResult) { var stations = this.nestResult[lineID]; var s = new Array(); for (stationID in stations) { s.push({ "name": stationID, "shops": stations[stationID] }); } l.push({ "name": lineID, "stations": s }); } this.numberingResult = l; return this.numberingResult; }, this.getLine = function(lineID) { return this.numberingResult[lineID]; }, this.getStation = function(lineID, stationID) { var lines = this.getLine(lineID); return lines.stations[stationID]; }, this.getHome = function(lineID, stationID, homeID) { var station = this.getStation(lineID, stationID); return station.shops[homeID]; }, this.classify = function() { this.classifyLines(); this.classifyStations(); this.numbering(); return this.numberingResult; } return this; }; })(jQuery);