//////////////////////////////////////////////////
// 		  slideshow.js			//
// 		  by Matt Rabe			//
//	    www.websolutionshack.com		//
//						//
// Javascript slideshow that integrates with	//
// PHP to create a completely dynamic, slick	//
// little slideshow.				//
//						//
//////////////////////////////////////////////////

//////////////////////////////////////////////////
// DO NOT EDIT BEYOND THIS POINT!
//////////////////////////////////////////////////

// Internal variables
var slideshow_imagefilenames = "";
var slideshow_imagewidths = "";
var slideshow_imageheights = "";
var currentx = 0;
div = document.createElement('div');
img = document.createElement('img');
img.id = 'slideshow_popup';

function startSlideshow(imagefilenames, imagewidths, imageheights)
{
	slideshow_speed = 1;
	slideshow_imagefilenames = (imagefilenames != null) ? imagefilenames.split(',') : slideshow_imagefilenames;
	slideshow_imagewidths = (imagewidths != null) ? imagewidths.split(',') : slideshow_imagewidths;
	slideshow_imageheights = (imageheights != null) ? imageheights.split(',') : slideshow_imageheights;

	if (slideshow_interval == null)
	{
		slideshow_interval = window.setInterval('doSlideshow("slideshow_images")', 75);
	}
}

function doSlideshow(id)
{
	if ($(id))
	{
		currentposition = ($(id).getStyle('backgroundPosition') != null) ? $(id).getStyle('backgroundPosition').split('px') : 0;
		currentx = (parseInt(currentposition[0]) > 0 || parseInt(currentposition[0]) < 0) ? parseInt(currentposition[0]) : 0;
		newposition = parseInt(currentx - slideshow_speed)+'px 0px';

		$(id).setStyle({backgroundPosition: newposition});
	}
}

function stopSlideshow()
{
	clearInterval(slideshow_interval);
	
	slideshow_interval = null;
}

function speedUpSlideshow(direction)
{
	var directionfactor = (direction == null || direction == 'left') ? -1 : 1;
	
	slideshow_speed = slideshow_speed * 5 * directionfactor;
}

function showSlideshowPopup()
{
	// Default mode
	var width = (slideshow_imagewidths[0] == null) ? 300 : slideshow_imagewidths[0];

	document.onmousemove = followslideshowpopup;
	
	refreshSlideshowPopup();
}

function hideSlideshowPopup()
{
	if (div.childNodes.length > 0)
	{
		div.removeChild(div.childNodes[0]);
	}
	$('slideshow_popup_wrapper').setStyle({display: 'none'});

	document.onmousemove = doNothing;
}

function doNothing(e){}

function followslideshowpopup(e)
{
	e = (typeof e != "undefined" ? e : event);
	
	var docwidth = document.viewport.getWidth()
	var docheight = document.viewport.getHeight();

	// Detect which image we are mousing over
	strip_leftx = Math.floor((docwidth - slideshow_width) / 2);
	cursor_leftx = parseInt(Event.pointerX(e) - strip_leftx);
	image_ind = Math.floor((cursor_leftx - currentx) / (slideshow_thumbwidth + slideshow_thumbmargin));
	// If the image index is out of range, "wrap" back into range
	while (image_ind >= slideshow_imagefilenames.length)
	{
		image_ind = image_ind - slideshow_imagefilenames.length;
	}
	while (image_ind < 0)
	{
		image_ind = image_ind + slideshow_imagefilenames.length;
	}

	// Set image width and height
	imgwidth = (slideshow_imagewidths[image_ind] > 500 ? 500 : parseInt(slideshow_imagewidths[image_ind]));
	imgheight = (slideshow_imagewidths[image_ind] > 500 ? parseInt(500 * (slideshow_imageheights[image_ind] / slideshow_imagewidths[image_ind])) : parseInt(slideshow_imageheights[image_ind]));

	// Set popup position in relation to mouse
	if (parseInt(Event.pointerX(e) + parseInt(imgwidth) + 30) >= docwidth)
	{
		// Position image to the left of the cursor
		xcoord = Event.pointerX(e) - imgwidth - 10;
	}else{
		// Position image to the right of the cursor
		xcoord = parseInt(Event.pointerX(e));
	}

	// Position image above cursor
	ycoord = Event.pointerY(e) - imgheight - 30;

	$('slideshow_popup_wrapper').setStyle({left: xcoord+'px'});
	$('slideshow_popup_wrapper').setStyle({top: ycoord+'px'});
	
	updateSlideshowPopup();
	
	// Update the image for the popup:
	img.src = imageObjects[image_ind+1].src; // +1 because we skip the first object since that is the strip image


	// Only display the popup if the users' browser window is wider than the slideshow wrapper
	if (docwidth > slideshow_wrapper_width)
	{
		// Update the popup
		$('slideshow_popup_wrapper').appendChild(div);
		$('slideshow_popup_wrapper').setStyle({display: 'inline'});
	}
}

function updateSlideshowPopup()
{
	if (imageObjects[image_ind+1].complete)
	{
		img.width = imgwidth;
		img.height = imgheight;
		refreshSlideshowPopup();
	}else{
		window.setTimeout('updateSlideshowPopup()', 500);
	}
}

function refreshSlideshowPopup()
{
	if (div.childNodes.length > 0)
	{
		div.removeChild(div.childNodes[0]);
	}
	div.appendChild(img);
}

// This function fixes a bug with IE6 that causes IE6 to continually reload the image from the server each time we refresh it within the mouseover popup - in other words, every time the mouse moves a single pixel, the browser tried to re-download the image
// With this function in place, IE6 just uses the cached version.  Does not have any effect on other browsers (via try,catch)
function fixIE6ImageCacheing()
{
	try
	{
		document.execCommand("BackgroundImageCache", false, true);
	}catch(err)
	{}
}
// Call the IE6 fix automatically
fixIE6ImageCacheing();
