﻿function get_width(obj){
var mainwidth = $(obj).parent().width();
//var imgwidth = $(obj).find('img').width();
var inputwidth;
inputwidth = mainwidth - 5;
//alert(mainwidth);alert(imgwidth);alert(inputwidth);
$(obj).find('input').width(inputwidth);
}


var sel_s=0;//表记第几个属性文本框获得焦点

function propertyData(fDivId,type,group)
{
 
    //创建fDivId内的内容
    $("#"+fDivId+"").append("<div class='input_container fl'><input id='property_"+type+group+"'  name='"+type+group+"' class='property_txt' /></div><input type='hidden' id='EnName_"+type+group+"'/>");
 
     //为创建的标签添加事件  
       $("#property_"+type+group+"").click(function(event){
        event.stopPropagation(); 
        $("#property_"+type+group+"").trigger("focus");
       })
     //文本框得到焦点
      
        $("#property_"+type+group+"").focus(function(){
          doselectlist();
        force_l=1;
			var ptp=$(".property_txt");
			ptp.each(function(k)
			{
			    $(ptp[k]).parent().parent().css({"position":""})
			})
			
		  $(this).parent().parent().css({"position":"relative"})
			
		  $(this).css('border-color','#FF6600');
			clearpro();

			    if($("#property_"+type+group+"").val()=="")
			    {

			         chkProperty(fDivId,type,group);			     
			    
			    }
			    else
			    {
			       
			      $("#property_"+type+group+"").trigger("keyup");
			    
			    }
	
			
        });
        
		$("#property_"+type+group+"").blur(function(){
			$(this).css('border-color','');
			ndoselectlist();
            
		});
        
   
        
        
        //文本框发生输入事件
        $("#property_"+type+group+"").keyup(function(event){

            var key=$(this).val();
           if(event.keyCode!=40&&event.keyCode!=38&&event.keyCode!=13&&event.keyCode!=37&&event.keyCode!=39)//输入键
           { 
             
                    if(key=="")
                    {
		                      if(event.keyCode!=9)
		                      {
		                    
		                      chkProperty(fDivId,type,group);
		                      }

                    }else
                    {
                    
		            clearpro();
		             
                         //下拉开始
                        //是否下拉框已经打开
                        if($("#porperty_list"+type+group+"").length==0)
                        {
                             $.ajax({
                                    url: "/Ajax/ReturnOperData.ashx",
                                    data: "t=0&key="+escape(key)+"&type="+type+"&group="+group,
                                    cache:false,         
                                    error:function(e){alert(e.toString())},
                                    success:function(data)
                                    {
                                         if(data.toString()!="err")
                                        {
                                         $("#property_"+type+group+"").parent().append("<div id='porperty_list"+type+group+"' class='porperty_list'>"+data+"<input type='hidden' name='group' value='"+group+"' /></div>");
                                         $("#property_"+type+group+"").parent().parent().parent().css("position","relative");
                                   				
                                        if(data=="")
                                        {
                                           $("#porperty_list"+type+group+"").css("display","none");
                                        }else
								        {
								             
								       
									         $("#porperty_list"+type+group+"").css("display","");
   								        }
                                     
                                        ulli(fDivId,type,group);
                                        }else
                                        {
                                            alert("保存出错！");
                                        
                                        }
                                    }
                                    
                            })  
                            
                        }else
                        {
                              $.ajax({
                                    url: "/Ajax/ReturnOperData.ashx",
                                    data: "t=0&type="+type+"&key="+escape(key)+"&group="+group,
                                    cache:false,         
                                    error:function(e){alert(e.toString())},
                                    success:function(data)
                                    {
                                         if(data.toString()!="err")
                                        {        
										        if(data=="")
										        {
										           $("#porperty_list"+type+group+"").css("display","none");
										        }else
										        {
										             
										         
											         $("#porperty_list"+type+group+"").css("display","");
											         $("#porperty_list"+type+group+"").html(data);
										        }
          
                                              ulli(fDivId,type,group);
                                        
                                        }else
                                        {
                                            alert("保存出错！");
                                        
                                        }
                                    }
                                    
                            })  
                        
                        }
                        //下拉结束
                    }
                 
         
                
             
        
         }else  //上下选择键
         {
             var lis;
             var len;
             var hov;
             //输入时提示下拉
            if($("#porperty_list"+type+group+"").css("display")!="none")
            {
               lis= $("#porperty_list"+type+group+" ul li");
               len=lis.length;
               hov= $("#porperty_list"+type+group+" ul li[class='li_hover']");
                  if(event.keyCode==38||event.keyCode==37)
                    {
                        lis.each(function(e){
                          
                            if(hov.length==0)
                            {
                                $(lis[len-1]).addClass("li_hover");
                                $("#property_"+type+group+"").val($(lis[len-1]).text());
                                 $("#EnName_"+type+group+"").val($(lis[len-1]).attr("name"));
                            }else
                            {
                                if($(lis[e]).hasClass("li_hover"))
                                {
                                    if(e==0)
                                    {
                                  
                                        $(lis[len-1]).addClass("li_hover");
                                        $(lis[e]).removeClass("li_hover");
                                        $("#property_"+type+group+"").val($(lis[len-1]).text());
                                        return false;
                                    }else
                                    {
                                        $(lis[e-1]).addClass("li_hover");
                                        $(lis[e]).removeClass("li_hover");
                                        $("#property_"+type+group+"").val($(lis[e-1]).text());
                                        return false;
                                    }
                                }
                            }
                        
                        })
                    }else if(event.keyCode==40||event.keyCode==39)
                    {
                         lis.each(function(e){
                            if(hov.length==0)
                            {
                                $(lis[0]).addClass("li_hover");
                                $("#property_"+type+group+"").val($(lis[0]).text());
                                return false;
                            }else
                            {
                               
                                if($(lis[e]).hasClass("li_hover"))
                                { 
                                    if(e==len-1)
                                    {
                                        $(lis[0]).addClass("li_hover");
                                        $(lis[e]).removeClass("li_hover");
                                        $("#property_"+type+group+"").val($(lis[0]).text());
                        
                                         return false;
                                    }else
                                    {
                                        $(lis[e+1]).addClass("li_hover");
                                        $(lis[e]).removeClass("li_hover");
                                        $("#property_"+type+group+"").val($(lis[e+1]).text());
                            
                                         return false;
                                    }
                                }
                            }
                        
                        })
                    
                    }else if(event.keyCode==13)//数据属性填写文本框添加 回车 切换焦点事件
                    {
                        var l_is=$(".property_txt");
                        var lenq=l_is.length;
                      
    
        					if(sel_s==lenq-1)
        					{
      
        					$(".property_img").trigger("click");
        					}else
        					{
        					  $(l_is[sel_s+1]).focus();
        					}	

                    }
                       
            }
            
            //获得焦点是下拉
           if($("#chkproperty"+type+group+"").css("display")!="none")
            {
              lis= $("#chkproperty"+type+group+" ul li");
              len=lis.length;
              hov= $("#chkproperty"+type+group+" ul li[class='li_hover']");
                 if(event.keyCode==38||event.keyCode==37)
                    {
                        lis.each(function(e){
                          
                            if(hov.length==0)
                            {
                                $(lis[len-1]).addClass("li_hover");
                                $("#property_"+type+group+"").val($(lis[len-1]).text());
           
                            }else
                            {
                                if($(lis[e]).hasClass("li_hover"))
                                {
                                    if(e==0)
                                    {
                                  
                                        $(lis[len-1]).addClass("li_hover");
                                        $(lis[e]).removeClass("li_hover");
                                        $("#property_"+type+group+"").val($(lis[len-1]).text());
                                        return false;
                                    }else
                                    {
                                        $(lis[e-1]).addClass("li_hover");
                                        $(lis[e]).removeClass("li_hover");
                                        $("#property_"+type+group+"").val($(lis[e-1]).text());
                                        return false;
                                    }
                                }
                            }
                        
                        })
                    }else if(event.keyCode==40||event.keyCode==39)
                    {
                         lis.each(function(e){
                            if(hov.length==0)
                            {
                                $(lis[0]).addClass("li_hover");
                                $("#property_"+type+group+"").val($(lis[0]).text());
                                 return false;
                            }else
                            {
                               
                                if($(lis[e]).hasClass("li_hover"))
                                { 
                                    if(e==len-1)
                                    {
                                        $(lis[0]).addClass("li_hover");
                                        $(lis[e]).removeClass("li_hover");
                                        $("#property_"+type+group+"").val($(lis[0]).text());
                                     //    $("#EnName_"+type+group+"").val($(lis[0]).find("span[name=chk_lname]").attr("class"));
                                         return false;
                                    }else
                                    {
                                        $(lis[e+1]).addClass("li_hover");
                                        $(lis[e]).removeClass("li_hover");
                                        $("#property_"+type+group+"").val($(lis[e+1]).text());
                                      //  $("#EnName_"+type+group+"").val($(lis[e+1]).find("span[name=chk_lname]").attr("class"));
                                         return false;
                                    }
                                }
                            }
                        
                        })
                    
                    }else if(event.keyCode==13)//数据属性填写文本框添加 回车 切换焦点事件
                    {
                        var l_is=$(".property_txt");
                        var lenq=l_is.length;
       				      
        					if(sel_s==lenq-1)
        					{

        					 $(".property_img").trigger("click");
        					}else
        					{
        					   $(l_is[sel_s+1]).focus();
        					}	


                    }
            }
          
    
         
         
         
         }
        
        })    

      
}


