//	argv.js - Functions dealing with arguments following URL
//
//	Copyright (C) 2006-2009 Charles A Upsdell, All Rights Reserved; www.upsdell.com


// Initialization .................... executed when the page is starting to load

// cState - define cState class

function cState ( size, gallery, photo, nTheme, testmode, gmode, gmode_old, svgallery, svphoto )
{
	if ( arguments.length == 0 )
	  {
		size = gallery = photo = null;
		nTheme = nDefaultTheme;
		testmode = 0;
		gmode = gmode_old = null;
	  }
	else if ( arguments.length != 9 )
		alert( 'cState error: wrong number of arguments, ' + arguments.length );
	this.size = size;
	this.gallery = gallery;
	this.photo = photo;
	this.nTheme = nTheme;
	this.testmode = testmode;
	this.gmode = gmode;
	this.gmode_old = gmode_old;
	this.svgallery = svgallery;
	this.svphoto = svphoto;
}

cState.prototype.getGalleryModeState = function ( page )
{
	if ( arguments.length == 0 )
		page = myGetFilename();
	var tempState = new cState();
	tempState = this;
	return ( tempState.setNewGmodeState('galleries.html') == page );
}

cState.prototype.getURL = function ( page, image_size, theme, gallery, photo )
{
	if ( arguments.length < 1 )
		alert( 'oState.getURL error: you forgot to specify a page' );

	var sURL = this.setNewState( page, image_size, theme, gallery, photo );

	var sHash = '';
	var i = sURL.lastIndexOf( '#' );
	if ( i >= 0 )
	  {
		sHash = sURL.substring( i );
		sURL = sURL.substring( 0, i );
	  }
	sURL += '?';
	if ( this.nTheme >= 0 )
		sURL += 'theme=' + Theme[this.nTheme].name + ';';
	if ( this.size != null )
		sURL += 'size=' + this.size + ';';
	if ( this.gallery != null )
		sURL += 'gallery=' + this.gallery.replace(/ /g,'%20') + ';';
	if ( this.photo != null )
		sURL += 'photo=' + this.photo + ';';
	if ( this.testmode == 1 )
		sURL += 'testmode=1;';
	if ( this.gmode != null )
		sURL += 'gmode=' + this.gmode + ';';
	if ( this.gmode_old != null )
		sURL += 'gmode_old=' + this.gmode_old + ';';
	if ( this.svgallery != null )
		sURL += 'svgallery=' + this.svgallery + ';';
	if ( this.svphoto != null )
		sURL += 'svphoto=' + this.svphoto + ';';
	sURL += sHash;
	return( sURL );
}

cState.prototype.gotoPage = function ( page, image_size, theme, gallery, photo )
{
	if ( arguments.length < 1 )
		alert( 'oState.gotoPage error: you forgot to specify a page' );
	window.location = this.getURL( page, image_size, theme, gallery, photo );
}

cState.prototype.gotoNextPhoto = function ( aOrderedPhotos )
{
	var i;
	for ( i = 0; i < aOrderedPhotos.length; ++i )
	  {
		if ( aOrderedPhotos[i] == this.photo )
			break;
	  }
	if ( i >= aOrderedPhotos.length )
	  {
		this.gotoPage( myGetFilename() );
	  }
	else if ( (i+1) < aOrderedPhotos.length )
	  {
		this.gotoPage( myGetFilename(), null, null, null, aOrderedPhotos[i+1] );
	  }
	else
	  {
		this.gotoPage( myGetFilename(), null, null, getNextGallery(), null );
	  }
}

