
var xmlHttp = null;

var Host = "";
var Locale = "";


function GetHint(SearchTerm)
{
    if(SearchTerm.length>2)
    {
        xmlHttp = GetXmlHttpObject();
        xmlHttp.onreadystatechange = StateChanged;
        
        var url = "/files/ajax/livesearch.php?sid="+Math.random();
        var params = "q="+SearchTerm;
        xmlHttp.open("POST", url, true);
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlHttp.send(params);
    }
    else
    {
        RemoveSearchHints();
    }
    
}


function SetLocale()
{
    var url = window.location.toString();
    
    var splitUrl = url.split("/");
    Host = splitUrl[0] + "//" + splitUrl[2];
    
    Locale = splitUrl[3];

    if ((Locale.substring(2,3) !== "-"))
    {
        Locale = "gb-en-gbp";
    }
}

function StateChanged()
{
    if(xmlHttp.readyState==4)
    {
        switch(xmlHttp.status)
        {
            case 200:
            
                RemoveSearchHints();
                
                var response = YAHOO.lang.JSON.parse(xmlHttp.responseText); 
                
                if(response.products.length>0)
                {
                
                    var table = document.createElement("table");
                    table.setAttribute("id", "search_hint");
                    table.style.cellPadding = 0;
                    table.style.cellSpacing = 0;
                 
                    document.getElementById("search_hint_container").appendChild(table);
                    
                    
                    
                    //alert(response.products);
                    
                    for(var products in response.products)
                    {
                        //alert(response.products[product]);
                        //for(var i in response.products[products])
                        //{
                            
                            var product = response.products[products];
                            
                            if(!Locale) SetLocale();
                            
                            var productRow = table.insertRow(-1);
                            productRow.onmouseover = function(){this.className="search_product_highlight";};
                            productRow.onmouseout = function(){this.className="search_product";};
                            productRow.setAttribute("link", Host+"/"+Locale+"/"+product.link);
                            productRow.onclick = function(){RemoveSearchHints();document.location = this.getAttribute("link")};
                            //productimagelink.onclick = RemoveSearchHints;
                            
                            
                            
                            productRow.setAttribute("class", "search_product");
                            
                            
                            var productimagelink = document.createElement("a");
                            //productimagelink.href = Host+"/"+Locale+"/"+product.link;
                            productimagelink.onclick = RemoveSearchHints;
                            
                            var imageCellTable = document.createElement("table");
                            imageCellTable.style.background = "none";
                            imageCellTable.style.backgroundColor = "#FFFFFF";
                            var imageCellRow = imageCellTable.insertRow(-1);
                            var imageCellCell= imageCellRow.insertCell(-1);
                            imageCellCell.style.height = "40px";
                            imageCellCell.style.width = "40px";
                            imageCellCell.style.verticalAlign = "middle";
                            imageCellCell.style.textAlign = "center";
                            imageCellCell.style.background = "none";
                            
                            //imageCellCell.style.backgroundColor = "#FFFFFF";
                            
                            
                            var productimage = document.createElement("img");
                            //alert("../ajax/imageresize.php?image="+product.image+"&height=40&width=40");
                            productimage.src = "/files/ajax/imageresize.php?image=../"+product.image+"&limit=40";
                            //productimage.style.position = "relative";
                            //productimage.style.top = "50%";
                           // alert(productimage.style.top.replace("px", ""));
                            //productimage.style.top = productimage.style.top.replace("px", "") - productimage.height+"px";
                            
                            imageCellCell.appendChild(productimage);
                            //imageTable.insertRow(imageTable.insertCell(-1).appendCell(productimage));
                            //imageDiv.appendChild(productimage);
                            
                            
                            //imageDiv.appendChild(document.createElement("table").insertRow(-1).insertCell(-1).appendChild(productimage));
                            //productimage.style.height = "40px";
                            //productimage.style.width = "40px";
                            //productimage.styleFloat = "left";
                          
                            //productimagelink.appendChild(productimage);
                            
                            imageCell = productRow.insertCell(-1);
                            imageCell.appendChild(imageCellTable);
                            imageCell.style.width = "40px";
                            
                            var productlink = document.createElement("a");
                            productlink.style.paddingLeft = "1px";
                            //productlink.href = Host+"/"+Locale+"/"+product.link;
                            productlink.appendChild(document.createTextNode(product.mfr+" "+product.name));
                            //productlink.onclick = RemoveSearchHints;
                            
                            nameCell = productRow.insertCell(-1);
                            nameCell.appendChild(productlink);
                            
                            //nameCell .innerHTML = "<a href=\""++"\">"++"</a>";
                           
                        //}
                    }
                    var countRow = table.insertRow(-1);
                    var countCell = countRow.insertCell(-1);
                    //var countCell = document.createElement("td");
                    countCell.id = "searchcount";
                    countCell.colSpan = "2";
                    var discontinued="";
                    if(response.discontinued=="1") discontinued = " discontinued";
                    countCell.appendChild(document.createTextNode("Displaying "+response.searchcount+discontinued+" items"));
                    //countRow.appendChild(countCell);
                    
                }
            break;
            default:
            break;
        }
    }
}


function RemoveSearchHints()
{
    var searchBox = document.getElementById("search_hint");
    if(searchBox)
    {
        /*var products = searchBox.getElementsByTagName("div");
        
        for(i=0; i<products.length; i++)
        {
            searchBox.removeChild(products[i]);
        }*/
    
        document.getElementById("search_hint_container").removeChild(searchBox);        
    }
}


document.onclick = DetectClick;

function DetectClick(e)
{
    if(e==null)
    {
         var isSearch = IsSearch(event.srcElement);
        
        if(!isSearch) RemoveSearchHints();
    }
    else
    {
        var isSearch = IsSearch(e.target);
        
        if(!isSearch) RemoveSearchHints();
        /*if(isSearch)
        {
            /*if(e.which == 3 || e.which == 2)
            {
                e.stopPropagation();
                e.preventDefault();
                return false;
            }
        }
        else
        {
            
        }*/
        
        
        //if(HideHints(e.target)) RemoveSearchHints();
        //alert(e.target.id);
    }
    return true;
}

function IsSearch(Element)
{
    try
    {
        if(Element.id=="search_hint_container") return true;
        while(Element.parentNode)
        {
            Element = Element.parentNode;
            if(Element.id=="search_hint_container") return true;
        }
    }
    catch(e)
    {
    
    }
    
    return false;
}
