Array.prototype.has = function(value)
{
    var i;

    for (var i = 0, loopCnt = this.length; i < loopCnt; i++)
    {
        if (this[i] === value) return (true);
    }

    return (false);
};

var version = new Array(2);
version[0] = 0;
version[1] = 0;
version[2] = 0;
version[3] = 0;
version[4] = 1;

function beejax_index(version3, version4, version5)
{
    version[0] = version3;
    version[1] = version4;
    version[2] = version5;

    beejax("http://beeqube.com/ajax/ajax_created.json", "latest_ajax", 0);
    beejax("http://beeqube.com/ajax/ajax_added.json", "recent_ajax", 1);
    beejax("http://beeqube.com/ajax/ajax_active.json", "active_ajax", 2);
    beejax("http://beeqube.com/ajax/ajax_grab.json", "grab_ajax", 3);
    beejax("http://beeqube.com/ajax/ajax_featured.json", "featured_ajax", 4);
}

function beejax(ajax_file, div_element, version_index)
{
    var xmlhttp = false;
    xmlhttp = getAjaxRequest();
    xmlhttp.open('GET', ajax_file, true);
    xmlhttp.setRequestHeader('If-Modified-Since', 'Wed, 15 Nov 1995 00:00:00 GMT');
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
                parseJason(xmlhttp, div_element, version_index);
        }
    };
    xmlhttp.send(null);
}

function getAjaxRequest()
{
    if (window.XMLHttpRequest)
    {
        return new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        try
        {
            return new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                return new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {}
        }
    } 

    return null;
}

function parseJason(xmlhttp, div_element, version_index)
{
    var text = '';

    if(xmlhttp.readyState==4)
    {
        if (xmlhttp.status == 200 || xmlhttp.status == 304)
        {
            if (xmlhttp.responseText != null)
            {
                data = eval('(' + xmlhttp.responseText + ')');
                if (data != null)
                {
                    switch (version_index)
                    {
                        case 3:
                        units = data.units;
                        unitCount = units.length;
                        var uniqueArray = Array(15);
                        var i = 0, panicCount = 0;
                        while (i < 15)
                        {
                            randValue = Math.floor(Math.random() * unitCount);
                            if (uniqueArray.has(randValue) == false)
                            {
                                if (!(i%2)) background = 'background-color: #ece9d8';
                                else background = 'border-color: #ece9d8;';

                                text += '<div class="search_collection_box" style="' + background + '"><div class="search_collection_title"><a href="' + data.units[randValue].url + '" title="' + data.units[randValue].title + '" class="search_collection"><b>' + data.units[randValue].title + '</b></a> </div> <div style="padding: 0px 10px 0px 10px;"> <a href="' + data.units[randValue].url + '" style="border-color: #ece9d8;" title="' + data.units[randValue].title + '" alt="' + data.units[randValue].title + '"> <img class="search_collection" style="border-color: #ece9d8;" src="' + data.units[randValue].image + '"> </a> </div> </div>';
                                uniqueArray[i++] = randValue;
                                if (panicCount++ > 100) i = 20;
                            }
                        }
                        document.getElementById(div_element).innerHTML = text;
                        break;

                        case 4:
                        var gotCount = 0;
                        units = data.units;
                        unitCount = units.length;
                        featuredIndex = version[version_index];

                        for (i = featuredIndex; i < unitCount; i++)
                        {
                            if (!(gotCount%2)) background = 'background-color: #ece9d8';
                            else background = 'border-color: #ece9d8;';

                            text += '<div class="search_collection_box" style="' + background + '"><div class="search_collection_title"><a href="' + data.units[i].url + '" title="' + data.units[i].title + '" class="search_collection"><b>' + data.units[i].title + '</b></a> </div> <div style="padding: 0px 10px 0px 10px;"> <a href="' + data.units[i].url + '" style="border-color: #ece9d8;" title="' + data.units[i].title + '" alt="' + data.units[i].title + '"> <img class="search_collection" style="border-color: #ece9d8;" src="' + data.units[i].image + '"> </a> </div> </div>';
                            if (gotCount++ > 3) break;
// alert("got: " + gotCount);
                        }
                        if (gotCount < 5)
                        {
// alert("ugh!");
                            for (i = 0; i < (5 - gotCount); i++)
                            {
                                if (!(gotCount%2)) background = 'background-color: #ece9d8';
                                else background = 'border-color: #ece9d8;';

                                text += '<div class="search_collection_box" style="' + background + '"><div class="search_collection_title"><a href="' + data.units[i].url + '" title="' + data.units[i].title + '" class="search_collection"><b>' + data.units[i].title + '</b></a> </div> <div style="padding: 0px 10px 0px 10px;"> <a href="' + data.units[i].url + '" style="border-color: #ece9d8;" title="' + data.units[i].title + '" alt="' + data.units[i].title + '"> <img class="search_collection" style="border-color: #ece9d8;" src="' + data.units[i].image + '"> </a> </div> </div>';
                            }
                        }

                        version[version_index]++;
                        document.getElementById(div_element).innerHTML = text;
                        break;

                        default:
                        newVersion = data.version;

                        if (version[version_index] != newVersion)
                        {
// alert("version_index: " + version_index + " :: version: " + version[version_index] + " :: new: " + newVersion);
                            version[version_index] = newVersion;
                            units = data.units;
                            unitCount = units.length;

                            for (i = 0; i < unitCount; i++)
                            {
                                if (!(i%2)) background = 'background-color: #ece9d8';
                                else background = 'border-color: #ece9d8;';

                                text += '<div class="search_collection_box" style="' + background + '"><div class="search_collection_title"><a href="' + data.units[i].url + '" title="' + data.units[i].title + '" class="search_collection"><b>' + data.units[i].title + '</b></a> </div> <div style="padding: 0px 10px 0px 10px;"> <a href="' + data.units[i].url + '" style="border-color: #ece9d8;" title="' + data.units[i].title + '" alt="' + data.units[i].title + '"> <img class="search_collection" style="border-color: #ece9d8;" src="' + data.units[i].image + '"> </a> </div> </div>';
                            }

                            document.getElementById(div_element).innerHTML = text;
                        }
                        break;
                    }
                }
            }
        }
    }
}
