
var feedTypes = {
    "external": 1,
    "local": 2
};

var deadEngines = {"cuil":1};

var providerIndex = { "google": 0,
                      "amazon": 1,
                      "yahoo": 2,
                      "ask": 3,
                      "youtube": 4,
                      "answers": 5,
                      "wikipedia": 6,
                      "live": 7,
                      "ebay": 8,
                      "buy": 9,
                      "weather":10,
                      "netflix":11,
                      "chacha":12,
                      "cuil":13,
                      "walmart":13,
                      "qq":14
                    };

var providers = []; //All search providers
var providerLookup = []; //All search providers
var searchDivs = [];//Currently in-use providers
var allProviders = [];

function providerObject( name, position, favicon, logo, searchUrl /*commit the search*/, searchResultsUrl /*search results*/, site /*e.g. site:amazon.com*/, feedUrl, feedType)
{
    this.name = name;
    this.index = providerIndex[name];
    this.position = position;
    this.favicon = favicon;
    this.logo = logo;
    this.searchUrl = searchUrl;
    this.searchResultsUrl = searchResultsUrl;
    this.site = site;
    this.feedUrl = feedUrl;
    this.feedType = feedType;
    this.visible = false;

};

providerObject.prototype.SetEncoding = function(provEnc)
{
    this.encoding = provEnc;
}

providerObject.prototype.BuildDiv = function()
{
    var divContainer = document.getElementById("divscontainer");
    var dataDiv = document.createElement("div");
    dataDiv.className = "provider";
    dataDiv.id = this.name;
    dataDiv.style.position = "absolute";
    dataDiv.style.display = "none";
    dataDiv.style.opacity = "0";
    dataDiv.style.overflow= "visible";
    divContainer.appendChild(dataDiv);

    var imgDiv = document.createElement("img");
    setTimeout((function(img,imgSrc) { return (function() { img.src=imgSrc; }); })(imgDiv,this.logo),10);
    //imgDiv.src = this.logo;
    imgDiv.id = this.name + "img";
    $(imgDiv).css({
        maxWidth:190
    });
    dataDiv.appendChild(imgDiv);

    var completeDiv = document.createElement("div");
    completeDiv.id = this.name + "complete";
    completeDiv.style.position = "absolute";
    completeDiv.style.width = "190px";
    completeDiv.style.filter = "alpha(opacity=100)";
    dataDiv.appendChild(completeDiv);
    
    this.searchDiv = dataDiv;
    this.completeDiv = completeDiv;
}

providerObject.prototype.Hide = function( useGrace )
{
    if ( this.visible )
    {
        if ( useGrace )
        {
            $(this.searchDiv).hide("slow");
        }
        else
        {
            this.searchDiv.style.display = "none";
        }

        this.visible = false;
    }
}

providerObject.prototype.Show = function( useGrace)
{
    if ( !this.visible )
    {
        if ( useGrace )
        {
            this.searchDiv.style.display = "";
            $(this.searchDiv).animate({opacity:"1"},"slow");
        }
        else
        {
            this.searchDiv.style.display = "";
        }

        this.visible = true;
    }
}

providerObject.prototype.SetPosition = function( posIndex, useGrace, placements)
{
    this.position = posIndex;
    var curPos = placements.positions[ posIndex % placements.positions.length ];
    this.SetPositionXY( curPos.x, curPos.y, useGrace);
}

providerObject.prototype.SetPositionXY = function( x, y, useGrace)
{
    if ( useGrace )
        $(this.searchDiv).animate({left:x, top:y}, "slow");
    else
    {
        this.searchDiv.style.left = x + "px";
        this.searchDiv.style.top = y + "px";
    }
}

var googleprov;
var amazon;
var wikipedia;
var ask;
var answers;
var youtube;
var yahoo;
var buy;
var weather;
var netflix;
var chacha;
var cuil;
var walmart;
var qq;