cState.prototype.gotoPreviousPhoto = function ( aOrderedPhotos )
{
	var i;
	for ( i = 0; i < aOrderedPhotos.length; ++i )
	  {
		if ( aOrderedPhotos[i] == this.photo )
			break;
	  }
	if ( i >= aOrderedPhotos.length )
	  {
		this.gotoPage( myGetFilename() );
	  }
	else if ( i > 0 )
	  {
		this.gotoPage( myGetFilename(), null, null, null, aOrderedPhotos[i-1] );
	  }
	else
	  {
		var sPreviousGallery = getPreviousGallery();
		var nPreviousGalleryLength = Gallery[getGalleryIndex(sPreviousGallery)].photo.length;
		this.gotoPage( myGetFilename(), null, null, sPreviousGallery, Photo[Gallery[getGalleryIndex(sPreviousGallery)].photo[nPreviousGalleryLength-1]].name );
	  }
}

cState.prototype.imageFilename = function ( root, type_of_image )
{
	if ( arguments.length < 2 )
		type_of_image = 'jpg';
	if ( (this.testmode==1) && (root == 'ico_fs') )
		root = 'ico_test';
	return ( root + Theme[this.nTheme].filename_suffix + '.' + type_of_image );
}

cState.prototype.loadArgumentsState = function ( argument_list )
{
	if ( (arguments.length = 0) || (argument_list == null) )
		argument_list = myArgParse();
	this.size = argument_list.size ? argument_list.size : null;
	this.gallery = argument_list.gallery ? argument_list.gallery : null;
	this.photo = argument_list.photo ? argument_list.photo : null;
	this.nTheme = argument_list.theme ? getThemeIndex(argument_list.theme) : nDefaultTheme;
	this.gmode = argument_list.gmode ? argument_list.gmode : null;
	this.gmode_old = argument_list.gmode_old ? argument_list.gmode_old : null;
	this.svgallery = argument_list.svgallery ? argument_list.svgallery : null;
	this.svphoto = argument_list.svphoto ? argument_list.svphoto : null;
	if ( this.nTheme == -1 )
	  {
		this.nTheme = nDefaultTheme;
		alert( 'cState.loadArgumentsState error: invalid theme ' + argument_list.theme + ', reset to default' );
	  }
}

cState.prototype.loadArgumentOverride = function ( argument_list )
{
	this.testmode = this.testmode; // Make sure this is considered a method
	if ( (arguments.length = 0) || (argument_list == null) )
		argument_list = myArgParse();
	if ( argument_list.override && (argument_list.override==1) )
		return( argument_list.override );
	else
		return( 0 );
}

cState.prototype.loadArgumentTestmode = function ( argument_list )
{
	if ( (arguments.length = 0) || (argument_list == null) )
		argument_list = myArgParse();
	if ( argument_list.testmode && (argument_list.testmode==1) )
		this.testmode = 1;
}

cState.prototype.loadCookieState = function ( )
{
	this.size = oCookie.size ? oCookie.size : null;
	this.gallery = oCookie.gallery ? oCookie.gallery : null;
	this.photo = oCookie.photo ? oCookie.photo : null;
	this.nTheme = oCookie.theme ? getThemeIndex(oCookie.theme) : nDefaultTheme;
	this.gmode = oCookie.gmode ? oCookie.gmode : null;
	this.gmode_old = oCookie.gmode_old ? oCookie.gmode_old : null;
	this.svgallery = oCookie.svgallery ? oCookie.svgallery : null;
	this.svphoto = oCookie.svphoto ? oCookie.svphoto : null;
	if ( this.nTheme == -1 )
	  {
		this.nTheme = nDefaultTheme;
		alert( 'cState.loadCookieState error: invalid theme ' + oCookie.theme + ', reset to default' );
	  }
	oCookie.store();
}

