﻿/**
* Slideshow Class
**************************************************************************/

var thisSlideshow = null;

function Slideshow(autoChange) {

    this.playing = true;
    this.autoChangeIn = autoChange;
    thisSlideshow = this;

    // Elements
    this.eyecatcher;
    this.statusMessage;
    this.imageElement;
    this.titleElement;
    this.descriptionElement;

    //null == first not shown
    this.opacity = null;

    this.imagePath = "/images/eyecatchers/";
    this.imageToFetch = new Array();
    this.imageArray = new Array();

    this.setLastChange();

    if (this.autoChangeIn > 0) {
        this.checkForAutoChange();
    }
}

Slideshow.prototype.togglePlayPause = function() {

    this.playing = !this.playing;

    var buttons = document.getElementsByName("eyecatcherPlayPause");

    for (var i = 0; i < buttons.length; i++) {
    
        var button = buttons[i];
    
        if (this.playing) {
            button.innerHTML = "||";
        } else {
            button.innerHTML = "&gt;";
        }
    }
}

Slideshow.prototype.setLastChange = function() {
    var now = new Date();
    this.lastChange = now.getTime();
}

Slideshow.prototype.checkForAutoChange = function() {

    if (this.playing) {
        var now = new Date();
        var millis = now.getTime();
        var changeAt = new Number(this.lastChange) + new Number(this.autoChangeIn);

        if (changeAt < millis) {

            if (this.getPages().length <= 1) {
                return;
            }

            //change image
            this.autoChange();
        }
    }

    setTimeout("thisSlideshow.checkForAutoChange()", 500);
}


Slideshow.prototype.autoChange = function() {

    var pages = this.getPages();

    var currentIndex = -1;

    for (var i = 0; i < pages.length; i++) {

        if (pages[i].className == "currentEyecatcherPage") {
            currentIndex = i;
        }
    }

    if (currentIndex != -1) {
        var nextIndex = 0;

        if (currentIndex != pages.length - 1) {
            nextIndex = currentIndex + 1;
        }

        this.showImage(nextIndex);
    }
}

Slideshow.prototype.showImage = function(index) {

    this.setPage(index);
}

Slideshow.prototype.setPage = function(pageId) {

    this.setLastChange();

    var fadeTime = 500;
    var pause = 200;

    this.fadeOutAndIn(fadeTime, pause);
    setTimeout("thisSlideshow.setContent(" + pageId + ")", fadeTime + (pause / 2));

}


Slideshow.prototype.getPages = function() {

    var pages = new Array();

    var id = 0;

    while (true) {
        var pageContainer = document.getElementById("eyecatcherPage_" + id);

        if (!pageContainer) {
            break;
        }

        pages[id] = pageContainer;
        id++;
    }

    return pages;
}

Slideshow.prototype.setContent = function(pageId) {

    var pages = this.getPages();

    for (var id = 0; id < pages.length; id++) {
        if (id == pageId) {
            pages[id].setAttribute("class", "currentEyecatcherPage");
            pages[id].setAttribute("className", "currentEyecatcherPage");
        }
        else {
            pages[id].setAttribute("class", "hiddenEyecatcherPage");
            pages[id].setAttribute("className", "hiddenEyecatcherPage");
        }
    }
}


String.prototype.endsWith = function(str)
{return (this.match(str+"$")==str)}

Slideshow.prototype.isFlash = function(path) {
    return path.toUpperCase().endsWith(".FLV") || path.toUpperCase().endsWith(".SWF");
}


Slideshow.prototype.populate = function() {
    this.statusMessage = document.getElementById("status");
    this.eyecatcher = document.getElementById("eyecatcher");

    var children = this.eyecatcher.childNodes;
    var length = children.length;

    for (var i = 0; i < length; i++) {
        if (children[i].nodeType == 1 && children[i].className) {
            if (children[i].className == "center black") {
                var child = children[i].getElementsByTagName("DIV")[0];

                this.imageElement = child;
                var subElements = child.getElementsByTagName("DIV");
                this.titleElement = subElements[0];
                this.descriptionElement = subElements[1];
            }
            else if (children[i].className == "navigation") {
                this.navigation = children[i];
            }
        }
    }
}

Slideshow.prototype.fetchImage = function(id, pos) {
    pos = (!pos) ? this.imageToFetch.length : pos;
    this.imageToFetch[pos] = "image[]=" + id + "&";
}

