/*
    This is the UI tip display engine
    
    Tips are stored in the array as follows:
    tipList[num] = new Array();
    tipList[num]["id"]
    tipList[num]["title"]
    tipList[num]["tip"]
    
    showTip(theTitle) = show a specific tip in the display area
    
    browseTips(theDir) = Use 1 or -1 to navigate through the tips one at a time
    
    browseIndex() = This will popup a window with every available tip listed.  Clicking on one will display it
    
    writeTip() is internal.  It does the dirty work of actually taking a given tip and putting it up for display
*/

function TipManager()
{
    this.tipList = new Array();
    this.currentTip = 0;
    
    this.addTip = addTip;
    this.showTip = showTip;
    this.browseTips = browseTips;
    this.browseIndex = browseIndex;
    
    this.writeTip = writeTip;
    this.getTip = getTip;
    
   //get a tip
    function getTip(theNum)
    {   return (theNum < this.tipList.length) ? this.tipList[theNum] : false;    }
    
   //add a tip for display
    function addTip(theID, theTip, theTitle)
    {
        if (isSet(theID))
        {
            var num = this.tipList.length;
            this.tipList[num] = new Array();
            this.tipList[num]["id"] = theID;
            this.tipList[num]["title"] = theTitle;
            this.tipList[num]["tip"] = theTip;
        }
    }
    
   //Give the list of tips to display -- NOT FINISHED
    function browseIndex()
    {
        var div = document.getElementById("browseTipIndexDiv");
        if (div.style.display == "block")
        {
            div.style.display = "none"
            div.style.left = "-200px";
            div.style.top = "-200px";
            div.innerHTML = "";
        }
        else
        {
            var html = "<table border='0' cellpadding='4' cellspacing='0'>";
            if (this.tipList.length > 0)
            {
                for (var i = 0; i < this.tipList.length; i++)
                {
                    if (isSet(this.tipList[i]["tip"]))
                    {
                        html += "<tr><td class='plainBrown' onclick='showTip(\"" + this.tipList[i]["id"] + "\"); document.getElementById(\"browseTipIndexDiv\").style.display = \"none\";' onmouseout='this.className = \"plainBrown\";' onmouseover='this.className = \"reverseBrown\";'>" + this.tipList[i]["title"] + "</td></tr>";
                    }
                }
            }
            else
            {   html += "<tr><td onclick='document.getElementById(\"browseTipIndexDiv\").style.display = \"none\";'>There are no tips for this section.</td></tr>";    }
            html += "</table>";
            div.innerHTML = html;
            div.style.display = "block";
            var x = (parseInt(document.getElementById("row_five_table").offsetWidth)) - parseInt(div.offsetWidth);
            var y = (parseInt(document.getElementById("row_six").offsetTop) + parseInt(document.getElementById("browseTipsTable").offsetTop)) - parseInt(div.offsetHeight) - 15;
            //alert("row_five_tableis [" + parseInt(document.getElementById("row_five_table").offsetWidth) + "]\nbrowseTipsTable is [" + parseInt(document.getElementById("browseTipsTable").offsetLeft) + "]\ndiv.offsetWidth is [" + parseInt(div.offsetWidth) + "]\nx is [" + x + "]\ny is [" + y + "]");
            div.style.left = x + "px";
            div.style.top = y + "px";
        }
    }//end browseIndex
    
   //Show a specific tip
    function showTip(theTipID)
    {
        if (isSet(theTipID) && this.tipList.length > 0)
        {
            for (var i = 0; i < this.tipList.length; i++)
            {
                if (this.tipList[i]["id"] == theTipID)
                {
                    this.currentTip = i;
                    this.writeTip();
                    break;
                }
            }
        }
    }//end showTip
    
   //Go in numerical order
    function browseTips(theDir)
    {
        if (this.tipList.length > 0)
        {
            theDir = (isSet(theDir)) ? theDir : 0;
            this.currentTip = (isSet(this.currentTip)) ? this.currentTip : 0;
            var num = (this.currentTip + theDir);
            num = (num >= this.tipList.length) ? 0 : (num < 0) ? (this.tipList.length - 1) : num;
            this.currentTip = num;
            this.writeTip();
        }
    }//end btowseTips
    
   //Display tip to the user
    function writeTip()
    {
        if (this.currentTip >= 0 && this.currentTip < this.tipList.length)
        { 
            var tip = (isSet(this.tipList[this.currentTip]["tip"])) ? this.tipList[this.currentTip]["tip"] : "Details are often the difference in getting a buyer's attention so be sure to cover all the key features.  Even if you've written your own ad, check out the words and options in our drop down menus to make sure you haven't missed anything.";
            var title = (isSet(this.tipList[this.currentTip]["tip"]) && isSet(this.tipList[this.currentTip]["title"])) ? this.tipList[this.currentTip]["title"] : "for writing effective classified ads";
            if (isSet(tip) && isDom(document.getElementById('browseTipsTable')))
            {
                if (isDom(document.getElementById("row_five_space")))
                {   document.getElementById("row_five_space").innerHTML = "<span id='tipsHeader'>Tips:</span> <span id='tipsTitle'>" + title + "</span>";    }
                if (isDom(document.getElementById("row_six_space")))
                {   document.getElementById("row_six_space").innerHTML = tip;    }
                if (isDom(document.getElementById("numberTip")))
                {   document.getElementById("numberTip").innerHTML = ("" + (this.currentTip + 1));    }
                if (isDom(document.getElementById("whichTip")))
                {   document.getElementById("whichTip").innerHTML = this.currentTip + 1;    }
                if (isDom(document.getElementById("totalTip")))
                {   document.getElementById("totalTip").innerHTML = this.tipList.length;    }
            }
        }
    }//end writeTip
    
}//end TipManager

//Now we create one
var tipManager = new TipManager();

//here are some global wrapper functions
function addTip(theID, theTip, theTitle)
{   tipManager.addTip(theID, theTip, theTitle);    }
function browseTips(theDir)
{   tipManager.browseTips(theDir);    }
function showTip(theTipID)
{   tipManager.showTip(theTipID);    }
function browseIndex()
{   tipManager.browseIndex();    }

