// Popup a new browser window.
// If "name" is specified, it is equivalent to target="XYZ" (reusable).
// If "name" is not specified, it is equivalent to target="_blank".
// NOTE: popNew is left in here for backwards compatibility.
function popNew(url) { return popExt(url); }
function popExt(url,name) {
	var popup;
	if ( name ) { popup = window.open(url,name); }
	else { popup = window.open(url); }
	popup.focus();
}

// Popup a window for PayPal track purchases.
function popTrackCart(url) {
	var paramstr = "toolbar=no,directories=no,scrollbars=yes,status=no";
	paramstr += ",menubar=no,resizable=yes";
	var width = 800;
	var height = 550;
	paramstr += ",width=" + width + ",height=" + height;
	popup = window.open(url,"paypal",paramstr);
	popup.focus();
}


// Clear a form field.
function clearFormField(elt) {
	if ( elt.defaultValue == elt.value ) {
		elt.value = "";
	}
}

// Get elements by class name
// THIS SHOULD BE ABANDONED IN FAVOR OF THE Prototype FUNCTION
function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];      
		if (oRegExp.test(oElement.className)) {
			arrReturnElements.push(oElement);
		}   
	}
	return (arrReturnElements);
}


// Read a cookie.
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

// Write a cookie.
function writeCookie(name, value, expires, path, domain, secure) {
	var today = new Date();
	today.setTime( today.getTime() );
	if (expires) { expires = expires * 1000 * 60 * 60 * 24; }
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

// Get a random teaser.
function randomTeaser(moduleId,eidArray,eltId) {
	var rand = Math.floor(Math.random() * (eidArray.length));
	var data = new Array();
	var url = "/" + moduleId + "/ajax/teaser";
	var myAjax = new Ajax.Updater(eltId,url,{
		method: 'get',
		parameters: 'id=' + eidArray[rand],
		evalScripts: true
	});
}


// Smooth Scroll opener.
// This is currently obsolete. It was being called by HTML.pm by a function
// called js_show_hide, and the only thing using it was the Music module.
// It was being used to show the album description on demand, but was dropped
// in favor of the overlay method.
function toggleShowHide(eltId,showText,hideText) {
	var elt = document.getElementById(eltId);
	var eltToggle = document.getElementById(eltId + "_t");
	if ( elt.style.display == 'none' ) {
		Effect.BlindDown(eltId,{duration:0.5});
		eltToggle.innerHTML = hideText;
	}
	else {
		Effect.BlindUp(eltId,{duration:0.5});
		eltToggle.innerHTML = showText;
	}
}


// Open the overlay with specified content.
function oOWC(content,params) { return openOverlayWithContent(content,params); }
function openOverlayWithContent(content,params) {
	if ( !params ) { params = {}; }
	var pageSize = getPageSize();
	var pageScroll = getPageScroll();
	defaultContentWidth = 400;
	defaultContentHeight = 300;
	var contentWidth = (params.width) ? params.width : defaultContentWidth;
	var contentHeight = (params.height) ? params.height : defaultContentHeight;
	// Put a top padding on the window to compensate for the effect of rounded
	// borders on the scrollbar that appears on content overflow.
	var windowPaddingTop = 5;
	// Close Height should match image height.
	var closeHeight = 22;
	var closePaddingTop = 4;
	var closePaddingBottom = 0;
	var closePaddingRight = 8;
	var closePaddingLeft = 8;
	var contentPaddingTop = 8;
	var contentPaddingBottom = 8;
	var contentPaddingRight = 8;
	var contentPaddingLeft = 8;
	var contentBottomBorderWidth = 1; // must match what is in the stylesheet.
	var windowHeight = 
			contentHeight + contentPaddingTop + contentPaddingBottom +
			closeHeight + closePaddingTop + closePaddingBottom +
			contentBottomBorderWidth + windowPaddingTop;
	var windowWidth = contentWidth + contentPaddingRight + contentPaddingLeft;
	var windowTop = pageScroll[1] + (pageSize[3] / 15);
	var windowLeft = (pageSize[2]/2) - (windowWidth/2);
	$('overlayer').style.height = pageSize[1] + 'px';
//	$('overlayer_window').style.top = (windowTop - pageScroll[1]) + 'px';
	$('overlayer_window').style.top = windowTop + 'px';
	$('overlayer_window').style.left = windowLeft + 'px';
	$('overlayer_window').style.width = windowWidth + 'px';
	$('overlayer_window').style.height = windowHeight + 'px';
	$('overlayer_window').style.paddingTop = windowPaddingTop + 'px';
	$('overlayer_window').style.visibility = 'visible';
	$('overlayer_content').style.height = contentHeight + 'px';
	$('overlayer_content').style.paddingTop = contentPaddingTop + 'px';
	$('overlayer_content').style.paddingBottom = contentPaddingBottom + 'px';
	$('overlayer_close').style.height = closeHeight + 'px';
	$('overlayer_close').style.paddingTop = closePaddingTop + 'px';
	$('overlayer_close').style.paddingBottom = closePaddingBottom + 'px';
	$('overlayer_close').style.paddingLeft = closePaddingLeft + 'px';
	$('overlayer_close').style.paddingRight = closePaddingRight + 'px';
	$('overlayer_content').innerHTML = content;
	var useEffects = false;
	if ( useEffects ) { Effect.Appear('overlayer',{duration:0.2}); }
	else { $('overlayer').style.display = 'block'; }
    // Hide Flash elements.
	document.getElementsByClassName('swfobject').each(
		function(node) { node.style.visibility = 'hidden' }
	);
	return null;
}



/*
 * Open the overlay and populate it with content from the specified div.
 * (The first function is for convenience in the code.)
 */
function oOWEC(id,params) { return openOverlayWithElementContent(id,params); }
function openOverlayWithElementContent(id,params) {
	var content = $(id).innerHTML;
	return openOverlayWithContent(content,params);
}



/*
 * Opens the overlayer with a login box.
 * For this to integrate properly with the CMS, we need to define
 * a UsersModule object that is popuplated by the header with variables
 * that tell what features are enabled in the Users Module.
 * (e.g. auto registation, password retrieval)
 */
var UsersModule = {};
function oOWLB(params) { return openOverlayWithLoginBox(params); }
function openOverlayWithLoginBox(params) {
	var redirect = (params.redirect) ? params.redirect : location.href;
	var c = '';
	c += "\
<div id='overlay_login'>\
	<div class='overlay_heading' style='margin-bottom:20px;'>Login</div>\
	<form method='post' action='/users/login' id='overlay_login_form' \
			onsubmit='javascript:validateUserPass();return false;'>\
		<div>\
			<input type='hidden' name='redirect' value=''\
				id='overlay_login_form_redirect' />\
		</div>\
		<table class='form'>\
			<tr>\
				<td class='field_name'>Username:</td>\
				<td>\
					<input type='text' name='username' size='32' \
						id='overlay_login_form_username'\
						style='width:220px;' />\
				</td>\
			</tr>\
			<tr>\
				<td class='field_name'>Password:</td>\
				<td>\
					<input type='password' name='password' size='32'\
						id='overlay_login_form_password'\
						style='width:220px;' />\
				</td>\
			</tr>\
			<tr id='overlay_login_form_status_row'>\
				<td class='buttons' style='text-align:center;'>\
					<div id='overlay_login_form_button'>\
						<input type='submit' value='Log in' />\
					</div>\
				</td>\
				<td style='font-weight:bold;text-align:center;'>\
					<div id='overlay_login_message'></div>\
				</td>\
			</tr>\
		</table>\
		<table style='padding-top:10px;font-size:90%;margin:auto;'>\
			<tr>\
";
	if ( UsersModule == null ) { UsersModule = {} }
	if ( UsersModule.allowPasswordRetrieval ) {
		c += "<td style='padding-right:12px;white-space:nowrap;'>";
		c += "<a href='/users/getpass'>Forgot password?</a>";
		c += "</td>";
	}
	else {
		c += "<td></td>"
	}
	if ( UsersModule.allowAutoRegistration ) {
		c += "<td style='padding-left:12px;white-space:nowrap;'>";
		c += "<a href='/users/register'>Create Account</a>";
		c += "</td>";
	}
	else {
		c += "<td></td>";
	}
	c += "\
			</tr>\
		</table>\
	</form>\
</div>\
";
	params.height = 175;
	params.width = 350;
	openOverlayWithContent(c,params);
	$('overlay_login_form_redirect').value = redirect;
	$('overlay_login_form_username').focus();
}

/*
 * Used by the overlay login form above.
 */
function validateUserPass() {
	var username = $('overlay_login_form_username').value;
	var password = $('overlay_login_form_password').value;
	if ( !username || !password ) {
		$('overlay_login_message').style.color = '#ff0000';
		$('overlay_login_message').innerHTML = 'Invalid Username or Password';
		$('overlay_login_message').style.display = 'block';
		return false;
	}
	var url = "/users/auth";
	var params = 'username=' + username;
	params += '&password=' + password;
	//	params += '&append_cryptpw=1';
	var myAjax = new Ajax.Request(url,{
		method: 'post',
		parameters: params,
		onComplete: function(originalRequest) {
			//			var uidCrypt = originalRequest.responseText;
			//			var uidCryptArray = uidCrypt.split(":");
			//			var uid = uidCryptArray[0];
			//			var cryptpw = uidCryptArray[1];
			var uid = originalRequest.responseText;
			if ( uid.match(/^-?\d+$/) && uid > 0 ) {
				//Effect.BlindUp('overlay_login_form_button',{duration:0.5});
				$('overlay_login_form_button').style.display = 'none';
				$('overlay_login_message').style.color = '#007700';
				$('overlay_login_message').innerHTML = 
					'Success! Logging in...';
				$('overlay_login_message').style.display = 'block';
				$('overlay_login_form').submit();
			}
			else {
				$('overlay_login_message').style.color = '#ff0000';
				$('overlay_login_message').innerHTML = 
					'Invalid Username or Password';
				$('overlay_login_message').style.display = 'block';
			}
		}
	});
	return false;
}

/*
 * Get rid of the overlayer.
 */
function closeOverlayer() {
	var bd = new BrowserDetect();
	// On Safari you must explicitly stop the player or it keeps on playing...
	// (This is stupid to be in the general closeOverlayer function, but...)
	if ( bd.isSafari && document.getElementById('qtplayer') ) {
		try { document.getElementById('qtplayer').Stop(); } catch(err) {}
	}
	// restart the frame player if it is running
	// ok, this was not working... we have to make sure the music was
	// actually playing first...
	//	var playerFrame = window.parent.frame_player;
	//	if ( playerFrame ) {
	//		var ffp = playerFrame.document.getElementById('FlashFramePlayer');
		//		if ( ffp ) { ffp.SetVariable("jsrc","play"); }
	//	}
	// MUST wipe content or else things like videos will continue
	// playing in the background.
	$('overlayer_content').innerHTML = '';
	Effect.Fade('overlayer',{duration:0.2});
    // Resume Flash elements.
	document.getElementsByClassName('swfobject').each(
		function(node) { node.style.visibility = 'visible' }
	);
}




// This has been deprecated and should no longer do anything.
// Delete on March 1, 2007
function overlayerIframe(url,params) { return false; }



function embedAudio(url,params) {
	if ( !url ) { return; }
	if ( !params ) { params = {}; }
	var loop = (params.loop) ? params.loop : 'false';
	var autostart = (params.autostart) ? params.autostart : 'true';
	var width = (params.width) ? params.width : 300;
	var height = (params.height) ? params.height : 42;
	var controller = (params.controller) ? params.controller : 'true';
	var style = 'text-align:center;margin:auto;';
	if (params.style) { style += params.style; }
	var tag = "<embed ";
	tag += "src='" + url + "' ";
	if ( width ) { tag += "width='" + width + "' "; }
	if ( height ) { tag += "height='" + height + "' "; }
	if ( autostart ) { tag += "autostart='" + autostart + "' "; }
	if ( loop ) { tag += "loop='" + loop + "' "; }
	if ( controller ) { tag += "controller='" + controller + "' "; }
	if ( controller == 'false' ) { tag += "hidden='true' "; }
	tag += "></embed>";
	var eltId = randomString();
	document.write("<div id='" + eltId + "' style='" + style + "'></div>");
	var elt = document.getElementById(eltId);
	elt.innerHTML = tag;
}


/*
 * Sets a track on the frame player or popup player, whichever is available.
 */
function playAudio(playlist) {
	var playerFrame = window.parent.frame_player;
	if ( playerFrame ) {
		var ffp = playerFrame.document.getElementById('FlashFramePlayer');
		if ( ffp ) {
			ffp.SetVariable("jsrcPlaylist",playlist);
			// For some reason if you put an alert box here it prevents
			// the player from calling /sitebuilder/flash/undefined
			// (you will see this all over the logs with it commented out)
			//alert(1);
			ffp.SetVariable("jsrcButton","play");
		}
	}
	else {
		var w = (flashPopupPlayerWidth) ? flashPopupPlayerWidth : 275;
		var h = (flashPopupPlayerHeight) ? flashPopupPlayerHeight : 310;
		var p = "toolbar=no,directories=no,scrollbars=no,status=no,menubar=no";
		p += ",resizable=no,width="+w+",height="+h;
		if ( !playlist.match('%') ) { playlist = escape(playlist); }
		var url = "/sitebuilder/flash/player?f=" + playlist;
		var popup = window.open(url,"flashplayer",p);
		popup.focus();
	}
}


/*
 * Overlay video player.
 */
function playVideo(type,url,width,height,title,description) {

	// stop the frame player if it is running
	var playerFrame = window.parent.frame_player;
	if ( playerFrame ) {
		var ffp = playerFrame.document.getElementById('FlashFramePlayer');
		if ( ffp ) { ffp.SetVariable("jsrcButton","pause"); }
	}

	// Set the default movie width and height, and allow override via params.
	var movieWidth = 320;
	var movieHeight = 240;
	if ( width ) { movieWidth = width; }
	if ( height ) { movieHeight = height; }

	// The total height must take into account the controls,
	var totalWidth = movieWidth;
	var totalHeight = movieHeight;
	if ( type == 'flv' ) { totalHeight += 20; }
	else if ( type == 'qt' ) { totalHeight += 16; }
	else if ( type == 'wm' ) { totalHeight += 44; }
	else if ( type == 'rp' ) { totalHeight += 48; }
	else if ( type == 'fl' ) { totalHeight += 0; }

	// Content width and height add a little extra to prevent scrollbars.
	var windowWidth = totalWidth + 4;
	var windowHeight = totalHeight + 4;

	var c = '';
	if ( type == 'flv') {
		c += "<div id='flash_video'></div>";
	}
	else if ( type == 'fl' ) {
		c += "<div id='flash_video'></div>";
	}
	else if ( type == 'wm' ) {
		// Safari must use embed because it does not recognize <param>
		var bd = new BrowserDetect();
		if ( bd.isGecko || bd.isSafari ) {
			c += "<embed src='" + url + "'";
			c += " width='" + totalWidth + "'";
			c += " height='" + totalHeight + "'";
			c += " type='video/x-ms-wmv'";
			c += " autostart='1'";
			c += " showcontrols='1'";
			c += " showstatusbar='0'";
			c += " autorewind='1'";
			c += " showdisplay='0'";
			c += " />";
		}
		else {
			c += "<object type='application/x-oleobject' ";
			c += "classid='CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6' ";
			c += "width='" + totalWidth + "' ";
			c += "height='" + totalHeight + "' ";
			c += ">";
			c += "<param name='url' value='" + url + "' />";
			c += "<param name='autoplay' value='true' />";
			c += "<param name='autostart' value='true' />";
			c += "<param name='showcontrols' value='true' />";
			c += "<param name='autorewind' value='true' />";
			c += "<param name='showstatusbar' value='false' />";
			c += "<param name='showdisplay' value='false' />";
			c += "</object>";
		}
	}
	else if ( type == 'rp' ) {
		// Safari must use embed because it does not recognize <param>
		var bd = new BrowserDetect();
		if ( bd.isGecko || bd.isSafari ) {
			c += "<embed src='" + url + "' ";
			c += "type='audio/x-pn-realaudio-plugin' ";
			c += "console='Clip1' ";
			c += "controls='ImageWindow' ";
			c += "autostart='1' ";
			c += "height='" + totalHeight + "' ";
			c += "width='" + totalWidth + "' />";
		}
		else {
			c += "<object ";
			c += "classid='clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA' ";
			c += "height='" + totalHeight + "' ";
			c += "width='" + totalWidth + "'>";
			c += "<param name='controls' value='ImageWindow'>";
			c += "<param name='console' value='Clip1'>";
			c += "<param name='autoplay' value='true' />";
			c += "<param name='autostart' value='true' />";
			c += "<param name='src' value='" + url + "'>";
			c += "</object>";
		}
	}
	else if ( type == 'qt' ) {
		// Safari must use embed because it does not recognize <param>
		var bd = new BrowserDetect();
		if ( bd.isGecko || bd.isSafari ) {
			c += "<embed id='qtplayer' ";
			c += " src='" + url + "'";
			c += " width='" + totalWidth + "'";
			c += " height='" + totalHeight + "'";
			c += " pluginspage='http://www.apple.com/quicktime/download/'";
			c += " type='video/quicktime'";
			c += " controller='true'";
			c += " autoplay='true'";
			c += " loop='false'";
			c += " enablejavascript='true'";
			c += " />";
		}
		else {
			c += "<object id='qtplayer' ";
			c += "classid='clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B' ";
			c += "codebase='http://www.apple.com/qtactivex/qtplugin.cab' ";
			c += "width='" + totalWidth + "' ";
			c += "height='" + totalHeight + "'";
			c += ">";
			c += "<param name='src' value='" + url + "' />";
			c += "<param name='autoplay' value='true' />";
			c += "<param name='autostart' value='true' />";
			c += "<param name='enablejavascript' value='true' />";
			c += "<param name='controller' value='true' />";
			c += "</object>";
		}
	}
	openOverlayWithContent(c,{width:windowWidth,height:windowHeight});
	if ( type == 'flv' ) {
		var player = '/sitebuilder/flash/flowplayer/1.19/FlowPlayer.swf';
		var so = new SWFObject(player,'',totalWidth,totalHeight,'7','');
		so.addParam("flashvars","config={videoFile:'"+url+"',useNativeFullScreen:true}");
		so.addParam("allowFullScreen","true");
		so.write('flash_video');
	}
	else if ( type == 'fl' ) {
		var so = new SWFObject(url,'',totalWidth,totalHeight,'7','');
		so.write('flash_video');
	}
	return null;
}



/*
 * Thumbnail scroller with fade-in/fade-out.
 *
 * Sample Usage:
 *
 *   var ts = new ThumbScroller('ts');
 *   ts.setWidth(200);
 *   ts.setHeight(200);
 *   ts.setPause(5);
 *   ts.addImage(url);
 *   ts.addImage(url);
 *   ts.addImage(url);
 *   ts.start();
 *
 */

function ThumbScroller(objname) {

	this.objectName = objname;

	this.width = "150";
	this.height = "150";
	this.pause = 5000;
	this.images = new Array();
	this.imagesF = new Array();
	this.imageTitles = new Array();
	this.imageDescriptions = new Array();
	this.randId1 = randomString();
	this.randId2 = randomString();
	this.inElt;
	this.outElt;
	this.inOpacity = 0;
	this.outOpacity = 100;
	this.inEltId = this.randId1;
	this.outEltId = this.randId2;
	this.index = 1;
	this.fader;
	this.entryId;
	this.evenIter = true;
	this.enableOverlay = 1;

	this.setHeight = function(height) {
		this.height = height;
	}

	this.setWidth = function(width) {
		this.width = width;
	}

	this.setPause = function(pause) {
		this.pause = pause * 1000;
	}

	this.addImage = function(thumb,full,title,description) {
		if ( !thumb || !full ) { return undef; }
		if ( !title ) { title = ''; }
		if ( !description ) { description = ''; }
		this.images.push(thumb);
		this.imagesF.push(full);
		this.imageTitles.push(title);
		this.imageDescriptions.push(description);
	}

	this.preload = function() {
		var preloadedImages = new Array()
		for ( p = 0; p < this.images.length; p++ ) {
			preloadedImages[p] = new Image();
			preloadedImages[p].src = this.images[p];
		}
	}

	this.fade = function() {
		if ( this.inOpacity < 100 ) {
			this.inOpacity += 10;
			this.outOpacity -= 10;
			if ( this.inElt.filters ) {
				this.inElt.filters.alpha.opacity = this.inOpacity;
				this.outElt.filters.alpha.opacity = this.outOpacity;
			}
			else if ( this.inElt.style.MozOpacity ) {
				this.inElt.style.MozOpacity = this.inOpacity/101;
				this.outElt.style.MozOpacity = this.outOpacity/101;
			}
			else if (this.inElt.style.KhtmlOpacity) {
				this.inElt.style.KhtmlOpacity = this.inOpacity/100;
				this.outElt.style.KhtmlOpacity = this.outOpacity/100;
			}

		}
		else {
			clearInterval(this.fader);

			this.inOpacity = 0;
			this.outOpacity = 100;

			this.inEltId = ( this.inEltId == this.randId1 ) ?
				this.randId2 : this.randId1;
			this.outEltId = ( this.outEltId == this.randId1 ) ? 
				this.randId2 : this.randId1;

			this.inElt = document.getElementById(this.inEltId);
			this.outElt = document.getElementById(this.outEltId);


			var nextIndex = this.index + 1;
			if ( nextIndex > (this.images.length-1) ) {
				nextIndex = 0;
			}
			if ( this.evenIter ) {
				this.outEltImg.src = this.images[nextIndex];
			}
			else {
				this.inEltImg.src = this.images[nextIndex];
			}

			if ( !this.inElt.filters && !this.inElt.style.MozOpacity && 
					!this.inElt.style.KhtmlOpacity ) {
				this.inElt.style.visibility = "hidden";
				this.outElt.style.visibility = "visible";
			}

			this.evenIter = ( this.evenIter == true ) ? false : true;
			this.index = nextIndex;
			setTimeout(this.objectName+".runFader()",this.pause)
		}
	}


	this.runFader = function() {
		var t = 'setInterval("' + this.objectName + '.fade()",50)';
		this.fader = eval(t);
	}


	this.writeHTML = function() {
		var s = "position:relative;";
		// Must add 4px or else stuff gets a little "off".
		this.width = parseInt(this.width) + 4;
		this.height = parseInt(this.height) + 4;
		s += "width:" + this.width + "px;";
		s += "height:" + this.height + "px;";
		s += "overflow:hidden;";
		var s2 = "position:absolute;"
		s2 += "width:" + this.width + ";";
		s2 += "height:" + this.height + ";";
		s2 += "top:0;left:0;";
		s2 += "filter:alpha(opacity=0);";
		s2 += "-moz-opacity:0;";
		s2 += "-khtml-opacity:0;";
		s2 += "visibility:hidden;";
		var aParams = " href='" + this.imagesF[0] + "' ";
		aParams += " onclick='lightbox.start(this);return false;' ";
		aParams += " rel='lightbox[" + this.objectName + "]' ";
		aParams += " title='" + this.imageTitles[0];
		aParams += "\t" + this.imageDescriptions[0] + "' ";
		var imgHTML = "<div class='thumbnail' style='" + s + "'>\n";
		imgHTML += "<div id='" + this.randId1 + "' style='" + s2 + "'>";
		imgHTML += "<a" + aParams + ">";
		imgHTML += "<img id='img_" + this.randId1 + "' src='' /></a></div>\n";
		imgHTML += "<div id='" + this.randId2 + "' style='" + s2 + "'>";
		imgHTML += "<a " + aParams + ">";
		imgHTML += "<img id='img_" + this.randId2 + "' src='' /></a></div>\n";
		if ( this.images.length > 1 ) {
			imgHTML += "<div style='display:none;'>";
			for ( var i = 1; i < this.images.length; i++ ) {
				aParams = " href='" + this.imagesF[i] + "' ";
				aParams += " onclick='lightbox.start(this);return false;' ";
				aParams += " rel='lightbox[" + this.objectName + "]' ";
				aParams += " title='" + this.imageTitles[i];
				aParams += "\t" + this.imageDescriptions[i] + "'";
				imgHTML += "<a " + aParams + ">";
				imgHTML += "<img src='" + this.images[i] + "' alt='' />";
				imgHTML += "</a>";
			}
			imgHTML += "</div>\n";
		}
		imgHTML += "</div>\n"; // closes div.thumbnail
		document.write(imgHTML);
	}

	this.start = function() {
		this.writeHTML();
		this.inElt = document.getElementById(this.inEltId);
		this.outElt = document.getElementById(this.outEltId);
		this.inEltImg = document.getElementById('img_' + this.inEltId);
		this.outEltImg = document.getElementById('img_' + this.outEltId);
		this.outEltImg.src = this.images[0];
		this.outElt.style.visibility = 'visible';
		if ( this.images.length > 1 ) {
			this.inEltImg.src = this.images[1];
			if (this.inElt.filters) {
				this.outElt.filters.alpha.opacity = 100;
				this.inElt.style.visibility = 'visible';
			}
			else if (this.inElt.style.MozOpacity) {
				this.outElt.style.MozOpacity = .99;
				this.inElt.style.visibility = 'visible';
			}
			else if (this.inElt.style.KhtmlOpacity) {
				this.outElt.style.KhtmlOpacity = 1;
				this.inElt.style.visibility = 'visible';
			}
			setTimeout(this.objectName+".runFader()",this.pause)
		}
		else {
			if (this.inElt.filters) {
				this.outElt.filters.alpha.opacity = 100;
			}
			else if (this.inElt.style.MozOpacity) {
				this.outElt.style.MozOpacity = 100/101;
			}
			else if (this.inElt.style.KhtmlOpacity) {
				this.outElt.style.KhtmlOpacity = 1;
			}
		}
	}

}


/*
 * Get a random string.
 */
function randomString(length) {
	if ( !length ) { length = 8; }
	var chars = "abcdefghiklmnopqrstuvwxyz";
	var r = '';
	for ( var i=0; i < length; i++ ) {
		var rnum = Math.floor(Math.random() * chars.length);
		r += chars.substring(rnum,rnum+1);
	}
	return r;
}




// Returns array with x,y page scroll values.
// Used in 'submitPleaseWait' above.
function getPageScroll() {
	var yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	}
	// IE6 Strict
	else if (document.documentElement && document.documentElement.scrollTop) {
		yScroll = document.documentElement.scrollTop;
	}
	else if (document.body) { // all other IE
		yScroll = document.body.scrollTop;
	}
	return new Array('',yScroll) 
}

