/* ##########################################################################
    Copyright 2005 BBB Systems, LLC, All rights reserved
########################################################################## */

var ajaxEnabled = false;
var b_xml = '';

ajxmlObject = function( ){
    //May add functionality later where the following variables are set
    //as of right now, it's just not necessary.
    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;
        }
    }

    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){
            var rtext = b_xml.xml_http.responseText;

            //rtext = rtext.replace(/\*\*\+\^\!/, '&');

            if(document.all){
                //ie bug which refuses to put quotes around text/plain
                rtext = rtext.replace(/type\=\"text\/plain\"/gi, '');
            }

            b_xml.workNode.innerHTML = unescape(rtext);
            //document.getElementById('copyright').innerHTML = '<pre style="text-align: left;">' + htmlschars(b_xml.workNode.innerHTML) + '</pre>';;

            if(b_xml.workNode.getElementsByTagName("div")[0].className != 'xmldata'){
                b_xml.isCurrentRequest = false;
                return;
            }

            //hack for images that fail to load (for various reasons)
            //customPreloader( );
            //defaultComplete called from customPreloader
            defaultComplete( );
        }
        else{
            //alert(b_xml.xml_http.status + ' - ' + b_xml.xml_http.statusText);
        }
    }
}

//This is the default most basic completion of ajax requests
function defaultComplete( ){
    var i;
    var xChildren = b_xml.workNode.getElementsByTagName("div")[0].childNodes;

    //maybe someday when the average box is faster (but it's purdy damn cool).
    //document.getElementById('contentContainer').style.opacity = 0;
    //if(document.all){
    //    document.getElementById('contentContainer').style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=0)";
    //}

    //alert(xChildren.length);

    //for(i = 0; i < xChildren.length; i++){
    i = 0;

    var xlen = xChildren.length;
    while(i < xChildren.length){
        var node = xChildren[i];

        //alert(i + 'a - ' + node.nodeType + ' - ' + node.className);
        //alert(node.innerHTML);

        if(node.nodeType != 1 || node.className == ''){ i++; continue; }


        if(node.className == "title"){
            for(j = 0; j < node.childNodes.length; j++){
                if(node.childNodes[j].nodeValue){
                    document.title = node.childNodes[j].nodeValue;
                    j = node.childNodes.length;
                }
            }
             i++;
            continue;
        }
        //else it's an inner document node.

        var docNode = document.getElementById(node.className);

        if(!docNode){
            i++;
            continue;
        }

        node.normalize( );

        docNode.innerHTML = "";

        docNode.appendChild(node);

        //ie hack, didn't want to use object detection for this.
        //Object detection could be ugly for some unknown browsers.
        if(xlen == xChildren.length){
            i++;
        }
        else{
            xlen = xChildren.length;
        }

    }

    b_xml.isCurrentRequest = false;
    b_xml.initialize( );

    //must be defined in pscript.js on a per site basis
    if(xmlInit){
        xmlInit( );
    }

    //fadeEffect( );//:(
}

//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 s_top = 0;
    if (self.pageYOffset){// all except Explorer
    	s_top = self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop){// Explorer 6 Strict
    	s_top = document.documentElement.scrollTop;
    }
    else if (document.body){// all other Explorers
    	s_top = document.body.scrollTop;
    }

    //alert(s_top);

    var pc_height = 0;

    if(window.outerHeight){
        pc_height = window.outerHeight / 2 + s_top - 256;
    }
    else{
        pc_height = window.screen.height / 2 + s_top - 256;
    }

    preloadContainer.style.top = pc_height + 'px';

    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++){
        //Safari has a bug which doesn't quite support complete and returns undefined (as I'm sure
        //other browsers will that don't support complte).  Anyway, if they fix it, it will work
        //for those browsers... but until then...
        if(imgs[i].complete || imgs[i].complete == undefined || 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;

        b_xml.imgIterations++;
        if(b_xml.imgIterations >= b_xml.maxImgIterations){
            preloadContainer.style.display = 'none';
            preloadBarInner.style.width = '0%';
            defaultComplete( );
            return;
        }

        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( )){
                setcookie('ajaxSupported','1')
            }
        }
    }
}

//fade function for happy content around and stuff.
var cur_fade = 100;
function fadeEffect( ){
    var fade_min = 0;
    var fade_max = 100;
    var fade_speed = 100;
    var fade_inc = 8;

    var content = document.getElementById('contentContainer');

    var exitFunction = false;

    cur_fade = content.style.opacity * 140;

    cur_fade += fade_inc;

    if(cur_fade >= fade_max){
        cur_fade = fade_max;
        exitFunction = true;
    }

    var std_fade = cur_fade/100;
    content.style.opacity = std_fade;
    if(document.all){
        content.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + parseInt(cur_fade) + ")";
    }

    if(exitFunction){
        return;
    }

    setTimeout('fadeEffect( );', fade_speed);
}


function htmlschars(str){
    str = str.replace(/\&/g, "&amp;");
    str = str.replace(/\</g, "&lt;");
    str = str.replace(/\>/g, "&gt;");
    str = str.replace(/\"/g, "&quot;");
    str = str.replace(/\'/g, "&#039;");
    str = str.replace(/\™/g, "&trade;");

    return str;
}