﻿//Slideshow


function SlideShow(source, options) {
    this.options = options;
    this.slides = new Array();
    this.currentSlide = 0;
    this.timer;
    this.e = document.createElement("div");
    this.e.id = "SlideShow";
    this.skipcontrol = document.createElement("a");
    this.skipcontrol.href = "#";
    this.skipcontrol.id = "SkipControl";
    this.skipcontrol.innerHTML = "Skip Intro";
    this.skipcontrol.onclick = function(o) { return function() { o.Finish(); } }(this);
    this.e.appendChild(this.skipcontrol);
    
    

    if (getHTTPRequest) {
        var request = getHTTPRequest();
        var o = this;
        if (request && source) {
          request.open("GET", source, true);
          request.onreadystatechange = function() {
            if(request.readyState == 4 && request.status == 200)
                o.Init(request.responseXML);
            else 
                return;
          }
          request.send(null);
        }
    }
}

SlideShow.prototype.Finish = function() {
    clearInterval(this.timer);
    this.e.style.display = "none";
    document.getElementById("PageBody").style.visibility = "visible";
	document.getElementById("SideBar").style.visibility = "visible";

}

SlideShow.prototype.Init = function(data) {
    var slideNodes = data.getElementsByTagName("slide");
    for (var i = 0; i < slideNodes.length; i++) {
        this.slides.push(new Slide(slideNodes[i]));
        this.slides[i].e.style.position = "absolute";
        this.slides[i].e.style.top = "0";
        this.slides[i].e.style.left = "0";
        this.slides[i].e.style.opacity = "0";
        this.slides[i].e.style.filter = "alpha(opacity=0)";
        this.slides[i].e.style.zIndex = 100 + slideNodes.length - i + "";
        this.e.appendChild(this.slides[i].e);
    }
    this.Start();
}

SlideShow.prototype.Start = function() {
    var o = this;
    
    new Fade(o.slides[o.currentSlide].e, null, 1);
    this.timer = setInterval(function() {
								
								if(o.currentSlide == o.slides.length - 1) {
								    document.getElementById("PageBody").style.opacity = "0";
								    document.getElementById("PageBody").style.opacity = "alpha(opacity=0)";
								    document.getElementById("PageBody").style.visibility = "visible";
								    document.getElementById("SideBar").style.opacity = "0";
								    document.getElementById("SideBar").style.opacity = "alpha(opacity=0)";
								    document.getElementById("SideBar").style.visibility = "visible";
								    new Fade(document.getElementById("PageBody"), o.e, 1, function() { o.Finish(); });
								    new Fade(document.getElementById("SideBar"), o.e, 1, function() { });

								    o.Stop();
								} else { 
								    new Fade(o.slides[o.currentSlide+1].e, o.slides[o.currentSlide].e, 1);
								    o.currentSlide++;
				                }								
			     }, 2500);
}

SlideShow.prototype.Stop = function() {
    clearInterval(this.timer);
}


/*
SlideShow.prototype.Next = function() {

}

SlideShow.prototype.Prev = function() {

}

SlideShow.prototype.ChangeSlide = function(n) {

}
*/
function Slide(slideData) {
    this.e = document.createElement("div");
    this.Images = new Array();
    var n, c;
     for(var i = 0; i < slideData.childNodes.length; i++) {
        n = slideData.childNodes[i];
        if(n.tagName == "img") {
            c = new Image();
            c.src = n.getAttribute("src");
            this.Images.push(c);
            c = document.createElement("img");
            c.src = n.getAttribute("src");
            this.e.appendChild(c);
        }
        else if(n.tagName == "p") {
            c = document.createElement("p");
            c.appendChild(document.createTextNode(n.firstChild.nodeValue));
            this.e.appendChild(c);
        }   
    }
    
}

function Fade(element1, element2, time, f) {
    
    this.e1 = element1;
    this.e2 = element2;

    
    this.timer;
    this.opacity1 = 0.0;
    this.opacity2 = 1.0;  
    this.counter = 0;
    this.countto = Math.ceil(time*30.3);
    this.deltao = 1.0 / this.countto;
   

    this.timer = setInterval( function(o) { return function() {
                                               
                                               if(o.e1) {
                                                o.opacity1 += o.deltao;
                                                o.e1.style.opacity = o.opacity1.toString();
                                                o.e1.style.filter = "alpha(opacity=" + (o.opacity1*100).toString() + ")";
                                               }
                                               if(o.e2) {
                                                o.opacity2 -= o.deltao;
                                                o.e2.style.opacity = o.opacity2.toString();
                                                o.e2.style.filter = "alpha(opacity=" + (o.opacity2*100).toString() + ")";
                                               }
                                               
                                               o.counter++;
                                              
                                               if(o.counter == o.countto) { 
                                                    if(o.e2) {
                                                        o.e2.style.opacity = "0";
                                                        o.e2.style.filter = "alpha(opacity=0)";
                                                    }
                                                    if(o.e1) {
                                                        o.e1.style.opacity = "1";
                                                        o.e1.style.filter = "alpha(opacity=100)";
                                                    }
                                                    if(f) {f();}
                                                    o.Stop();
                                               }
                                           }; }(this), 33);
}

Fade.prototype.Stop = function() {
    clearInterval(this.timer);
}

/*

function CrossFade(e1, e2, time) {

var timer;
var deltao;
this.counter = 0;
this.countto = Math.ceil(time * 20);
this.deltao = this.deltao / this.countto;
this.e.animating = true;
this.timer = setInterval(function(o) {
return function() {
o.e.style.opacity = (parseFloat(o.e.style.opacity) + o.deltao).toString();
o.e.style.filter = "alpha(opacity=" + (parseFloat(o.e.style.opacity) * 100).toString() + ")";
o.counter++;

if (o.counter == o.countto) {
o.Stop();
o.e.style.opacity = o.opacity.toString();
o.e.style.filter = "alpha(opacity=" + (parseFloat(o.e.style.opacity) * 100).toString() + ")";
}
};
} (this), 50);
}

*/
