/*
	Slideshow object - implements image slideshow.

	Cookies: the slideshow saves the following cookies:
			slideshow.lastOriginalArtwork
			slideshow.lastShownArtwork
			slideshow.zoom			
		
	HTML requirements:
		Required is existence of: div id 'main', img id 'mainImage'.
		Optinal is existence of: div id 'controlPanel', div id 'slideshowTrigger'.
		
	Page parameter requirements: page must accept these parameters:
			artwork: artwork id, a number. This is used when slideshow must reload
				the page with a different artwork.
			slideshow: value='on' means start slideshow immediately on page load
			
	Callback requirement:
		The page must call slideshow.onResize() whenever window size has changed.
		Example: window.onresize = function onResize(){ mySlideshow.onResize(); };

	Copyright (c) 2007 Taitl Design, LLC. All rights reserved.
*/

// Slideshow constructor
function Slideshow(objectName, pageName, artworks, screenshots, curArtworkIndex, mainImageWidth)
{
	// Constants
	this.TRANSITION_NONE = 1;
	this.TRANSITION_HIDE = 2;
	this.defaultOptions = this.TRANSITION_HIDE;
	this.options = this.defaultOptions;
	
	// Independent members
	this.objectName = objectName; // Name of current object, for eval() and setTimeout()
	this.pageName = pageName; // Name of current page, for redirects
	this.artworks = artworks; // Array of artwork IDs
	this.screenshots = screenshots; // Array of artwork screenshots (image URLs)
	this.curArtworkIndex = curArtworkIndex; // Index of current artwork in artworks array
	this.width = mainImageWidth; // Initial width of displayed image (may be subsequently zoomed)
	this.zoom = 125; // Current zoom
	this.fitVertPercent = 85; // When zoom is 'fit', max percent to occupy vertically
	this.fitHorPercent = 97; // When zoom is 'fit', max percent to occupy horizontally
	this.waitBeforeStart = false; // Wait slideshowInterval ms before starting slideshow
	this.enableLastOriginalArtwork = true; // Auto-jump to last shown artwork when returning to this page
	this.rememberZoom = true; // Save and restore last zoom setting.
	
	// Control panel is usually hidden when slideshow is on, and shown when slideshow is off ('auto' mode).
	// If user has manually switching it on and off, control panel changes to 'manual' mode
	// and does not auto hides/shows itself anymore.
	this.controlPanelToggleMode = this.getCookie('slideshow.controlPanelToggleMode');
	if (!this.controlPanelToggleMode) this.controlPanelToggleMode = 'auto';
	this.controlPanelHidden = 'true' == this.getCookie('slideshow.controlPanelHidden');

	// Controlling slideshow interval:
	this.slideshowDefaultInterval = 5000; // Default delay between images (millseconds)
	this.slideshowInterval = this.getCookie('slideshow.interval'); // Current delay between images
	if (!this.slideshowInterval) this.slideshowInterval = this.slideshowDefaultInterval; 
	this.minInterval = 2800; // Do not go below this delay (milliseconds)
	this.maxInterval = 100000; // Do not go above this delay (milliseconds)
	this.intervalReductionRate = 0.67; // Reduce delay by this rate when speeding up
	this.midInterval = 2000; // At this value, change delay growth from growth1 to growth2
	this.intervalGrowth1 = 1.5; // Multiply by this value during slideshow slowdown
	this.intervalGrowth2 = 2000; // Increase by this value during slideshow slowdown (milliseconds)
	this.timeLimit = 30 * 60; // Maximum time to run slideshow (seconds)
	
	// Derived members
	this.artwork = this.artworks[curArtworkIndex]; // Current artwork id
	this.screenshot = this.screenshots[curArtworkIndex]; // Current screenshot
	this.artworkCount = this.artworks.length; // Number of artworks

	// Other
	this.mainImage = null; // Main image div, will be initiated during slideshow.init()
	this.inSlideshow = false; // Are we currently running slideshow?
	this.slideshowTimeoutID = 0; // Remeber last timeout id
	this.cookieDefaultExpiration = new Date(2036, 1, 1); // Far in the future
	this.today = new Date();
	this.tomorrow = new Date(this.today.getTime() + 3600 * 24 * 1000);
	this.initialZoom = true;
	
	// Get page parameters
	this.parameters = new Array();
	if (location.search)
	{
		var parStr = location.search.replace(/^\?/,'').split(/[&;]/);
		for (var i = 0;i < parStr.length; i++) 
		{
			var part = parStr[i].split(/=/,2);
			this.parameters[part[0]]=part[1];
		}
	}	
	
	this.autoStart = this.getCookie('slideshow.autoStart');
	this.afterSetArtwork = null; // Optional external handler
	this.afterSetZoom = null; // Optional external handler
}

