// loading js
jQuery("<script type=\"text\/javascript\" src=\"/dwr/interface/Industry.js\"><"+"\/scr"+"ipt>").prependTo("head");
jQuery("<script type=\"text\/javascript\" src=\"/components/gsuUtils/gsuSelect.js\"><"+"\/scr"+"ipt>").prependTo("head");
jQuery("<link rel=\"stylesheet\" href=\"/components/button/v1/sliding.css\" type=\"text\/css\" media=\"screen\" \/>").prependTo("head");
// create temp element
var jqmWindowIndustry=""
  +"<div class=\"jqmWindow\" id=\"jqmWindowSelectIndustry\" style=\"z-index:3001\">"
  +"<input type=\"hidden\" id=\"jqmWindowSelectIndustryCurrent\" value=\"\"/>"
  + "<select name=\"industryParent\" id=\"industryParent\" size=\"15\" style=\"height:150px;width:180px;\">"
  + "</select>&nbsp;"
  + "<select name=\"industryChild\" id=\"industryFirstChild\" size=\"15\" style=\"height:150px;width:180px;\">"
  + "</select>&nbsp;"
  + "<select name=\"industrySecondly\"  id=\"industrySecondly\" size=\"15\" style=\"height:150px;width:180px;\">"
  + "</select>"
  + "<br/><br/><ul class=\"blue\"><li><a href=\"javascript:void(0);\" class=\"current refreshIndustry\" style=\"display:none\"><span></span>刷新</a></li><li><a href=\"javascript:void(0);\" class=\"current closeIndustry\"><span></span>选好了</a></li></ul>"
  + "</div>";
/*
 * 客户端代码使用方法： <br>
 * 1. 嵌入jQuery，嵌入dwr必备js，嵌入jModal: 注意顺序在dwr必备js之后，另外dwr必备js不允许嵌入多次 <br>
 * 2. 在被绑定域(如：id=myindustry的输入框)HTML代码的后面，或者window.onload方法中执行: <br>
 * jQuery("#myindustry").gsuIndustryUtils({<br>
 *   readOnly:false,<br>
 *   border:"solid 2px red",<br>
 *   loadByChain:true<br>
 * }).industryChooser(); <br> 或者
 * 3、绑定一个列表，class均为industrys：<br>
 *  jQuery(".ownindustry").gsuIndustryUtils({
 *    readOnly:true
 *  }).industryChains();
 */
/*
 * GsuIndustryUtils 类定义
 */
