﻿
var objImg1;
var objImg2;
var objName;
var objUrl;
var objUrl2;
var objUrl3;
var imgNum1;
var imgNum2;
var randNum;
var currentNumber;
var child;
var xmlDoc = null; 
var blState;
var position;
var thumbCollectionID;
var largeCollectionID;
var random = false;

function loadImageGallery(thumbCollectionId, largeCollectionId, blRand){
    if (document.getElementById("imgSplash1")) objImg1 = document.getElementById("imgSplash1");
    if (document.getElementById("imgSplash2")) objImg2 = document.getElementById("imgSplash2");
    if (document.getElementById("slideName")) objName = document.getElementById("slideName");
    if (document.getElementById("lightboxLink")) objUrl = document.getElementById("lightboxLink");
    if (document.getElementById("imgSplashLink1")) objUrl2 = document.getElementById("imgSplashLink1");
    if (document.getElementById("imgSplashLink2")) objUrl3 = document.getElementById("imgSplashLink2");
    
    random = blRand;
    
    randNum = 0;
    currentNumber = 1;
    thumbCollectionID = thumbCollectionId;
    largeCollectionID = largeCollectionId;

    loadXML('../_xml/imageCollections.xml');
    
    blState = true;
    position = 0;
}
function loadXML(xmlFile) {
    xmlDoc = new XMLHttpRequest();
    xmlDoc.open('GET', xmlFile, true);
    xmlDoc.onreadystatechange=verify;  
    xmlDoc.send(null); 
}
function verify() {     
    if (xmlDoc.readyState != 4) { 
        return false; 
    } else {
         if (xmlDoc.status == 200){
            showFirstItem();
            initInterval();
        } else {
            //error 
        }
    }  
}
function showFirstItem() {
    if (xmlDoc.responseXML.childNodes(1) != null){    
        
        if (random){        
            imgNum1 = getNewRandomNumber();
            imgNum2 = getNewRandomNumber();        
        } else {
            imgNum1 = 0;
            imgNum2 = 1;    
        }
        
        objName.innerText = xmlDoc.responseXML.childNodes(1).childNodes(thumbCollectionID-1).childNodes(imgNum1).getAttribute("description");
        objUrl.href = '/_images/collection' + largeCollectionID + '/'+xmlDoc.responseXML.childNodes(1).childNodes(largeCollectionID-1).childNodes(imgNum1).getAttribute("href");
        objUrl.title = xmlDoc.responseXML.childNodes(1).childNodes(largeCollectionID-1).childNodes(imgNum1).getAttribute("description");
        
        objUrl2.href = '/_images/collection' + largeCollectionID + '/'+xmlDoc.responseXML.childNodes(1).childNodes(largeCollectionID-1).childNodes(imgNum1).getAttribute("href");
        objUrl2.title = xmlDoc.responseXML.childNodes(1).childNodes(largeCollectionID-1).childNodes(imgNum1).getAttribute("description");
        
        objImg1.src = '/_images/collection'+ thumbCollectionID + '/'+xmlDoc.responseXML.childNodes(1).childNodes(thumbCollectionID-1).childNodes(imgNum1).getAttribute("href"); 
        objImg2.src = '/_images/collection'+ thumbCollectionID + '/'+xmlDoc.responseXML.childNodes(1).childNodes(thumbCollectionID-1).childNodes(imgNum2).getAttribute("href");       
    }
}  
function initInterval(){ 
    setTimeout(fadeItem, 5000);
}   
function fadeItem() {
    objName.innerText = xmlDoc.responseXML.childNodes(1).childNodes(thumbCollectionID-1).childNodes(imgNum2).getAttribute("description");
    objUrl.href = '/_images/collection' + largeCollectionID + '/'+xmlDoc.responseXML.childNodes(1).childNodes(largeCollectionID-1).childNodes(imgNum2).getAttribute("href");
    objUrl.title = xmlDoc.responseXML.childNodes(1).childNodes(largeCollectionID-1).childNodes(imgNum2).getAttribute("description");
    
    objUrl3.href = '/_images/collection' + largeCollectionID + '/'+xmlDoc.responseXML.childNodes(1).childNodes(largeCollectionID-1).childNodes(imgNum2).getAttribute("href");
    objUrl3.title = xmlDoc.responseXML.childNodes(1).childNodes(largeCollectionID-1).childNodes(imgNum2).getAttribute("description");
    
    var myEffect = new Effect.Fade('imgSplash1', {
      duration: 1.0,
      afterFinish: doAppear
    });
}   
function doAppear(){    
    setTimeout(appearItem, 5000);
}
function appearItem() {       
    if (xmlDoc.responseXML.childNodes(1) != null){
        if (random){        
            imgNum1 = getNewRandomNumber();
            imgNum2 = getNewRandomNumber();        
        } else {
            imgNum1 = getNextNumber();
            imgNum2 = getNextNumber();   
        }
               
        objImg1.src = '/_images/collection' + thumbCollectionID + '/'+xmlDoc.responseXML.childNodes(1).childNodes(thumbCollectionID-1).childNodes(imgNum1).getAttribute("href");                
        objName.innerText = xmlDoc.responseXML.childNodes(1).childNodes(thumbCollectionID-1).childNodes(imgNum1).getAttribute("description");
        objUrl.href = '/_images/collection' + largeCollectionID + '/'+xmlDoc.responseXML.childNodes(1).childNodes(largeCollectionID-1).childNodes(imgNum1).getAttribute("href");
        objUrl.link = xmlDoc.responseXML.childNodes(1).childNodes(largeCollectionID-1).childNodes(imgNum1).getAttribute("description");
        
        objUrl2.href = '/_images/collection' + largeCollectionID + '/'+xmlDoc.responseXML.childNodes(1).childNodes(largeCollectionID-1).childNodes(imgNum1).getAttribute("href");
        objUrl2.title = xmlDoc.responseXML.childNodes(1).childNodes(largeCollectionID-1).childNodes(imgNum1).getAttribute("description");
        
        Effect.Appear('imgSplash1', { duration: 1.0, afterFinish: changeTwo });
        this.initInterval();        
    }
}
function changeTwo() {
    if (xmlDoc.responseXML.childNodes(1) != null){
        objImg2.src = '/_images/collection' + thumbCollectionID + '/'+xmlDoc.responseXML.childNodes(1).childNodes(thumbCollectionID-1).childNodes(imgNum2).getAttribute("href");
    }
}
function getNewRandomNumber() {
    var tmpRand = randNum;
    randNum = Math.floor((xmlDoc.responseXML.childNodes(1).childNodes(thumbCollectionID-1).childNodes.length-0)*Math.random());    
    
    while(randNum == tmpRand) {
        randNum = Math.floor((xmlDoc.responseXML.childNodes(1).childNodes(thumbCollectionID-1).childNodes.length-0)*Math.random());
    }

    return randNum;
}
function getNextNumber(){
    var number = currentNumber;
    
    if (currentNumber == (xmlDoc.responseXML.childNodes(1).childNodes(thumbCollectionID-1).childNodes.length -1))
        number = 0;
    else
        number++;
        
        currentNumber = number;
    
    return number;
}
function getNewFollowingNumber(strDirection){
    if (strDirection == "next") {
        if (position < xmlDoc.responseXML.childNodes(1).childNodes(thumbCollectionID-1).childNodes.length)
            position++;
    } else if (strDirection == "previous") {
        if (position <= xmlDoc.responseXML.childNodes(1).childNodes(thumbCollectionID-1).childNodes.length)
            position--;
    }
}
if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