// Slideshow methods

// Initlializes slideshow. Call from body onload handler.
Slideshow.prototype.init = function SlideshowInit()
{
	this.mainImage = this.findItem('mainImage');
//	alert(this.mainImage.src);
	
	// Test if our browser has problem changing image source from JavaScript
	// (happens in Firefox/Mac).
	var source = this.mainImage.src;
	this.mainImage.src += '.jpg';
	this.browserCantChangeImages = (this.mainImage.src != source + '.jpg');
	// Uncomment to test page reload mode
	//this.browserCantChangeImages = true;
	this.mainImage.src = source;

	// Restore most recently used zoom
	var savedZoom = this.getCookie('slideshow.zoom');
	
	if (savedZoom && this.rememberZoom)
	{
		//this.setZoom(savedZoom);
		this.zoom = savedZoom;
	}

	// Determine which artwork to show.
	// Try to show the last shown artwork.
	// If we viewed this page for some time, perhaps using the slideshow,
	// the last shown artwork differs from the originally shown artwork.
	// If we then navigated from this page to some other, perhaps by
	// clicking on the main image (which is linked with artworks page), and then
	// pressed browser's Back button, we expect the last shown artwork
	// to appear. Same if we refresh this page - we want the refreshed page to
	// display the artwork last shown before the refresh, rather than to revert 
	// to the originally displayed artwork.
	var lastOriginalArtwork = this.getCookie('slideshow.lastOriginalArtwork');
	var lastShownArtwork = this.getCookie('slideshow.lastShownArtwork');
	var jumpedToLastShownArtwork = false;

	if (lastOriginalArtwork != this.artwork)
	{	//alert('saving artwork cookie');
		this.setCookie('slideshow.lastOriginalArtwork', this.artwork, this.tomorrow);
	}
	else
	{ 
		if (lastOriginalArtwork != null && lastShownArtwork != null
					&& this.enableLastOriginalArtwork)
		{
			//alert('reviewing same artwork. looking for last artwork ' + lastShownArtwork);
			for (var i = 0; i < this.artworks.length; i++)
			{
				if (this.artworks[i] == lastShownArtwork)
				{
					//alert('lastShownArtwork found');
					this.curArtworkIndex = i;
					this.artwork = this.artworks[i];
					//alert('Jumping to last shown artwork ' + this.artwork);
					jumpedToLastShownArtwork = true;
					break;
				}
			}
		}
	}
	
	this.setArtwork(this.curArtworkIndex);

	if (jumpedToLastShownArtwork)
	{
		// Ignore autostart parameter. This happens when user clicked Back to this page 
		// from another page, or clicked Refresh on current page. We don't need to 
		// autostart a slideshow.
		if (this.autoStart)
			this.autoStart = false;			
	}
	
	if (jumpedToLastShownArtwork)
	{ //alert('resetting toggling info!');
		// Loading a brand new artist or artwork.
		// Reset control panel toggling options.
		this.deleteCookie('slideshow.controlPanelToggleMode');
		this.deleteCookie('slideshow.controlPanelHidden');
		this.controlPanelToggleMode = 'auto';
		this.controlPanelHidden = false;		
	}
	
	// Start slideshow if necessary
	if (this.autoStart)
	{
		this.waitBeforeStart = true;
		this.lastStartTime = this.getCookie('slideshow.lastStartTime');
		this.start();
	}

	// Hide or show control panel according to current toggling mode
	if (this.controlPanelToggleMode == 'auto' && this.autoStart)
	{ 
		this.controlPanelHidden = 'true';
	}
	
	if (this.controlPanelHidden)
		this.hideControlPanel();	
	else
		this.showControlPanel();
}