//用于前面单选的属性
function chkProperty(divid,type,group)
{
        
        clearpro();
        $("#"+divid).parents('tr').find('.property_txt').css('border-color','');
        
         //$("#"+divid).css('margin-top','1px');
         $("#"+divid+" .property_txt").css('border-color','#FF6600');
            

       //是否已经获取相关属性数据
            if($("#chkproperty"+type+group+"").length>0)
            {
           
                $("#chkproperty"+type+group+"").css("display","");
              
          
            
                  checkpro(divid,type,group);
            }else
            {
           
             //获取数据 创建层
                 $.ajax({
                        url: "/Ajax/ReturnOperData.ashx",
                        data: "t=1&type="+type+"&group="+group,
                        cache:false,         
                        error:function(e){alert(e.toString())},
                        success:function(data)
                        {
                      
                             if(data.toString()!="err")
                            {
                                    if(type==5)
                                    {
                                        if(document.getElementById('chkproperty'+type+group)==null)
                                        {
                                            $("#"+divid+"").append("<div id='chkproperty"+type+group+"' class='chk_propertys' >"+data+"<input type='hidden' name='group' value='"+group+"' /></div>");
                            
                                            $("#chkproperty"+type+group+"").parent().css("position","relative");
                                            $("#chkproperty"+type+group+"").css({"top":"20px","right":"0"});  
                                        }
                                    
                                    }else if(type==999)
                                    {
                                         if(document.getElementById('chkproperty'+type+group)==null)
                                        {
                                            $("#"+divid+"").append("<div id='chkproperty"+type+group+"' class='chk_propertys ' >"+data+"<input type='hidden' name='group' value='"+group+"' /></div>");
                            
                                            $("#chkproperty"+type+group+"").parent().css("position","relative");
                                            $("#chkproperty"+type+group+"").css({"top":"20px","right":"0"});  
                                        }
                                    
                                    }else
                                    {
                                         if(document.getElementById('chkproperty'+type+group)==null)
                                        {
                                            $("#"+divid+"").append("<div id='chkproperty"+type+group+"' class='chk_propertys'>"+data+"<input type='hidden' name='group' value='"+group+"' /></div>");
                            
                                            $("#chkproperty"+type+group+"").parent().css("position","relative");
                                            $("#chkproperty"+type+group+"").css({"top":"20px","left":"0"});  
                                        }
                                    
                                    }
                                change_height(type,group)
                                //单击li触发单击checkbox事件
                                var li_c=$("#chkproperty"+type+group+" span[name=chk_lname]");
                                 li_c.each(function(m){
                               
                                    $(li_c[m]).click(function(){
                                         $(li_c[m]).parent().find("input[type=checkbox]").trigger("click");
                                    
                                    })
                         
                                }) 
                   
                    
                           
                            }else
                            {
                                alert("保存出错！");
                            
                            }
                           
                             checkpro(divid,type,group);
                        
                        }
                        
                })  
                
              }
              
    
    
   // })

}

