// ============================================================================
// brdcast
// ============================================================================
function brdcast() {}

// ============================================================================
// brdcast.multiPage
// ============================================================================
brdcast.multiPage = function(type, controls, page, max, data)
{
    // init vars
    this.node  = document.getElementById(type + 'Wrapper');
    this.honey = new buffalo.honey(this.node, 15);
    this.ajax  = new buffalo.utils.ajax(type + '.php');
    this.page  = page;
    this.max   = max;
    this.type  = type;
    this.data  = data;
    this.controls = controls;
    
    // set up the pages
    this.changePage();
}

// set up the pages
brdcast.multiPage.prototype.changePage = function()
{
    // get the controls
    var controls = buffalo.utils.getElementsByClassName(this.controls, this.node);
    
    // loop through the controls, replacing important bits
    for (var i = 0; i < controls.length; i ++)
    {
        // get the buttons
        var buttons = controls[i].getElementsByTagName('li');
        var array   = new Array('Last', 'Next', '', 'Prev', 'First');
        
        // loop thorough each button
        for (var j = 0; j < buttons.length; j ++)
        {
            // get the data to override
            if (((j == 0 || j == 1) && this.page < this.max) || ((j == 3 || j == 4) && this.page > 1))
            {
                var page = j == 0 ? this.max : j == 1 ? this.page + 1 : j == 3 ? this.page - 1 : 1;
                var data = '<a href="#" title="Click here for the ' + array[j].toLowerCase() + ' page" class="' + this.type + 'Control" onclick="' + this.type + '.setData(' + page + '); return false;">' + array[j] + '</a>';
            }
            else if (j == 2)
                var data = 'Page ' + this.page + '/' + this.max;
            else
                var data = array[j];
            
            // change the buttons to do what I want
            buttons[j].innerHTML = data;
        }
    }
}

// set up the inspirations
brdcast.multiPage.prototype.setData = function(page)
{
    this.honey.changeFade({initial:100, alter:-10, max:0, after:this.type + ".ajax.setData('page=" + page + "', '" + this.type + ".getData(" + page + ")');"});
}

// create a function to get the inspirations    
brdcast.multiPage.prototype.getData = function(page)
{
    // make sure the xml is valid
    if(this.ajax.requestObject.readyState != 4 || this.ajax.requestObject.status != 200)
        return;
    
    // init vars
    var data = this.ajax.getData('data', this.data);
    
    // change the page number
    this.page = page;
    
    // loop through and add the new data
    buffalo.utils.template(document.getElementById(this.type + 'Template'), document.getElementById(this.type), data);
    
    // add the links into the new data
    buffalo.grass.trackLinks('tracker.php', document.getElementById(this.type).getElementsByTagName('a'));
    
    // change the page data
    this.changePage();
    
    // unhide the page
    this.honey.changeFade({initial:0, alter:10, max:100});
}