// Standard JavaScript - Copyright (C) 2007-2010 Charles A Upsdell, All Rights Reserved; www.upsdell.com
//
// This has JavaScript available to all pages.

// cRandomizedList Class
function cRandomizedList ( length )
{
	if ( arguments.length < 1 )
		length = 52;
	this.length = length;		// Length of list of integers
	this.index = length;		// Index into list of integers
	this.list = new Array();	// List of integers
	return;
}

// Method to randomize the list in a cRandomizedList object
cRandomizedList.prototype.randomize = function ()
{
	var i, j, randomInteger;
	for ( i = 0; i < this.length; ++i )
		this.list[i] = i;
	for ( i = 0; i < this.length; ++i )
	  {
		j = i + Math.floor( (this.length-i)*Math.random() );
		randomInteger = this.list[j];
		this.list[j] = this.list[i];
		this.list[i] = randomInteger;
	  }
	return;
}

// Method to get next random number from cRandomizedList object
cRandomizedList.prototype.getNumber = function ()
{
	if ( this.length == this.index )
	  {
		this.randomize();
		this.index = 0;
	  }
	var value = this.list[this.index++];
	return( value );
}

// getElement - get element associated with an ID, or null if not found
function getElement ( id )
{
	var element = null;
	if ( (arguments.length == 0) || (id == "") )
		return( null );
	if ( document.getElementById )
		element = document.getElementById( id );
	else if ( document.all )
		element = document.all[id];
	return( element );
}

String.prototype.alert = function ( method, arglist )
{
var errmsg = this;
errmsg += (method == '') ? '()' : ('.' + method + '()');
errmsg += ' error: ' + arguments[1];
for ( var i = 2; i < arguments.length; ++i )
	errmsg += ((i == 2 ) ? ': ' : ', ' ) + arguments[i];
alert( errmsg );
}

// Function to write code to display a randomly-selected photo for home page. myRandomPhoto[] has ID and list of photos.
function myGetPhoto ( nOldImage )
{
	if ( typeof(myRandomPhoto) != typeof(noSuchArray) )
	  {
		var o = getElement( myRandomPhoto[0] );
		if ( o == null )
			return;
		if ( arguments.length < 1 )
			nOldImage = 0;
		var nImage;
		do { nImage = 1 + myRandomizedList.getNumber() }
			while (nImage == nOldImage);
		var sPhoto = myRandomPhoto[nImage];
		var photo, title;
		var photo_length = sPhoto.indexOf(' ');
		if ( photo_length == -1 )
		  {
			photo = sPhoto;
			title = '';
		  }
		else
		  {
			photo = sPhoto.substring(0,photo_length);
			title = sPhoto.substring(photo_length+1);
		  }
		var sHTML = '<p class="tiny">';
		sHTML += '<img src="img/' + photo + '" onclick="myGetPhoto(' + nImage + ');" class="photo" title="Click to see another photo" alt="Action photo" />';
		if ( title != '' )
			sHTML += title;
		sHTML += '</p>';
		o.innerHTML = sHTML;
	  }
}

// Function to write code to enable links to the Photos page
function myEnablePhotos ( sID )
{
	if ( arguments.length < 1 )
		sID = 'photos_menuitem';
	var o = getElement( sID );
	if ( o == null )
		return;
	o.style.display = 'block';
}

//	Function called when page has been loaded to do DHTML
function myOnLoad( )
  {
	myGetPhoto();
	if ( typeof(cal) == 'object' )
	  {
		if ( typeof(cal.toNewsHTML) !== typeof(noSuchFunction) )
			cal.toNewsHTML();
		if ( typeof(cal.toCalendarHTML) !== typeof(noSuchFunction) )
			cal.toCalendarHTML( 'grid', 'ins_calendar_dump', window.location.hash );
		if ( typeof(cal.toGamesHTML) !== typeof(noSuchFunction) )
			cal.toGamesHTML();
		if ( typeof(calendarTournament) !== typeof(noSuchData) )
			cal.toTournamentGamesHTML( calendarTournamentID, calendarTournament );
	  }
  }


// Scroll to an anchor; return false
function myScrollToAnchor ( id )
{
	if ( arguments.length < 1 )
		id = window.location.hash;
	if ( id.indexOf('#') == 0 )
		id = id.substring(1);
	var o = getElement( id );
	if ( myBrowser.isIE || (o==null) )
	  {
		window.location.replace( '#' + id );
	  }
	else if ( myBrowser.isO && (myBrowser.v.v[0]==8) )
	  {
		return( true );
	  }
	else
	  {
		if ( (typeof(o.offsetTop) !== typeof(nosuchvalue)) && (typeof(window.scrollTo) !== typeof(noSuchFunction)) )
		  {
			var x = 0;
			var y = 0;
			while ( o )
			  {
				x += o.offsetLeft;
				y += o.offsetTop;
				o = o.offsetParent;
			  }
			window.scrollTo( x, y );
		  }
		else
			window.location.replace( '#' + id );
	  }
	return( false );
}


// Scroll to a position, defaulting to top of page; return false if done
function myScrollTo ( left, top )
{
	if ( arguments.length < 1 )
		left = 0;
	if ( arguments.length < 2 )
		top = 0;
	var value = true;
	if ( typeof(window.scrollTo) !== typeof(noSuchFunction) )
	  {
	 	window.scrollTo( left, top );
		value = false;
	  }
	return( value );
}



