﻿var delay = 10000  // millisecs;
var now = new Date().getTime();
var querystring = Math.floor(now/(1000*60*15)); // forces a fresh update of the XML file after 15 minutes
// var querystring = now;
// use above line to de-cache in an emergency

function slideshow(name, nickname, autoPlay, delay, nCount) {
    this.name = name;
    this.nickname = nickname;
    this.autoPlay = autoPlay;
    this.delay = delay;
    var N = nCount;
    
    var timer;
    var currentSlide = 0;

    //Toggle the first slide display to be the selected slide
    classAdd(document.getElementById(nickname+'Nav0'),'on');
	
    // slide controls	
    this.show = function(n) {
        if (n!=currentSlide) {	
		    // hide current slide	
		    currentSlideID = '' + nickname + currentSlide;
		    animateOpacity(currentSlideID, 100, 0, 800);			
		    setTimeout("hide('"+currentSlideID+"')",800);

		    // show new slide
		    newSlideID = '' + nickname + n;
		    animateOpacity(newSlideID, 0, 100, 800);
		    setTimeout("show('"+newSlideID+"')",100); // delay to reduce flicker	

		    // update nav highlight states
		    classRemove(document.getElementById(nickname+'Nav'+currentSlide),'on');
		    classAdd(document.getElementById(nickname+'Nav'+n),'on');
			
		    currentSlide = n;
	    }
	    return false;
    }
    
    this.show1 = function(n) {
	    this.show(n);
	    this.pause();
	    return false;
    }	
    this.back = function() {
	    x = currentSlide -1;
	    if (x < 0) x = N-1;
	    this.show(x);
	    return false;
    }
    this.next = function() {
	    x = currentSlide + 1;
	    if (x >= N) x = 0;
	    this.show(x);
	    return false;
    }
    this.play = function() {
	    str = name + '.next()';
	    timer = setInterval(str,delay);
//	    document.getElementById(name+'Play').style.display = 'none';
//	    document.getElementById(name+'Pause').style.display = 'inline';
	    return false;
    }
    this.pause = function() {
	    clearInterval(timer);
//	    document.getElementById(name+'Play').style.display = 'inline';
//	    document.getElementById(name+'Pause').style.display = 'none';
	    return false;
    }
	
    if (autoPlay) {
	    this.play();
    } else {
	    // randomise positions of bottom slideshows
	    if (this.name == 'sitesSlideshow') {
		    this.show(0);
	    } else {
		    this.show(Math.floor(Math.random()*N));
	    }
    }
}

/* ------------------------------------ */

// Create an XMLHttpRequest object (or the ActiveX equivalent for IE)
//function makeHttpObject() {
//    var xmlHttpObj;
//    // branch for Activex version (Microsoft IE)
//    /*@cc_on
//    @if (@_jscript_version >= 5)
//	    try {
//		    xmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
//	    } catch (e) {
//		    try {
//			    xmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
//		    } catch (E) {
//			    xmlHttpObj = false;
//		    }
//	    }
//    @else
//	    xmlHttpObj = false;
//    @end @*/
//    // branch for native XMLHttpRequest object (Mozilla & Safari)
//    if (!xmlHttpObj && typeof XMLHttpRequest != 'undefined') {
//	    try {
//		    xmlHttpObj = new XMLHttpRequest();
//	    } catch (e) {
//		    xmlHttpObj = false;
//	    }
//    }
//    return xmlHttpObj;
//}

/* ------------------------------------ */
// Opacity functions
// (source: http://www.brainerror.net/scripts_js_blendtrans.php)

// Fade from one opacity setting to another
function animateOpacity(id, opacStart, opacEnd, millisec) {
    var skip = 5;
    var speed = Math.round((millisec / 100) * skip);
    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-=skip) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for (i = opacStart; i <= opacEnd; i+=skip) {
            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 = (opacity < 100 ? "alpha(opacity=" + opacity + ")" : "none"); 
}

// Add new string functions
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

function classAdd(element, theclass) {
    // Add a CSS class to the specified element (will not add the class if it already exists)
    if (!element) return;
    if (!element.className) element.className = '';
    var reg = new RegExp('(^| )'+theclass+'( |$)', 'g');
    if (element.className.search(reg) == -1) element.className = (element.className + ' ' + theclass).trim();
}

function classRemove(element, theclass) {
// Remove a CSS class from the specified element
if (!element) return;
if (!element.className) return;
var reg = new RegExp('(^| )'+theclass+'( |$)', 'g');
element.className = element.className.replace(reg, ' ').trim();
}

// Show/Hide functions clean up later...
function show(id) {
    document.getElementById(id).style.display = 'block';
}
function hide(id) {
    document.getElementById(id).style.display = 'none';
}
