var appVersion = navigator.appVersion,
	ie4 = (document.all) ? true : false,
	ie5 = ( navigator.appVersion.indexOf("MSIE 5") != -1 ),
	ns4 = ( document.layers) ? true : false,
	ns6 = ( (navigator.appName.indexOf("Netscape") != -1) && parseInt(navigator.appVersion) >= 5 ),
	mac = ( navigator.userAgent.indexOf("Mac") != -1) ? true : false,
	gek = ( navigator.userAgent.indexOf("Firefox") != -1 ),
	sf01 = ( navigator.userAgent.indexOf("KHTML") != -1),
	sf12 = ( parseInt(navigator.appVersion.replace(/.*AppleWebKit\//i, "")) >= 124 ) ,
	sf3 = ( parseInt(navigator.appVersion.replace(/.*AppleWebKit\//i, "")) >= 500 ) ,
	saf = ( navigator.userAgent.indexOf("Safari") > -1 );

if (ns4) { window.onresize=fixMe; }
if (ns4) { origWidth = innerWidth; origHeight = innerHeight; }
function fixMe() { if (innerWidth != origWidth || innerHeight != origHeight) self.location.reload(); }

function imgComplete( img ) { return ( img.complete ); }
function sfImgComplete( img ) { return ( img.width > 0 ); }
function ieRedrawWindow() { window.resizeBy( 0, 1 ); window.resizeBy( 0, -1 ); }
function redrawWindow() { }

if ( mac && ie5 ) Array.prototype.push = function()
{
	redrawWindow = ieRedrawWindow;

	var thisArray = this, x;
	for ( x=0; x < arguments.length; x++ )
	{
		thisArray[ thisArray.length ] = arguments[x];
	}
	return thisArray.length;
}

if ( sf01 || sf12 ) imgComplete = sfImgComplete;


var urlValueOf = new Array(), queryDataArray = location.search.substr(1).split(/\&|;|\&;/);
for ( var x=0; x < queryDataArray.length; x++ )
{
	var thisQueryVariable = queryDataArray[x].split("=");
	urlValueOf[ thisQueryVariable[0] ] = new Object();
	urlValueOf[ thisQueryVariable[0] ] = thisQueryVariable[1];
}


function nsD(elementName, w){ w = (w) ? w : top; return w.document.layers[elementName]; }
function nsShow(element, w) { d(element, w).display = "block"; }
function nsHide(element, w) { d(element, w).display = "none"; }
function nsClip(element, t, r, b, l) 
{
	var obj = d(element);
	obj.clip.top = t;
	obj.clip.right = r;
	obj.clip.bottom = b;
	obj.clip.left = l;
}

function mozD(elementName, w) { w = (w) ? w : self; return w.document.getElementById(elementName); }
function mozShow(elementID, w)  { w = (w) ? w : self; d(elementID, w).style.display = "block"; }
function mozHide(elementID, w)  { w = (w) ? w : self; d(elementID, w).style.display = "none"; }
function mozClip(elementID, t, r, b, l)  { w = (w) ? w : self; d(elementID, w).style.clip = "rect(" + t + "," + r + "," + b + "," + l + ")"; }
function mozSetOpacity(element, alpha) { element.style.MozOpacity = alpha; }
function w3cSetOpacity(element, alpha) { element.style.opacity = alpha; }
function ieSetOpacity(element, alpha) { element.style.filter = "Alpha(Opacity=" + alpha*100 + ")"; }
function fakeSetOpacity(element, alpha) { if ( alpha > 0 ) Show(element.id); else Hide(element.id); }
function ie_bindEvent( element, event, func ) {	element.attachEvent( 'on' + event, func ); }
function w3_bindEvent( element, event, func ) {	element.addEventListener(event, func, false ); }

var d, Show, Hide, Clip, setOpacity, bindEvent;
if ( ns4 )
{
	d = nsD,
	Show = nsShow;
	Hide = nsHide;
	Clip = nsClip;
}
else
{
	d = mozD;
	Show = mozShow;
	Hide = mozHide;
	Clip = mozClip;
	bindEvent = w3_bindEvent;

	if ( (mac && ie4) || (sf01 && !sf12) ) setOpacity = fakeSetOpacity;
	else if ( !mac && ie4 )
	{
		bindEvent = ie_bindEvent;
		setOpacity = ieSetOpacity;
	}
	else if ( sf12 ) setOpacity = w3cSetOpacity;
	else if ( ns6 ) setOpacity = mozSetOpacity;
}

function Swap( oldID, newID, w )
{
	Hide( oldID, w );
	Show( newID, w );
	return newID;
}

/**************************************************************
	F A D I N G    F U N C T I O N S
**************************************************************/
var rate = 80,
	smoothFade = [ 0, 0.15, 0.3090169943749475, .435, 0.5877852522924731, .69, 0.8090169943749475, .875, 0.9510565162951535, .975, .9999999999999 ],
	fadeSteps = [ 0, 0.3090169943749475, 0.5877852522924731, 0.8090169943749475, 0.9510565162951535, .9999999 ],
	quickFade = [ 0, .33, .66, .9999 ],
	cutsOnly = [ 0, .9999 ],
	fadeObj = new Array();
function FadeObj(element, ramp)
{
	this.style = element.style;
	this.buffer = 0;		//	buffers events during fades to address after the fade
	this.idle = true;		// to ignore events while a fade is in progress
	this.timerCache = null;
	this.ramp = ( ramp ) ? ramp : fadeSteps;
	this.f = this.ramp.length;
	this.HTMLelement = element;
	this.id = element.id;
	// Benchmarking variables
	this.performance = new Object();
	this.performance.enable = false;									// Performance calibration is inactive by default. To enable, set this to true
	this.performance.referenceTime = ( this.ramp.length-1 ) * rate;		// ideal time required to complete fade in milliseconds
	this.performance.startTimeIndex = 0;
	this.performance.stopTimeIndex = 0;
	this.performance.actualDuration = 0;
	this.performance.benchmark = 0;

	//this.performance = performance;
	fadeObj[this.id] = this;
	return this;
}

FadeObj.prototype.fade = function()		//	determines whether to fade up or down
{
	var thisObj = this;
	if ( thisObj.idle )
	{
		thisObj.idle = false;
		if (thisObj.f > 0) thisObj.fadeDown();
		else thisObj.fadeUp();
	}
	else thisObj.buffer++;
}

FadeObj.prototype.fadeUp = function()
{
	var thisObj = this;
	if ( thisObj.f == 0 )
		thisObj.performance.startTimeIndex = new Date().getTime();
	var x = thisObj.f+1;
	if ( x < thisObj.ramp.length )
	{
		thisObj.f++;
		setOpacity(thisObj.HTMLelement, thisObj.ramp[x]);
		thisObj.timerCache = setTimeout(function() {thisObj.fadeUp() }, rate);
	}
	else
	{
		thisObj.f = thisObj.ramp.length;
		thisObj.callibrate();

		thisObj.callBack();
		if ( thisObj.buffer > 0 )
		{
			thisObj.buffer--;
			thisObj.idle = true;
			thisObj.fade();
		}
		else thisObj.idle = true;
	}
}

FadeObj.prototype.fadeDown = function()
{
	var thisObj = this;
	if ( thisObj.f == 0 )
		thisObj.performance.startTimeIndex = new Date().getTime();
	var x = thisObj.f-2;
	if ( x > -1 )
	{
		thisObj.f--;
		setOpacity(thisObj.HTMLelement, thisObj.ramp[x]);
		thisObj.timerCache = setTimeout(function() { thisObj.fadeDown() }, rate);
	}
	else
	{
		thisObj.f = 0;
		thisObj.callibrate();

		thisObj.callBack();
		if ( thisObj.buffer > 0 )
		{
			thisObj.buffer--;
			thisObj.idle = true;
			thisObj.fade();
		}
		else thisObj.idle = true;
	}
}

FadeObj.prototype.setOpacity = function() { setOpacity(this, 0); this.f = 0; };	// depricated

FadeObj.prototype.snapOff = function() { setOpacity(this, 0); this.f = 0; };
FadeObj.prototype.snapOn = function() { setOpacity(this, .9999); this.f = this.ramp.length; };
FadeObj.prototype.callBack = function() {};
FadeObj.prototype.callibrate = function()
{
	var thisObj = this;
	if ( thisObj.performance.enable )
	{
		thisObj.performance.stopTimeIndex = new Date().getTime();
		thisObj.performance.actualDuration = thisObj.performance.stopTimeIndex - thisObj.performance.startTimeIndex;
		thisObj.performance.benchmark = thisObj.performance.actualDuration / thisObj.performance.referenceTime;
		if ( thisObj.performance.benchmark < 1.5 )
		{
			thisObj.ramp = smoothFade;
		}
		if ( thisObj.performance.benchmark > 1.5 && thisObj.performance.benchmark <= 2 )
		{
			thisObj.ramp = fadeSteps;
		}
		else if ( thisObj.performance.benchmark > 2 && thisObj.performance.benchmark <= 6 )
		{
			thisObj.ramp = quickFade;
		}
		else if ( thisObj.performance.benchmark > 6 && thisObj.performance.benchmark != Infinity )
		{
			thisObj.ramp = cutsOnly;
		}
		thisObj.performance.referenceTime = ( thisObj.ramp.length-2 ) * rate;
// window.status = 'actual: ' + thisObj.performance.actualDuration + ', ref: ' + thisObj.performance.referenceTime + ', bench: ' + thisObj.performance.benchmark;
	}
}

/**************************************************************
	S L I D E S H O W    F U N C T I O N S
**************************************************************/
var slideShowObj = new Array();
function SlideshowObject( containerElement, frameElements, delay, initialDelay, fadeRamp )
{
	var thisObj = this;
	thisObj.id = containerElement;
	thisObj.slides = d( containerElement ).getElementsByTagName( frameElements );
	thisObj.delay = delay * 1000;
	thisObj.initialDelay = ( initialDelay ) ? initialDelay * 1000 : 0;
	thisObj.ramp = ( fadeRamp ) ? fadeRamp : fadeSteps;
	thisObj.dynRamp = thisObj.ramp;		//	dynamically selected ramp based on performance
	thisObj.cycleFrames = [];			//	array of frames
	thisObj.currentSlide = 0;
	thisObj.reverse = false;			//	determines play order
	thisObj.slideShowTimer = '';		//	stores setInterval & setTimeout IDs
	thisObj.pauseStartTime = 0;			//	To preserve slideshow timing through pauses, the pause length is used to set the delay when
	thisObj.pauseStopTime = 0;			//	playback is resumed. If pauseLength > slideshow delay, next slide is displayed instantly.

	for ( x=0; x < thisObj.slides.length; x++ )
	{
		thisObj.cycleFrames.push( new FadeObj( thisObj.slides[x], thisObj.ramp ) );
		redrawWindow();						// prevents MacIE from clearing top & left values
		thisObj.cycleFrames[x].performance.enable = true;	// enable performance calibration for slideshow
		thisObj.cycleFrames[x].snapOff();
	}
	thisObj.cycleFrames[0].fadeUp();
	slideShowObj[ containerElement ] = thisObj;
	thisObj.slideShowTimer = setTimeout( function() { thisObj.rotateSlide(); thisObj.play(); }, thisObj.initialDelay );
	return thisObj;
}

SlideshowObject.prototype.pause = function()
{
	var thisObj = this;
	thisObj.pauseStartTime = new Date().getTime();
	clearInterval( thisObj.slideShowTimer );
}

SlideshowObject.prototype.play = function()
{
	var thisObj = this, pauseLength;
	if ( thisObj.pauseStartTime > 0 )
	{
		thisObj.pauseStopTime = new Date().getTime();
		remainingPauseLength = thisObj.delay - ( thisObj.pauseStopTime - thisObj.pauseStartTime );
		thisObj.slideShowTimer = setTimeout( function() { thisObj.rotateSlide(); thisObj.play(); }, remainingPauseLength );
		thisObj.pauseStartTime = 0;
	}
	else
		thisObj.slideShowTimer = setInterval( function() { thisObj.rotateSlide() }, thisObj.delay );
}

SlideshowObject.prototype.prevSlide = function()
{
	var thisObj = this,
		nextFrame,
		prevFrame = thisObj.currentSlide;
	thisObj.currentSlide--;
	nextFrame = thisObj.currentSlide;
	if ( thisObj.currentSlide == -1 )
	{
		thisObj.currentSlide = nextFrame = thisObj.cycleFrames.length-1;
		thisObj.cycleFrames[ nextFrame ].ramp = thisObj.cycleFrames[ prevFrame ].ramp;
		thisObj.cycleFrames[ nextFrame ].callBack = function() { thisObj.dynRamp = thisObj.cycleFrames[ nextFrame ].ramp; }		// set callBack to update slideshowObject's dynRamp value
		thisObj.cycleFrames[ nextFrame ].fade();			// just fade in the last one
	}
	else
	{
		thisObj.cycleFrames[ prevFrame ].ramp = thisObj.dynRamp;
		thisObj.cycleFrames[ nextFrame ].snapOn();
		thisObj.cycleFrames[ prevFrame ].callBack = function() { thisObj.cycleFrames[ prevFrame ].snapOff(); thisObj.dynRamp = thisObj.cycleFrames[ prevFrame ].ramp; }		// set callBack to hide previous slide when fade is complete
		thisObj.cycleFrames[ prevFrame ].fade();
	}
// window.status = 'ramp length: ' + thisObj.cycleFrames[ nextFrame ].ramp.length + ', bench: ' + thisObj.cycleFrames[ prevFrame ].performance.benchmark + ', prev: ' + prevFrame + ', next: ' + nextFrame + ', curr: ' + thisObj.currentSlide + ', id: ' + thisObj.cycleFrames[ prevFrame ].id;
}

SlideshowObject.prototype.nextSlide = function()
{
	var nextFrame, prevFrame;
		thisObj = this;
	prevFrame = thisObj.currentSlide;
	thisObj.currentSlide++;
	nextFrame = thisObj.currentSlide;
	if ( thisObj.currentSlide == thisObj.cycleFrames.length )
	{
		thisObj.currentSlide = nextFrame = 0;
		thisObj.cycleFrames[ prevFrame ].ramp = thisObj.dynRamp;
		thisObj.cycleFrames[ prevFrame ].callBack = function() { thisObj.dynRamp = thisObj.cycleFrames[ prevFrame ].ramp; }		// set callBack to update slideshowObject's dynRamp value
		thisObj.cycleFrames[ prevFrame ].fade();						// just fade down the last slide in the list to reveal the first one
	}
	else if ( thisObj.currentSlide == 1 )
	{
		thisObj.cycleFrames[ nextFrame ].ramp = thisObj.dynRamp;
		thisObj.cycleFrames[ nextFrame ].callBack = function() { thisObj.dynRamp = thisObj.cycleFrames[ nextFrame ].ramp; }		// set callBack to update slideshowObject's dynRamp value
		thisObj.cycleFrames[ nextFrame ].fade();
	}
	else
	{
		thisObj.cycleFrames[ nextFrame ].ramp = thisObj.dynRamp;
		thisObj.cycleFrames[ nextFrame ].callBack = function() { thisObj.cycleFrames[ prevFrame ].snapOff(); thisObj.dynRamp = thisObj.cycleFrames[ nextFrame ].ramp; }		// set callBack to hide previous slide when fade is complete
		thisObj.cycleFrames[ nextFrame ].fade();
	}
// window.status = 'ramp length: ' + thisObj.cycleFrames[ nextFrame ].ramp.length + ', Bench: ' + thisObj.cycleFrames[ prevFrame ].performance.benchmark + ', prev: ' + prevFrame + ', next: ' + nextFrame + ', curr: ' + thisObj.currentSlide + ', id: ' + thisObj.cycleFrames[ prevFrame ].id;
}

SlideshowObject.prototype.rotateSlide = function()
{
	if ( this.reverse )
		this.prevSlide();
	else
		this.nextSlide();
}

/**************************************************************
	S C R O L L B A R    F U N C T I O N S
**************************************************************/
var sThumb, scrollableContent, scrollableWindow, sWell, sWellHeight, sWellDist, scrollPaneIncr, scrollTimer, scrollIncrement = 15, scrollDelay = 50;

/* Initialize elements for scrolling: container, contents, scrollbar, well, and shuttle */
function initScrollbar( scrollableElementID, scrollContainerID, containerWidth, containerHeight )
{
	sThumb = d("scrollThumbShell");
	var upArrow = d("upArrow"),
		downArrow = d("downArrow"),
		scrollWell = d("scrollWell");

	scrollableContent = d( scrollableElementID );
	scrollableContent.style.width = containerWidth - 20 + 'px';

	scrollableWindow = d( scrollContainerID );
	scrollableWindow.style.height = containerHeight + 'px';

	bindEvent( top.window, "keypress", keyboardEvents );
	bindEvent( window, "keypress", keyboardEvents );
	bindEvent( window, "DOMMouseScroll", getMouseWheel );
// 	bindEvent( window, 'resize', fitPage );
	bindEvent( top.window, "mousewheel", getMouseWheel );
	bindEvent( scrollWell, 'mousedown', setMouseOffset );

	bindEvent( document, "mousewheel", getMouseWheel );
	bindEvent( document, 'mousemove', dragPane );
	bindEvent( document, 'mouseup', function() { mouseDown = false; } );

	bindEvent( upArrow, 'click', moveUp );
	bindEvent( upArrow, 'mousedown', function() { scrollUp(); upArrow.blur(); } );
	bindEvent( upArrow, 'mouseup', stopScrolling );
	bindEvent( upArrow, 'mouseout', stopScrolling );

	bindEvent( downArrow, 'click', moveDown );
	bindEvent( downArrow, 'mousedown', function() { scrollDown(); downArrow.blur() } );
	bindEvent( downArrow, 'mouseup', stopScrolling );
	bindEvent( downArrow, 'mouseout', stopScrolling );

	sWellHeight = containerHeight - 32;
	scrollWell.style.height = sWellHeight + 'px';

	sThumbHeight = ( containerHeight >= scrollableContent.clientHeight ) ? sWellHeight : containerHeight / scrollableContent.clientHeight * sWellHeight;
	sWellDist = sWellHeight - sThumbHeight;
	sWinDist = scrollableContent.clientHeight - containerHeight;
	d("scrollThumb").style.height = sThumbHeight + "px";

	if ( sWellDist <= 0 )
		Hide('scrollBar');
	else
		Show('scrollBar');

// 	function fitPage()
// 	{
// 		if ( window.innerHeight < scrollableContent.height )
// 			Show('scrollBar');
// 		else
// 			Hide('scrollBar');
// 	
// 		var pageHeight = window.innerHeight || document.body.clientHeight;
// 	
// 		initScrollbar( scrollableElementID, scrollContainerID, window.innerWidth || document.body.clientWidth, pageHeight-20 );
// 		sThumb.style.top = '0px';
// 		scrollableContent.style.top = '0px';
// 		redrawWindow();
// 	}
}

function getMouseWheel(event)
{
	var delta = 0;
	if (!event) event = window.event;
	if (event.wheelDelta)
	{
		delta = event.wheelDelta/120; 
		if (window.opera) delta = -delta;
	}
	else if (event.detail)
	{
		delta = -event.detail/3;
	}
	scrollPage(delta*15);
}

function scrollPage( distance )
{
	if ( distance < 0 ) moveDown( -distance );
	else moveUp( distance );
}

function keyboardEvents(event)
{
	var upArrowCode, upArrowCode, pageUpCode, pageDownCode, homeCode, endCode;
	if ( sf01 || sf12 )
	{
		upArrowCode = 63232;
		downArrowCode = 63233;
		pageUpCode = 63276;
		pageDownCode =63277;
		homeCode = 63273;
		endCode = 63275;
	}
	else
	{
		upArrowCode = 38;
		downArrowCode = 40;
		pageUpCode = 33;
		pageDownCode = 34;
		homeCode = 36;
		endCode = 35;
	}
	
	switch( event.keyCode )
	{
		case upArrowCode:
			moveUp();
			break;
		case downArrowCode:
			moveDown();
			break;
		case pageUpCode:
			moveUp( sWellHeight>>1 );
			break;
		case pageDownCode:
			moveDown( sWellHeight>>1 );
			break;
		case homeCode:
			moveUp( sWinDist );
			break;
		case endCode:
			moveDown( 99999999 );
			break;
	}
}

function moveUp( page )
{
	if ( sThumb.offsetTop >= 0 )
	{
		var increment = ( page ) ? page : scrollIncrement;
			newPaneTop = ( scrollableContent.offsetTop + increment > 0 ) ? 0 : scrollableContent.offsetTop + increment,
			newThumbTop = 1-newPaneTop/sWinDist * sWellDist;

		sThumb.style.top = newThumbTop + "px";
		scrollableContent.style.top = newPaneTop + "px";
		redrawWindow();
	}
	else stopScrolling();
}

function moveDown( page )
{
	if ( sThumb.offsetTop < sWellDist )
	{
		var increment = ( page ) ? page : scrollIncrement;
			newPaneTop =  ( scrollableContent.offsetTop - increment < sWinDist * -1 ) ? 0 - sWinDist : scrollableContent.offsetTop - increment,
			newThumbTop = 1-newPaneTop/sWinDist * sWellDist;

		sThumb.style.top = newThumbTop + "px";
		scrollableContent.style.top = newPaneTop + "px";
		redrawWindow();
	}
	else stopScrolling();
}

function scrollUp() { scrollTimer = setInterval( "moveUp()", scrollDelay ); }
function scrollDown() { scrollTimer = setInterval( "moveDown()", scrollDelay ); }
function stopScrolling() { clearTimeout( scrollTimer ); }

var mouseDown = false, offsetY;

function setMouseOffset( event )
{
	offsetY = event.clientY - sThumb.offsetTop - 1;
	mouseDown=true;
}

function dragPane( event )
{
	var coordY = event.clientY, newThumbY;
	if ( coordY - offsetY < 0 ) newThumbY = 0;
	else if ( coordY - offsetY > sWellDist ) newThumbY = sWellDist;
	else newThumbY = coordY - offsetY;
	if ( mouseDown == true )
	{
		sThumb.style.top = newThumbY + "px";
		scrollableContent.style.top = Math.round(0 - newThumbY / sWellDist * sWinDist ) + "px";
		redrawWindow();
	}
}

/**************************************************************
	H O V E R I N G    F U N C T I O N S
**************************************************************/
var mouseX, mouseY,
	midPointX, midPointY,					//	Midpoint where hovering changes direction
	nonHoverThrottle = 400,					//	Throttle setting that generates no movement
	initialHoverThrottle = 6,				//	Higher values produce slower motion
	hoverThrottle = initialHoverThrottle,
	hoverTimeout,
	hoverDelay = 50,
	captureMouse = ( saf && !sf3 )
	?
		function(event)
		{
			mouseX = event.clientX - window.scrollX;
			mouseY = event.clientY - window.scrollY;
		}
	:
		function(event)
		{
			mouseX = event.clientX;
			mouseY = event.clientY;
	};

function initHover( axis )
{
	if ( axis == "x" )
		setInterval( hoverRailX, hoverDelay );
	else if ( axis == "y" )
		setInterval( hoverRailY, hoverDelay );
	else
	{
		setInterval( hoverRailX, hoverDelay );
		setInterval( hoverRailY, hoverDelay );
	}
	setHoverMidPoints();
	bindEvent( self, 'resize', setHoverMidPoints );
	bindEvent( document.body, 'mouseover', startHover );
	bindEvent( document.getElementsByTagName("html")[0], 'mousemove', captureMouse );
	bindEvent( parent.document.getElementById('galleryFrame'), 'mouseout', stopHover );
}

function setHoverMidPoints()
{
	if ( parent.document.getElementById('galleryFrame'))
	{
		midPointX = parent.document.getElementById('galleryFrame').clientWidth/2;
		midPointY = parent.document.getElementById('galleryFrame').clientHeight/2;
	}
	else
	{
		midPointX = document.body.clientWidth/2;
		midPointY = document.body.clientHeight/2;
	}
}

function hoverRailX() { window.scrollBy( ( mouseX - midPointX )/ hoverThrottle, 0 ); }
function hoverRailY() { window.scrollBy( 0, ( mouseY - midPointY )/ hoverThrottle ); }

/* startHover & stopHover gradually accellerate & decellerate hovering */
function startHover()
{
	clearTimeout( hoverTimeout );
	hoverThrottle = initialHoverThrottle;
/*	if (  hoverThrottle > initialHoverThrottle )
{
		hoverThrottle = hoverThrottle - 30;
		hoverTimeout = setTimeout( startHover, 100 );
	}
	else hoverThrottle = initialHoverThrottle;
*/
}

function stopHover()
{
	if (  hoverThrottle < nonHoverThrottle )
	{
		hoverThrottle = hoverThrottle + 10;
		hoverTimeout = setTimeout( stopHover, 100 );
	}
}



/**************************************************************
	Parses hidden data contained in hidden span tags
	and returns an array of the data:
		ID's are used as entry IDs
		classNames are used as field names
**************************************************************/
function parseHTMLData( data )
{
	var DataObject = [], thisEntry;
	DataObject.fields = [];
	for ( var x=0; x < data.getElementsByTagName('div').length; x++ )
	{
		thisEntry = data.getElementsByTagName('div')[x];
		thisObject = new Object();
		thisObject.id = thisEntry.id.substr(1);
		DataObject.fields = [ 'id' ];

		for ( var y=0; y < thisEntry.getElementsByTagName('span').length; y++ )
		{
			var	thisVaiable = thisEntry.getElementsByTagName('span')[y],
				thisKey = thisVaiable.className,
				thisValue = thisVaiable.innerHTML;
			eval( "thisObject." + thisKey + " = thisValue " );

			if ( DataObject.fields.toString().indexOf( thisKey ) == -1 ) DataObject.fields.push( thisKey );
		}
		DataObject.push( thisObject );
	}
	return DataObject;
}


function HTMLtemplate( middle, first, last )
{
	this.middle = middle;
	this.first = ( first ) ? first : middle;
	this.last = ( last ) ? last : middle;
}

/* Returns a template populated with data */
function buildContent( template, data, debug )
{
	var out = '', thisChunk, thisField, thisToken;

	// Populate first item using first template
	thisChunk = template.first;
	for ( var y=0; y < data.fields.length; y++ )
	{
		thisField = data.fields[y];
		thisToken = new RegExp('@@' + data.fields[y] + '@@', 'gi');
		thisChunk = thisChunk.replace( thisToken, eval( 'data[0].' + thisField ) );
	}
	thisChunk = thisChunk.replace( /@@index@@/gi, '0' );
	out += thisChunk;

	// Populate 2nd thru last-1 item using middle template
	for ( var x=1; x < data.length-1; x++ )
	{
		thisChunk = template.middle;
		for ( var y=0; y < data.fields.length; y++ )
		{
			thisField = data.fields[y];
			thisToken = new RegExp('@@' + data.fields[y] + '@@', 'gi');
			thisChunk = thisChunk.replace( thisToken, eval( 'data[x].' + thisField ) );
		}
		thisChunk = thisChunk.replace( /@@index@@/gi, x );
		out += thisChunk;
	}

	// Populate last item using last template
	thisChunk = template.last;
	for ( var y=0; y < data.fields.length; y++ )
	{
		thisField = data.fields[y];
		thisToken = new RegExp('@@' + data.fields[y] + '@@', 'gi');
		thisChunk = thisChunk.replace( thisToken, eval( 'data[ data.length-1].' + thisField ) );
	}
	thisChunk = thisChunk.replace( /@@index@@/gi, data.length-1 );
	out += thisChunk;

	if ( debug ) Debug( out, true );
	return out;
}

function newWin( url, name, options )
{
	var newWin = window.open( url, name, options );
}

/* Sets a Cookie with the given name and value.
	name       Name of the cookie
	value      Value of the cookie
	[expires]  Expiration date of the cookie (default: end of current session)
	[path]     Path where the cookie is valid (default: path of calling document)
	[domain]   Domain where the cookie is valid
					(default: domain of calling document)
	[secure]   Boolean value indicating if the cookie transmission requires a
					secure transmission
*/
function setCookie(name, value, path, expires, domain, secure)
{
	document.cookie = 
		name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}

function clearCookie ( name )
{
	var cookie_date = new Date ();			// current date & time
	cookie_date.setTime ( cookie_date.getTime() - 1 );
	document.cookie = name += "=; expires=" + cookie_date.toGMTString();
}

function getCookie(name)
{
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1)
	{
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	}
	else
	{
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
	{
		end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
}

function generateHTML5video( options )
{
	var thisObject = this;
	thisObject.src = options.src;
	thisObject.width = options.w;
	thisObject.title = (options.t) ? options.t : '';
	d('movieTitle').innerHTML = thisObject.title;
	if ( ie5 )
		return '<video src="' + options.src + '" width="' + options.w + '" height="' + options.h + '" controls="controls" autoplay="autoplay">' + generateQTcode( options.src, options.w, options.h, thisObject.title ) + '</video>'
	else if ( gek )
		return generateQTcode( thisObject.src, thisObject.width, options.h, thisObject.title );
}

function generateQTcode( src, w, h, title, splash )
{
	if ( src )
	{
		var mov_src = src,
			mov_w = ( w ) ? w : 320,
			mov_h = ( h ) ? h : 240 + 16;
			document.title += ( title ) ? ' - ' + title : '';
			mov_splash = ( splash ) ? splash : false;
	}
	else
	{
		var mov_src = vidDir + urlValueOf['src'],
			mov_w = ( urlValueOf['w'] ) ? urlValueOf['w'] : 320,
			mov_h = parseInt( ( urlValueOf['h'] ) ? urlValueOf['h'] : 240 ) + 16;
			document.title += ( urlValueOf['title'] ) ? ' - ' + unescape( urlValueOf['title'] ): '';
			mov_splash = ( urlValueOf['splash'] ) ? urlValueOf['splash']: false;
	}
	var out = 
		  '\n	<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="' + mov_w + '" height="' + mov_h + '" codebase="http://www.apple.com/qtactivex/qtplugin.cab">'
		+ '\n		<param name="src" value="' + mov_src + '">'
		+ '\n		<param name="controller" value="true">'
		+ ( ( mov_splash ) ? '\n		<param name="src" value="' + mov_splash + '">' : '' )
		+ '\n		<param name="target" value="myself">'
		+ '\n		<param name="autoplay" value="true">'
		+ '\n		<param name="bgcolor" value="black">'
		+ '\n	<param name="scale" value="tofit">'
		+ '\n	<param name="kioskmode value="true">'
		+ '\n		<param name="pluginspage" value="http://www.apple.com/quicktime/download/">'
		+ '\n		<embed autoplay="true" scale="tofit" kioskmode="true" width="' + mov_w + '" height="' + mov_h + '" ' + ( ( mov_splash ) ? 'src="' + mov_splash + '"' : '' ) + ' controller="true" target="myself" src="' + mov_src + '" bgcolor="black" border="0" pluginspage="http://www.apple.com/quicktime/download/indext.html"></embed>\r	</object>';
	return out;
//Debug( out );
}

function generateFPcode( movieSrc, vID, w, h )
{
	if ( urlValueOf['title'] )
	{
		document.title += " - " + unescape( urlValueOf['title'] );
		d("movieTitle").innerHTML = unescape( urlValueOf['title'] );
	}

	if ( movieSrc )
	{
		vID = vID;
		mov_w = ( w ) ? w : 320;
		mov_h = ( h ) ? h : 240 + 16;
	}
	else
	{
		movieSrc = vidDir + urlValueOf['src'];
		vID = urlValueOf['vID'];
		mov_w = urlValueOf['w'];
		mov_h = urlValueOf['h'] + 16;
	}

	d(vID).style.width = mov_w + 'px';
	d(vID).style.height = mov_h + 'px';

	flowplayer( vID, '/swf/flowplayer-3.1.5.swf',
	{
		playlist: [
			{
				url: movieSrc
			},
			{
				// these two configuration variables does the trick
				accelerated: true,
				scaling: 'fit',
				autoPlay: true,
				autoBuffering: true // <- do not place a comma here  
			}
		]
	});
}


function Debug(str,code)
{
	var str_debug = "";
	if ( code )
	{
		str_debug += '<body marginheight="0" marginwidth="0">\n\n';
		str_debug += str;
	}
	else
	{
		str_debug += '<html><body marginheight="0" style="white-space: pre;font-family: Monaco,Courier;font-size:9px;">\n\n';
		str_debug += str.replace(/</g, "&lt;").replace(/>/g, "&gt;");
	}
	str_debug += "\n\n</body></html>";
	var win_debug = window.open();
	win_debug.document.write( str_debug );
}