function BuildProviders()
{
    googleprov = new providerObject( "google", 0, 
                                     "http://soovle.com/images/googlefavicon.png", 
                                     "/images/google.png", 
                                     "http://www.google.com/search?hl=en&btnG=Google+Search&aq=f&oq=&q=", 
                                     "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&context=&callback=InsertGoogleResults&start=0&rsz=small&q=", 
                                     null, 
                                     "http://soovle.com/google.php?q=",
                                     feedTypes.external); 
    googleprov.BuildDiv();

    providers["google"] = googleprov;
    allProviders[allProviders.length] = googleprov;
    providerLookup[providerIndex.google] = googleprov;

    amazon = new providerObject( "amazon", 1, 
                                     "http://www.amazon.com/favicon.ico", 
                                     "/images/amazon.png", 
                                     "http://www.amazon.com/s/?url=search-alias%3Daps&x=0&y=0&tag=soovle-20&field-keywords=", 
                                     "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&context=&callback=InsertGoogleResults&start=0&rsz=small&q=", 
                                     "amazon.com",
                                     "http://completion.amazon.com/search/complete?mkt=1&search-alias=aps&x=updateAmazon&q=", 
                                     feedTypes.external); 
    amazon.BuildDiv();

    providers["amazon"] = amazon;
    providerLookup[providerIndex.amazon] = amazon;
    allProviders[allProviders.length] = amazon;


    yahoo = new providerObject( "yahoo", 2, 
                                     "http://www.yahoo.com/favicon.ico",
                                     "/images/yahool.png",
                                     "http://search.yahoo.com/search?fr=yfp-t-501&toggle=1&cop=mss&ei=UTF-8&p=",
                                     "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&context=&callback=InsertGoogleResults&start=0&rsz=small&q=", 
                                     null, 
                                     "http://sugg.search.yahoo.net/sg/?output=fxjsonp&nresults=10&command=",
                                     feedTypes.external); 
    yahoo.BuildDiv();

    providers["yahoo"] = yahoo;
    allProviders[allProviders.length] = yahoo;
    providerLookup[providerIndex.yahoo] = yahoo;

    ask = new providerObject( "ask", 3, 
                                     "http://soovle.com/images/askfavicon.ico",
                                     "/images/ask.png",
                                     "http://www.ask.com/web?search=search&qsrc=0&o=0&l=dir&q=",
                                     "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&context=&callback=InsertGoogleResults&start=0&rsz=small&q=", 
                                     null, 
                                     "http://ss.ask.com/query?fn=updateAsk&sstype=prefix&q=",
                                     feedTypes.external); 
    ask.BuildDiv();

    providers["ask"] = ask;
    allProviders[allProviders.length] = ask;
    providerLookup[providerIndex.ask] = ask;

    youtube = new providerObject( "youtube", 4,
                                     "http://www.youtube.com/favicon.ico",
                                     "/images/youtube.gif",
                                     "http://www.youtube.com/results?search_type=&aq=f&search_query=",
                                     "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&context=&callback=InsertGoogleResults&start=0&rsz=small&q=", 
                                     "youtube.com", 
                                     "http://clients1.google.com/complete/search?client=youtube&hl=en&gl=us&ds=yt&cp=1&gs_id=5&callback=window.google.ac.jsonRPCDone&q=",
                                     feedTypes.external); 
    youtube.BuildDiv();

    providers["youtube"] = youtube;
    allProviders[allProviders.length] = youtube;
    providerLookup[providerIndex.youtube] = youtube;

    answers = new providerObject( "answers", 5,
                                     "http://www.answers.com/favicon.ico",
                                     "/images/answers1-sml.gif",
                                     "http://www.answers.com/",
                                     "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&context=&callback=InsertGoogleResults&start=0&rsz=small&q=", 
                                     "answers.com", 
                                     "/answers.php?q=",
                                     feedTypes.local); 
    answers.BuildDiv();

    providers["answers"] = answers;
    allProviders[allProviders.length] = answers;
    providerLookup[providerIndex.answers] = answers;

    wikipedia = new providerObject( "wikipedia", 6, 
                                     "http://soovle.com/images/wikifavicon.ico",
                                     "/images/wikipedia.png",
                                     "http://en.wikipedia.org/wiki/",
                                     "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&context=&callback=InsertGoogleResults&start=0&rsz=small&q=", 
                                     "wikipedia.com", 
                                     "/wiki.php?q=",
                                     feedTypes.local); 
    wikipedia.BuildDiv();
    providers["wikipedia"] = wikipedia;
    allProviders[allProviders.length] = wikipedia;
    providerLookup[providerIndex.wikipedia] = wikipedia;

    live = new providerObject( "live", 7, 
                                     "http://soovle.com/images/msnfav.ico",
                                     "/images/bing.png",
                                     "http://www.bing.com/search?go=&form=QBLH&qs=n&q=",
                                     "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&context=&callback=InsertGoogleResults&start=0&rsz=small&q=", 
                                     null, 
                                     "http://api.search.live.com/qson.aspx?market=en-US&options=EnableSuggestionType&JsonType=callback&JsonCallback=sas_SearchAutoSuggest.setData&query=",
                                     feedTypes.external); 
    live.BuildDiv();
    providers["live"] = live;
    allProviders[allProviders.length] = live;
    providerLookup[providerIndex.live] = live;

    ebay = new providerObject( "ebay", 8, 
                                     "http://www.ebay.com/favicon.ico",
                                     "/images/logoEbay_x45.gif",
                                     "http://shop.ebay.com/?_from=R40&_trksid=m38&_nkw=",
                                     "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&context=&callback=InsertGoogleResults&start=0&rsz=small&q=", 
                                     "ebay.com", 
                                     "/ebay.php?q=",
                                     feedTypes.local); 
    ebay.BuildDiv();
    providers["ebay"] = ebay;
    allProviders[allProviders.length] = ebay;
    providerLookup[providerIndex.ebay] = ebay;

    buy = new providerObject( "buy", 9, 
                                     "http://www.buy.com/favicon.ico",
                                     "/images/buycom.gif",
                                     "http://affiliate.buy.com/deeplink?id=pbC9zvV6XSI&mid=36342&murl="+encodeURIComponent("http://www.buy.com/sr/searchresults.aspx?qxt=home&display=col&qu="),
                                     "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&context=&callback=InsertGoogleResults&start=0&rsz=small&q=", 
                                     "buy.com", 
                                     "/buy.php?q=",
                                     feedTypes.local); 
    buy.BuildDiv();
    providers["buy"] = buy;
    allProviders[allProviders.length] = buy;
    providerLookup[providerIndex.buy] = buy;

    weather = new providerObject( "weather", 10, 
                                     "http://soovle.com/images/weatherfav.ico",
                                     "/images/weather.png",
                                     "http://www.weather.com/search/enhancedlocalsearch?whatprefs=&what=Weather36HourOutdoorsCommand&lswa=Weather36HourOutdoorsCommand&from=searchbox_localwx&googleTypeSearch=on&Search.x=0&Search.y=0&Search=Search&where=",
                                     "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&context=&callback=InsertGoogleResults&start=0&rsz=small&q=", 
                                     "weather.com", 
                                     "/weather.php?q=",
                                     feedTypes.local); 
    weather.BuildDiv();
    providers["weather"] = weather;
    allProviders[allProviders.length] = weather;
    providerLookup[providerIndex.weather] = weather;

    netflix = new providerObject( "netflix", 11, 
                                     "http://soovle.com/images/netflixfav.ico",
                                     "/images/netflix_logo.gif",
                                     "http://www.netflix.com/Search?search_submit.y=0&search_submit.x=0&v1=",
                                     "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&context=&callback=InsertGoogleResults&start=0&rsz=small&q=", 
                                     "netflix.com", 
                                     "/netflix.php?q=",
                                     feedTypes.local); 
    netflix.BuildDiv();
    providers["netflix"] = netflix;
    allProviders[allProviders.length] = netflix;
    providerLookup[providerIndex.netflix] = netflix;

    chacha = new providerObject( "chacha", 12, 
                                     "http://soovle.com/images/chachafavicon.ico",
                                     "/images/chacha_logo.png",
                                     "http://www.chacha.com/search/",
                                     "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&context=&callback=InsertGoogleResults&start=0&rsz=small&q=", 
                                     "chacha.com", 
                                     "/chacha.php?q=",
                                     feedTypes.local); 
    chacha.BuildDiv();
    providers["chacha"] = chacha;
    allProviders[allProviders.length] = chacha;
    providerLookup[providerIndex.chacha] = chacha;


    walmart = new providerObject( "walmart", 13, 
                                     "http://www.walmart.com/favicon.ico",
                                     "http://i2.walmartimages.com/i/header_wide/walmart_logo_214x54.gif",
                                     "http://www.walmart.com/search/search-ng.do?ic=48_0&Find=Find&search_constraint=0&search_query=",
                                     "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&context=&callback=InsertGoogleResults&start=0&rsz=small&q=", 
                                     null, 
                                     "http://search-cdn.walmart.com/typeahead/130d053b1b7c/0/????.js",
                                     feedTypes.external); 


    providers["cuil"] = walmart;
    walmart.BuildDiv();
    providers["walmart"] = walmart;
    allProviders[allProviders.length] = walmart;
    providerLookup[providerIndex.walmart] = walmart;

    qq = new providerObject( "qq", 14, 
                                     "http://www.qq.com/favicon.ico",
                                     "/images/qq.gif",
                                     "http://www.soso.com/q?sp=S&sc=web&cid=w.q.in.sb.web&ty=1&bn=&op=entry&kw=&w=",
                                     "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&context=&callback=InsertGoogleResults&start=0&rsz=small&q=", 
                                     "qq.com", 
                                     "/qq.php?q=",
                                     feedTypes.local); 
    qq.BuildDiv();
    providers["qq"] = qq;
    allProviders[allProviders.length] = qq;
    providerLookup[providerIndex.qq] = qq;

}

