/**
 *	ImageFlow For Joomla 1.0.9 based on ImageFlow 0.9
 *
 *	This code is based on Michael L. Perrys Cover flow in Javascript.
 *	For he wrote that "You can take this code and use it as your own" [1]
 *	this is my attempt to improve some things. Feel free to use it! If
 *	you have any questions on it leave me a message in my shoutbox [2].
 *
 *	The reflection is generated server-sided by a slightly hacked
 *	version of Richard Daveys easyreflections [3] written in PHP.
 *
 *	The mouse wheel support is an implementation of Adomas Paltanavicius
 *	JavaScript mouse wheel code [4].
 *
 *	Thanks to Stephan Droste ImageFlow is now compatible with Safari 1.x.
 *
 *
 *	[1] http://www.adventuresinsoftware.com/blog/?p=104#comment-1981
 *	[2] http://shoutbox.finnrudolph.de/
 *	[3] http://reflection.corephp.co.uk/v2.php
 *	[4] http://adomas.org/javascript-mouse-wheel/
 */
/**
 *
 * @access public
 * @return void
 **/
function imageFlow( opts )
{

var thisObject = this;

if (typeof opts.id == 'undefined')
{
	throw 'Required value \'id\' missing.';
}

this.id = opts.id;

/* Configuration variables */
        									// Sets the height of the reflection in % of the source image
this.conf_reflection_p = ((typeof opts.reflectionheight != 'undefined') ? opts.reflectionheight : 0.5);
											// Sets the numbers of images on each side of the focused one
this.conf_focus = ((typeof opts.imagestack != 'undefined') ? opts.imagestack : 4);
this.conf_slider_width = 14;          		// Sets the px width of the slider div
this.conf_slider_cursor = 'e-resize'; 		// Sets the slider cursor type: try "e-resize" default is 'default'
											// Thumbnail classname

/* Id names used in the HTML */
this.conf_imageflow = this.id;    								// Default is module id
this.conf_loading = this.id + '_imageflow_loading';    			// Default is module id.'_imageflow_loading'
this.conf_images = this.id +'_imageflow_images';      			// Default is module id.'_imageflow_images'
this.conf_captions = this.id + '_imageflow_captions';  			// Default is module id.'_imageflow_captions'
this.conf_navbuttons = this.id + '_imageflow_navbuttons';		// Default is module id.'_imageflow_navbuttons'
this.conf_previous = this.id + '_imageflow_prevbutton';			// Default is module id.'_imageflow_prevbutton'
this.conf_next = this.id + '_imageflow_nextbutton';				// Default is module id.'_imageflow_nextbutton'
this.conf_playpause = this.id + '_imageflow_playpausebutton';	// Default is module id.'_imageflow_playpausebutton'


/* class names used in the HTML */
this.conf_playbutton = 'imageflow_playbutton';
this.conf_pausebutton = 'imageflow_pausebutton';
this.conf_prevbutton = 'imageflow_prevbutton';
this.conf_nextbutton = 'imageflow_nextbutton';
this.conf_spacerbutton = 'imageflow_spacerbutton';
this.conf_scrollbar = 'imageflow_scrollbar';
this.conf_noscrollbar = 'imageflow_noscrollbar';
this.conf_slider = 'imageflow_slider';

/* Define global variables */
this.caption_id = ((typeof opts.glideto != 'undefined') ? opts.glideto : 0);
this.new_caption_id = 0;
this.xstep = 150;
this.current = ((typeof opts.glideto != 'undefined') ? (opts.glideto * -this.xstep) : 0);
this.target = 0;
this.mem_target = 0;
this.timer = 0;
this.array_images = [];
this.new_slider_pos = 0;
this.dragging = false;
this.dragobject = null;
this.dragx = 0;
this.posx = 0;
this.new_posx = 0;
this.img_div = null;
this.imageflow_div = null;
this.navbar = null;
this.scrollbar = null;
this.autostep_ctrl = null;
this.slider = null;
this.caption_div = null;
this.navbuttons_div = null;
this.initTimer = null;
this.hide_slider = ((typeof opts.hideslider != 'undefined') ? opts.hideslider : false);
this.hide_caption = ((typeof opts.hidecaption != 'undefined') ? opts.hidecaption : false);
this.images_width = 0;
this.images_top = 0;
this.images_left = 0;
this.size = 0;
this.scrollbar_width = 0;
this.max_height = 0;
this.max = 0;
this.loaded = false;
this.max_conf_focus = 0;
this.visible_images = [];
this.imgsloaded = false;
this.domready = false;
this.loadcount = 0;
this.loadcountlimit = ((typeof opts.loadcount != 'undefined') ? opts.loadcount : 100);
this.focused = false;
this.linkFocusedOnly = ((typeof opts.linkfocusedonly != 'undefined') ? opts.linkfocusedonly : 0);
this.thumbEffects = ((typeof opts.thumbeffects != 'undefined') ? opts.thumbeffects : false );
this.autoStep = ((typeof opts.autostep != 'undefined') ? opts.autostep : 0);
this.previousnextbuttons = ((typeof opts.previousnextbuttons != 'undefined') ? opts.previousnextbuttons : false);
this.autoStepTimer = null;
this.playbutton = true;
this.playpause_ctrl = null;
this.next_ctrl = null;
this.prev_ctrl = null;
this.isIE = (navigator.userAgent.search(/msie/i) != -1);

this.step = function()
{
	if (thisObject.target < thisObject.current-1 || thisObject.target > thisObject.current+1)
	{
		thisObject.moveTo(thisObject.current + (thisObject.target-thisObject.current)/3);
		window.setTimeout(thisObject.step, 50);
		thisObject.timer = 1;
		thisObject.clearAutoStep();
	}
	else
	{
		thisObject.timer = 0;
		thisObject.startAutoStep();
	}
}

this.startAutoStep = function()
{
	if (thisObject.autoStep > 0 && thisObject.playbutton)
	{
		thisObject.autoStepTimer = window.setTimeout( thisObject.doAutoStep, thisObject.autoStep * 1000 );
	}
}

this.beginAutoStep = function()
{
	thisObject.playbutton = true;
	thisObject.removeClass( thisObject.playpause_ctrl, thisObject.conf_playbutton );
	thisObject.addClass( thisObject.playpause_ctrl, thisObject.conf_pausebutton );
	thisObject.doAutoStep();
}


this.clearAutoStep = function()
{
	if (thisObject.autoStepTimer != null)
	{
		window.clearTimeout( thisObject.autoStepTimer );
		thisObject.autoStepTimer = null;
	}
}

this.stopAutoStep = function()
{
	thisObject.clearAutoStep();
	thisObject.playbutton = false;
	if (thisObject.autoStep > 0 && thisObject.playpause_ctrl != null)
	{
		thisObject.removeClass( thisObject.playpause_ctrl, thisObject.conf_pausebutton );
		thisObject.addClass( thisObject.playpause_ctrl, thisObject.conf_playbutton );
	}
}

this.doAutoStep = function()
{
	thisObject.autoStepTimer = null;
	var i = thisObject.caption_id + 1;
	if (i >= thisObject.max)
	{
		i = 0;
	}
	thisObject.glideTo( i * -thisObject.xstep, i );
}

this.toggleAutoStep = function()
{
	if (thisObject.playbutton)
	{
		thisObject.stopAutoStep();
	}
	else
	{
		thisObject.beginAutoStep();
	}
}

this.nextbutton = function()
{
	thisObject.stopAutoStep();
	thisObject.handle( -1 );
}

this.prevbutton = function()
{
	thisObject.stopAutoStep();
	thisObject.handle( 1 );
}

this.glideTo = function(x, new_caption_id)
{
	/* Animate gliding to new x position */
	this.target = x;
	this.mem_target = x;
	if (this.timer == 0)
	{
		window.setTimeout(thisObject.step, 50);
		this.timer = 1;
	}

	/* Display new caption */
	this.caption_id = new_caption_id;
	var caption = '&nbsp';

	if (! this.hide_caption)
	{
		var image = this.getImage( this.caption_id );
		if (image != null)
		{
			caption = image.getAttribute('alt');
		}
	}
	if (this.caption_div != null)
	{
		this.caption_div.innerHTML = caption;
	}

	/* Set scrollbar slider to new position */
	if (this.dragging == false)
	{
		this.new_slider_pos = (this.scrollbar_width * (-(x*100/((this.max-1)*this.xstep))) / 100) - this.new_posx;
		if (this.slider != null)
		{
			this.slider.style.marginLeft = this.scrollbar.offsetLeft + (this.new_slider_pos - this.conf_slider_width) + 'px';
			this.slider.style.left = this.new_posx + 'px';
		}
	}
}

this.hideImage  = function( image )
{
	if( !image.ishidden )
	{
		image.ishidden = true;
		image.style.visibility = 'hidden';
		image.style.display = 'none';
	}
}

this.showImage = function( image )
{
	if (image.ishidden)
	{
		image.style.display = 'block';
		image.style.visibility = 'visible';
		image.ishidden = false;
		this.visible_images.push( image.i );
	}
}

this.moveTo = function(x)
{
	var image = null;

	for (var index = 0; index < this.visible_images.length; index++)
	{
		var vndx = this.visible_images.shift();
		image = this.getImage( vndx );

		/* Don't display images that are not this.conf_focussed */
		if (image.max_focus_right < this.mem_target || image.max_focus_left > this.mem_target)
		{
			this.hideImage( image );
		}
		else
		{
			this.visible_images.push( vndx );
		}
	}

	this.current = x;
	var zIndex = this.max;
	var low = Math.max( 0, this.caption_id - (this.conf_focus + 2) );
	var high = Math.min( this.max, this.caption_id + (this.conf_focus + 2));
	x += this.xstep * low;


	/* Main loop */
	for (low; low < high; low++)
	{
		image = this.getImage( low );

		/* Don't display images that are not this.conf_focussed */
		if (image.max_focus_right < this.mem_target || image.max_focus_left > this.mem_target)
		{
			this.hideImage( image );
		}
		else
		{
			var z = Math.sqrt(10000 + x * x) + 100;
			var xs = x / z * this.size + this.size;

			/* Process new image height and image width */
			var new_img_h = Math.round((image.h / image.w * image.pc) / z * this.size);
			var new_img_w = 0;

			if ( new_img_h > this.max_height )
			{
				new_img_h = this.max_height;
				new_img_w = image.max_width;
			}
			else
			{
				new_img_w = Math.ceil(image.pc / z * this.size);
			}
/*			var thmb = this.max_height / (this.conf_reflection_p + 1);
			var rflct = this.max_height - thmb;
			var new_img_top = (thmb - new_img_h) + this.images_top + ((new_img_h / (this.conf_reflection_p + 1)+ .5) * this.conf_reflection_p); */
			var new_thumb_h = new_img_h / (this.conf_reflection_p + 1);
			var new_img_top = Math.round(this.images_width * .34 - new_img_h) + this.images_top + (new_thumb_h * this.conf_reflection_p + 1);

			/* Set new image properties */
			image.style.left = xs - (image.pc / 2) / z * this.size + this.images_left + 'px';
			/* Set image layer through zIndex */
			if ( x < 0 )
			{
				zIndex++;
			}
			else
			{
				zIndex--;
			}

			/* Change zIndex and onclick function of the focussed image */
			if ( image.i == this.caption_id )
			{
				zIndex++;
			}
			if(new_img_w && new_img_h)
			{
				image.style.height = new_img_h + 'px';
				image.style.width = new_img_w + 'px';
				image.style.zIndex = zIndex;
				var top = new_img_top + 'px';
				var clip = null;
				if (this.thumbEffects)
				{
					image.style.height = (new_img_h*2) + 'px';
					if ( image.i == this.caption_id )
					{
						clip = 'rect(0px,' + (new_img_w-1) + 'px,' + (new_img_h-1) + 'px,0px)';
					}
					else
					{
						clip = 'rect(' + new_img_h + 'px,' + (new_img_w-1) + 'px,' + ((new_img_h*2)-1) + 'px,0px)';
						top = new_img_top - new_img_h + 'px';
					}
					image.style.clip = clip;
					image.style.overflow = 'hidden';
				}
				image.style.top = top;
			}
			this.showImage( image );
		}
		x += this.xstep;
	}
}

this.findImage = function( image )
{
	if (image.nodeName == 'IMG' || image.childNodes.length == 0)
	{
		return image;
	}

	for (var i = 0; i < image.childNodes.length; i++)
	{
		if (image.childNodes[i].nodeName == 'IMG')
		{
			return image.childNodes[i];
		}
	}
	return image;
}

this.getImage = function( index )
{
	var image = null;

	try
	{
		image = this.img_div.childNodes.item(this.array_images[index]);
	}
	catch(e)
	{
		this.cacheImages( false );
		image = this.img_div.childNodes.item(this.array_images[index]);
	}

	if (image.nodeType != 1 || (image.nodeName != 'A' && image.nodeName != 'IMG'))
	{
		this.cacheImages( false );
		image = this.img_div.childNodes.item(this.array_images[index]);
	}
	if (image.nodeName == 'A' && image.nextExists && (image.nextSibling == null || image.nextSibling.nodeName != image.nextNodeName))
	{
		this.cacheImages( false );
		image = this.img_div.childNodes.item(this.array_images[index]);
	}
	return this.findImage( image );
}

this.handleHsOnclick = function(e)
{
	var event = e || window.event;
	return thisObject.doClick(event, this);
}

this.handleHsOnclick.toString = function()
{
	var str = "function () {return hs.expand(this, {slideshowGroup: '" + thisObject.id + "'});}";
	return str;
}

this.handleOnclick = function(e)
{
	var event = e || window.event;
	return thisObject.doClick(event, this);
}

this.handleOnmouseover = function( e )
{
	var target = null;

	if (e)
	{
		target = e.relatedTarget;
	}
	else
	{
		if (window.event.type == 'mouseover')
		{
    		target = window.event.fromElement;
		}
		else
		{
			target = window.event.toElement;
		}
	}

    if(!thisObject.contains(this, target))
    {
        //	Mouse is over the container element
		var event = e || window.event;
		return thisObject.doClick(event, this);

    }
	return true;
}

this.doClick = function(e, ele)
{
	if (typeof e == "undefined" || e == null || (e.type != 'click' && e.type != 'mouseover'))
	{
		if (typeof ele.saveOnclick == 'function')
		{
			return ele.saveOnclick();
		}
		else
		{
			return ele.saveOnclick;
		}
	}

	var image = thisObject.findImage( ele );
	if (thisObject.caption_id == image.i || thisObject.linkFocusedOnly == 0)
	{
		if (e.type == 'click')
		{
			thisObject.stopAutoStep();
			if (typeof ele.saveOnclick == 'function')
			{
				return ele.saveOnclick();
			}
			else
			{
				return ele.saveOnclick;
			}
		}
	}
	else
	{
		thisObject.glideTo( (image.i * -thisObject.xstep), image.i );
	}
	return false;
}

this.cacheImages = function()
{
	/* Cache EVERYTHING! */
	this.array_images.length = 0;
	this.max = this.img_div.childNodes.length;
	var i = 0;
	var updateAnchors = false;

	for (var index = 0; index < this.max; index++)
	{
		var ele = this.img_div.childNodes.item(index);
		if (ele.nodeType == 1)
		{
			if (ele.nodeName == 'A')
			{
				if (ele.nextSibling != null)
				{
					ele.nextExists = true;
					ele.nextNodeName = ele.nextSibling.nodeName;
				}
				else
				{
					ele.nextExists = false;
				}
				if ((this.linkFocusedOnly != 0 || this.autoStep > 0) && typeof ele.saveOnclick == 'undefined')
				{
					if (typeof ele.onclick == 'undefined' || ele.onclick == null)
					{
						ele.saveOnclick = ele;
					}
					else
					{
						ele.saveOnclick = ele.onclick;
					}
					if (typeof hs != "undefined" && typeof hs.isHsAnchor == "function" && hs.isHsAnchor( ele ))
					{
						ele.onclick = this.handleHsOnclick;
						updateAnchors = true;
					}
					else
					{
						ele.onclick = this.handleOnclick;
					}
					if (this.linkFocusedOnly == 2)
					{
						ele.onmouseover = this.handleOnmouseover;
					}
				}
			}
			var image = this.findImage( ele );
			if (image.nodeName == "IMG")
			{
				var x_pos = (-i * this.xstep);
				this.array_images[i] = index;
				image.max_focus_right = x_pos + this.max_conf_focus;
				image.max_focus_left = x_pos - this.max_conf_focus;
				image.i = i;

				/* Add width and height as attributes ONLY once onload */
				if(typeof image.w === 'undefined')
				{
					image.w = image.width;
					image.h = image.height;
					if (this.thumbEffects)
					{
						image.h = image.height/2;
					}
					image.max_width = Math.round(image.w * this.max_height / image.h);
					this.hideImage( image );
				}

				/* Check source image format. Get image height minus reflection height! */
				if ((image.w + 1) > (image.h / (this.conf_reflection_p + 1)))
				{
					/* Landscape format */
					image.pc = 118;
				}
				else
				{
					/* Portrait and square format */
					image.pc = 100;
				}
				i++;
			}
		}
	}
	if (updateAnchors && typeof hs != "undefined" && typeof hs.updateAnchors == 'function')
	{
		hs.updateAnchors();
	}
	this.max = this.array_images.length;
}

this.isVisible = function(el)
{
	while( el != null )
	{
		if (typeof el.style != 'undefined' && el.style.display == 'none')
		{
			return false;
		}
		el = el.parentNode;
	}
	return true;
}


/* Main function */
this.refresh = function()
{
	/* Cache document objects in global variables */
	this.imageflow_div = document.getElementById(this.conf_imageflow);
	if (thisObject.initTimer != null)
	{
		window.clearTimeout( thisObject.initTimer );
		thisObject.initTimer = null;
	}
	if (! this.isVisible( this.imageflow_div ))
	{
		return false;
	}
	this.img_div = document.getElementById(this.conf_images);
	this.caption_div = document.getElementById(this.conf_captions);

	/* Cache global variables, that only change on refresh */
	if (this.images_width == this.img_div.offsetWidth)
	{
		return true;
	}
	this.images_width = this.img_div.offsetWidth;
	this.images_top = this.img_div.offsetTop;
	this.images_left = this.img_div.offsetLeft;
	this.size = this.images_width * 0.5;
	this.scrollbar_width = this.images_width * 0.6;
	this.conf_slider_width = this.conf_slider_width * 0.5;
	this.max_height = (this.images_width * 0.51) + 0.5;
	this.max_conf_focus = this.conf_focus * this.xstep;

	/* Change imageflow div properties */
	this.imageflow_div.style.height = (this.max_height+.5) + 'px';

	/* Change images div properties */
	this.img_div.style.height = this.images_width * 0.338 + 'px';


	/* Cache EVERYTHING! */
	this.cacheImages();

	if (this.caption_div != null)
	{
		/* Change captions div properties */
		this.caption_div.style.width = this.images_width + 'px';
		this.caption_div.style.marginTop = this.images_width * 0.03 + 'px';
		this.caption_div.style.zIndex = this.max + this.conf_focus + 10;
	}
	this.navbuttons_div = document.getElementById(this.conf_navbuttons);
	if (this.navbuttons_div != null)
	{
		var navwidth = 0;
		this.navbuttons_div.style.zIndex = this.max + this.conf_focus + 10;
		if (this.navbar != null)
		{
			this.navbuttons_div.removeChild( this.navbar );
		}
		this.navbar = document.createElement( 'ul' );
		this.navbar.style.zIndex = this.max + this.conf_focus + 10;
		this.navbuttons_div.appendChild( this.navbar );
		if (this.autoStep > 0)
		{
			this.playpause_ctrl = document.createElement( 'li' );
			this.playpause_ctrl.style.zIndex = this.max + this.conf_focus + 10;
			this.removeClass( this.playpause_ctrl, (this.playbutton ? this.conf_playbutton : this.conf_pausebutton ));
			this.addClass( this.playpause_ctrl, (this.playbutton ? this.conf_pausebutton : this.conf_playbutton ));
			this.playpause_ctrl.onclick = this.toggleAutoStep;
			this.navbar.appendChild( this.playpause_ctrl );
			navwidth += 24;
		}
		if (this.previousnextbuttons)
		{
			this.prev_ctrl = document.createElement( 'li' );
			this.prev_ctrl.style.zIndex = this.max + this.conf_focus + 10;
			this.addClass( this.prev_ctrl, this.conf_prevbutton );
			this.prev_ctrl.onclick = this.prevbutton;
			this.navbar.appendChild( this.prev_ctrl );
			navwidth += 24;
		}
		/* Change scrollbar properties */
		this.scrollbar = document.createElement( 'li' );
		this.scrollbar.style.width = this.scrollbar_width + 'px';
		this.scrollbar.style.zIndex = this.max + this.conf_focus + 10;
		if (! this.hide_slider)
		{
			this.addClass( this.scrollbar, this.conf_scrollbar );
		}
		else
		{
			this.addClass( this.scrollbar, this.conf_noscrollbar );
		}
		this.navbar.appendChild( this.scrollbar );
		navwidth += this.scrollbar_width;
		if (! this.hide_slider)
		{
			/* Set slider attributes */
			this.slider = document.createElement( 'li' );
			this.slider.onmousedown = function () { thisObject.dragstart(this);return false; };
			this.slider.style.cursor = this.conf_slider_cursor;
			this.slider.style.zIndex = this.max + this.conf_focus + 12;
			this.addClass( this.slider, this.conf_slider );
			this.navbar.appendChild( this.slider );
		}
		if (this.previousnextbuttons)
		{
			this.next_ctrl = document.createElement( 'li' );
			this.next_ctrl.style.zIndex = this.max + this.conf_focus + 10;
			this.addClass( this.next_ctrl, this.conf_nextbutton );
			this.next_ctrl.onclick = this.nextbutton;
			this.navbar.appendChild( this.next_ctrl );
			navwidth += 24;
		}
		this.navbuttons_div.style.width = navwidth + 'px';
		this.navbuttons_div.style.zIndex = this.max + this.conf_focus + 10;
	}

	/* Display images in current order */
	this.moveTo(this.current);
	return true;
}

/* Show/hide element functions */
this.show = function(id)
{
	var element = document.getElementById(id);
	element.style.visibility = 'visible';
}

this.hide = function(id)
{
	var element = document.getElementById(id);
	element.style.visibility = 'hidden';
	element.style.display = 'none';
}

/* Hide loading bar, show content and initialize mouse event listening after loading */
this.initOnLoad = function()
{
	if(document.getElementById(thisObject.conf_imageflow))
	{
		if (! thisObject.loaded)
		{
			thisObject.hide(thisObject.conf_loading);
			if (! thisObject.refresh())
			{
				if (thisObject.initTimer == null)
				{
					thisObject.initTimer = window.setTimeout( thisObject.initOnLoad, 500 );
				}
				return;
			}
			thisObject.show(thisObject.conf_images);
			thisObject.initMouseWheel();
			if (!thisObject.hide_slider)
			{
				thisObject.initMouseDrag();
			}
			thisObject.imageflow_div.onmouseover = function (){thisObject.focused = true; return true;};
			thisObject.imageflow_div.onmouseout = thisObject.mouseout;
			thisObject.glideTo(thisObject.current, thisObject.caption_id);
			thisObject.moveTo(5000);
			thisObject.loaded = true;
		}
	}
}

this.mouseout = function( e )
{
    var target = e ? e.relatedTarget : event.toElement;

    if(!thisObject.contains(this, target))
    {
        //	Mouse has left the container element
		thisObject.focused = false;
		thisObject.dragstop();
    }
	return true;
}

this.contains = function(node1, node2)
{
	if(node2 == node1)
	{
		return true;
	}
    if(node2 == null)
	{
		return false;
	}
    else
	{
		return this.contains(node1, node2.parentNode);
	}
}

/* Refresh ImageFlow on window resize */
this.resize = function()
{
	if(document.getElementById(thisObject.conf_imageflow) && thisObject.loaded === true) thisObject.refresh();
}

this.unload = function()
{
	if(! thisObject.isIE)
	{
  		document = null;
	}
}

/* Handle the wheel angle change (delta) of the mouse wheel */
this.handle = function(delta)
{
	var change = false;
	if (delta > 0)
	{
		if (this.caption_id >= 1)
		{
			this.target = this.target + this.xstep;
			this.new_caption_id = this.caption_id - 1;
			change = true;
		}
	}
	else
	{
		if (this.caption_id < (this.max-1))
		{
			this.target = this.target - this.xstep;
			this.new_caption_id = this.caption_id + 1;
			change = true;
		}
	}

	/* Glide to next (mouse wheel down) / previous (mouse wheel up) image */
	if (change == true)
	{
		this.glideTo(this.target, this.new_caption_id);
	}
}

/* Event handler for mouse wheel event */
this.wheel = function(event)
{
	var delta = 0;
	if (!event) event = window.event;
	if (event.wheelDelta)
	{
		delta = event.wheelDelta / 120;
	}
	else
	if (event.detail)
	{
		delta = -event.detail / 3;
	}
	if (delta) thisObject.handle(delta);
	if (event.preventDefault) event.preventDefault();
	event.returnValue = false;
}

this.addEventListener = function (el, event, func) {
	try {
		el.addEventListener(event, func, false);
	} catch (e) {
		try {
			el.detachEvent('on'+ event, func);
			el.attachEvent('on'+ event, func);
		} catch (e) {
			el['on'+ event] = func;
		}
	}
}

/* Initialize mouse wheel event listener */
this.initMouseWheel = function()
{
	if(window.addEventListener) this.imageflow_div.addEventListener('DOMMouseScroll', thisObject.wheel, false);
	this.imageflow_div.onmousewheel = thisObject.wheel;
}

/* This function is called to drag an object (= slider div) */
this.dragstart = function(element)
{
	thisObject.stopAutoStep();
	thisObject.dragobject = element;
	thisObject.dragx = thisObject.posx - thisObject.dragobject.offsetLeft + thisObject.scrollbar.offsetLeft + thisObject.new_slider_pos;
}

/* This function is called to stop dragging an object */
this.dragstop = function()
{
	thisObject.dragobject = null;
	thisObject.dragging = false;
	return true;
}

/* This function is called on mouse movement and moves an object (= slider div) on user action */
this.drag = function(e)
{
	thisObject.posx = document.all ? window.event.clientX : e.pageX;
	if(thisObject.dragobject != null)
	{
		thisObject.dragging = true;
		thisObject.new_posx = (thisObject.posx - thisObject.dragx) + thisObject.conf_slider_width;

		/* Make sure, that the slider is moved in proper relation to previous movements by the glideTo function */
		if(thisObject.new_posx < ( - thisObject.new_slider_pos)) thisObject.new_posx = - thisObject.new_slider_pos;
		if(thisObject.new_posx > (thisObject.scrollbar_width - thisObject.new_slider_pos)) thisObject.new_posx = thisObject.scrollbar_width - thisObject.new_slider_pos;

		var slider_pos = (thisObject.new_posx + thisObject.new_slider_pos);
		var step_width = slider_pos / ((thisObject.scrollbar_width) / (thisObject.max-1));
		var image_number = Math.round(step_width);
		var new_target = (image_number) * -thisObject.xstep;
		var new_caption_id = image_number;

		thisObject.slider.style.left = thisObject.new_posx + 'px';
		thisObject.glideTo(new_target, new_caption_id);
		return false;
	}
}

/* Initialize mouse event listener */
this.initMouseDrag = function()
{
	thisObject.imageflow_div.onmousemove = thisObject.drag;
	thisObject.imageflow_div.onmouseup = thisObject.dragstop;
	/* Avoid text and image selection while dragging  */
	thisObject.slider.onselectstart = function ()
	{
		return (!(thisObject.dragging == true));
	};
}

this.getKeyCode = function(event)
{
	event = event || window.event;
	return event.keyCode;
}

this.keyHandler = function(event)
{
	if (thisObject.focused)
	{
		var charCode  = thisObject.getKeyCode(event);
		switch (charCode)
		{
			/* Right arrow key */
			case 39:
				thisObject.handle(-1);
				break;

			/* Left arrow key */
			case 37:
				thisObject.handle(1);
				break;

			default:
				break;
		}
	}
	return true;
}

this.imgloaded = function( image )
{
	if (this.img_div == null)
	{
		this.img_div = document.getElementById(this.conf_images);
	}
	if (! this.loaded)
	{
		this.loadcount++;
		if (this.loadcount >= this.loadcountlimit)
		{
			this.imgsloaded = true;
		}
		if (this.imgsloaded && this.domready)
		{
			this.initOnLoad();
		}
	}
}

this.hasClass = function(ele,cls)
{
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}

this.addClass = function(ele,cls)
{
	if (!this.hasClass(ele,cls)) ele.className += " "+cls;
}

this.removeClass = function(ele,cls)
{
	if (this.hasClass(ele,cls))
	{
		var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
		ele.className=ele.className.replace(reg,' ');
	}
}

this.setdomready = function()
{
	this.domready = true;
	if (this.imgsloaded)
	{
		this.initOnLoad();
	}
}

this.addEventListener(document, 'keydown', thisObject.keyHandler);
this.addEventListener(window, 'load', thisObject.initOnLoad);
this.addEventListener(window, 'unload', thisObject.unload);
this.addEventListener(window, 'resize', thisObject.resize);

};