// Changes picture zoom according to specified percentage (50, 100, 150 etc).
Slideshow.prototype.setZoom = function SlideshowSetZoom(percent, temporarily)
{
	var isTempZoom = (temporarily != null && (temporarily != 0 || temporarily == true));
	
	if (this.rememberZoom && !isTempZoom)
	{
		this.zoom = percent;
	}
	
	//alert('SlideshowSetZoom: ' + percent);
	
	if (percent == 'fit')
	{
		// If we just assign image height, it will lead to this problem:
		// since both width and height will now be explicitly specified,
		// browser will lose ability to adjust height to keep asect ratio
		// whenever whidhs is change. This leads to distorted images
		// when using different zooms.
		//mainImage.height = zoomedHeight;

		// The solution is change width so that the resized image 
		// acquires desired height (provided preserved aspect ratio)
		var zoomedHeight = this.w_y * this.fitVertPercent / 100;
		var zoomedWidth = zoomedHeight * this.mainImage.width / this.mainImage.height;
		
		// If zoomedWidth extends beyond screen, adjust it to fit the screen
		if (zoomedWidth > this.w_x * this.fitHorPercent / 100)
			zoomedWidth = this.w_x * this.fitHorPercent / 100;
		
		this.mainImage.width = zoomedWidth;
	}
	else if (percent == 'fit_horizontally')
	{
		var zoomedWidth = this.w_x * this.fitHorPercent / 100;
		//alert('fitting horizontally, width=' + zoomedWidth);
		this.mainImage.width = zoomedWidth;
	}
	else if (percent == 'actual')
	{
		this.mainImage.width = this.width;
	}
	else
	{
		var zoomedWidth = this.width * percent / 100;	

		// If we just set the width of the new image, it is going to be a problem
		// for the horizontally elongated images: the image height will become too 
		// small, and the image itself will become considerably smaller compared
		// to square or vertically elongated images. To prevent this, we will 
		// try to do the following: take the area of what a square image
		// would occupy (width^2), and calculate image size so that the area
		// occupied by a non-square image would be equal to that value.
		// Thus, all images will be of same area regardless of their proportions.
		//
		// Do not do this if we are in initial zoom
		var enableSquareFactor = !this.initialZoom;
		
		if (enableSquareFactor)
		{
			var desiredArea = zoomedWidth * zoomedWidth;
			var widthToHeightRatio = this.mainImage.width / this.mainImage.height;
			zoomedWidth = Math.sqrt(desiredArea * widthToHeightRatio);
		}
		
		//if (this.initialZoom) alert('initial zoom');
		//alert('zooming to width ' + zoomedWidth);	
		this.mainImage.width = zoomedWidth;
	}

	if (this.rememberZoom && !isTempZoom)
	{
		this.setCookie('slideshow.zoom', this.zoom);
	}

	if (this.initialZoom && !isTempZoom)
	{
		this.initialZoom = false;
	}

	if (this.afterSetZoom)
	{
		this.afterSetZoom(this, this.curArtworkIndex, percent, temporarily);
	}
}

// Changes picture zoom according to specified percentage (50, 100, 150 etc),
// without remembering this setting in cookies.
Slideshow.prototype.setTempZoom = function SlideshowSetTempZoom(zoom)
{
	this.setZoom(zoom, 1);
}

// Changes picture size to its actual (unzoomed) size.
// Leaves current zoom settings intact, to be able to restore 
// previous zoom if needed. If you want to remember the 
// actual setting, call setZoom('actual') instead of thie method.
Slideshow.prototype.showActualSize = function SlideshowShowActualSize()
{
	this.setTempZoom('actual');
}

// Changes picture size to fit.
// Leaves current zoom settings intact, to be able to restore 
// previous zoom if needed. If you want to remember the 
// actual setting, call setZoom('fit') instead of thie method.
Slideshow.prototype.showFitPageSize = function SlideshowShowFitPageSize()
{
	this.setTempZoom('fit');
}

// Changes picture size to fit.
// Leaves current zoom settings intact, to be able to restore 
// previous zoom if needed. If you want to remember the 
// actual setting, call setZoom('fit_horizontally') instead of thie method.
Slideshow.prototype.showFitHorizontallyPageSize = function SlideshowShowFitHorizontallyPageSize()
{
	this.setTempZoom('fit_horizontally');
}

