/* ##########################################################################
    Copyright 2005 BBB Systems, LLC, All rights reserved
########################################################################## */

var ajaxEnabled = true;
var b_xml = '';
var c_xml = '';

ajxmlObject = function( ){
    //May add functionality later where the following variables are set
    //as of right now, it's just not necessary.

    this.chatRequest = false;
    if(ajxmlObject.arguments.length > 0 && ajxmlObject.arguments[0]){
        this.chatRequest = true
    }

    this.responseText = '';
    this.responseXML = '';

    this.status = '';
    this.statusText = '';

    //If this is set, the title of the document will dynamicly change
    this.title = '';

    this.isCurrentRequest = false;

    this.xml_http = null;
    this.isSupported = false;

    //If you don't want the workNode to be a part of the actual document,
    //you can change this to be a document.createElement instead.
    this.workNodeID = 'workNode';
    this.workNode = null;

    if(window.XMLHttpRequest || window.ActiveXObject){
        this.isSupported = true;
        this.initialize( );
    }

}

ajxmlObject.prototype = {
/* ---------------------------------------------- */

request: function(url, rMethod){

    if(!this.supported( ) || this.isCurrentRequest){
        return;
    }

    if(!this.workNode){
        this.workNode = document.getElementById(this.workNodeID);
    }

    this.isCurrentRequest = true;

    var oMethod = "GET";
    if(rMethod == "POST"){
        oMethod = "POST";
    }

    this.xml_http.open(oMethod, url, true);



    if(this.xml_http.overrideMimeType){
        this.xml_http.overrideMimeType("text/xml");
    }
    else{
        this.xml_http.setRequestHeader("Content-Type", "text/xml");
    }

    this.xml_http.send(null);
},

initialize: function( ){
    if(this.xml_http){
        this.xml_http = null;
    }

    if(window.XMLHttpRequest){
        this.xml_http = new XMLHttpRequest( );
    }
    else if(window.ActiveXObject){
        this.xml_http = new ActiveXObject("Microsoft.XMLHTTP");
        if(!this.xml_http){
            this.isSupported = false;
        }
    }

    if(this.chatRequest){
        this.xml_http.onreadystatechange = ajxmlObjectProcessChat;
    }
    else{
        this.xml_http.onreadystatechange = ajxmlObjectProcess;
    }
},

supported: function( ){
    return this.isSupported;
}

/* ---------------------------------------------- */
}//end ajxmlObject.prototype

b_xml = new ajxmlObject( );

function ajxmlObjectProcess( ){
    if(b_xml){
        if(b_xml.xml_http.readyState != 4){
            return;
        }

        if(b_xml.xml_http.status == 200){
            b_xml.workNode.innerHTML = b_xml.xml_http.responseText;
            //alert(b_xml.xml_http.responseText);
            if(b_xml.workNode.getElementsByTagName("div")[0].className != 'xmldata'){
                return;
            }

            if(window.ajxShowPreloader){
                customPreloader( );
                return;
            }
            defaultComplete( );

            var b_xmlDivs = b_xml.workNode.getElementsByTagName("div");

            if(b_xmlDivs.length >= 2 && (b_xmlDivs[1].className == 'memberCal' || b_xmlDivs[1].className == 'largeCal')){
                initCalendarNav( );
            }

            if(b_xmlDivs.length >= 2 && (b_xmlDivs[1].className == 'movieTrailer')){
                initObjects( );
            }

            initAjaxTabs( );
            initDirPhoneNumbers( );
            initInputButtons( );

            if(b_xml.workNode.innerHTML.match('loadGoogleMaps')){
                loadGoogleMaps( );
            }
        }
        else{
            //alert(b_xml.xml_http.status + ' - ' + b_xml.xml_http.statusText);
        }
    }
}


function ajxmlObjectProcessChat( ){
    if(c_xml){
        if(c_xml.xml_http.readyState != 4){
            return;
        }

        if(c_xml.xml_http.status == 200){
            c_xml.workNode.innerHTML = c_xml.xml_http.responseText;
            //alert(c_xml.xml_http.responseText);
            if(c_xml.workNode.getElementsByTagName("div")[0].className != 'xmldata'){
                return;
            }

            defaultCompleteChat( );

            if(!chatScrolling){
                var chat_area = document.getElementById('chat_area');
                chat_area.scrollTop = chat_area.scrollHeight;
            }
        }
    }
}

