var l = new Array('Category','Shape','AlloyTemper','Type','Size','Thickness','Width','Length','Size','SizeA','SizeB','Web','Wall','Diameter'); 

var ProductFinder = {
     
    init : function()
    {
        var x = "";
        $("div[@class=options]").hide(); 
         
        $.getJSON("/admin/productfinder",{action:'start'},function(json)
        {
            $("select[@id=Category]").addOption(json);
            $("select[@id=Category]").selectOptions("0");
        });

        $("select[@id=Category]").change(function()
        {  
            $("div[@id=ProductFinder.Image]").html("");
            $("select[@class=SearchOption]").each(function()
            {
                $(this).unbind();
                $(this).empty();
                $("div[@id=ProductFinder."+$(this).attr("name")+"]").hide();  
                $("div[@id=MainContent]").hide();
            });
             
            if($(this).attr("value")=="0")
            {
                return;
            }
            
            $("div[@id=ProductFinder.Results]").hide();
            ProductFinder.ImageQuery("/products/image/Category/"+$("select[@id=Category]").val()+"");   
            $.getJSON("/admin/productfinder/Category/"+$("select[@id=Category]").val()+"",{action:'options'},function(json)
            {    
                
                ProductFinder.Message(json.message);
                
                $.each(json.data,function()
                {
                    if(this.hide)
                    { 
                        $("select[@id="+this.name+"]").empty();
                        $("div[@id=ProductFinder."+this.name+"]").hide();  
                        return;
                    }
                    $("select[@id="+this.name+"]").addOption(this.list); 
                    $("select[@id="+this.name+"]").selectOptions("0");
                    $("div[@id=ProductFinder."+this.name+"]").show();
                    $("select[@id="+this.name+"]").change(function()
                    {
                        $("select[@class=SearchOption]").attr("title","");
                        ProductFinder.NoChange(this.name);
                        ProductFinder.Action(); 
                        ProductFinder.Search(); 
                    });     
                });   
            });
        });   
                     
        $("div[@id=ProductFinder.Button]").show();
        $("div[@id=ProductFinder.Button]").click(function()
        {
            ProductFinder.Search();
        });
    },
    Message:function(i)
    {
        (!parseInt(i))?$("div[@id=ProductFinder.Message]").html(i):$("div[@id=ProductFinder.Message]").html("");
    },
    Image: function(i)
    {
        product.image = (parseInt(i.length))?'<img src="/products/img/'+i+'" /><br />Images do not necessarily represent the alloy specificied':'';
        $("div[@id=ProductFinder.Image]").html(product.image);         
    },
    ImageQuery:function(i)
    {
        var img = $.ajax({url:'/products/image'+i+'',async:false}).responseText;
        img = (parseInt(img.length))?img:'';
        ProductFinder.Image(img);     
    },
    
    Action:function()
    {
        if(ProductFinder.Unbind())
        {
            if(ProductFinder.SubOptions())
            {   
                return true;           
            }
        }      
    },
    
    NoChange:function(Name)
    {
        var C = "false";
        $("select[@id="+Name+"]").attr("title","ignore");
        for(i=0; i<l.length; i++) 
        {
            if(l[i]==Name)
            {
                C=i;
            }
            if(C == "false")
            {
                $("select[@id="+l[i]+"]").attr("title","ignore_a");
            }
        }
        return;  
    },
    
    Unbind:function()
    {
        $("select[@class=SearchOption]").unbind();
        return true;
    },
    
    Hide: function()
    {
        return;
        for(i=0; i<l.length; i++) 
        {
            if('active'==$("select[@id="+l[i]+"]").attr("class"))
            {
                $("div[@id=ProductFinder."+l[i]+"]").hide();      
            }
        }
        return true;
    },
    
    Qs  : function()
    {
        var a = new Object();  
        var b = new Array();
        var c = 0;
         
        for(i=0; i<l.length; i++)
        { 
            if((x = $("select[@id="+l[i]+"]").val())&&(x!=='0'))
            { 
                b[c] = $("select[@id="+l[i]+"]").attr("name")+"/"+x; c++;     
            }  
        }
        
        if(b.length>0)
        {
            a['path'] = b.join("/");   
            return a;
        }
    },
    
    SubOptions:function()
    {
        var i = false;
       
        if(i = ProductFinder.Qs())
        {         
            ProductFinder.ImageQuery('/'+i.path+'');
            $.getJSON("/admin/productfinder/"+i.path,{action:'options'},function(json)
            {
                
               
                
                $.each(json.data,function()
                {
                   if(this.hide>"0")
                   {
                        $("div[@id=ProductFinder."+this.name+"]").hide();
                        return true;
                   }
                   if(this.count>"0")
                   {
                        if($("select[@id="+this.name+"]").attr("title") == "ignore")
                        { 
                            $("select[@id="+this.name+"]").change(function()
                            {
                                $("select[@class=SearchOption]").attr("title","");
                                ProductFinder.NoChange(this.name);
                                ProductFinder.Action();
                                ProductFinder.Search();
                            }); 
                            return;
                            
                        }
                        if($("select[@id="+this.name+"]").attr("title") == "ignore_a")
                        {
                            $("select[@id="+this.name+"]").change(function()
                            {
                                $("select[@class=SearchOption]").attr("title","");
                                ProductFinder.NoChange(this.name);
                                ProductFinder.Action();
                                ProductFinder.Search();
                            }); 
                            return;
                        }
                        
                        var ii = (i=$("select[@id="+this.name+"]").attr('value'))?i:'0';
                        $("select[@id="+this.name+"]").empty(); 
                        $("select[@id="+this.name+"]").addOption(this.list); 
                        $("select[@id="+this.name+"]").selectOptions(ii);
                        $("div[@id=ProductFinder."+this.name+"]").show();
                        $("select[@id="+this.name+"]").change(function()
                        {
                            $("select[@class=SearchOption]").attr("title","");
                            ProductFinder.NoChange(this.name);
                            ProductFinder.Action();
                            ProductFinder.Search();
                        });
                        
                        return true; 
                   }     
                });     
            });
            $("div[@id=ProductFinder.Button]").show();     
        }    
        return true;
    },
    
    Search:function()
    {
        if(i = ProductFinder.Qs())
        {       
            $("div[@id=ProductFinder.Results]").hide();
            $("div[@id=ProductFinder.Results]").html("");
            $("div[@id=MainContent]").hide();   
            $.post("/admin/productfinder/"+i.path,{action:'search'},function(data)
            {
                $("div[@id=ProductFinder.Results]").html(""+data+""); 
                $("div[@id=ProductFinder.Results]").show();  
            });     
        }    
    },
    
    Browse:function()
    {
        if(product.path)
        { 
            ProductFinder.ImageQuery(''+product.path+''); 
            QueryString(product.path); 
            //$("div[@id=ProductFinder.Image]").html("");
            // $.getJSON("/admin/productfinder"+product.path,{action:'options'},function(json)
            //{
            //  var img = (json.image)?'<img src="/products/img/'+json.image+'" /><br />Images do not necessarily represent the alloy specificied':'';
            //  $("div[@id=ProductFinder.Image]").html(img);
            //});
            
        }    
    },
    
    Request:function(i)
    {
        alert(i);
    }
};
 
function QueryString(s)
{
    if(!parseInt(s.length))
    return;
    $("div[@id=ProductFinder.Results]").html(" "); 
    $.post("/admin/productfinder"+s,{action:'search'},function(data)
    {
        if(!parseInt(data.length))
        return;
        $("div[@id=ProductFinder.Results]").html(""+data+""); 
        $("div[@id=ProductFinder.Results]").show();  
    });            
}  

function DebugMsg(i)
{   
    if(i)
    {
      $("div[@id=ProductFinder.Dev]").html("<code>"+i+"</code>");
    }
}