// Used for slideshows
// ----------------------------------------------
loader = new Image();
loader.src = "/i/suite/loading.gif";

flip="Start"; // global to save state

var Effect = new Object();

var Class = {
  create: function() {
    return function() { 
      this.initialize.apply(this, arguments);
    }
  }
}
function $() {
  var elements = new Array();
  
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1) 
      return element;
      
    elements.push(element);
  }
  
  return elements;
}

Function.prototype.bind = function(object) {
  var method = this;
  return function() {
    method.apply(object, arguments);
  }
}

Effect.Appear = Class.create();
Effect.Appear.prototype = {
  initialize: function(element) {
    this.element = $(element);
    this.start  = 0;
    this.finish = 100;
    this.current = this.start;
    this.fade();
  },
  
  fade: function() {
    if (this.isFinished()) return;
    if (this.timer) clearTimeout(this.timer);
    this.setOpacity(this.element, this.current);
    this.current += 10;
    this.timer = setTimeout(this.fade.bind(this), 50);
  },
  
  isFinished: function() {
    return this.current > this.finish;
  },
  
  setOpacity: function(element, opacity) {
    opacity = (opacity == 100) ? 99.999 : opacity;
    element.style.filter = "alpha(opacity:"+opacity+")";
    element.style.opacity = opacity/100 /*//*/;
    element.style.display = '';
  }
}

function toggle() {
	if (flip == "Start") {
		flip="Stop";
   		//insert start code here
		copy ='<img src="images/slideshows/btn_autoplay_on.gif" />';
		document.getElementById('autoplay').innerHTML=copy;
	} else {
	flip="Start";
	//insert stop code here
	copy ='<img src="images/slideshows/btn_autoplay_off.gif" />';
	document.getElementById('autoplay').innerHTML=copy;
	}
}
var delay = 2500; // Time delay between Slides in milliseconds

var lock = false;
var run;
var fromPageLoad = false;

function changeImage(direction) { // Enable 'next' and 'previous' buttons
	if (document.images) {
		imageNumber = imageNumber + direction;
	if (imageNumber > imageLength) {
	if (lock && fromPageLoad)
		autoPlay(false);
		imageNumber = 0;
	}
	
if (imageNumber < 0) {
	imageNumber = imageLength;
}
	document.getElementById("slideshowTarget").src = imagePreloaded[imageNumber].src; // 'slideshowTarget' refers to the NAME of the image that will be swapped out.
	new Effect.Appear('slideshowTarget',
        { duration: .66, 
          fps: 40 });
   }
}
function chooseImage(number) { // Provide random access to any image in slideshow
	imageNumber = number;
	if (imagePreloaded[imageNumber].complete) {
		document.getElementById("slideshowTarget").src = imagePreloaded[imageNumber].src;
		
	new Effect.Appear('slideshowTarget',
        { duration: .66, 
          fps: 40 });
    } else {
        document.getElementById("slideshowTarget").src = loader.src;
        new Effect.Appear('slideshowTarget',
        { duration: .66, 
          fps: 40 });
        imagePreloaded[imageNumber].onload = function() {
        	document.getElementById("slideshowTarget").src = imagePreloaded[imageNumber].src;
        }
    }
}
function autoPlay(type) { // Enable 'autoplay' and 'pause' buttons
	fromPageLoad = type;
	if (lock == true) {
		lock = false;
		window.clearInterval(run);
		toggle();
	}
else if (lock == false) {
	lock = true;
	run = setInterval("changeImage(1)", delay);
	toggle();
   }
}
function launchImage() { // Set things up to launch the large images
	var screenHeight=window.screen.height;
	var screenWidth=window.screen.width;
	var windowWidth = 800;
	var windowHeight = 600;
	var Xcoordinate = (screenWidth-windowWidth)/2;
	var Ycoordinate = (screenHeight-windowHeight)/4;
	poppedWindow = window.open(imagesLarge[imageNumber],'Giant','width='+windowWidth+',height='+windowHeight+',scrollbars=auto,toolbar=no,menubar=no,status=yes,directories=no,location=no,resizable=yes');
	poppedWindow.moveTo(Xcoordinate,Ycoordinate);
	poppedWindow.focus();
}
// Slideshow controls
// ----------------------------------------------

function enableSlideshowControls() {
if (!document.getElementsByTagName) return false;
	var links = document.getElementsByTagName("a");
	for (var i=0; i<links.length; i++) {
	if (links[i].getAttribute("class") == "previous") {
	links[i].onclick = function() {
		changeImage(-1);
		return false;
	}
}
	if (links[i].getAttribute("class") == "autoplay") {
	links[i].onclick = function() {
		autoPlay(false);
		return false;
	}
}
	if (links[i].getAttribute("class") == "next") {
	links[i].onclick = function() {
		changeImage(1);
		return false;
	}
}
	if (links[i].getAttribute("class") == "launch") {
	links[i].onclick = function() {
		launchImage();
		return false;
	}
}
	if (links[i].getAttribute("class") == "slideshowTarget") {
	links[i].onclick = function() {
		launchImage();
		return false;
	}
}
}
}