// Returns array with page width, height and window width, height
// Used in 'submitPleaseWait' above.
function getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {  
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight) {
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) { // all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}       
	// for small pages with total height less then height of the viewport
	if (yScroll < windowHeight) { pageHeight = windowHeight; }
	else { pageHeight = yScroll; }
	// for small pages with total width less then width of the viewport
	if (xScroll < windowWidth) { pageWidth = windowWidth; }
	else { pageWidth = xScroll; }
	return new Array(pageWidth,pageHeight,windowWidth,windowHeight);
}




/*
 * Browser Detect  v2.1.6
 * Documentation: http://www.dithered.com/javascript/browser_detect/index.html
 * License: http://creativecommons.org/licenses/by/1.0/
 * Code by Chris Nott (chris[at]dithered[dot]com)
 *
 * Example:
 *
 *	<script type="text/javascript" src="BrowserDetect.js"></script>
 *	<script type="text/javascript">
 *		//<[!CDATA[
 *		var bd = new BrowserDetect();
 *		if (bd.isIE5xMac) { barf; }
 *		//]]>
 *	</script>
 *
 */
function BrowserDetect() {
	var ua = navigator.userAgent.toLowerCase(); 
	// browser engine name
	this.isGecko       = (ua.indexOf('gecko') != -1 && ua.indexOf('safari') == -1);
	this.isAppleWebKit = (ua.indexOf('applewebkit') != -1);

	// browser name
	this.isKonqueror   = (ua.indexOf('konqueror') != -1); 
	this.isSafari      = (ua.indexOf('safari') != - 1);
	this.isOmniweb     = (ua.indexOf('omniweb') != - 1);
	this.isOpera       = (ua.indexOf('opera') != -1); 
	this.isIcab	= (ua.indexOf('icab') != -1); 
	this.isAol	 = (ua.indexOf('aol') != -1); 
	this.isIE	  = (ua.indexOf('msie') != -1 && !this.isOpera && (ua.indexOf('webtv') == -1) ); 
	this.isMozilla     = (this.isGecko && ua.indexOf('gecko/') + 14 == ua.length);
	this.isFirebird    = (ua.indexOf('firebird/') != -1);
	this.isNS	  = ( (this.isGecko) ? (ua.indexOf('netscape') != -1) : ( (ua.indexOf('mozilla') != -1) && !this.isOpera && !this.isSafari && (ua.indexOf('spoofer') == -1) && (ua.indexOf('compatible') == -1) && (ua.indexOf('webtv') == -1) && (ua.indexOf('hotjava') == -1) ) );
	
	// spoofing and compatible browsers
	this.isIECompatible = ( (ua.indexOf('msie') != -1) && !this.isIE);
	this.isNSCompatible = ( (ua.indexOf('mozilla') != -1) && !this.isNS && !this.isMozilla);
	
	// rendering engine versions
	this.geckoVersion = ( (this.isGecko) ? ua.substring( (ua.lastIndexOf('gecko/') + 6), (ua.lastIndexOf('gecko/') + 14) ) : -1 );
	this.equivalentMozilla = ( (this.isGecko) ? parseFloat( ua.substring( ua.indexOf('rv:') + 3 ) ) : -1 );
	this.appleWebKitVersion = ( (this.isAppleWebKit) ? parseFloat( ua.substring( ua.indexOf('applewebkit/') + 12) ) : -1 );
	
	// browser version
	this.versionMinor = parseFloat(navigator.appVersion); 
	
	// correct version number
	if (this.isGecko && !this.isMozilla) {
	   this.versionMinor = parseFloat( ua.substring( ua.indexOf('/', ua.indexOf('gecko/') + 6) + 1 ) );
	}
	else if (this.isMozilla) {
	   this.versionMinor = parseFloat( ua.substring( ua.indexOf('rv:') + 3 ) );
	}
	else if (this.isIE && this.versionMinor >= 4) {
	   this.versionMinor = parseFloat( ua.substring( ua.indexOf('msie ') + 5 ) );
	}
	else if (this.isKonqueror) {
	   this.versionMinor = parseFloat( ua.substring( ua.indexOf('konqueror/') + 10 ) );
	}
	else if (this.isSafari) {
	   this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('safari/') + 7 ) );
	}
	else if (this.isOmniweb) {
	   this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('omniweb/') + 8 ) );
	}
	else if (this.isOpera) {
	   this.versionMinor = parseFloat( ua.substring( ua.indexOf('opera') + 6 ) );
	}
	else if (this.isIcab) {
	   this.versionMinor = parseFloat( ua.substring( ua.indexOf('icab') + 5 ) );
	}
	
	this.versionMajor = parseInt(this.versionMinor); 
	
	// dom support
	this.isDOM1 = (document.getElementById);
	this.isDOM2Event = (document.addEventListener && document.removeEventListener);
	
	// css compatibility mode
	this.mode = document.compatMode ? document.compatMode : 'BackCompat';

	// platform
	this.isWin    = (ua.indexOf('win') != -1);
	this.isWin32  = (this.isWin && ( ua.indexOf('95') != -1 || ua.indexOf('98') != -1 || ua.indexOf('nt') != -1 || ua.indexOf('win32') != -1 || ua.indexOf('32bit') != -1 || ua.indexOf('xp') != -1) );
	this.isMac    = (ua.indexOf('mac') != -1);
	this.isUnix   = (ua.indexOf('unix') != -1 || ua.indexOf('sunos') != -1 || ua.indexOf('bsd') != -1 || ua.indexOf('x11') != -1)
	this.isLinux  = (ua.indexOf('linux') != -1);
	
	// specific browser shortcuts
	this.isNS4x = (this.isNS && this.versionMajor == 4);
	this.isNS40x = (this.isNS4x && this.versionMinor < 4.5);
	this.isNS47x = (this.isNS4x && this.versionMinor >= 4.7);
	this.isNS4up = (this.isNS && this.versionMinor >= 4);
	this.isNS6x = (this.isNS && this.versionMajor == 6);
	this.isNS6up = (this.isNS && this.versionMajor >= 6);
	this.isNS7x = (this.isNS && this.versionMajor == 7);
	this.isNS7up = (this.isNS && this.versionMajor >= 7);
	
	this.isIE4x = (this.isIE && this.versionMajor == 4);
	this.isIE4up = (this.isIE && this.versionMajor >= 4);
	this.isIE5x = (this.isIE && this.versionMajor == 5);
	this.isIE55 = (this.isIE && this.versionMinor == 5.5);
	this.isIE5up = (this.isIE && this.versionMajor >= 5);
	this.isIE6x = (this.isIE && this.versionMajor == 6);
	this.isIE6up = (this.isIE && this.versionMajor >= 6);
	
	this.isIE4xMac = (this.isIE4x && this.isMac);
	this.isIE5xMac = (this.isIE5x && this.isMac);
}