var activeProviders = [];
function BuildProvidersFromList(providerString, providerCount)
{
    try
    {
        var providerList;
        var maxProviders = 7;
        if ( providerCount && providerCount > 4 )
        {
            maxProviders = providerCount;
        }

        var providerList = engines;
        if ( providerString && (providerString+"").match(/,/) ) {
            providerList = (providerString+"").split(/,/);

            if ( !providerList ) {
                providerList = engines;
            }

            if ( !providerCount )
                maxProviders = providerList.length;

            providerList = providerList.concat(engines);
            
        }
        else 
        {
            if ( window.innerHeight > 900 ) {
                maxProviders = 11;
            }

            if ( window.innerHeight > 1300 )
            {
                maxProviders = 15;
            }

            providerList = engines;
        }

        var added = [];
        searchDivs = [];
        activeProviders = [];
        for ( var pCt = 0; pCt < providerList.length && searchDivs.length < maxProviders; pCt++ )
        {
            if ( added[providerList[pCt]] == null )
            {
                added[providerList[pCt]] = 1;
                searchDivs[searchDivs.length] = providers[providerList[pCt]];
                activeProviders[activeProviders.length] = providerList[pCt];

                providers[providerList[pCt]].Show(true);
            }
        }

        InstallSelectors();
    }
    catch(ex)
    {
        alert("Build providers: " + ex.message);
    }
}