(function($) {
$.fn.gsuIndustryUtils=function(o){
  var p={
    overlay: 50,
    width:100,
    border:"solid 1px gray",
    readOnly : F,
    modal : F,
    loadByChain : F,
    toTop : true,
    overflow : "visible",
    change : F
  };
  return this.each(function(){
    if(this._ind) {
      return HashIndustry[this._ind].c=$.extend({},HashIndustry[this._ind].c,o);
    }
    sInd++;
    this._ind=sInd;
    $(this).after("<div class=\"gsu_util_industry_box gsu_util_industry_box"+sInd+"\">"
             +"<input type=\"button\" title=\"清空数据\" id=\"gsu_util_industry_clear"+sInd+"\" class=\"gsu_util_industry_clear\" value=\"×\">"
             +"<input type=\"button\" title=\"选择数据\" id=\"gsu_util_industry_chooser"+sInd+"\" class=\"gsu_util_industry_chooser\" value=\"…\">"
             +"<div id='gsu_util_industry"+sInd+"' class='gsu_util_industry gsu_util_industry"+sInd+"'/>"
             +"</div>");
    HashIndustry[sInd]={
      c:$.extend(p,$.gsuIndustryUtils.params,o),
      w:$(this).addClass('industryID'+sInd).css('display','none'),
      sInd:sInd
    };
    $("div.gsu_util_industry_box"+sInd).css({width:HashIndustry[sInd].c.width,height:"20px","text-indent":"2px",height:"20px","line-height":"20px",border:HashIndustry[sInd].c.border,"font-size":"12px",color:"black"});
    if(ie6)$("div.gsu_util_industry"+sInd).css({width:HashIndustry[sInd].c.width-52,"white-space":"nowrap"});
    else $("div.gsu_util_industry"+sInd).css({"white-space":"nowrap","text-overflow":"ellipsis"});
    if(HashIndustry[sInd].c.overflow) $("div.gsu_util_industry"+sInd).css({"overflow":HashIndustry[sInd].c.overflow});
    $("input#gsu_util_industry_clear"+sInd).css({float:"right","margin-top":"3px","margin-right":"3px",width:"20px",height:"13px",border:"dotted 1px green",background:"#FFF",color:"red","font-weight":"bold"});
    $("input#gsu_util_industry_chooser"+sInd).css({float:"right","margin-top":"3px","margin-right":"3px",width:"20px",height:"13px",border:"dotted 1px green",background:"#FFF","font-weight":"bold"});
    if($("#jqmWindowSelectIndustry").html()==null){ // 第一次创建预备弹出的窗体
      $(jqmWindowIndustry).insertBefore("body");
      $("#jqmWindowSelectIndustry").jqm({
	    closeClass : 'closeIndustry',
	    toTop : HashIndustry[sInd].c.toTop,
	    modal : HashIndustry[sInd].c.modal,
	    onShow : function(hash){
	      hash.w.show(); 
	    }
	  });
	  Industry.getTopIndustrys(function(data){
	    DWRUtil.removeAllOptions("industryParent");
	    DWRUtil.addOptions("industryParent", data, 'industryid', 'industryname');
      });
    }
    var _sInd=sInd;
    $("input#gsu_util_industry_clear"+_sInd).click(function(){
      if($("div.gsu_util_industry"+_sInd).text()=="") {alert("没有数据，不需要清空");return;}
        $("div.gsu_util_industry"+_sInd).text("");
      HashIndustry[_sInd].w.val("");
      $("#industryParent").setSelectedValue("0");
      $("#jqmWindowSelectIndustryCurrent").val("");
      DWRUtil.removeAllOptions("industrySecondly");
      DWRUtil.removeAllOptions("industryFirstChild");
      $(".gsu_util_industry_box"+_sInd).focus();
    });
    if(HashIndustry[this._ind].c.readOnly){
      $("#gsu_util_industry_chooser"+this._ind).remove();
      $("#gsu_util_industry_clear"+this._ind).remove();
    }
    else{
      var _sInd=sInd;
      $("#gsu_util_industry_chooser"+sInd).click(function(){
        $("#gsu_util_industry_chooser"+_sInd).blur();
        $(".gsu_util_industry" + _sInd).click();
      });
    }
    if(!HashIndustry[this._ind].c.readOnly)
    $("#gsu_util_industry" + sInd).click(function(){
      $(".refreshIndustry").unbind("click");
      $(".refreshIndustry").click(function(){
    	  Industry.refreshCache(function(data){
    		  DWRUtil.removeAllOptions("industryParent");
    		  DWRUtil.removeAllOptions("industrySecondly");
    	      DWRUtil.removeAllOptions("industryFirstChild");
  			  $(".closeIndustry").click();
  		  });
  	  });
      var ind=$(this).attr("id").substring("gsu_util_industry".length);
      var val=$(this).parent().prev().val();
      if($("#jqmWindowSelectIndustryCurrent").val()==""||$("#jqmWindowSelectIndustryCurrent").val()!=val){
        // 如果打开的是原来打开过的窗口，直接打开即可，否则还需要刷新数据。
        DWRUtil.removeAllOptions("industrySecondly");
        DWRUtil.removeAllOptions("industryFirstChild");
        if(HashIndustry[ind].c.loadByChain){
          // 根据当前行业链，找到链条中所有行业标识，然后按照顺序，把第二级，第三级的行业全部加载出来，并指定选中行业。
          if(HashIndustry[ind].w.val()!="")
          Industry.getIndustryChainsByChain(HashIndustry[ind].w.val(), function(data) {
            var data0=data[0],data1=data[1],data2=data[2];
            $.gsuIndustryUtils.loadsecondandthirdindustry(data0,data1,data2);
          });
        }
        else{
          // 根据当前行业标识，找到链条中所有行业标识，然后按照顺序，把第二级，第三级的行业全部加载出来，并指定选中行业。
          if(HashIndustry[ind].w.val()!="")
          Industry.getIndustryChains(parseInt(HashIndustry[ind].w.val()), function(data) {
            var data0=data[0],data1=data[1],data2=data[2];
            $.gsuIndustryUtils.loadsecondandthirdindustry(data0,data1,data2);
          });
        }
        $("#jqmWindowSelectIndustryCurrent").val(val);
        // 创建选择事件
        $("#industryParent").unbind("click");
        $("#industryFirstChild").unbind("click");
        $("#industrySecondly").unbind("click");
        $("#industryParent").click(function(){
          DWRUtil.removeAllOptions("industrySecondly");
          DWRUtil.removeAllOptions("industryFirstChild");
          $.gsuIndustryUtils.loadsecondandthirdindustry($(this).val(),null,null);
          if(HashIndustry[ind].c.loadByChain){
            if(HashIndustry[ind].w.attr("value")==null) HashIndustry[ind].w.text($(this).children("option:selected").text());
            else HashIndustry[ind].w.val($(this).children("option:selected").text());
          }else{
            if(HashIndustry[ind].w.attr("value")==null) HashIndustry[ind].w.text($(this).val());
            else HashIndustry[ind].w.val($(this).val());
          }
          $.gsuIndustryUtils.loadchainstring(ind);
        });
        $("#industryFirstChild").click(function(){
          if($(this).val()!=null){
            DWRUtil.removeAllOptions("industrySecondly");
            $.gsuIndustryUtils.loadthirdindustry($(this).val(),null);
            if(HashIndustry[ind].c.loadByChain){
              if(HashIndustry[ind].w.attr("value")==null) HashIndustry[ind].w.text($("#industryParent option:selected").text() + " >> " +$(this).children("option:selected").text());
              else HashIndustry[ind].w.val($("#industryParent option:selected").text() + " >> " +$(this).children("option:selected").text());
            }else{
              if(HashIndustry[ind].w.attr("value")==null) HashIndustry[ind].w.text($(this).val());
              else HashIndustry[ind].w.val($(this).val());
            }
            $.gsuIndustryUtils.loadchainstring(ind);
          }else{
            alert("没有数据可供选择");
          }
        });
        $("#industrySecondly").click(function(){
          if($(this).val()!=null){
            if(HashIndustry[ind].c.loadByChain){
              if(HashIndustry[ind].w.attr("value")==null) HashIndustry[ind].w.text($("#industryParent option:selected").text() + " >> " + $("#industryFirstChild option:selected").text() + " >> " + $(this).children("option:selected").text());
              else HashIndustry[ind].w.val($("#industryParent option:selected").text() + " >> " + $("#industryFirstChild option:selected").text() + " >> " + $(this).children("option:selected").text());
            }else{
              if(HashIndustry[ind].w.attr("value")==null) HashIndustry[ind].w.text($(this).val());
              else HashIndustry[ind].w.val($(this).val());
            }
            $.gsuIndustryUtils.loadchainstring(ind);
          }else{
            alert("没有数据可供选择");
          }
        });
      }
      $("#jqmWindowSelectIndustry").jqmShow();
    });
    return $(this);
  });
};
$.fn.industryChains=function(){ // 加载table 的列表，自动将所有同一个class的隐藏域的id转化成行业链文字说明
  var industryIdArray = new Array();
  var cls;
  this.each(function(){
    cls=(cls==undefined)?HashIndustry[this._ind].w.attr("class").split(" ")[0]:cls;
  });
  $("."+cls).each(function(index){
    if($(this).val())
    industryIdArray[index]=$(this).val();
    else
    industryIdArray[index]=$(this).text();
  });
  Industry.getIndustryChainStrings(industryIdArray,	function(data) {
    $("."+cls).each(function(index){
      $(this).next(".gsu_util_industry_box").css("border","0");
      $(this).next(".gsu_util_industry_box").children(".gsu_util_industry").text(data[index]);
    });
  });
};
$.fn.industryChooser=function(){
  return this.each(function(){
    $.gsuIndustryUtils.open(this._ind);
  });
};
$.gsuIndustryUtils={
  hash:{},
  open:function(ss){
    var o=HashIndustry[ss].w;
    $.gsuIndustryUtils.loadchainstring(ss);
  },
  autowidth:function(ss,text){
    if(ie6){
      var a1=$(".gsu_util_industry"+ss).width();
      var a2=$(".gsu_util_industry_box"+ss).width();
      if(a1+62>a2){
        $(".gsu_util_industry_box"+ss).width(a1+62);
        $(".gsu_util_industry_box"+ss).css("width",(a1+62)+"px");
      }
    }else{
      var x=text.length*14-20;
      var y=$(".gsu_util_industry_box"+ss).width();
      $(".gsu_util_industry_box"+ss).css("width",(x>y?x:y)+"px");
    }
  },
  loadchainstring:function(ss){
    var val=HashIndustry[ss].w.val();
    if(!val) val=HashIndustry[ss].w.text();
    if(HashIndustry[ss].c.loadByChain){
      $(".gsu_util_industry"+ss).text(val);
      $(".gsu_util_industry"+ss).attr("title",val);
      $.gsuIndustryUtils.autowidth(ss,$.trim(val));
      $.gsuIndustryUtils.changeEvent(ss);
    }
    else if(val!=null && val!="" && !isNaN(val)){
      Industry.getIndustryChainString(parseInt(val),function(data){
        $(".gsu_util_industry"+ss).text($.trim(data));
        $(".gsu_util_industry"+ss).attr("title",$.trim(data));
        $.gsuIndustryUtils.autowidth(ss,data);
        $.gsuIndustryUtils.changeEvent(ss);
      });
    }
    else{
      $("#jqmWindowSelectIndustryCurrent").val("");
      if(HashIndustry[ss].w.attr("value")==null) HashIndustry[ss].w.text("-1");
      else HashIndustry[ss].w.val(-1); //默认选中第一个行业
      Industry.getIndustryChainString(-1,function(data){
        $(".gsu_util_industry"+ss).text($.trim(data));
        $(".gsu_util_industry"+ss).attr("title",$.trim(data));
        $.gsuIndustryUtils.autowidth(ss,data);
        $.gsuIndustryUtils.changeEvent(ss);
      });
    }
  },
  changeEvent:function(ss){
    var val=$(".gsu_util_industry"+ss).text();
    if(HashIndustry[ss].c.change)HashIndustry[ss].c.change(val);
  },
  loadsecondandthirdindustry:function(data0,data1,data2){
    if (data0 != undefined && data0 != null) {
      Industry.getIndustrys(data0,function(data){
	    DWRUtil.addOptions("industryFirstChild", data, 'industryid','industryname');
	    $("#industryFirstChild option[value='" + data1 + "']").attr('selected', "true");
	    //$("#industryFirstChild option[@value='" + data1 + "']").attr('selected', "true");
	  });
      $("#industryParent option[value='" + data0 + "']").attr('selected', "true");
      //$("#industryParent option[@value='" + data0 + "']").attr('selected', "true");
      $.gsuIndustryUtils.loadthirdindustry(data1,data2);
    }
  },
  loadthirdindustry:function(data1,data2){
    if (data1 != undefined && data1 != null) {
      Industry.getIndustrys(data1,function(data){
	    DWRUtil.addOptions("industrySecondly", data, 'industryid', 'industryname');
	    $("#industrySecondly option[value='" + data2 + "']").attr('selected', "true");
	    //$("#industrySecondly option[@value='" + data2 + "']").attr('selected', "true");
	  });
    }
  },
  params:{}
};
var sInd=0,HashIndustry=$.gsuIndustryUtils.hash,ie6=$.browser.msie&&($.browser.version == "6.0"),F=false
})(jQuery);