// Sets the artwork to display, specified by index in the artworks array.
Slideshow.prototype.setArtwork = function SlideshowSetArtwork(index)
{	
	this.curArtworkIndex = index;
	this.artwork = this.artworks[index];
	this.screenshot = this.screenshots[index];
	this.setCookie('slideshow.lastShownArtwork', this.artwork, this.tomorrow);

	// Repeat setZoom() in order to adjust new image
	// size to fit the screen in case of 'fit' zoom.
	// This is because different images may have different aspect
	// ratios, therefore, may need to be zoomed differently
	// in order to fit vertically.
	
	// If we'll just assign image.src and then call setZoom(),
	// then the old image will be resized, and then overwritten
	// by the new. This happens because assignment of src
	// is an asynchronous operation (system is loading an image into the browser),
	// while assignment of new dimensions inside setZoom()
	// is a synchronous operation.
	
	// Instead, we'll wait for image to load, and then call setZoom():

	var mainDiv = this.findItem('main');
	var oldSource = this.mainImage.src;
	
	if (this.browserCantChangeImages)
	{
		// Browser has difficulty changing images. 
		// Reload page with new image as argument.
		mainDiv.style.visibility = 'inherit';
		if (oldSource != this.screenshot)
		{	// alert('could not change image - reloading');
			if (this.inSlideshow)
			{
				var now = new Date();
				var nextMinute = new Date();
				nextMinute.setTime(now.getTime() + 60000);
				// Using a cookie rather than a page parameter, to 
				// avoid autostarting slideshow from a bookmarked/reloaded page.
				this.setCookie('slideshow.autoStart', '1', nextMinute);
			}
			location = this.pageName + '?artwork=' + this.artwork;
		}
	}
	else
	{
		if((this.options & this.TRANSITION_HIDE) != 0)
		{ //alert('hiding main layer')
			mainDiv.style.visibility = 'hidden';
		}
		
		this.mainImage = this.findItem('mainImage');
		this.mainImage.slideshow = this;
		this.oldOnloadHandler = this.mainImage.onload;
		this.mainImage.onload = function () { this.slideshow.redrawMainImage(); }
			// Note: in Safari and Firefox/Mac, onload won't be called if 
			// image source file is alreay mentioned in HTML,
			// which makes it being loaded simultaneously with the page.
			// Thus, in HTML, we don't specify main image source.
		this.mainImage.src = this.screenshot;
	}
}

// This is an image.onload callback function called when we have changed to another artwork.
Slideshow.prototype.redrawMainImage = function SlideshowRedrawMainImage()
{ //alert('redrawMainImage');
	
	// Firefox calls onload handler twice.
	// Prevent this handler from being called for second time
	// to avoid disrupting zoom logic for initial zoom.
	this.mainImage.onload = this.oldOnloadHandler;	
	
	if (this.rememberZoom)
	{
		this.setZoom(this.zoom);
	}
	var mainDiv = this.findItem('main');
	mainDiv.style.visibility = 'inherit';
	
	// If in slideshow, continue slideshow.
	// This code is placed here rather than directly into nextSlideshowArtwork()
	// to prevent several nextSlideshowArtwork() calls jammed while images wait
	// to be donwnloaded from remote site. Here, we tie the calls to image's onload 
	// event, therefore next call to nextSlideshowArtwork() is guaranteed to be
	// after previous image has successfully downloaded and displayed for the user.
	if (this.inSlideshow)
	{
		this.slideshowTimeoutID = setTimeout(this.objectName + '.nextSlideshowArtwork()', this.slideshowInterval);
	}	
	
	if (this.afterSetArtwork)
	{
		//alert('calling afterSetArtwork');
		this.afterSetArtwork(this, this.curArtworkIndex);
	}
}

// Go to previous artwork.
Slideshow.prototype.prevArtwork = function SlideshowPrevArtwork()
{
	if (this.curArtworkIndex > 0)
	{
		this.setArtwork(this.curArtworkIndex - 1);
	}
	else
	{
		this.setArtwork(this.artworkCount - 1);
	}
}

// Go to next artwork.
Slideshow.prototype.nextArtwork = function SlideshowNextArtwork()
{
	if (this.curArtworkIndex < this.artworkCount - 1)
	{
		this.setArtwork(this.curArtworkIndex + 1);
	}
	else
	{
		this.setArtwork(0);
	}
}

// Go to next artwork in slideshow sequence. This method is auto called repeatedly
// to keep the show going.
Slideshow.prototype.nextSlideshowArtwork = function SlideshowNextSlideshowArtwork()
{
	if (this.inSlideshow)
	{
		if (this.lastStartTime)
		{
			var now = new Date();
			//alert('seconds since slideshow started: ' + ((now.getTime() - this.lastStartTime) / 1000));
			if (this.timeLimit && (now.getTime() - this.lastStartTime) / 1000 > this.timeLimit)
			{
				//alert('slideshow is going for too long - stopping it');
				this.stop();
				return;
			}
		}
	
		this.curArtworkIndex = (this.curArtworkIndex + 1) % this.artworkCount;
		this.setArtwork(this.curArtworkIndex);
		// This call moved to redrawMainImage(), see comment there.
		//this.slideshowTimeoutID = setTimeout('nextSlideshowArtwork()', this.slideshowInterval);
	}
}