function RebuildProviders(newList, count, noTrigger)
{
    for ( var pCt = 0; pCt < allProviders.length; pCt++ ) {
        allProviders[pCt].Hide();
    }

    StoreEngineSet( newList );
    BuildProvidersFromList(newList, count);
    PlaceProvider(0, false);

    if ( $(document.body).peertrigger && !noTrigger ) {
        $(document.body).peertrigger("rebuild", [newList,count]);
    }
}

function StoreEngineSet( engineSet )
{
    localStorage.setItem('engines', engineSet);
}

function RestoreEngines()
{
    var engine = (window.location.href + "" ).replace(/.*s=([^&]+).*/,"$1");

    if ( !engines || ( engine && engine == window.location.href + "" ))
    {
        var engineval = localStorage.getItem('engines');
        if ( engineval != null )
        {
           try
           {
               RebuildProviders(engineval);
           }
           catch(ex)
           {
               alert("Exception rebuilding: " + ex.message);
           }
        }
    }
}

function ReloadEngines(count)
{
    var engineval = localStorage.getItem('engines');
    if ( engineval != null )
    {
       RebuildProviders(engineval, count);
    }
}

function ClearEngineCustomization()
{
    localStorage.setItem('engines', null);
    for ( var pCt = 0; pCt < allProviders.length; pCt++ )
    {
        allProviders[pCt].Hide();
    }
    BuildProvidersFromList(); 
}

window.google = { ac: { yup: "t" } };
window.google.ac.h = function (dataObj)
{
    var img = $('#googleimg')[0];
    if ( !img.seeThrough )
    {
        img.seeThrough = true;
        img.style.position = "absolute";
        $(img).stop().animate({opacity:".3"},300);
    }

    var gData = $('#googlecomplete')[0];
    while ( gData.firstChild )
    {
        gData.removeChild(gData.firstChild);
    }

    var corArray = dataObj[1];
    completions[providerIndex.google] = [];
    for ( var gdCt = 0; gdCt < corArray.length; gdCt++ )
    {
        var curCor = corArray[gdCt];
        var div = AddSuggestion(gData, curCor, providerIndex.google);
        completions[providerIndex.google][gdCt] = div;
    }
}