Slideshow.prototype.fetchXML = function() {
    this.loadingMessage(1);
    var images = this.imageToFetch.join("");
    images = images.substring(0, images.length - 1);


    var ajax = new dqc.ajax.Ajax('xml');

    success = ajax.get("/eye-catcher/index?" + images, "", dojo.hitch(this, "AjaxCallbackSuccess"), dojo.hitch(this, "AjaxCallbackError"), new Array());
}

Slideshow.prototype.AjaxCallbackSuccess = function(extraArgs, data) {
    var xml = new RSSChannel(data);

    this.titleElement.style.backgroundColor = "#333";
    this.descriptionElement.style.backgroundColor = "#333";

    this.parseXML(xml);
    this.setImage(0);
    this.setNavigation();

    this.statusMessage.style.backgroundImage = "";
}

Slideshow.prototype.AjaxCallbackError = function(extraArgs, type, error, httpObj)
{ }


Slideshow.prototype.loadingMessage = function(i) {
    switch (i) {
        case -1: 	// Error
            this.statusMessage.innerHTML = "The eyecatcher could not load!";
            break;
        case 1: 		// Loading
            this.statusMessage.style.backgroundImage = "url(/images/gfx/ajax_loader_black.gif)";
            break;

        case 2: 		// Success
            this.statusMessage.style.backgroundImage = "";
            break;
    }
}

Slideshow.prototype.parseXML = function(xml) {
    var items = xml.items.length;
    var j;

    for (var i = 0; i < items; i++) {
        if (xml.items[i].id != null) {
            j = this.imageArray.length;
            this.imageArray[j] = new Array();
            this.imageArray[j] = [xml.items[i].id,
									xml.items[i].title,
									xml.items[i].description,
									xml.items[i].filename];
        }
    }
}

Slideshow.prototype.setImage = function(i) {
    if (this.imageArray.length > i) {
        if (this.opacity == null) {
            this.opacity = 100;
            this.changeImage("'" + this.imagePath + this.imageArray[i][3] + ".jpg'", this.imageArray[i][1], this.imageArray[i][2]);
        } else {
            this.fadeOutAndIn(500);
            setTimeout(dojo.hitch(this, "changeImage", this.imagePath + this.imageArray[i][3] + ".jpg", this.imageArray[i][1], this.imageArray[i][2]), 500);
            //setTimeout(function(thisObj, img, text, desc) { thisObj.changeImage(img, text, desc); }, 500, this, this.imagePath + this.imageArray[i][3] + ".jpg", this.imageArray[i][1], this.imageArray[i][2]);
        }
    }
    else {
        return false;
    }
}

Slideshow.prototype.changeImage = function(img, text, desc) {
    this.imageElement.style.backgroundImage = "url(" + img + ")";
    this.titleElement.innerHTML = text;
    this.descriptionElement.innerHTML = desc;
}

Slideshow.prototype.setNavigation = function() {
    var obj = this;
    var length = this.imageArray.length;

    for (var i = 0; i < length; i++) {
        var page = document.createElement("A");
        page.innerHTML = i + 1;
        page.href = "javascript: void(0);";
        page.parameter = i;
        page.onclick = function() { obj.setImage(this.parameter); };
        this.navigation.appendChild(page);
    }
}


Slideshow.prototype.fadeOutAndIn = function(fadeTime, pause) {

    this.opacity = 100;

    var obj = this;

    //fade out
    while (this.opacity > 0) {
        setTimeout("thisSlideshow.updateOpac(" + this.opacity + ")", (fadeTime / 100) * (100 - this.opacity));
        this.opacity--;
    }

    //fade in
    while (this.opacity < 100) {
        setTimeout("thisSlideshow.updateOpac(" + this.opacity + ")", pause + fadeTime + (5 * this.opacity));
        this.opacity++;
    }

    setTimeout("thisSlideshow.updateOpac(" + this.opacity + ")", pause + (fadeTime * 2));
}

//change the opacity for different browsers
Slideshow.prototype.updateOpac = function(opac) {

    var eyecatcherContainer = document.getElementById("eyecatcherContainer");

    var object = eyecatcherContainer.style;
    object.opacity = (opac / 100);
    object.MozOpacity = (opac / 100);
    object.KhtmlOpacity = (opac / 100);
    object.filter = "alpha(opacity=" + opac + ")";
}


