// ---------------------
// AJAX widgets
// main functions are at top, 
//
// ---------------
// common librarys from Dynamic Drive :
// createAjaxObj()
// ajaxpack(object)
// 
// ---------------
// XML processing functions
// 
// GetXMLObject()       - returns a single object with property name/values pages converted from nodes/node value
// GetXMLObjectArray()  - returns an array of objects of similiar structure to GetXMLObject
// ---------------------

//See it pop
function seeitPop(theURL, top, left, width, height) {
    myPop = window.open(theURL ,'seeit','width=' + width + ', height=' + height + ',scrollbars=0, top=' + top + ', left=' + left);
    if(myPop){
      myPop.focus();
    }
    else{
      alert('We have detected that you are using popup blocking software.\nTo view the chosen item, please enable popups for this website');
    }
}

// ---------------------
// get gallery details
// ---------------------
var factXML;
var factIndex = 0;
function getFactXML(){
    var myajax      = ajaxpack.ajaxobj;
    var myfiletype  = ajaxpack.filetype;
    var gXML;
    if (myajax.readyState == 4){ //if request of file completed
        if (myajax.status==200 || window.location.href.indexOf("http")==-1){ //if request was successful or running script locally
            gXML    = myajax.responseXML;
            factXML = new GetXMLObjectArray(gXML, 'Fact');
            showFact(0);
        } // got file
    } // req completed
}


// ---------------------
// display fact, need factIndex to keep track of current fact
// ---------------------
function showFact(change){
    var gPanelDiv = document.getElementById('fact');
    var factDiv = document.getElementById('fact-copy');
    if (gPanelDiv){
    
        factIndex += change;
        
        //switch round if too high
        if (factIndex >= factXML.length){
            factIndex = 0;
        }
        
        // back to start
        if (factIndex < 0){
            factIndex = factXML.length -1;
        }
      
        oFact = factXML[factIndex];
        
        if (oFact){   
        
            factDiv.innerHTML = oFact['factcopy'];
            
        } else {
        
          factDiv.innerHTML = 'no fact!';
        } // populate content
    }
}



// ---------------------
// returns an array of objects with properties based on XML attributes
// ---------------------
function GetXMLObjectArray(XML, nodename){

    var markers     = XML.documentElement.getElementsByTagName(nodename);   
    var thisEval    = '';
    var aNode      = new Array();   // array of nodes to be returned
    var node;                       // object containing all properties
    var curStr      = '';
    for (var m = 0; m < markers.length; m++) {
        node = new Object;
        // load xml vars
        for (j=0;j<markers[m].childNodes.length;j++){

            if (markers[m].childNodes[j].nodeType != 1) continue;
            // check if value exists for nodename			    
            if( markers[m].childNodes[j].firstChild){
                curStr = markers[m].childNodes[j].firstChild.nodeValue
                curStr = curStr.replace(/\r\n|\r|\n/g, '') // all line breaks
                curStr = curStr.replace(/\"/g, '\\"') // escape quotes
                thisEval = 'node.' + markers[m].childNodes[j].nodeName + ' = "' + curStr + '";';
            }
            else{
                thisEval = 'node.' + markers[m].childNodes[j].nodeName + ' = "";';
            }
        //  if (node.id == '19' || node.id == '17'){ta.value += thisEval + '\n\n';alert(thisEval)} //alert(thisEval)}
            eval(thisEval);
         } // load vars for loop
        
        // add objects to an array
        aNode[m] = node;
    }          
    return aNode;
}

// ---------------------
// returns an object with properties based on XML attributes
// ---------------------
function GetXMLObject(XML, nodename){

    var markers     = XML.documentElement.getElementsByTagName(nodename);   
    var thisEval    = '';

    for (var m = 0; m < markers.length; m++) {
        // load xml vars
        for (j=0;j<markers[m].childNodes.length;j++){

            if (markers[m].childNodes[j].nodeType != 1) continue;
            // check if value exists for nodename			    
            if( markers[m].childNodes[j].firstChild){
                thisEval = 'this.' + markers[m].childNodes[j].nodeName + ' = "' + markers[m].childNodes[j].firstChild.nodeValue + '";';
            }
            else{
                thisEval = 'this.' + markers[m].childNodes[j].nodeName + ' = "";';
            }
            eval(thisEval);

        } // load vars for loop
    }          

}


// ---------------------
// string truncate
// ---------------------
var punctuation = '.,;!? ';
function trunc(s,size)
{
      if(!size)size=30;
      if(s.length<=size)return s;

      p=-1;

      for(var i=0;i<size;i++)
        if(punctuation.indexOf(s.charAt(i))!=-1)p=i;

      if(p==-1)p=size-1;

      return ''+s.substr(0,p)+'...';
}

// ---------------------
//Basic Ajax Routine- Author: Dynamic Drive (http://www.dynamicdrive.com)
//Last updated: Jan 15th, 06'
// ---------------------

function createAjaxObj(){
    var httprequest=false;
    if (window.XMLHttpRequest){ // if Mozilla, Safari etc
        httprequest=new XMLHttpRequest()
        if (httprequest.overrideMimeType)
         httprequest.overrideMimeType('text/xml');
        }
        else if (window.ActiveXObject){ // if IE
        try {
        httprequest=new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e){
        try{
        httprequest=new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e){}
        }
    }
    return httprequest;
}

var ajaxpack=new Object();
ajaxpack.basedomain="http://"+window.location.hostname;
ajaxpack.ajaxobj=createAjaxObj();
ajaxpack.filetype="txt";
ajaxpack.addrandomnumber=0 ;//Set to 1 or 0. See documentation.

ajaxpack.getAjaxRequest=function(url, parameters, callbackfunc, filetype){
    /*alert('ajaxpack.getAjaxRequest' + url + parameters + callbackfunc + filetype)*/
    ajaxpack.ajaxobj=createAjaxObj();   //recreate ajax object to defeat cache problem in IE
    if (ajaxpack.addrandomnumber==1)    //Further defeat caching problem in IE?
    var parameters=parameters+"&ajaxcachebust="+new Date().getTime()
    if (this.ajaxobj){
        
        this.filetype=filetype;
        this.ajaxobj.onreadystatechange=callbackfunc;
        this.ajaxobj.open('GET', url+"?"+parameters, true)
        this.ajaxobj.send(null)
    }
}