// JavaScript Document
var currentPhoto = 0;
var secondPhoto = 1;

var currentOpacity = new Array();
var imageTempArray = new Array()
var imageArray = new Array();

var FADE_STEP = 6;
var FADE_INTERVAL = 10;

var blnContinue = true;
var xmlDoc;
var lgPath;

function init() {
	if (document.implementation && document.implementation.createDocument) {
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.onload = loadImages();
		//xmlDoc.load("http://www.danieljscogna.com/captions.xml");
		//alert(xmlDoc.documentElement.nodeName);
	} else 
		if (window.ActiveXObject) {
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4) loadImages();
		}
	} else {
		alert('Problem with browser');
		return;
	}

	xmlDoc.load("http://www.danieljscogna.com/captions.xml");
}

function loadImages() {
	//check for ff to make sure it is open
	if (xmlDoc.documentElement == null) {
		ti = setTimeout("loadImages()",100); 
		return;
	}

    var album = xmlDoc.getElementsByTagName('album');
    lgPath = album[0].getAttribute('lgPath');
    var arrCnt = 0
    
    for (i=0; i<album[0].childNodes.length; i++) {
		if (album[0].childNodes[i].nodeName == 'img') {
			imageTempArray[arrCnt] = [album[0].childNodes[i].getAttribute('gatewayImg'),album[0].childNodes[i].getAttribute('caption'),""];
			arrCnt++;
		}
    }

    var imageOrderArray = new Array();
   
    while (imageOrderArray.length != imageTempArray.length) {
        intSeed = (Math.round(Math.random()*(imageTempArray.length-1)));
        blnFound = false;
        for (i=0; i<imageOrderArray.length; i++) {
            if (imageOrderArray[i] == intSeed) {
                i = imageOrderArray.length;
                blnFound = true;
            }
        }
        
        if (!blnFound) imageOrderArray[imageOrderArray.length] = intSeed;
    }

    for (i=0; i<imageOrderArray.length; i++) {
        imageArray[i] = [imageTempArray[imageOrderArray[i]][0],imageTempArray[imageOrderArray[i]][1],imageTempArray[imageOrderArray[i]][2]];
    }

	displayImgs();
}

function displayImgs() {
    currentOpacity[0]=99;
    for(i=1;i<imageArray.length;i++)currentOpacity[i]=0;
    mHTML="";
    for(i=0;i<imageArray.length;i++)mHTML+='<div id="photo" name="photo" class="mPhoto" onmouseover="if (blnContinue) clearTimeout(mInterval);" onmouseout="if (blnContinue) mInterval = setTimeout(\'crossFade()\',FADE_INTERVAL);"><a href="http://www.danieljscogna.com/gallery.html" id="indeximage"><img src="' + lgPath + imageArray[i][0] + ' "></a></div>';
    document.getElementById("mContainer").innerHTML = mHTML;

    if(document.all) {
        document.getElementsByName("photo")[currentPhoto].style.filter="alpha(opacity=100)";
    } else {
        document.getElementsByName("photo")[currentPhoto].style.MozOpacity = .99;
    }

    mInterval = setTimeout("crossFade()",5000);
}

function crossFade() {
    currentOpacity[currentPhoto]-=FADE_STEP;
    currentOpacity[secondPhoto] += FADE_STEP;

    if(document.all) {
        document.getElementsByName("photo")[currentPhoto].style.filter = "alpha(opacity=" + currentOpacity[currentPhoto] + ")";
        document.getElementsByName("photo")[secondPhoto].style.filter = "alpha(opacity=" + currentOpacity[secondPhoto] + ")";
    } else {
        document.getElementsByName("photo")[currentPhoto].style.MozOpacity = currentOpacity[currentPhoto]/100;
        document.getElementsByName("photo")[secondPhoto].style.MozOpacity =currentOpacity[secondPhoto]/100;
    }

    if(currentOpacity[secondPhoto]/100>=.98) {
        currentPhoto = secondPhoto;
        secondPhoto++;
        if(secondPhoto == imageArray.length)secondPhoto=0;
        if (blnContinue) mInterval = setTimeout("crossFade()",5000);
        else blnContinue = true;
    } else mInterval = setTimeout("crossFade()",FADE_INTERVAL);
}