/*
function prevnext(groupId,window) {
	var d;
	if ( window ) { d = window.document; }
	else { d = document; }

	my ( $params ) = @_;
	my %params = %$params;
	my $rpp = $params{'rpp'};
	if ( !$rpp || $rpp !~ m/^[0-9]+$/ ) { $rpp = 25; }
	if ( $rpp > 250 ) { $rpp = 250; }
	if ( $rpp < 1 ) { $rpp = 1; }
	my $limit = $params{'limit'};
	if ( !$limit || $limit !~ m/^[0-9]+$/ ) { undef $limit; }
	my $page = $params{'page'};
	if ( !$page || $page !~ /^[0-9]+$/ || $page < 1 ) { $page = 1; }
	if ( $page > 100 ) { $page = 100; }
	my $maxslots = $params{'maxslots'};
	if ( !$maxslots || $maxslots !~ m/^[0-9]+$/ ) { $maxslots = 11; }
	if ( $maxslots % 2 == 0 ) { $maxslots++; }
	if ( $maxslots < 5 ) { $maxslots = 5; }
	if ( $maxslots > 25 ) { $maxslots = 25; }
	my $disable_numbers = $params{'disable_numbers'};
	my $return_count = $params{'count'};

	my $sql = $params{'sql'};
	my $sql_params = $params{'sql_params'};
	my @sql_params; if ( $sql_params ) { @sql_params = @$sql_params; }
	my $qsa = $params{'qsa'};
	my %qsa; if ( $qsa ) { %qsa = %$qsa; }
	my $qspass = '';
	if ( %qsa ) {
		foreach ( sort keys %qsa ) {
			if ( $qsa{$_} ) {
				$qspass .= ';' .$_ . '=' . CMS::HTTP::encode($qs
															 a{$_});
			}
		}
	}

	# Begin the logic.
	my $dbh = CMS::Database::get_handle();
	my ($count) = $dbh->selectrow_array($sql,undef,@sql_params);
	$dbh->disconnect();
	$$return_count = $count;
	# If the count is less than the number of results per page, return nothi
ng.
	if ( !$count || $count == 0 || $count <= $rpp ) { return undef; }
	# Impose limit, if any.
if ( $limit && $count > $limit ) { $count = $limit; }
my $numpages = ceil($count/$rpp);
if ( $page > $numpages ) { $page = $numpages; }
my $offset = ( $rpp * $page ) - $rpp;
my $numleft = $count - ( $offset + $rpp );
	# By default, startnum and endnum are 1, but if count != 0, then 
my $startnum = $offset + 1;
my $endnum;
if ( $numleft > 0 ) { $endnum = $offset + $rpp; }
else { $endnum = $count; }
my $output .= "<div class='prev_next_links'>";
if ( $offset > 0 ) {
	my $prevcount;
	if ( $offset < $rpp ) { $prevcount = $offset; } 
	else { $prevcount = $rpp; }
	my $prevpage = $page - 1;
	$output .= "<span class='prev_next_button prev_button'>";
	$output .= "<a href='?p=${prevpage}${qspass}'>";
		$output .= "<img src='/sitebuilder/images/arrows/left-link.png' 
";
		$output .= "alt='[Prev]' />";
		$output .= "</a>";
		$output .= "</span>";
		$output .= "\n";
}
else { 
	$output .= "<span class='prev_next_button prev_button'>";
	$output .= "<img src='/sitebuilder/images/arrows/left.png' ";
	$output .= "alt='[Prev]' />";
	$output .= "</span>";
	$output .= "\n";
}
my $c = 0; my $cc = 1;
my $lo_ellipsis_printed = 0;
my $hi_ellipsis_printed = 0;
while ( $c < $count ) {
	my $hide_this_num = 0;
	if ( $numpages > $maxslots ) {
			# The low-end elipsis
		if ( $cc != 1 ) {
			if ( ($page <= $numpages-($maxslots-1)/2 && 
				  $page > ($maxslots+1)/2 && 
				  $cc <= $page-($maxslots-3)/2) ||
				 ($page > $numpages-($maxslots-1)/2 && 
				  $cc < $numpages-($maxslots-3)) )
				{
					$hide_this_num = 1;
					if ( !$lo_ellipsis_printed ) {
						$output .= "<span class='prev_ne
xt_number'>";
						$output .= "&hellip;";
						$output .= "</span>";
						$output .= "\n";
						$lo_ellipsis_printed = 1;
					}
				}
		}
			# The high-end elipsis
		if ( $cc != $numpages ) {
			if ( $page <= ($maxslots+1)/2 ) {
				if ( $cc >= $maxslots-1 ) {
					$hide_this_num = 1;
					if ( !$hi_ellipsis_printed ) {
						$hide_this_num = 1;
							$output .= "<span class=
'prev_next_number'>";
							$output .= "&hellip;";
							$output .= "</span>";
							$output .= "\n";
							$hi_ellipsis_printed = 1
															;
					}
				}
			}
			else {
				if( $hi_ellipsis_printed ) {
					$hide_this_num = 1;
				}
				elsif ( ($page < $numpages-($maxslots-1)
						 /2) && 
						($cc >= ($page+($maxslot
										s-3)/2)) ) {
					$hide_this_num = 1;
						$output .= "<span class='prev_ne
xt_number'>";
						$output .= "&hellip;";
						$output .= "</span>";
						$output .= "\n";
						$hi_ellipsis_printed = 1;
				}
			}
		}
	}
	if ( !$disable_numbers && !$hide_this_num ) {
		$output .= "<span class='prev_next_number'>";
		if ( $startnum - 1 == $c ) { $output .= "${cc}"; }
		else {
			$output .= "<a class='prev_next_link' ";
			$output .= "href='?p=${cc}${qspass}'>";
			$output .= ${cc};
			$output .= "</a>";
		}
		$output .= "</span>";
		$output .= "\n";
	}
	$c += $rpp; $cc++;
}

my $lastoffset = $c - $rpp;
if ( $numleft > 0 ) {
	my $nextcount;
	if ( $numleft < $rpp ) { $nextcount = $numleft; }
	else { $nextcount = $rpp; }
	my $nextpage = $page + 1;
	$output .= "<span class='prev_next_button next_button'>";
	$output .= "<a href='?p=${nextpage}${qspass}'>";
		$output .= "<img src='/sitebuilder/images/arrows/right-link.png'
 ";
		$output .= "alt='[Next]' />";
		$output .= "</a>";
		$output .= "</span>";
		$output .= "\n";
}
else {
	$output .= "<span class='prev_next_button next_button'>";
	$output .= "<img src='/sitebuilder/images/arrows/right.png' ";
	$output .= "alt='[Next]' />";
	$output .= "</span>";
	$output .= "\n";
}
$output .= "</div>\n";
return $output;
}
*/


/*
function drawPopupMenu() {
	if ( document.getElementById && document.nsknv ) {
		var submenuPosition = 'bottom';
		var menu = document.getElementById("main_links");
		for (var i=0; i < menu.childNodes.length; i++) {
			var group = menu.childNodes[i];
			if ( group.nodeName == "UL" ) {
				for (var j=0; j < group.childNodes.length; j++) {
					var item = group.childNodes[j];
					if (item.nodeName == "LI" && item.childNodes.length == 2) {
						var submenu = item.childNodes[1];
						var relNode = item;
						imgNode = item.childNodes[0].childNodes[0];
						if ( imgNode && imgNode.nodeName == "IMG" ) {
							relNode = imgNode;
						}
						submenu.style.position = 'absolute';
						if ( submenuPosition == 'bottom' ) {
							submenu.style.left = '0px';
							submenu.style.top = relNode.offsetHeight + 'px';
						}
						else if ( submenuPosition == 'right' ) {
							submenu.style.left = relNode.offsetWidth + 'px';
							submenu.style.top = '0px';
						}
						item.onmouseover = function() {
							this.className += " hover";
						}
						item.onmouseout = function() {
							this.className=this.className.replace(" hover","");
						}
					}
				}
			}
		}
	}
}

*/



