var totalImages = 4;
var eventsToShow = 5;
var newsToShow =1;
var currentImage = 0;

function getRandomNumber(total){
    return Math.floor(Math.random()*total); 
}
function loadPage(){
    importXML();
}
function updateToCurrentImage(){
    var bigDiv = document.getElementById("bigimage");
    bigDiv.style.backgroundImage = "url('./images/"+currentImage+".jpg')";
	
}

function nextBgImage(){
    if(currentImage==totalImages-1){
        currentImage=0;
        updateToCurrentImage();
    }else{
    currentImage++;
    updateToCurrentImage();
}
}
function previousBgImage(){
    if(currentImage==0){
        currentImage=totalImages-1;
        updateToCurrentImage();
    }else{
    currentImage--;
    updateToCurrentImage();
}
}
function importXML(){

    req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	if(req) {
		req.onreadystatechange = processReqChange;
		req.open("GET", "page.xml", true);
		req.send("");
	}
    
}
function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
		xmlDoc = req.responseXML;
		createItem();
	}
}

function createItem(){
    createQuote();
    
    createHomepage();
}

function createHomepage(){
    cleanupPage();
    currentImage = getRandomNumber(totalImages);
    updateToCurrentImage();
    /*var imglks = document.createElement("div");
    imglks.setAttribute("id","imageLinks");
    var imgbk = document.createElement("a");
    imgbk.setAttribute("href","javascript:nextBgImage()");
    imgbk.appendChild(document.createTextNode("<< "));
    imglks.appendChild(imgbk);
    imglks.appendChild(document.createTextNode("images"));
    var imgfw = document.createElement("a");
    imgfw.setAttribute("href","javascript:previousBgImage()");
    imgfw.appendChild(document.createTextNode(" >>"));
    imglks.appendChild(imgfw);
    document.getElementById("bigimage").appendChild(imglks);
    */
	//var brownie = document.createElement("object");
	//brownie.setAttribute("id","brown_ne");
	//brownie.setAttribute("type","application/x-shockwave-flash");
	//brownie.setAttribute("data","newsevents/index.lzx.lzr=swf8.swf?lzproxied=false");
	//brownie.setAttribute("width","634");
	//brownie.setAttribute("height","234");
	//brownie.appendChild(createParams("movie","newsevents/index.lzx.lzr=swf8.swf?lzproxied=false"));
	//brownie.appendChild(createParams("quality","high"));
	//brownie.appendChild(createParams("scale","noscale"));
	//brownie.appendChild(createParams("salign","LT"));
	//brownie.appendChild(createParams("menu","false"));
	//document.getElementById("bigimage").appendChild(brownie);
	
}

function createParams(pname,pvalue){
 var param1 = document.createElement("param");
 param1.setAttribute("name",pname);
 param1.setAttribute("value",pvalue);
 return param1;
}

function createQuote(){
    var allQuotes = xmlDoc.getElementsByTagName("quote");
    var quoteCount = allQuotes.length;
    var quoteLine = document.createElement("p");
    var boldSpan = document.createElement("span");
    boldSpan.appendChild(document.createTextNode("Great Thoughts: "));
    quoteLine.appendChild(boldSpan);
    var quote = document.createTextNode(allQuotes[getRandomNumber(quoteCount)].firstChild.nodeValue);
    quoteLine.appendChild(quote);
    document.getElementById("greatquotes").appendChild(quoteLine);
}


function CBG(divToLight, newColor){
    var divObject = document.getElementById(divToLight);
    divObject.style.backgroundColor=newColor;
}

function cleanupPage(){
    var bigDiv = document.getElementById("bigimage");
    
    var cc = document.getElementById("contentContainer");
    if(cc){bigDiv.removeChild(cc);}
}
function setupChildPage(child){
    var childName = xmlDoc.getElementsByTagName(child);
    var bigDiv = document.getElementById("bigimage");
    var il = document.getElementById("imageLinks");
    if(il){bigDiv.removeChild(il);}
    var ne = document.getElementById("brown_ne");
	var lsz = document.getElementById("lzstinks");
	if(ne){
	if(lsz){
	ne.removeChild(lsz);
	}
	bigDiv.removeChild(ne);
	}
	 var cc = document.getElementById("contentContainer");
    if(cc){bigDiv.removeChild(cc);}
	bigDiv.style.backgroundImage = "none";
    var contentContainer = document.createElement("div");
    contentContainer.setAttribute("id","contentContainer");
    var childImage = document.createElement("img");
    childImage.setAttribute("src",childName[0].getAttribute("image"));
    childImage.setAttribute("id","contentImage");
    childImage.setAttribute("width",childName[0].getAttribute("imgwidth"));
    childImage.setAttribute("height",childName[0].getAttribute("imgheight"));
    contentContainer.appendChild(childImage);
    var childTitle = document.createElement("h3");
    childTitle.appendChild(document.createTextNode(childName[0].getAttribute("title")));
    contentContainer.appendChild(childTitle);
    var paragraphs = childName[0].getElementsByTagName("paragraph");
    for(var i=0;i<paragraphs.length;i++){
        var par = document.createElement("p");
        for(var j=0;j<paragraphs[i].childNodes.length;j++){
            var parseElement = paragraphs[i].childNodes[j];
            if(parseElement.nodeName=="text"){
                par.appendChild(document.createTextNode(parseElement.firstChild.nodeValue));
            }else if(parseElement.nodeName=="a"){
            var alink = document.createElement("a");
            alink.setAttribute("href",parseElement.getAttribute("href"));
            alink.appendChild(document.createTextNode(parseElement.firstChild.nodeValue));
            par.appendChild(alink);
        }
    }
    contentContainer.appendChild(par);
}

bigDiv.appendChild(contentContainer);
}

function openPage(page,width,height){
    window.open (page,'mywindow','menubar=0,resizable=0,scrollbars=1,width='+width+',height='+height+'') ;
}