//绑定 属性选择 单击事件
    function checkpro(divid,type,group)
    { 
       
       var chk= $("#chkproperty"+type+group+" li");
          chk.each(function(m){
          //单击
           $(chk[m]).unbind("click");
            $(chk[m]).click(function(){
                  $("#"+divid+" .property_txt").val($(this).find("span[name=chk_lname]").text()); 
                  $("#EnName_"+type+group+"").val($(this).find("span[name=chk_lname]").attr("class"));       
                  $("#chkproperty"+type+group+"").css("display","none");		    
       
            })
 
        }) 

       var chk_li= $("#chkproperty"+type+group+" li");
       chk_li.each(function(m){
        //鼠标移入
           $(chk_li[m]).hover(function(){
                $(chk_li[m]).addClass("li_hover");
           },function(){
               $(chk_li[m]).removeClass("li_hover");
           
           })
       
       })
     

    }
 
 function clearpro()
 {
   var  alllis=$(".porperty_list");
   var  allchk=$(".chk_propertys");
   alllis.each(function(m)
   {
     $(this).css("display","none");
   
   })
   
   allchk.each(function(n)
   {
    $(this).css("display","none");
   
   })
 
 }     


//绑定 下拉出属性的单击事件
function ulli(divid,type,group)
{
    var lis=$("#"+divid+" ul li");
    var len=lis.length;
    lis.each(function(e){
        $(lis[e]).click(function(event){
           
            $("#"+divid+" .property_txt").val($(this).text());
            $("#EnName_"+type+group+"").val($(this).attr("name"));
            $("#porperty_list"+type+group+"").html("");
            $("#porperty_list"+type+group+"").css("display","none");
 
        })
        
 //鼠标移入
           $(lis[e]).hover(function(){
                $(lis[e]).addClass("li_hover");
           
           },function(){
               $(lis[e]).removeClass("li_hover");
                
           
           })

    })
}