// Start slideshow.
Slideshow.prototype.start = function SlideshowStart()
{
	if (!this.inSlideshow)
	{
		this.inSlideshow = true;
		var slideshowTrigger = this.findItem('slideshowTrigger');
		if (slideshowTrigger)
		{
			slideshowTrigger.style.fontWeight = 'bold';
			slideshowTrigger.blur();
		}

		var slideshowSpeedControls = this.findItem('slideshowSpeedControls');
		if (slideshowSpeedControls)
		{
			slideshowSpeedControls.style.visibility = 'inherit';
			slideshowSpeedControls.blur();
		}

		var slideshowStaticControls = this.findItem('slideshowStaticControls');
		if (slideshowStaticControls)
		{
			slideshowStaticControls.style.visibility = 'hidden';
			slideshowStaticControls.blur();
		}

		if (this.controlPanelToggleMode == 'auto')
		{
			this.hideControlPanel();
		}
		if (this.waitBeforeStart)
			this.slideshowTimeoutID = setTimeout(this.objectName + '.nextSlideshowArtwork()', this.slideshowInterval);
		else
			this.nextSlideshowArtwork();

		if (!this.lastStartTime)
		{
			this.lastStartTime = new Date();
			this.setCookie('slideshow.lastStartTime', this.lastStartTime.getTime(), this.tomorrow);
		}
	}
}

// Stop slideshow.
Slideshow.prototype.stop = function SlideshowStop()
{
	if (this.inSlideshow)
	{
		clearTimeout(this.slideshowTimeoutID);
		this.inSlideshow = false;

		// Reset slideshow interval. This is done since user frequently
		// increases slideshow interval during slideshow, but it irritates
		// her in the start of the next slideshow.
		this.setInterval(this.slideshowDefaultInterval);
		
		// De-bold the slideshow trigger.
		var slideshowTrigger = this.findItem('slideshowTrigger');
		if (slideshowTrigger)
		{
			slideshowTrigger.style.fontWeight = 'normal';
			slideshowTrigger.blur();
			if (this.controlPanelToggleMode == 'auto')
			{
				this.showControlPanel();
			}
		}		

		var slideshowSpeedControls = this.findItem('slideshowSpeedControls');
		if (slideshowSpeedControls)
		{
			slideshowSpeedControls.style.visibility = 'hidden';
			slideshowSpeedControls.blur();
		}

		var slideshowStaticControls = this.findItem('slideshowStaticControls');
		if (slideshowStaticControls)
		{
			slideshowStaticControls.style.visibility = 'inherit';
			slideshowStaticControls.blur();
		}

		this.lastStartTime = null;
		this.deleteCookie('slideshow.lastStartTime');
		this.deleteCookie('slideshow.autoStart');
	}
}

// Restart slideshow, possibly with updated parameters.
Slideshow.prototype.restart = function SlideshowRestart()
{
	if (this.inSlideshow)
	{
		clearTimeout(this.slideshowTimeoutID);
	}
	this.nextSlideshowArtwork();
}

// Increases slideshow speed.
Slideshow.prototype.speedup = function SlideshowSpeedup()
{
	if (!this.inSlideshow)
	{
		this.start();
	}
	else
	{
		// Decrease slideshow interval
		if((this.slideshowInterval * this.intervalReductionRate) > this.minInterval) 
		{
			this.setInterval(this.slideshowInterval * this.intervalReductionRate);
			this.restart();
		}
	}
}

// Dereases slideshow speed.
Slideshow.prototype.slowdown = function SlideshowSlowdown()
{
	if (!this.inSlideshow)
	{
		this.start();
	}
	else if (this.slideshowInterval < this.maxInterval)
	{
		// Increase slideshow interval
		if (this.slideshowInterval < this.midInterval)
			this.setInterval(this.slideshowInterval * this.intervalGrowth1);
		else
			this.setInterval(parseInt(this.slideshowInterval) + this.intervalGrowth2);
	}
}