window.google.ac.jsonRPCDone = function(dataObj)
{
    var img = $('#youtubeimg')[0];
    if ( !img.seeThrough )
    {
        img.seeThrough = true;
        img.style.position = "absolute";
        $(img).stop().animate({opacity:".3"},300);
    }

    var gData = $('#youtubecomplete')[0];
    while ( gData.firstChild )
    {
        gData.removeChild(gData.firstChild);
    }

    var corArray = dataObj[1];
    completions[providerIndex.youtube] = [];
    for ( var gdCt = 0; gdCt < corArray.length; gdCt++ )
    {
        var curCor = corArray[gdCt][0];
        var div = AddSuggestion(gData, curCor, providerIndex.youtube);
        completions[providerIndex.youtube][gdCt] = div;
    }
}

function fxsearch(data)
{
   var searchBox = $('#searchinput')[0];
   var suggestionDiv = $('#yahoocomplete')[0];
   var suggestionImg = $('#yahooimg')[0];

   if ( !suggestionImg.seeThrough )
   {
       suggestionImg.seeThrough = true;
       suggestionImg.style.position = "absolute";
       $(suggestionImg).stop().animate({opacity:".3"},300);
   }

   while( suggestionDiv.firstChild )
   {
       suggestionDiv.removeChild(suggestionDiv.firstChild);
   }

   allCompletionsLeft = [];

   var terms = [];
   if ( data.length > 1 )
   {
       terms = data[1];
       log("Terms: " + terms);
   }

   completions[providerIndex.yahoo] = [];
   for (var termCt = 0; termCt < terms.length; termCt++ )
   {
       var term = terms[termCt];
       if (allCompletionsLeft.length > 10 )
       {
           break;
       }

       var div = AddSuggestion(suggestionDiv, term, providerIndex.yahoo);
       completions[providerIndex.yahoo][termCt] = div;
   }
}

function updateWiki(data)
{
    if ( !data || !data.length )
        return;

    var searchBox = $('#searchinput')[0];
    var suggestionDiv = $('#wikipediacomplete')[0];
    var suggestionImg = $('#wikipediaimg')[0];

    if ( !suggestionImg.seeThrough )
    {
        suggestionImg.seeThrough = true;
        suggestionImg.style.position = "absolute";
        $(suggestionImg).stop().animate({opacity:".3"},300);
    }

    while( suggestionDiv.firstChild )
    {
        suggestionDiv.removeChild(suggestionDiv.firstChild);
    }

    allCompletionsLeft = [];

    var terms = [];
    if ( data.length > 1 )
    {
        terms = data[1];
        log("Terms: " + terms);
    }

    completions[providerIndex.wikipedia] = [];
    for (var termCt = 0; termCt < terms.length; termCt++ )
    {
        var term = terms[termCt];
        if (allCompletionsLeft.length > 10 )
        {
            break;
        }

        var div = AddSuggestion(suggestionDiv, term, providerIndex.wikipedia);
        completions[providerIndex.wikipedia][termCt] = div;
    }
}


function updateAnswers(data)
{
    if ( !data || !data.length )
        return;

    var searchBox = $('#searchinput')[0];
    var suggestionDiv = $('#answerscomplete')[0];
    var suggestionImg = $('#answersimg')[0];

    if ( !suggestionImg.seeThrough )
    {
        suggestionImg.seeThrough = true;
        suggestionImg.style.position = "absolute";
        $(suggestionImg).stop().animate({opacity:".3"},300);
    }

    while( suggestionDiv.firstChild )
    {
        suggestionDiv.removeChild(suggestionDiv.firstChild);
    }

    var terms = [];
    var exData = [];
    if ( data.length > 1 )
    {
        terms = data[1];
        exData = data[2];
        log("Terms: " + terms);
    }

    completions[providerIndex.answers] = [];
    for (var termCt = 0; termCt < terms.length; termCt++ )
    {
        var term = terms[termCt]
            if ( exData[termCt] != "" )
                term = term + " - " + exData[termCt];

        var div = AddSuggestion(suggestionDiv, term, providerIndex.answers);
        completions[providerIndex.answers][termCt] = div;
    }
}


function updateAsk(data)
{
    if ( !data || !data.length )
        return;

    var searchBox = $('#searchinput')[0];
    var suggestionDiv = $('#askcomplete')[0];
    var suggestionImg = $('#askimg')[0];

    if ( !suggestionImg.seeThrough )
    {
        suggestionImg.seeThrough = true;
        suggestionImg.style.position = "absolute";
        $(suggestionImg).stop().animate({opacity:".3"},300);
    }

    while( suggestionDiv.firstChild )
    {
        suggestionDiv.removeChild(suggestionDiv.firstChild);
    }

    var terms = [];
    if ( data.length > 1 )
    {
        terms = data[1];
    }

    completions[providerIndex.ask] = [];
    for (var termCt = 0; termCt < terms.length; termCt++ )
    {
        var term = terms[termCt].replace(/<[^>]+>/g,"");
        var div = AddSuggestion(suggestionDiv, term, providerIndex.ask);
        completions[providerIndex.ask][termCt] = div;
    }
}

