(function($){$.fn.ld=function(options){var opts;var DATA_NAME="ld";if(typeof options=="string"){if(options=="api"){return $(this).data(DATA_NAME)}}else{var options=options||{};opts=$.extend(true,{},$.fn.ld.defaults,options)}if($(this).size()>0){var ld=new yijs.Ld(opts);ld.$applyTo=$(this);ld.render();$(this).data(DATA_NAME,ld)}return $(this)};var yijs=yijs||{};yijs.Ld=function(options){this.options=options;this.$applyTo=this.options.applyTo&&$(this.options.applyTo)||null;this.cachePrefix="data_";this.OPTIONS_CLASS="ld-option";this.cache={}};yijs.Ld.prototype={render:function(){var _that=this;var _opts=this.options;if(this.$applyTo!=null&&this.size()>0){_opts.style!=null&&this.css(_opts.style);this.load(_opts.defaultLoadSelectIndex,_opts.defaultParentId);_opts.texts.length>0&&this.selected(_opts.texts);this.$applyTo.each(function(i){$(this).bind(_opts.drevent+".ld",{target:_that,index:i},_that.onchange)})}return this},texts:function(ts){var that=this;var $select=this.$applyTo;var _arr=[];var txt=null;var $options;$select.each(function(){txt=$(this).children("."+that.OPTIONS_CLASS+":selected").text();_arr.push(txt)});return _arr},size:function(){return this.$applyTo.size()},css:function(style){style&&this.$applyTo.css(style);return this},load:function(selectIndex,parent_id,callback){var _that=this;if(parent_id.length==0){$("#load_layer").hide();return}for(var i=selectIndex;i<_that.size();i++){_that.removeOptions(i)}var _ajaxOptions=this.options.ajaxOptions;var _d=_ajaxOptions.data;var _parentIdField=this.options.field["parent_id"];_d[_parentIdField]=parent_id;_ajaxOptions.data=_d;_ajaxOptions.success=function(data){$("#load_layer").hide();$(".finecms-select-category").each(function(){if($(this).attr("iid")>selectIndex){$(this).hide()}});if(data.length>0){_that.$applyTo.eq(selectIndex).show();$("#dr_post_submit").hide()}else{_that.$applyTo.eq(selectIndex).hide();if($("#dr_catid_"+parent_id).attr("child")==1){dr_tips("此栏目无法发布或者权限不够");return}$("#dr_post_submit").show();return}var _h=_that._getOptionsHtml(data);_that._create(_h,selectIndex);_that.$applyTo.eq(selectIndex).trigger("afterLoad.ld");if(callback){callback.call(this)}};$.ajax(_ajaxOptions)},removeOptions:function(index){this.$applyTo.eq(index).children("."+this.OPTIONS_CLASS).remove();return this},selected:function(t,completeCallBack){var _that=this;if(t&&typeof t=="object"&&t.length>0){var $select=this.$applyTo;_load(_that.options.defaultLoadSelectIndex,_that.options.defaultParentId)}function _load(selectIndex,parent_id){_that.load(selectIndex,parent_id,function(){var id=_selected(selectIndex,t[selectIndex]);selectIndex++;if(selectIndex>_that.size()-1){if(completeCallBack){completeCallBack.call(this)}return}_load(selectIndex,id)})}function _selected(index,text){var id=0;_that.$applyTo.eq(index).children().each(function(){if(text&&text.toString()==$(this).attr("value")){$(this).attr("class","onbox dr_catid_"+(index+1));id=$(this).attr("value");return}});return id}return this},onchange:function(e){var _that=e.data.target;var index=e.data.index;var $target=$(e.target);var _parentId=$target.val();var _i=index+1;$(".dr_catid_"+_i).attr("class","dr_catid_"+_i);$("#dr_catid_"+_parentId).attr("class","onbox dr_catid_"+_i);$("#load_layer").show();$("#dr_post_catid").val($("#dr_catid_"+_parentId).attr("value"));_that.load(_i,_parentId)},_getOptionsHtml:function(data){var _that=this;var ajaxOptions=this.options.ajaxOptions;var dataType=ajaxOptions.dataType;var field=this.options.field;var _h="";_h=_getOptions(data,dataType,field).join("");function _getOptions(data,dataType,field){var optionClass=_that.OPTIONS_CLASS;var aStr=[];var id,name,child;$.each(data,function(i){id=data[i][field.region_id];name=data[i][field.region_name];child=data[i][field.region_child];var _option="<li id='dr_catid_"+id+"' style='"+(child==1?"":"background-image:none")+"' value='"+id+"' child='"+child+"'>"+name+"</li>";aStr.push(_option)});return aStr}return _h},_create:function(_h,index){var _that=this;this.removeOptions(index);this.$applyTo.eq(index).html(_h)}};$.fn.ld.defaults={selects:null,drevent:"change",ajaxOptions:{url:null,type:"get",data:{},dataType:"json",success:function(){},beforeSend:function(){}},defaultParentId:0,defaultLoadSelectIndex:0,texts:[],style:null,change:function(){},field:{parent_id:"parent_id",region_id:"region_id",region_name:"region_name",region_child:"region_child"}}})(jQuery);