/* vim:ts=4:sts=4:sw=2:noai:noexpandtab
 *
 * JavaScript implementation of an alpha blend filter.
 * Copyright (c) 2005 Steven McCoy <fnjordy@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

function slideshow (containerid,slideid, slides, topPadding, page) {

   if (!topPadding)
   {
      topPadding = 5;
   }
    document.getElementById('homeHolder').style.border = '0px solid black';
	this.slide = document.getElementById(slideid);
	this.slide.container = document.getElementById(containerid);
	this.slides = slides;

/* delay after pre-loading next image to fading in */
	this.wait_delay = 3000;

/* delay between fade updates */
	this.fade_delay = 50;

/* change in opacity per update */
	this.fade_increment = 10;

/* create new layer */
	this.slide2 = document.createElement('IMG');
	this.slide2.style.position = 'absolute';
	this.slide2.zIndex = this.slide.zIndex + 1;
	this.slide2.src = this.slide.src;

/* adjust size and locations */
	var left = this.total_offset(this.slide,'offsetLeft') + "px";	
	var contentTop = this.total_offset(this.slide,'offsetTop');
	var top = contentTop + topPadding + "px";
   if ( (navigator.userAgent.indexOf("Firefox") != -1) || (document.getElementById && !document.all))
   {
      if (page == "AgentHome")
      {
         left = "500px";
      }
   }
	
if (page == "Home")
   {
       var ua = window.navigator.userAgent
       var msie = ua.indexOf ( "MSIE " )
       // If Internet Explorer, get version number
       if ( msie > 0 &&  parseInt (ua.substring (msie+5, ua.indexOf (".", msie ))) > 7)
       { // fix beginning alignment for IE8 and greater
         left = this.total_offset(this.slide,'offsetLeft') - 10 + "px";	
   	     top = contentTop - 200 + topPadding + "px"; // need enough room to clear calender on menu
   	   }
   }
/* MSIE incorrectly calculates left & top by one pixel, so workaround by moving original */

	this.slide.style.position = 'absolute';
	this.slide.style.left = left;
	this.slide.style.top = top;

	this.slide2.style.left = left;
	this.slide2.style.top = top;
	this.slide2.style.width = this.slide.offsetWidth + "px";
	this.slide2.style.height = this.slide.offsetHeight + "px";
    document.getElementById('homeHolder').appendChild(this.slide2);
//var arg = document.createElement('DIV');
//arg.style.textAlign = 'center';

//arg.style.top = '0';
//arg.style.right = '0';
//arg.style.position = 'absolute';
//arg.style.padding = '10px';
this.content = document.createElement('DIV');
this.content.style.align = 'right';
this.content.style.position = 'relative';
//arg.style.fontSize = '24px';
//arg.style.fontFamily = 'Bradley Hand ITC, Lucida Handwriting, Brush Script MT, Monotype Corsiva, Verdana';
//arg.style.color = 'white';
//arg.style.fontWeight = 'bold';
this.content.zIndex = this.slide2.zIndex + 1;
var contentTopFinal = contentTop;
this.content.style.top = contentTopFinal + "px";
this.content.style.width = this.slide.offsetWidth + "px";
this.content.style.height = this.slide.offsetHeight + "px";
//arg.appendChild(document.createTextNode('We Know Hawaii Best...'));
//arg.appendChild(document.createElement('BR'));
//arg.appendChild(document.createTextNode('Because Hawaii Is Our Home!'));
//this.content.appendChild(arg)
document.getElementById('homeHolder').appendChild(this.content);

	this.paused = false;
	this.timer = null;
	this.slide_index = 0;

	this.next_slide(0);
}

slideshow.prototype.total_offset = function (element, property) {
    
    if (document.getElementById && !document.all)
    { //netscape and firefox
        return element[property];
    }
    else
    { // ie and others (may need to filter further on.
        return element[property] + element.offsetParent[property];
    }
}

slideshow.prototype.set_opacity = function (element, opacity) {
/* turn off some funky flickering */
	if ( (navigator.userAgent.indexOf("Firefox") != -1) || (document.getElementById && !document.all))
		if (opacity >= 100) opacity = 99.999;
	element.style.filter = "alpha(opacity:" + opacity + ")";
	element.style.KhtmlOpacity = opacity / 100;
	element.style.MozOpacity = opacity / 100;
	element.style.opacity = opacity / 100;
}

/* img.onmouseover */
slideshow.prototype.onmouseover = function () {
	this.slideshow.paused = true;
//	this.slideshow.navigation.innerHTML = '<a href="javascript:void(0)" onclick="this.parentNode.previous()">previous</a> | <a href="javascript:void(0)" onclick="this.parentNode.next()">next</a> (<i>paused</i>)';
	clearTimeout(this.slideshow.timer);
}

/* img.onmouseout */
slideshow.prototype.onmouseout = function () {
	this.slideshow.paused = false;
//	this.slideshow.navigation.innerHTML = '<a href="javascript:void(0)" onclick="this.parentNode.previous()">previous</a> | <a href="javascript:void(0)" onclick="this.parentNode.next()">next</a>';
	clearTimeout(this.slideshow.timer);
	this.slideshow.next_slide(this.slideshow.slide_index);
}

slideshow.prototype.next = function () {
	this.slideshow.slide_index++;
	if (this.slideshow.slide_index >= this.slideshow.slides.length) this.slideshow.slide_index = 0;
	this.slideshow.slide.src = this.slideshow.slides[this.slideshow.slide_index];
	this.slideshow.slide2.src = this.slideshow.slide.src;
	this.slideshow.set_opacity(this.slideshow.slide2, 0);
}

slideshow.prototype.previous = function () {
	this.slideshow.slide_index--;
	if (this.slideshow.slide_index < 0) this.slideshow.slide_index = this.slideshow.slides.length-1;
	this.slideshow.slide.src = this.slideshow.slides[this.slideshow.slide_index];
	this.slideshow.slide2.src = this.slideshow.slide.src;
	this.slideshow.set_opacity(this.slideshow.slide2, 0);
}

slideshow.prototype.next_slide = function (i) {
// set this slide to background
	this.slide_index = i;
	this.slide.src = this.slides[this.slide_index];
	if (++i >= this.slides.length) i = 0;


// start loading next image
	var slideshow = this;
	this.slide2.onLoad = slideshow.wait_slide(i);
	this.slide2.src = this.slides[i];
}

slideshow.prototype.wait_slide = function (i) {
// set new image 100% transparent above displayed image
	this.set_opacity(this.slide2, 0);
// image has loaded, now wait a bit
	var slideshow = this;
	this.timer = setTimeout(function() { slideshow.fade_slide(i,0); }, this.wait_delay);
}

slideshow.prototype.calculate_opacity = function (opacity) {
	return opacity + this.fade_increment;
}

slideshow.prototype.fade_slide = function (i, opacity) {
	if (this.paused) {
		return;
	}

	opacity = this.calculate_opacity(opacity);
	if (opacity <= 100) {
		var slideshow = this;
		this.timer = setTimeout(function() { slideshow.fade_slide(i,opacity); }, this.fade_delay);
	}
	if (opacity > 100) {
		this.next_slide(i);
	} else {
		this.set_opacity(this.slide2,opacity);
	}
}


slideshow.prototype.pause_resume = function () {
   if (this.slideshow.paused)
   {
     this.slideshow.paused = false;
	  clearTimeout(this.slideshow.timer);
	  this.slideshow.next_slide(this.slideshow.slide_index);
   }
   else
   {
	  this.slideshow.paused = true;
	  clearTimeout(this.slideshow.timer);
	}
}