var sas_SearchAutoSuggest = {};
sas_SearchAutoSuggest.setData = function( data )
{
    if ( !data )
        return;

    if ( !data["SearchSuggestion"] )
    {
        return;
    }

    if ( !data["SearchSuggestion"]["Section"] )
    {
        return;
    }

    var msnData = data["SearchSuggestion"]["Section"];

    var searchBox = $('#searchinput')[0];
    var suggestionDiv = $('#livecomplete')[0];
    var suggestionImg = $('#liveimg')[0];

    if ( !suggestionImg.seeThrough )
    {
        suggestionImg.seeThrough = true;
        suggestionImg.style.position = "absolute";
        $(suggestionImg).animate({opacity:".3"},300);
    }

    while( suggestionDiv.firstChild )
    {
        suggestionDiv.removeChild(suggestionDiv.firstChild);
    }

    var terms = [];
    for ( var msnCt = 0; msnCt < msnData.length; msnCt++ )
    {
        terms[terms.length] = msnData[msnCt]["Text"];
    }

    completions[providerIndex.live] = [];
    for (var termCt = 0; termCt < terms.length; termCt++ )
    {
        var term = terms[termCt]
        var div = AddSuggestion(suggestionDiv, term, providerIndex.live);
        completions[providerIndex.live][termCt] = div;
    }
}

function updateAmazon()
{
   var results = completion;

   var terms = [];
   if ( results.length > 1 )
   {
       terms = results[1];
       log("Terms: " + terms);
   }

   var searchBox = $('#searchinput')[0];
   var suggestionDiv = $('#amazoncomplete')[0];
   var suggestionImg = $('#amazonimg')[0];

   if ( !suggestionImg.seeThrough )
   {
       suggestionImg.seeThrough = true;
       suggestionImg.style.position = "absolute";
       $(suggestionImg).animate({opacity:".3"},300);
   }

   while( suggestionDiv.firstChild )
   {
       suggestionDiv.removeChild(suggestionDiv.firstChild);
   }

   completions[providerIndex.amazon] = [];
   for (var termCt = 0; termCt < terms.length; termCt++ )
   {
       var term = terms[termCt];
       var div = AddSuggestion(suggestionDiv, term, providerIndex.amazon);
       completions[providerIndex.amazon][termCt] = div;
   }
}

function updateBuy(data)
{
   var terms = data.suggestions;

   var searchBox = $('#searchinput')[0];
   var suggestionDiv = $('#buycomplete')[0];
   var suggestionImg = $('#buyimg')[0];

   if ( !suggestionImg.seeThrough )
   {
       suggestionImg.seeThrough = true;
       suggestionImg.style.position = "absolute";
       $(suggestionImg).animate({opacity:".3"},300);
   }

   while( suggestionDiv.firstChild )
   {
       suggestionDiv.removeChild(suggestionDiv.firstChild);
   }

   completions[providerIndex.buy] = [];
   for (var termCt = 0; termCt < terms.length; termCt++ )
   {
       var term = terms[termCt].phrase;
       var div = AddSuggestion(suggestionDiv, term, providerIndex.buy);
       completions[providerIndex.buy][termCt] = div;
   }
}

function updateEbay(data)
{
   var terms = data;

   var searchBox = $('#searchinput')[0];
   var suggestionDiv = $('#ebaycomplete')[0];
   var suggestionImg = $('#ebayimg')[0];

   if ( !suggestionImg.seeThrough )
   {
       suggestionImg.seeThrough = true;
       suggestionImg.style.position = "absolute";
       $(suggestionImg).animate({opacity:".3"},300);
   }

   while( suggestionDiv.firstChild )
   {
       suggestionDiv.removeChild(suggestionDiv.firstChild);
   }

   completions[providerIndex.ebay] = [];
   for (var termCt = 0; termCt < terms.length; termCt++ )
   {
       var term = terms[termCt];
       var div = AddSuggestion(suggestionDiv, term, providerIndex.ebay);
       completions[providerIndex.ebay][termCt] = div;
   }
}