cState.prototype.repairStates = function ( )
{
  // Define repair values
	var oFixState = new cState( 'small', sFirstGallery, null, nDefaultTheme, 0, GalleryModeInfo[nGalleryModeDefault].gmode, GalleryModeInfo[nGalleryModeDefault].gmode, sServicesGallery, sServicesPhoto );
  // Check and repair states
	var nRepairs = 0;
	if ( (this.size != 'small') && (this.size != 'large') )
	  {
		if ( this.testmode == 1 )
			alert( 'cState.repairStates: warning, fix invalid image size, ' + this.size );
		this.size = oFixState.size;
		++nRepairs;
	  }
	if ( (this.nTheme < 0) || (this.nTheme > Theme.length) )
	  {
		if ( this.testmode == 1 )
			alert( 'cState.repairStates: warning, fix invalid theme, ' + this.nTheme );
		this.nTheme = oFixState.nTheme;
		++nRepairs;
	  }
	var nGalleryIndex = getGalleryIndex( this.gallery );
	if ( nGalleryIndex == -1 )
	  {
		if ( this.testmode == 1 )
			alert( 'cState.repairStates: warning, fix invalid gallery, ' + this.gallery );
		this.gallery = oFixState.gallery;
		++nRepairs;
	  }
	var nPhotoIndex = getPhotoIndex( this.gallery, this.photo );
	if ( nPhotoIndex == -1 )
	  {
		if ( toString(this.photo) != toString('') )
		  {
			if ( this.testmode == 1 )
				alert( 'cState.repairStates: warning, fix invalid photo, ' + this.photo + ' in gallery ' + this.gallery );
			this.photo = oFixState.photo;
			++nRepairs;
		  }
	  }
	var nGalleryModeIndex = getGalleryModeIndex( this.gmode );
	if ( (this.gmode == null) || (nGalleryModeIndex == -1) )
	  {
		if ( this.testmode == 1 )
			alert( 'cState.repairStates: warning, fix invalid gallery mode, ' + this.gmode );
		this.gmode = oFixState.gmode;
		++nRepairs;
	  }
	nGalleryModeIndex = getGalleryModeIndex( this.gmode_old );
	if ( (this.gmode_old == null) || (nGalleryModeIndex == -1) )
	  {
		if ( this.testmode == 1 )
			alert( 'cState.repairStates: warning, fix invalid previous gallery mode, ' + this.gmode_old );
		this.gmode_old = oFixState.gmode_old;
		++nRepairs;
	  }
	nGalleryIndex = getGalleryIndex( this.svgallery );
	if ( nGalleryIndex == -1 )
	  {
		if ( this.testmode == 1 )
			alert( 'cState.repairStates: warning, fix invalid svgallery, ' + this.svgallery );
		this.svgallery = oFixState.svgallery;
		++nRepairs;
	  }
	nPhotoIndex = getPhotoIndex( this.svgallery, this.svphoto );
	if ( nPhotoIndex == -1 )
	  {
		if ( this.testmode == 1 )
			alert( 'cState.repairStates: warning, fix invalid photo, ' + this.svphoto + ' in gallery ' + this.svgallery );
		this.svphoto = oFixState.svphoto;
		++nRepairs;
	  }
	if ( nRepairs > 0 )
		this.saveState();
}

cState.prototype.saveState = function ( )
{
	oCookie.size = this.size;
	oCookie.gallery = this.gallery;
	oCookie.photo = this.photo;
	oCookie.theme= Theme[this.nTheme].name;
	oCookie.gmode = this.gmode;
	oCookie.gmode_old = this.gmode_old;
	oCookie.svgallery = this.svgallery;
	oCookie.svphoto = this.svphoto;
	oCookie.store();
}

cState.prototype.setDefaults = function ( width_limit, height_limit, nTheme )
{
	if ( (this.size != 'small') && (this.size != 'large') )
		this.size = getArgDefaultImageSize( width_limit, height_limit );
	if ( (this.nTheme < 0) || (this.nTheme > Theme.length) )
		this.theme = nTheme;
}

