/*! Chinese region picker - v0.0.1 - 2013-10-24 * https://github.com/xixilive/chineserp * Copyright (c) 2013 xixilive; Licensed MIT */ (function(a){"use strict";"indexOf"in Array.prototype||(Array.prototype.indexOf=function(a,b){void 0===b&&(b=0),0>b&&(b+=this.length),0>b&&(b=0);for(var c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1}),"forEach"in Array.prototype||(Array.prototype.forEach=function(a,b){for(var c=0,d=this.length;d>c;c++)c in this&&a.call(b,this[c],c,this)}),"map"in Array.prototype||(Array.prototype.map=function(a,b){for(var c=new Array(this.length),d=0,e=this.length;e>d;d++)d in this&&(c[d]=a.call(b,this[d],d,this));return c}),"filter"in Array.prototype||(Array.prototype.filter=function(a,b){for(var c,d=[],e=0,f=this.length;f>e;e++)e in this&&a.call(b,c=this[e],e,this)&&d.push(c);return d});var b=this,c=b.ChineseRegion={};c.$=a,c._caches={},c.$&&!c.$.getJSON&&b.Ajax&&(c.$.getJSON=function(){var a,c,d=arguments[0];switch(arguments.length){case 2:a=null,c=arguments[1];break;case 3:a=arguments[1],c=arguments[2]}"function"!=typeof c&&(c=function(){});var e=new b.Ajax.Request(d,{method:"get",parameters:a,evalJSON:"force",onSuccess:function(a){c(a.responseJSON)}});return e.transport}),c.getJSON=function(){return c.$.getJSON.apply(c.$,arguments)},c.ifn=function(a){return"function"==typeof a};var d=c.RegionCollection=function(a,b){return this.collection=a||[],this.name=b,this};d.prototype={constructor:d,select:function(a){return this.collection.filter(a)},map:function(a){return this.collection.map(a)},first:function(a){return this.select(a)[0]},findByNames:function(a,b,c){c=c||{},c.key=c.key||"n";var d=[];return a&&(d[0]=this.first(function(b){return b[c.key]===a})||this.collection[0]),b&&d[0]&&d[0].c&&(d[1]=d[0].c.filter(function(a){return a[c.key]===b})[0]||d[0].c[0]),d},findById:function(a){var b=[];return b[0]=this.first(function(b){return b.i===a})||this.first(function(b){return b.i===a.substr(0,4)+"00"})||this.collection[0],b[0]&&b[0].c&&(b[1]=b[0].c.filter(function(b){return b.i===a})[0]||b[0].c[0]),b}};var e=c.DataProxy=function(a,b){return this._remote=(a+"/").replace(/\/+/g,"/"),this.load("index",function(a,d){c.ifn(b)&&b(d,a)}),this};e.prototype={_index:function(a){return a.replace(/\d{4}$/,"0000")},_cacheid:function(a){return"cached_"+this._index(a)},_url:function(a){return this._remote+this._index(a)+".json"},load:function(a,b){var e=this,f=this._cacheid(a);return c.ifn(b)||(b=function(){}),c._caches[f]?(b(new d(c._caches[f],f),e),void 0):(c.getJSON(this._url(a),function(a){c._caches[f]=a,b(new d(a,f),e)}),void 0)},collections:function(){var a=[];for(var b in c._caches)a.push(new d(c._caches[b],b));return a},collection:function(a){var b=this._cacheid(a);return this.collections().filter(function(a){return a.name===b})[0]},indices:function(){return this.collection("index")}};var f=c.RegionPicker=function(a){var b=this;return this.options=a,new e(a.remote||"",function(a){b.initialize(a)}),b};f.prototype={initialize:function(a){this.proxy=a,c.ifn(this.options.initialized)&&this.options.initialized(this)},pick:function(){var a,b=arguments[0],d={};switch(arguments.length){case 2:a=arguments[1];break;case 3:d=arguments[1],a=arguments[2]}c.ifn(a)||(a=function(){});var e=this.proxy;return this.proxy?((null===b||""===b)&&(b=e.indices().collection[0].i),/^\d+$/.test(b)?(this._pickById(b,a),void 0):("string"==typeof b&&(b=b.split(/[,\s]+/,3)),this._pickByNames(b,d,a),void 0)):(a([]),void 0)},_pickedCollections:function(a){if(!a||!a.map)return[];var b=[];return a[0]&&b.push(this.proxy.collection("index").collection),a[1]&&b.push(this.proxy.collection(a[1].i).collection),a[2]&&a[1].c&&b.push(a[1].c),b},_pickById:function(a,b){var c=this,d=this.proxy,e=[];e[0]=d.indices().first(function(b){return b.i===a.substr(0,2)+"0000"}),d.load(a,function(d){e=e.concat(d.findById(a)),b(e,c._pickedCollections(e))})},_pickByNames:function(a,b,c){var d=this,e=this.proxy,f=[];return f[0]=e.indices().first(function(b){return b.n===a[0]}),f[0]?(e.load(f[0].i,function(e){f=f.concat(e.findByNames(a[1],a[2],b)),c(f,d._pickedCollections(f))}),void 0):(c([]),void 0)}}}).call(this,this.jQuery||this.$||this.Zepto||this.Prototype);