function updateWeather(data)
{
   var terms = data['results'];
   if ( !terms )
       return;

   var searchBox = $('#searchinput')[0];
   var suggestionDiv = $('#weathercomplete')[0];
   var suggestionImg = $('#weatherimg')[0];

   if ( !suggestionImg.seeThrough )
   {
       suggestionImg.seeThrough = true;
       suggestionImg.style.position = "absolute";
       $(suggestionImg).animate({opacity:".3"},300);
   }

   while( suggestionDiv.firstChild )
   {
       suggestionDiv.removeChild(suggestionDiv.firstChild);
   }

   completions[providerIndex.weather] = [];
   for (var termCt = 0; termCt < terms.length; termCt++ )
   {
       var term = terms[termCt];
       var result = term.pn +", " + term.st + ", " + term.cy;
       var div = AddSuggestion(suggestionDiv, result, providerIndex.weather);
       completions[providerIndex.weather][termCt] = div;
   }
}

function typeaheadResult(data)
{
   if ( !data || !data.R )
        return;

   var terms = data.R;

   var searchBox = $('#searchinput')[0];
   var suggestionDiv = $('#walmartcomplete')[0];
   var suggestionImg = $('#walmartimg')[0];

   if ( !suggestionImg.seeThrough )
   {
       suggestionImg.seeThrough = true;
       suggestionImg.style.position = "absolute";
       $(suggestionImg).animate({opacity:".3"},300);
   }

   while( suggestionDiv.firstChild )
   {
       suggestionDiv.removeChild(suggestionDiv.firstChild);
   }

   completions[providerIndex.walmart] = [];
   for (var termCt = 0; termCt < terms.length; termCt++ )
   {
       var term = terms[termCt][0];
       if ( termCt > 0 )
           term = terms[termCt];
       var div = AddSuggestion(suggestionDiv, term, providerIndex.walmart);
       completions[providerIndex.walmart][termCt] = div;
   }
}

function updateNetflix(data)
{
   var terms = data["movies"];

   var searchBox = $('#searchinput');
   var suggestionDiv = $('#netflixcomplete')[0];
   var suggestionImg = $('#netfliximg')[0];

   if ( !suggestionImg.seeThrough )
   {
       suggestionImg.seeThrough = true;
       suggestionImg.style.position = "absolute";
       $(suggestionImg).animate({opacity:".3"},300);
   }

   while( suggestionDiv.firstChild )
   {
       suggestionDiv.removeChild(suggestionDiv.firstChild);
   }

   completions[providerIndex.netflix] = [];
   for (var termCt = 0; termCt < terms.length; termCt++ )
   {
       var term = terms[termCt];
       var div = AddSuggestion(suggestionDiv, term.title, providerIndex.netflix);
       completions[providerIndex.netflix][termCt] = div;
   }
}

function updateChaCha(data)
{
   var terms = data["results"];

   var searchBox = $('#searchinput');
   var suggestionDiv = $('#chachacomplete')[0];
   var suggestionImg = $('#chachaimg')[0];

   if ( !suggestionImg.seeThrough )
   {
       suggestionImg.seeThrough = true;
       suggestionImg.style.position = "absolute";
       $(suggestionImg).animate({opacity:".3"},300);
   }

   while( suggestionDiv.firstChild )
   {
       suggestionDiv.removeChild(suggestionDiv.firstChild);
   }

   completions[providerIndex.chacha] = [];
   for (var termCt = 0; termCt < terms.length; termCt++ )
   {
       var term = terms[termCt];
       var div = AddSuggestion(suggestionDiv, term.name, providerIndex.chacha);
       completions[providerIndex.chacha][termCt] = div;
   }
}

function updateQQ(data)
{
   var terms = data.split("\n");

   var searchBox = $('#searchinput');
   var suggestionDiv = $('#qqcomplete')[0];
   var suggestionImg = $('#qqimg')[0];

   if ( !suggestionImg.seeThrough )
   {
       suggestionImg.seeThrough = true;
       suggestionImg.style.position = "absolute";
       $(suggestionImg).animate({opacity:".3"},300);
   }

   while( suggestionDiv.firstChild )
   {
       suggestionDiv.removeChild(suggestionDiv.firstChild);
   }

   completions[providerIndex.qq] = [];
   for (var termCt = 0; termCt < terms.length; termCt++ )
   {
       var term = terms[termCt].split("\t");
       var div = AddSuggestion(suggestionDiv, term[1], providerIndex.qq);
       completions[providerIndex.qq][termCt] = div;
   }
}

