var req;
var xmlDocument;
var imageShow = new Array();
var image1 = "";
var lastIndex = 0;

function jsImageShow() {
    document.write("<img id='flashImage1' name='flashImage1' src='' />");
    
    startImageShow();
}

function startImageShow() {
    var xmlFile = "/library/javascript/rotate.xml";
	
	
  
    req = new getHTTPObject();
    req.open('GET', xmlFile, true);
    req.onreadystatechange = loadImageShow;
    req.send(null); 
}

getHTTPObject = function() {
	if (typeof XMLHttpRequest != 'undefined') {
		return new XMLHttpRequest();
	} 
	try {
		return new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			return new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	}
	return false;
}

function loadImageShow() {
    if (req.readyState == 4) {
        if(req.status == 200) {
            xmlDocument = req.responseXML;
            var portfolio = xmlDocument.getElementsByTagName('portfolio')[0];
            var contentGroups = portfolio.getElementsByTagName('pContent');

            for( var i=0; i<contentGroups.length; i++ ) {
                imageShow[i] = contentGroups[i].getElementsByTagName('id')[0].childNodes[0].nodeValue;
                imageShow[i] += "::" + contentGroups[i].getElementsByTagName('id')[1].childNodes[0].nodeValue;
            }
            iniateImages();
        }
    }
}

function getImages() {
    var randIndex = lastIndex;
    while( randIndex == lastIndex ) {
        randIndex = Math.round(Math.random()*( imageShow.length - 1));
    }
    lastIndex = randIndex;
    return imageShow[ randIndex ];
}

function iniateImages() {
    document.images.flashImage1.style.opacity = 0;
    displayImages();
    shiftOpacity( 'flashImage1', 1000 );
    setTimeout( "updateImages()", 6000 );
}

function displayImages() {
    var images = getImages();
    image1 = images.split("::")[0];
    document.images.flashImage1.src = image1;
}

function alertAllImages() { /* for debugging purposes */
    for( var i=0; i<imageShow.length; i++) {
        alert( imageShow[i] );
    }
}

function updateImages() {
    shiftOpacity( 'flashImage1', 1000 );

    setTimeout( "displayImages()", 1000 );
    setTimeout( "shiftOpacity( 'flashImage1', 1000 )", 1000 );

    setTimeout( "updateImages()", 7000 );
}


function shiftOpacity(id, millisec) {
    //if an element is invisible, make it visible, else make it ivisible
    if(document.getElementById(id).style.opacity == 0) {
        opacity(id, 0, 100, millisec);
    } else {
        opacity(id, 100, 0, millisec);
    }
} 

function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 






