function getAbsPoint(obj)   
{            
   var rect = obj.getBoundingClientRect();
   rect.top += document.documentElement.scrollTop;
   rect.left += document.documentElement.scrollLeft;
   return rect;
} 

function RCAddressItem(tUrl, tDesc)
{
   this.url = tUrl;
   this.desc = tDesc;
}

function RCAddressBar()
{   
    this.status = -1;
    this.addressEditObj = null;
    this.inputTextId = "";
    this.addressListDiv = null;
    this.curUrlInputData = "";
    this.curSelectItem = -1;
    this.itemCount = 0;
    this.lastSelectItem = null;
    this.lastItemCololr = 0;

    this.Init = function(obj)
    {
        this.addressListDiv = obj;
        this.addressListDiv.style.display = "none";
        this.addressListDiv.style.height = 200;
        this.addressListDiv.style.width = 352;
    }
    this.Attach = function(editObj, inputId)
    {
        this.addressEditObj = editObj;
        this.inputTextId = inputId;
    }
    this.SelectItemToInput = function()
    {
        this.lastSelectItem.onclick();
        this.Hide();
    }
    this.SelectItem =function(isDown)
    {
        if(isDown)
        {
            
            if((this.curSelectItem+2) *20 + this.addressListDiv.scrollTop > 20*10)
            {
                this.addressListDiv.scrollTop += 20*10;
            }
            //this.addressListDiv.scrollTop += 10;
            if(this.curSelectItem == this.itemCount - 1)
            {
                return;
            }
            var obj = document.getElementById("Item"+ (this.curSelectItem));
            if(obj != null)
            {
                obj.onmouseout();
            }
     
            obj = document.getElementById("Item"+ (this.curSelectItem + 1));
            if(obj != null)
            {
                addressBar.lastSelectItem = obj;
                this.lastItemCololr = obj.style.backgroundColor;
                obj.onmouseover();
            }
            
        }
        else
        {
            if((this.curSelectItem - 1) *20 < this.addressListDiv.scrollTop)
            {
                this.addressListDiv.scrollTop -= 20*10;
            }
            if(this.curSelectItem == 0)
            {
                return;
            }
            var obj = document.getElementById("Item"+ (this.curSelectItem));
            if(obj != null)
            {
                obj.onmouseout();
            }         
            obj = document.getElementById("Item"+ (this.curSelectItem - 1));
            if(obj != null)
            {
                addressBar.lastSelectItem = obj;
                this.lastItemCololr = obj.style.backgroundColor;
                obj.onmouseover();
            }
            
        }

    }
    this.UpdateUrlData = function(urlInput)
    {       
            this.curUrlInputData = urlInput;
            if(status < 0)
            {
                return ;
            }
            this.addressListDiv.innerHTML = "";
            var htmlCode = "<table cellSpacing=0 cellPadding=0>";
   
   
             var bkcolor = "#ffffff";
             var bkcolorjs = "0xffffff";
             var itemList = this.GetListData(this.curUrlInputData);
             this.itemCount = itemList.length;
             for(var i=0; i< itemList.length; i++)
             {
                var setTextCode = "document.getElementById('" + this.inputTextId +"').value='" + itemList[i].url + "'";
                //eval(setTextCode);
                if (bkcolor == "#ffffff")
                {
                    bkcolor = "#eeeeee";
                    bkcolorjs = "0xeeeeee";
                }
                else
                {
                     bkcolor = "#ffffff";
                     bkcolorjs = "0xffffff";
                }
                
                htmlCode += "<tr id='" + "Item"+ i  +  "'style='height:20px; background-color:" + bkcolor + "' onmouseout = 'this.style.backgroundColor=" + bkcolorjs + "' onmouseover = 'if(addressBar.lastSelectItem != null){addressBar.lastSelectItem.style.backgroundColor =  addressBar.lastItemCololr } ;addressBar.curSelectItem= " + i + ";this.style.backgroundColor=0x8196b6' onclick =" + setTextCode  + "><td><li style = 'float:left;width:200px; text-overflow:ellipsis; white-space:nowrap; overflow:hidden;'>" + itemList[i].url + "</li>";
                htmlCode += "<li style = 'float:left;width:135px; text-overflow:ellipsis; white-space:nowrap; overflow:hidden;'>" + itemList[i].desc + "</li></ul></td></tr>";
             }
            htmlCode += "</table>";
            this.addressListDiv.innerHTML = htmlCode;
            if(itemList.length <= 0)
            {
                this.Hide();
            }
            return true;
    }
    this.OnPositionChange = function()
    {
        if(this.status == 1)
        {
            this.Show();
        }
    }
    this.Show = function()
    {       
            var rect = getAbsPoint(this.addressEditObj);
            this.addressListDiv.style.position='absolute';
            this.addressListDiv.style.zIndex = 9999;
            this.addressListDiv.style.left = rect.left;
            this.addressListDiv.style.top = rect.top + 25;
            this.addressListDiv.style.display = "block";
            this.status = 1;
            this.curSelectItem = -1;
            this.lastSelectItem = null;
            this.lastItemCololr = 0;
    }
    
    this.OnBarRender = function()
    {
        this.curUrlInputData = "";
        if(this.status < 0)
        {
            this.Show();
            this.UpdateUrlData(null);
            //this.addressListDiv.style.display = "block";
        }
        else
        {
            this.Hide();
        }
    }
    
    this.Hide = function()
    {
        this.status = -1;
        this.addressListDiv.style.display = "none";
    }
    
    this.GetListData = function(urlFilter)
    {
        var stringRet = window.external.OnInputUrl(urlFilter);
        var itemList=new Array();   
        var rawItem =  stringRet.split(";");
        for(var i=0; i< rawItem.length; i++)
        {
            //alert(rawItem[i]);
            var rawItemData =  rawItem[i].split("|||");
            if(rawItemData[0] != null && rawItemData[0] != "")
            {
                itemList.push(new RCAddressItem(rawItemData[0], rawItemData[1]));
            }
            
        }
        /*itemList.push(new RCAddressItem("www.2.com", "2"));
        itemList.push(new RCAddressItem("www.3.com", "3"));
        itemList.push(new RCAddressItem("www.163.com", "ÍøÒ×"));
        itemList.push(new RCAddressItem("www.5.com", "5"));
        itemList.push(new RCAddressItem("www.6.com", "6"));
        itemList.push(new RCAddressItem("www.7.com", "7"));*/
        return itemList;
    }
}

function OpenBestHome(obj)
{
    obj.target ="_blank";
    obj.href   = "coral:home";
    obj.click() ;
}