cState.prototype.setNewGmodeState = function ( page )
{
	var nThisGalleryModeFilenameIndex = getGalleryModeFilenameIndex( myGetFilename() );
	var nNextGalleryModeFilenameIndex = getGalleryModeFilenameIndex( page );
	if ( nThisGalleryModeFilenameIndex == nNextGalleryModeFilenameIndex )
		return( page );
	if ( (nThisGalleryModeFilenameIndex != -1) && (GalleryModeInfo[nThisGalleryModeFilenameIndex].gmode != this.gmode) )
	  {
		this.gmode_old = this.gmode;
		this.gmode = GalleryModeInfo[nThisGalleryModeFilenameIndex].gmode;
	  }
	if ( nNextGalleryModeFilenameIndex != -1 )
	  {
		if ( nNextGalleryModeFilenameIndex != nGalleryModeGeneric )
		  {
			this.gmode_old = this.gmode;
			this.gmode = GalleryModeInfo[nNextGalleryModeFilenameIndex].gmode;
		  }
		else
		  {
			if ( nThisGalleryModeFilenameIndex != -1 )
				this.gmode = this.gmode_old;
			var nCurrentGmode = getGalleryModeIndex(this.gmode);
			if ( nCurrentGmode >= 0 )
				page = GalleryModeInfo[nCurrentGmode].filename;
		  }
	  }
	return( page );	
}

cState.prototype.setNewState = function ( page, image_size, theme, gallery, photo )
{
	if ( (arguments.length > 0) && (page) )
	  {
		page = this.setNewGmodeState( page );
	  }
	if ( (arguments.length > 1) && (image_size) )
	  {
		this.size = image_size;
		if ( (this.size != 'small') && (this.size != 'large') )
			alert( 'oState.newState error: invalid image size (' + this.size + ')' );
	  }
	if ( (arguments.length > 2) && (theme) )
	  {
		this.nTheme = getThemeIndex( theme );
		if ( this.nTheme == -1 )
			alert( 'oState.newState error: invalid theme (' + theme + ')' );
	  }
	if ( (arguments.length > 3) && (gallery) )
	  {
		this.gallery = gallery;
		if ( this.testmode && (getGalleryIndex(gallery) == -1) )
			alert( 'oState.newState error: invalid gallery (' + this.gallery + ')' );
	  }
	if ( (arguments.length > 4) && (photo) )
	  {
		this.photo = photo;
		if ( this.testmode && (getPhotoIndex(gallery,photo) == -1) )
			alert( 'oState.newState error: invalid photo (' + this.photo + ')' );
	  }
	this.saveState();
	return( page );
}

// cGalleryMode - define cGalleryMode class

function cGalleryMode ( gmode, filename )
{
	if ( arguments.length != 2 )
		alert( 'cGalleryMode error, wrong number of arguments, ' + arguments.length );
	this.gmode = gmode;
	this.filename = filename;
}

function getGalleryModeIndex ( gmode )
{
	for ( var i = 0; i < GalleryModeInfo.length; ++i )
	  {
		if ( GalleryModeInfo[i].gmode == gmode )
			return( i );
	  }
	return( -1 );
}

function getGalleryModeFilenameIndex ( filename )
{
	for ( var i = 0; i < GalleryModeInfo.length; ++i )
	  {
		if ( GalleryModeInfo[i].filename == filename )
			return( i );
	  }
	return( -1 );
}


