var main_sections = new Array ("home", "shows", "aboutus", "contactus", "hireus");
var comment_sections = new Array ("comment", "mailus", "callus");
var us_sections = new Array ("mission", "players", "crew", "alumni");

var current_collage = 1;

// ***************** AJAX stuff *************************

// creates a request object with which the data can be
// "tunneled" through
function create_request(){
    var req;                    // the request object that will be created
    // if using a regular complaince standard browser
    if (window.XMLHttpRequest){
        req = new XMLHttpRequest();
    }// if using Internet Explorer
    else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (!req) {
        alert('Cannot create an XMLHTTP instance');
        return false;
    }else{
        return req;
    }
}
/* the base ajax calling function
  * usually you want to set up a function that calls this one
  * and supplies it with the parameters for a specific <div>tag you need to work with
  * curDiv = the id of the div tag to put HTML into
  * url = the dynamic page waiting for post data, that will return the HTML
  * callback = the function that will be called when data is received by the browser
  * sendStr = a string of name=value pairs seperated by &s, which get sent to the url page
  * show = whether you want to change a hidden div to a visible one
  */
function base_ajax(tagID, url, sendStr, callback){
    var req = create_request();
    if(req){
        req.open("POST", url, true);
        /* when the data has been completely loaded back into the browser
          * and the status is OK, it puts the text string that was returned
          * into the tag specified in curDiv, and shows the div (if specified)
          */
        req.onreadystatechange = function(){
            if (req.readyState == 4 && req.status == 200) {
                if(document.getElementById(tagID)){
                    var elem = document.getElementById(tagID);
                    elem.innerHTML = req.responseText;
                    if(callback){
                        eval(callback);
                    }
                }
            }   
        }
        req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        req.send(sendStr);
    }
}

function swap_collage()
{
    var c = "collage" + current_collage;
    if (current_collage == 1) current_collage = 2; else current_collage = 1;
    var c2 = "collage" + current_collage;
    $(c).hide();
    $(c2).appear();
    base_ajax(c, "collage.php");
}

function contains(a, obj){
    for(var i = 0; i < a.length; i++) {
        if(a[i] === obj){
            return true;
        }
    }
    return false;
}

function get_url_section()
{
    var url = window.location.href;
    var parts = url.split('#');
    var section;
    
    if (parts[1] !== undefined && contains(main_sections, parts[1]))
    {
        section = parts[1];
    }
    else
    {
        section = "home";
    }
    //document.getElementById('testlink').innerHTML = section + ", " + parts;    
    return section;
}

function check_url()
{
    show_section(get_url_section());
}

function show_section(current, sections)
{   
    if (sections !== undefined)
    {
        for (var i = 0; i < sections.length; i++)
        {
            if ($(sections[i]) !== null)
            {
                    
                if (sections[i] != current)
                {
                    $(sections[i]).hide();    
                    document.getElementById(sections[i] + "_link").className = "link";
                }
                else
                {
                    $(sections[i]).appear();    
                    document.getElementById(sections[i] + "_link").className = "selected";               
                }
            }
        }
             
    }
}


function init()
{
    setInterval('swap_collage()', 15000);
    check_url();
}