// Sets slideshow interval (delay between images).
Slideshow.prototype.setInterval = function SlideshowSetInterval(interval)
{
	this.slideshowInterval = interval;
	this.setCookie("slideshow.interval", this.slideshowInterval, this.tomorrow);
}

// This method must be called from window.onresize handler,
// to syncronize slideshow image with the changed size of
// browser window.
Slideshow.prototype.onResize = function SlideshowOnResize()
{
	//if (window.innerWidth) alert('window.innerWidth ' + window.innerWidth);
	//if (document.documentElement) alert('document.documentElement.clientWidth ' + document.documentElement.clientWidth);
	//if (document.body) alert('document.body.clientWidth ' + document.body.clientWidth);
	//this.w_x = document.body.clientWidth;
	this.w_x = window.innerWidth ? window.innerWidth : 
		(document.documentElement && document.documentElement.clientWidth) ? document.documentElement.clientWidth : 
		document.body ? document.body.clientWidth : 0;
	//this.w_y = document.body.clientHeight;
	this.w_y = window.innerHeight ? window.innerHeight : 
		(document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : 
		document.body ? document.body.clientHeight : 0;
	//alert(this.w_x + ', ' + this.w_y);
}

// Hides control panel (the bar with background shades and zoom numbers).
Slideshow.prototype.hideControlPanel = function SlideshowHideControlPanel()
{
	this.findItem('controlPanel').style.visibility = 'hidden';
	this.setCookie('slideshow.controlPanelHidden', 'true', this.tomorrow);
}

// Shows control panel (the bar with background shades and zoom numbers).
Slideshow.prototype.showControlPanel = function SlideshowShowControlPanel()
{
	this.findItem('controlPanel').style.visibility = 'inherit';
	this.deleteCookie('slideshow.controlPanelHidden');
}

// Toggles control panel (the bar with background shades and zoom numbers).
Slideshow.prototype.toggleControlPanel = function SlideshowToggleControlPanel()
{	
	var controlPanel = this.findItem('controlPanel')
	var controlPanelWasHidden = controlPanel.style.visibility == 'hidden';
	if (controlPanelWasHidden)
		this.showControlPanel();
	else
		this.hideControlPanel();
	if (!this.inSlideshow || controlPanelWasHidden)
	{
		this.controlPanelToggleMode = 'manual';
		this.setCookie('slideshow.controlPanelToggleMode', 'manual', this.tomorrow);
	}
}

/* Slideshow helper methods, for private use */

Slideshow.prototype.findItem = function SlideshowFindItem(item) 
{
	if (document.all) return(document.all[item]);
	if (document.getElementById) return(document.getElementById(item));
	return(false);
}

Slideshow.prototype.setCookie = function SlideshowSetCookie(cookieName, cookieValue, expires, path, domain, secure)
{
  if (!expires)
	{
		if (cookieDefaultExpiration)		
			expires = cookieDefaultExpiration; 
		else
			expires = this.cookieDefaultExpiration; 
	}

  document.cookie = 
    escape(cookieName) + '=' + escape(cookieValue) 
    + (expires ? '; EXPIRES=' + expires.toGMTString() : '')
    + (path ? '; PATH=' + path : '')
    + (domain ? '; DOMAIN=' + domain : '')
    + (secure ? '; SECURE' : '');
}

Slideshow.prototype.getCookie = function SlideshowGetCookie(cookieName) 
{
  var cookieValue = null;
  var posName = document.cookie.indexOf(escape(cookieName) + '=');
  if (posName != -1)
  {
	var posValue = posName + (escape(cookieName) + '=').length;
	var endPos = document.cookie.indexOf(';', posValue);
	if (endPos != -1)
	  cookieValue = unescape(document.cookie.substring(posValue, endPos));
	else
	  cookieValue = unescape(document.cookie.substring(posValue));
  }
  return cookieValue;
}

Slideshow.prototype.deleteCookie = function SlideshowDeleteCookie(name,path,domain)
{
	if (this.getCookie(name))
	{
		document.cookie = name + '=' +
		';expires=Thu, 01-Jan-70 00:00:01 GMT' +
		((path) ? ';path=' + path :'') +
		((domain) ? ';domain=' + domain :'')
	}
}

// Static function to forget unwanted cookies
Slideshow.prototype.reset = function SlideshowReset()
{
	Slideshow.prototype.deleteCookie('slideshow.lastOriginalArtwork');
	Slideshow.prototype.deleteCookie('slideshow.lastShownArtwork');
	//alert("slideshow is being reset");
}