function GetPagePositions(layout)
{
    var width = document.body.clientWidth;
    if ( width < 880 )
    {
        width = 880;
    }

    return GetPositions(layout, width, 85)
}

function GetPositions(layout, width, top)
{
    switch( layout )
    {
        case "star": 
        default: return GetStarPositions( width, top );
    }
}

function GetStarPositions(width, top)
{
    var starShoulder = top;
    var starFeet = top + 245; 
    var starMod = -110;
    var perLevel = 250;

    var searchPosition = { x:Math.floor(width/2) - 50 + starMod, y: 245}; 
    var positions = [   {x: Math.floor(width * 2.5 / 5)  + starMod, y:20 },
                        {x: Math.floor(width * 4 / 5) + starMod + 30, y:starShoulder},
                        {x: Math.floor(width * 4 / 5) + starMod + 30, y:starFeet},
                        {x: Math.floor(width * 3 / 5) + starMod + 40, y:starFeet + 80},
                        {x: Math.floor(width * 2 / 5) + starMod + 20, y:starFeet + 80},
                        {x: Math.floor(width * 1 / 5) + starMod, y:starFeet},
                        {x: Math.floor(width * 1 / 5) + starMod, y:starShoulder},
                        {x: Math.floor(width * 4 / 5) + starMod + 40, y:starFeet + perLevel},
                        {x: Math.floor(width * 3 / 5) + starMod + 40, y:starFeet + 80 + perLevel},
                        {x: Math.floor(width * 2 / 5) + starMod + 20, y:starFeet + 80 + perLevel},
                        {x: Math.floor(width * 1 / 5) + starMod, y:starFeet + perLevel},
                        {x: Math.floor(width * 4 / 5) + starMod + 40, y:starFeet + (2*perLevel)},
                        {x: Math.floor(width * 3 / 5) + starMod + 40, y:starFeet + 80 + (2*perLevel)},
                        {x: Math.floor(width * 2 / 5) + starMod + 20, y:starFeet + 80 + (2*perLevel)},
                        {x: Math.floor(width * 1 / 5) + starMod, y:starFeet + (2*perLevel)}
                    ];

    var fleft = { x: positions[0].x - 80, y: positions[0].y }; 
    var fright = { x: positions[0].x + 200, y: positions[0].y }; 

   return {width: width, frameLeftPosition: fleft, frameRightPosition: fright, searchPosition: searchPosition, positions: positions };
}

function GetMiniPositions(layout, width, height, top, left)
{
    switch( layout )
    {
        case "star": 
        default: return GetMiniStarPositions( width, height, top, left);
    }
}

function GetMiniStarPositions( width, height, top, left)
{
    var starShoulder = top + 20;
    var starFeet = top + height; 
    var perLevel = 40;

    var positions = [   {x: Math.floor(width * 2.5 / 5) + left, y:top },
                        {x: Math.floor(width * 4 / 5) + left, y:starShoulder},
                        {x: Math.floor(width * 4 / 5) + left, y:starFeet},
                        {x: Math.floor(width * 3 / 5) + left, y:starFeet + 23},
                        {x: Math.floor(width * 2 / 5) + left, y:starFeet + 23},
                        {x: Math.floor(width * 1 / 5) + left, y:starFeet},
                        {x: Math.floor(width * 1 / 5) + left, y:starShoulder},
                        {x: Math.floor(width * 4 / 5) + left, y:starFeet + perLevel},
                        {x: Math.floor(width * 3 / 5) + left, y:starFeet + 23 + perLevel},
                        {x: Math.floor(width * 2 / 5) + left, y:starFeet + 23 + perLevel},
                        {x: Math.floor(width * 1 / 5) + left, y:starFeet + perLevel},
                        {x: Math.floor(width * 4 / 5) + left, y:starFeet + (2*perLevel)},
                        {x: Math.floor(width * 3 / 5) + left, y:starFeet + 23 + (2*perLevel)},
                        {x: Math.floor(width * 2 / 5) + left, y:starFeet + 23 + (2*perLevel)},
                        {x: Math.floor(width * 1 / 5) + left, y:starFeet + (2*perLevel)},
                    ];

   return {width: width, positions: positions };
}

var engines = [
               "google",
               "amazon",
               "yahoo",
               "live",
               "youtube",
               "answers",
               "wikipedia",
               "ebay",
               "buy",
               "weather",
               "netflix",
               "hulu",
               "bn",
               "pg",
               "goodsearch"
               ];


