/***********************************************************************************************

Copyright (c) 2005 - Alf Magne Kalleland post@dhtmlgoodies.com

Completely rewritten by Roger Stebler

Get this and other scripts at www.dhtmlgoodies.com

You can use this script freely as long as this copyright message is kept intact.

***********************************************************************************************/ 

function SlideShow (name) 
{
	this.name = name;
  	this.imageGalleryLeftPos = false;
	this.imageGalleryWidth = false;
	this.imageGalleryObj = false;
	this.maxGalleryXPos = false;
	this.slideSpeed = 0;
	
	this.startSlide = function (element, e)
	{
		var className = element.className;
		if(className=='arrow_right') {
			this.slideSpeedMultiply = Math.ceil((e.clientX - get_element_pos(element).x) / 4);
			this.slideSpeed = -1*this.slideSpeedMultiply;
			this.slideSpeed = Math.max(-10,this.slideSpeed);
		} else {
			this.slideSpeedMultiply = 10 - Math.ceil((e.clientX - get_element_pos(element).x) / 4);
			this.slideSpeed = 1*this.slideSpeedMultiply;
			this.slideSpeed = Math.min(10,this.slideSpeed);
			if(this.slideSpeed<0)this.slideSpeed=10;
		}
	}
	
	this.releaseSlide = function (element)
	{
		this.slideSpeed=0;
	}
	
	this.gallerySlide = function ()
	{
		if(this.slideSpeed!=0) {
			var leftPos = this.imageGalleryObj.offsetLeft;
			if(this.minGalleryXPos < 0) leftPos = leftPos + this.slideSpeed;
			if(leftPos>this.maxGalleryXPos) {
				leftPos = this.maxGalleryXPos;
				this.slideSpeed = 0;
			}
			if(leftPos<this.minGalleryXPos && this.minGalleryXPos < 0) {
				leftPos = this.minGalleryXPos;
				this.slideSpeed=0;
			}
			
			this.imageGalleryObj.style.left = leftPos + 'px';
		}
		var that = this;
		setTimeout(function(){ that.gallerySlide();},20);
	}
	
	this.initSlideShow = function ()
	{
		var elements = new Array();
		var slideShowElements = document.getElementById(this.name).getElementsByTagName('div');
		for(var i = 0, maxI = slideShowElements.length; i < maxI; i++) {
			elements[slideShowElements.item(i).className] = slideShowElements.item(i);
		}
		
		this.galleryContainer = elements["galleryContainer"];
		this.arrow_left = elements["arrow_left"];
		this.arrow_right = elements["arrow_right"];
		this.Images = elements["theImages"];
		this.slideEnd = elements["slideEnd"];
		
		var that = this;
		this.arrow_left.onmousemove = function(e){ var e=(!e)?window.event:e; that.startSlide(this,e); };
		this.arrow_left.onmouseout = function(){ that.releaseSlide(this); };
		this.arrow_right.onmousemove = function(e){ var e=(!e)?window.event:e; that.startSlide(this,e); };
		this.arrow_right.onmouseout = function(){ that.releaseSlide(this); };
		
		this.imageGalleryObj = this.Images;
		this.imageGalleryLeftPos = this.imageGalleryObj.offsetLeft;
		this.imageGalleryWidth = this.galleryContainer.offsetWidth - 40;
		this.maxGalleryXPos = this.imageGalleryObj.offsetLeft;
		this.minGalleryXPos = this.imageGalleryWidth - this.slideEnd.offsetLeft;
		
		this.gallerySlide();
	}
}

function get_element_pos(obj){
	var yOffset = obj.offsetTop||0;
    var xOffset = obj.offsetLeft||0;
    var elParent = obj.offsetParent;
    while(elParent){
        yOffset += (elParent.offsetTop||0);
        xOffset += (elParent.offsetLeft||0);
        elParent = elParent.offsetParent;
    }
    var readScroll;
    if((window.document.compatMode) && (window.document.compatMode == 'CSS1Compat') ){
        readScroll = window.document.documentElement;
    }else{
        readScroll = window.document.body;
    }
    xOffset -= (readScroll.scrollLeft - readScroll.clientLeft);
    yOffset -= (readScroll.scrollTop - readScroll.clientTop);
    return {x:xOffset,y:yOffset};
}