//This is the default most basic completion of ajax requests
function defaultComplete( ){
    var i;
    var xChildren = b_xml.workNode.getElementsByTagName("div")[0].childNodes;

    for(i = 0; i < xChildren.length; i++){
        var node = xChildren[i];

        if(node.nodeType != 1){ continue; }

        /*
        if(node.className == "title"){
            for(j = 0; j < node.childNodes.length; j++){
                if(node.childNodes[j].nodeValue){
                    document.title = node.childNodes[j].nodeValue;
                    break;
                }
            }
            continue;
        }
        */
        //else it's an inner document node.

        var docNode = document.getElementById(node.className);

        if(!docNode){
            continue;
        }

        node.normalize( );
        var nodeCopy = node.cloneNode(true);

        docNode.innerHTML = "";
        if(document.insertAdjacentElement){
            docNode.insertAdjacentElement("beforeEnd", nodeCopy);
        }
        else{
            docNode.appendChild(nodeCopy);
        }
    }

    b_xml.isCurrentRequest = false;
    b_xml.initialize( );

    //must be defined in pscript.js on a per site basis
    //if(xmlInit){
    //    xmlInit( );
    //}
}

function defaultCompleteChat( ){
    var i;
    var xChildren = c_xml.workNode.getElementsByTagName("div")[0].childNodes;

    for(i = 0; i < xChildren.length; i++){
        var node = xChildren[i];

        if(node.nodeType != 1){ continue; }

        var docNode = document.getElementById(node.className);

        if(!docNode){
            continue;
        }

        var error_alert = document.getElementById('error_alert');
        if(error_alert){
            docNode.innerHTML += '<div id="chat_force_refresh" style="display: none;"></div>';
        }

        node.normalize( );
        var nodeCopy = node.cloneNode(true);

        if(node.className == 'chat_area' && node.innerHTML != ''){
            docNode.innerHTML += node.innerHTML;
        }
        else if(node.className != 'chat_area'){
            var d_node_html = '';
            if(docNode.getElementsByTagName("div").length > 0){
                d_node_html = docNode.getElementsByTagName("div")[0].innerHTML;
            }

            if(nodeCopy.innerHTML != d_node_html){
                docNode.innerHTML = '';
                if(document.insertAdjacentElement){
                    docNode.insertAdjacentElement("beforeEnd", nodeCopy);
                }
                else{
                    docNode.appendChild(nodeCopy);
                }
            }
        }
    }

    c_xml.isCurrentRequest = false;
    c_xml.initialize( );

    //must be defined in pscript.js on a per site basis
    //if(xmlInit){
    //    xmlInit( );
    //}
}

//Preloading image functions should be replaced by
//The reason these are in the ajax file is so images load up gracefully
//to give the end user a better experience between page loads.
function customPreloader( ){
    //var docImages = this.workNode.getElementsByTagName("img");
    var imgs = document.getElementsByTagName('img');
    var len = imgs.length;

    if(len == 0){
        defaultComplete( );
        return;
    }

    var preloadContainer = document.getElementById('preloadContainer');
    var preloadBarInner = document.getElementById('preloadBarInner');

    if(preloadContainer.style.display != 'block'){
        preloadBarInner.style.width = '0%';
        preloadContainer.style.display = 'block';
    }

    var numComplete = 0;
    for(var i = 0; i < len; i++){
        if(imgs[i].complete || b_xml.xml_http.status == 404){
            numComplete++;
        }
    }
    if(numComplete == len){
        preloadContainer.style.display = 'none';
        preloadBarInner.style.width = '0%';
        defaultComplete( );
    }
    else{
        var percent = parseInt( (numComplete/len) * 100);
        var nextStep = parseInt( ((numComplete + 1)/len) * 100 );
        var curPercent = parseInt(preloadBarInner.style.width);
        var nextLoad = 50;

        //stuck on a big image, slow it down a bit
        if(percent <= curPercent && curPercent + 1 < nextStep){
            percent = curPercent + 1;//add a little to make it look cooler.
            nextLoad = 200;

        }
        else if(percent <= curPercent){
            percent = curPercent;
        }

        var percentStr = percent + "%";

        preloadBarInner.style.width = percentStr;

        setTimeout('customPreloader( );', nextLoad);
    }
}

function launchAjaxApp(loc){
    //var a_width = screen.availWidth;
    //var a_height = screen.availHeight;

    var a_width = 927;
    var a_height = 614;

    //if(a_width < 927){ a_width = 927; }
    //if(a_height < 614){ a_height = 614; }


    var ajaxApp = window.open(loc, "ajaxApp", "width=" + a_width + ", height=" + a_height + " , scrollbars=0, status=no,toolbar=no,resizable=no");

    ajaxApp.focus( );
    window.location = "/splash";

    return false;
}

//getCookie and setCookie must be defined somewhere.
//element navContainer must be defined.
function ajaxCheck( ){
    if(window.name != 'ajaxApp'){
        if(ajaxEnabled && b_xml){
            if(b_xml.supported( )){
                if(document.getElementById('navContainer')){
                    var navContainer = document.getElementById('navContainer');

                    navContainer.innerHTML = '<li id="navLaunch" onclick="launchAjaxApp(window.location.href);">Click to Launch Application Version</li>';

                    ajaxEnabled = false;
                    b_xml = null;
                }
            }
        }
    }
}