// Start of initialization

	// Set global variables
	var oState = new cState();		// Object holding state information
	var GalleryModeInfo = new Array();	// Object holding gallery mode information
	GalleryModeInfo[0] = new cGalleryMode( 'galleries', 'galleries.html' );
	GalleryModeInfo[1] = new cGalleryMode( 'list', 'index_list.html' );
	GalleryModeInfo[2] = new cGalleryMode( 'map', 'index_map.html' );
	GalleryModeInfo[3] = new cGalleryMode( 'thumbnail', 'index.html' );
	var nGalleryModeGeneric = getGalleryModeIndex( 'galleries' );
	var nGalleryModeDefault = getGalleryModeIndex( 'thumbnail' );

	// Set local variables
	var isImageSizeByBrowserWidth = false;

	// Load cookie or URL argument variables
	var oCookie = new Cookie( document, 'gallery_state', 0, '/' );
	if ( (oState.loadArgumentOverride()==0) && oCookie.load() )
		oState.loadCookieState();
	else
		oState.loadArgumentsState();
	oState.loadArgumentTestmode();

	// Set defaults for invalid image size or theme
	oState.setDefaults( 849, 630, nDefaultTheme );

	// Save cookie variables
	oState.saveState();

	// Load CSS for current image size
	if ( oState.size == 'large' )
		document.writeln( '<link rel="stylesheet" type="text/css" href="size_large.css">\n' );
	else // default is 'small'
		document.writeln( '<link rel="stylesheet" type="text/css" href="size_small.css">\n' );

	// Set CSS to control page width
	{
	var sHTML = '';
	var nEmWidth = (oState.size == 'large') ? '55' : '55';
	var nPixelWidth = (oState.size == 'large') ? '850' : '675';
	var nWindowWidth = getArgWindowWidth();
	if ( isModernBrowser )
	  {
		sHTML = '<style type="text/css">\n';
		sHTML += 'table.contents { width:100%; max-width:' + nEmWidth + 'em; margin:0 auto; }\n';
		if ( nWindowWidth < nPixelWidth )
			sHTML += 'table.contents { min-width:' + nPixelWidth + 'px; }\n';
		sHTML += '</style>\n';
		document.write( sHTML );
	  }
	else
	  {
		if ( (nWindowWidth > nPixelWidth) || (nWindowWidth == 0) )
		  {
			sHTML = '<style type="text/css">\n';
			sHTML += 'table.contents tr td { width:' + nPixelWidth + 'px; }\n';
			sHTML += 'table.gallery_names { width:20%; }\n';
			sHTML += '</style>\n';
			document.write( sHTML );
		  }
	  }
	}

	// Load CSS for current theme
	if ( oState.nTheme >= 0 )
		Theme[oState.nTheme].loadCSS();

// End of initialization


// Local functions ................... called by this module only

// Parse arguments into an array
function myArgParse()
{
	var args = new Object();
	var query = location.search.substring(1);
	var pairs = query.split(";");
	for ( var i = 0; i < pairs.length; i++ )
	  {
		var pos = pairs[i].indexOf('=');
		if ( pos == -1 ) continue;
		var argname = pairs[i].substring(0,pos);
		var value = pairs[i].substring(pos+1);
		args[argname] = unescape(value);
	  }
	return( args );
}

// Get the default image size, based on the browser window size
function getArgDefaultImageSize( width_limit, height_limit )
{
	if ( isImageSizeByBrowserWidth )
	  {
		var myWidth = getArgWindowWidth();
		if ( myWidth > width_limit )
			return( 'large' );
		else
			return( 'small' );
	  }
	else
	  {
		var myHeight = getArgWindowHeight();
		if ( myHeight > height_limit )
			return( 'large' );
		else
			return( 'small' );
	  }
}

// Get the browser window height
function getArgWindowHeight( )
{
	var myHeight = 0;
	if ( typeof( window.innerHeight ) == 'number' )
		myHeight = window.innerHeight;
	else if ( document.documentElement && document.documentElement.clientHeight )
		myHeight = document.documentElement.clientHeight;
	else if ( document.body && document.body.clientHeight )
	    myHeight = document.body.clientHeight;
	return ( myHeight );
}

// Get the browser window width
function getArgWindowWidth( )
{
	var myWidth = 0;
	if ( typeof( window.innerWidth ) == 'number' )
		myWidth = window.innerWidth;
	else if ( document.documentElement && document.documentElement.clientWidth )
		myWidth = document.documentElement.clientWidth;
	else if ( document.body && document.body.clientWidth )
	    myWidth = document.body.clientWidth;
	return ( myWidth );
}