function option_focus()
{
     var l_is=$(".property_txt");
        l_is.each(function(m){
           $(l_is[m]).focus(function(){                  
                 sel_s=m;
                
           });                    
        })

}

$(document).ready(function(){

 $("body").click(function(e){  
     clearpro();  
  })

})






function datachange_s()
{
    //单价 
      var p=$("#pp input[name=price]");
      p.each(function(e){
            $(p[e]).change(function(){
              index_s=e;  
            })
          
      })
    
    //库存
      var c=$("#pp input[name=kc]");
        c.each(function(e){
            $(c[e]).change(function(){
              index_s=e;  
            })
          
      })

}

//自适应 改变加载 属性的宽度
function change_height(type,group)
{
     var m_nb=15;  //每列最高个数
     var m_lb=3;  //默认列数      
     var li_w=100;//单个li宽度   
     var listCount=$("#chkproperty"+type+group+" ul li span[name=chk_lname]").length;
    
     if(Math.ceil(listCount/m_nb)>m_lb)
     {
        m_lb=Math.ceil(listCount/m_nb);
     }
     
     
        var ul_w=m_lb*li_w;//ul宽度
        $("#chkproperty"+type+group+" ul").css("width",""+ul_w+"px");

}



function doselectlist()
{
    if(navigator.appName == "Microsoft Internet Explorer")
    {
         //alert(navigator.appVersion);
           //if(navigator.appVersion.match(/6./i)=='6.')
           if(navigator.appVersion.indexOf('MSIE 6')>0 )
           {
                 // alert( '当前浏览器是IE 6' )
                //价格排序
                //var p=$("#ctl00_ContentPlaceHolder1_ddlPrice").find("option:selected").text();
                $("#ctl00_ContentPlaceHolder1_ddlPrice").hide();
                //$("#ctl00_ContentPlaceHolder1_ddlPrice").parent().append("<span class='s_price' name='moni'>"+p+"</span>")
                //日期范围
                //var d=$("#ctl00_ContentPlaceHolder1_ddlEffectDate").find("option:selected").text();
                $("#ctl00_ContentPlaceHolder1_ddlEffectDate").hide();
                //$("#ctl00_ContentPlaceHolder1_ddlEffectDate").parent().append("<span class='s_date'  name='moni'>"+d+"</span>")
           }

    } 


}


function ndoselectlist()
{
//价格排序
$("#ctl00_ContentPlaceHolder1_ddlPrice").show();
//$("#ctl00_ContentPlaceHolder1_ddlPrice").parent().find("span[name=moni]").remove();
//日期范围
$("#ctl00_ContentPlaceHolder1_ddlEffectDate").show();
//$("#ctl00_ContentPlaceHolder1_ddlEffectDate").parent().find("span[name=moni]").remove();

}






