
//-----------------------------------------------------------------------------------------------------------

// Functions & Data for Terrie Reddish

//-----------------------------------------------------------------------------------------------------------

//--------
// Classes
//--------

//-----------------------------------------------------------------------------------------------------------

// Start: TRImage

//-----------------------------------------------------------------------------------------------------------

function TRImage( n, w, h, t )

//-----------------------------------------------------------------------------------------------------------
{
	this.n	= n;
	this.w	= w;
	this.h	= h;
	this.t	= t;

	this.get_tn_img_tag_for	= tri_get_tn_img_tag_for;
	this.get_ga_img_tag_for	= tri_get_ga_img_tag_for;
	this.get_tn_url			= tri_get_tn_url;
	this.get_ga_url			= tri_get_ga_url;
	this.get_tn_html		= tri_get_tn_html;
	this.get_ga_html		= tri_get_ga_html;
}
//-----------------------------------------------------------------------------------------------------------

function tri_get_tn_url()

//-----------------------------------------------------------------------------------------------------------

// The URL is derived from the Image Name
// All Images are assumed to be in the "images" directory and have the ".jpg" extension

//-----------------------------------------------------------------------------------------------------------
{
	return( 'images/' + this.n + '.jpg' );
}
//-----------------------------------------------------------------------------------------------------------

function tri_get_ga_url()

//-----------------------------------------------------------------------------------------------------------

// The URL is derived from the Image Name
// All Images are assumed to be in the "images" directory and have the ".jpg" extension

//-----------------------------------------------------------------------------------------------------------
{
	return( '../images/' + this.n + '.jpg' );
}
//-----------------------------------------------------------------------------------------------------------

function tri_get_tn_img_tag_for( sw, sh )

//-----------------------------------------------------------------------------------------------------------
{
	var w1 = sw;
	var h1 = Math.floor((this.h * sw) / this.w);

	if( h1 > sh )
	{
		h1 = sh;
		w1 = Math.floor((this.w * sh) / this.h);
	}

	//alert( "(sw, sh) = (" + sw + ", " + sh + "),  (w1, h1) = (" + w1 + ", " + h1 + ")" );

	var url = this.get_tn_url();

	return( "<img title='"+this.t+"' src='images/tr1x1.gif' name='"+url+"' width="+w1+" height="+h1+" border=0>" );
}
//-----------------------------------------------------------------------------------------------------------

function tri_get_ga_img_tag_for( sw, sh )

//-----------------------------------------------------------------------------------------------------------
{
	var w1 = sw;
	var h1 = Math.floor((this.h * sw) / this.w);

	if( h1 > sh )
	{
		h1 = sh;
		w1 = Math.floor((this.w * sh) / this.h);
	}

	//alert( "(sw, sh) = (" + sw + ", " + sh + "),  (w1, h1) = (" + w1 + ", " + h1 + ")" );

	var url = this.get_ga_url();

	return( "<img title='"+this.t+"' src='../images/tr1x1.gif' name='"+url+"' width="+w1+" height="+h1+" border=0>" );
}
//-----------------------------------------------------------------------------------------------------------

function tri_get_tn_html( tnw, tnh )

//-----------------------------------------------------------------------------------------------------------
{
	var ha = new Array();

	var itg = this.get_tn_img_tag_for( tnw, tnh );

//	ha.push( "<span onclick='open_gallery_window(\""+this.n+"\")' style='cursor:pointer;cursor:hand'>" );
	ha.push( itg );
//	ha.push( "</span>" );

	return( ha.join("") );
}
//-----------------------------------------------------------------------------------------------------------

function tri_get_ga_html( tnw, tnh )

//-----------------------------------------------------------------------------------------------------------
{
	var ha = new Array();

	var itg = this.get_ga_img_tag_for( tnw, tnh );

	ha.push( "<span onclick='open_gallery_window(\""+this.n+"\")' style='cursor:pointer;cursor:hand'>" );
	ha.push( itg );
	ha.push( "</span>" );

	return( ha.join("") );
}
//-----------------------------------------------------------------------------------------------------------

// Non-Class Functions

//-----------------------------------------------------------------------------------------------------------

function get_gallery_html_filename( n )

//-----------------------------------------------------------------------------------------------------------

// The HTML Filename is derived from the Image Name
// All HTML files are assumed to be in the "ghf" (g[allery ]h[tml ]f[iles]) directory with the ".html" extension

//-----------------------------------------------------------------------------------------------------------
{
	return( 'ghf/' + n + '.html' );
}
//-----------------------------------------------------------------------------------------------------------

function open_gallery_window( n )

//-----------------------------------------------------------------------------------------------------------
{
	var hfn = get_gallery_html_filename( n );

	var xbdr = 25;
	var ybdr = 10;

	var xleft = Math.floor( xbdr / 2 );
	var ytop  = Math.floor( ybdr / 2 );

//	var args1 = ",location=0,menubar=0,status=0,titlebar=0,scrollbars=0,resizable=0,left="+xleft+",top="+ytop;
	var args1 = ",location=0,menubar=0,status=0,titlebar=0,scrollbars=1,resizable=1,left="+xleft+",top="+ytop;

	var ww = window.screen.availWidth  - xbdr;
	var wh = window.screen.availHeight - ybdr;

	if( browser == 'IE' ) { wh -= 32; }


	//var wof = "window.open('"+hfn+"', '"+n+"', 'width=" + ww + ",height=" + wh + args + "')";

	var args = "width=" + ww + ",height=" + wh + args1;

	//alert( "ww = " + ww + ", wh = " + wh + ", '" + args + "'" );

	if( global_wo )
	{
		global_wo.close();
	}

	global_wo = window.open( hfn, n, args );

	global_wo.focus();
}
//-----------------------------------------------------------------------------------------------------------

// End: TRImage

//-----------------------------------------------------------------------------------------------------------

//-----
// Data
//-----

//-----------------------------------------------------------------------------------------------------------

//-------
//
// Screen
//
//-------

	var screen_width	= 800;
	var screen_height	= 600;

//--------
// Colours
//--------

	var trcol			= '#007f00';
	var trcol_light		= '#00bb00';
	var trcol_lighter	= '#55dd44';
	var trcol_lightest	= '#66ff44';
	var trcol_dark		= '#004400';



//----------------------
//
// Gallery Image Objects
//
//----------------------

	var tri_nikau		= new TRImage( 'Nikau_626x1179_c20', 626, 1179, 'Nikau Palm' );

	var tri_flaxbase	= new TRImage( 'RHS_Base_559x768_c20', 559, 768, 'Flax - Base' );

	var tri_flaxflowers	= new TRImage( 'Rhs_Flax_flowers_552x768_c20', 552, 768, 'Flax Flowers' );

	var tri_flaxroots	= new TRImage( 'RHS_Flax_roots_768x1056_c20', 768, 1056, 'Flax - Roots' );

	var tri_flaxtop		= new TRImage( 'Flax_top_1024x924_c20',   1024, 924, 'Flax - Top' );
	var tri_flaxleft	= new TRImage( 'Flax_left_1024x924_c20',  1024, 924, 'Flax - Left' );
	var tri_flaxright	= new TRImage( 'Flax_right_1024x924_c20', 1024, 924, 'Flax - Right' );

	var tri_panorama	= new TRImage( 'RHS_Panorama_1280x912_c20', 1280, 912, 'Flax - Panorama' );

	var tri_flaxmeta	= new TRImage( 'flaxmeta_1280x357_c20', 1280, 357, 'RHS Flax Metamorphosis' );

	var tri_pohusprig	= new TRImage( 'Pohutukawa_sprig_540x768_c20', 540, 768, 'Pohutukawa Sprig' );

	var tri_pohuleaves	= new TRImage( 'PohutukawaLeaves_1280x570_c20', 1280, 570, 'Pohutukawa Leaves' );

	var tri_treefern	= new TRImage( 'Black_tree_fern_1123x1024_c20', 1123, 1024, 'Black Tree Fern' );

	var tri_puka		= new TRImage( 'Puka_Merta_sinclairii_1280x964_c20', 1280, 964, 'Puka Merta Sinclairii' );

	var tri_3zucchini	= new TRImage( '3_Zucchini_860x768_c20', 860, 768, 'Zucchini' );

	var tri_hawaii		= new TRImage( 'Hawaii_672x768_c20', 672, 768, 'Hawaii' );




//---------------------
//
// Thumbnail Image Data
//
//---------------------

	// Thumbnail Image Size

	var tnw = 128;
	var tnh = 128;

	// An Array of Arrays of TRImage Objects

	var thumbnail_images = new Array();

	var row1 = new Array();
	row1.push( tri_nikau );
	row1.push( tri_flaxbase );
	row1.push( tri_flaxflowers );
	row1.push( tri_flaxroots );
	row1.push( tri_flaxtop );
	thumbnail_images.push( row1 );

	var row2 = new Array();
	row2.push( tri_panorama );
	row2.push( tri_flaxmeta );
	row2.push( tri_pohusprig );
	row2.push( tri_treefern );
	row2.push( tri_flaxleft );
	thumbnail_images.push( row2 );

	var row3 = new Array();
	row3.push( tri_pohuleaves );
	row3.push( tri_hawaii );
	row3.push( tri_3zucchini );
	row3.push( tri_puka );
	row3.push( tri_flaxright );
	thumbnail_images.push( row3 );


	// Gallery Image Objects for reference by Gallery Image HTML Files. Each TRI is keyed to its name.

	var gallery_image_objects = new Object();

	for( var tnx=0; tnx < thumbnail_images.length; tnx ++ )
	{
		var tn_row = thumbnail_images[ tnx ];

		for( var tnrx=0; tnrx < tn_row.length; tnrx ++ )
		{
			var tri = tn_row[ tnrx ];

			gallery_image_objects[tri.n] = tri;
		}
	}



//-----------------
// Global Workspace
//-----------------

	var UniqueIDIndex = 0;

	var global_wo = 0;

	var browser = which_browser();

//-----------------------------------------------------------------------------------------------------------

//----------
// Functions
//----------

//-----------------------------------------------------------------------------------------------------------

function draw_thumbnails( tnw, tnh )

//-----------------------------------------------------------------------------------------------------------
{
	// An Array of Arrays of TRImage Objects

	var bdrcol = trcol_lighter;

	var tn_border_style0 = 'text-align:center;align:center; cursor:pointer;cursor:hand;BORDER-TOP: '+bdrcol+' 1px solid; BORDER-RIGHT: '+bdrcol+' 1px solid; BORDER-LEFT: '+bdrcol+' 1px solid; BORDER-BOTTOM: '+bdrcol+' 1px solid';

	document.write( "<table width=100% border=0 cellpadding=0 cellspacing=4 align='center' valign=top>" );

	for( var tnx=0; tnx < thumbnail_images.length; tnx ++ )
	{
		var tn_row = thumbnail_images[ tnx ];

		document.write( "<tr>" );

		for( var tnrx=0; tnrx < tn_row.length; tnrx ++ )
		{
			var tri = tn_row[ tnrx ];

			var uid = get_unique_id();

			// IE will not centre the cell content if width is set
		//	document.write( "<td width="+tnw+" height="+tnh+" id='"+uid+"' onclick='open_gallery_window(\""+tri.n+"\")' onmouseout='lowlight_border(\""+uid+"\")' onmouseover='highlight_border(\""+uid+"\")' class='bstyle bld' align=center valign=middle style='"+tn_border_style0+"'>" );
			document.write( "<td height="+tnh+" id='"+uid+"' onclick='open_gallery_window(\""+tri.n+"\")' onmouseout='lowlight_border(\""+uid+"\")' onmouseover='highlight_border(\""+uid+"\")' class='bstyle bld' align=center valign=middle style='"+tn_border_style0+"'>" );

			var tn_html = tri.get_tn_html( tnw, tnh );

			document.write( tn_html );

			document.write( "</td>" );
		}

		document.write( "</tr>" );
	}

	document.write( "</table>" );
}
//-----------------------------------------------------------------------------------------------------------

function tr_onload()

//-----------------------------------------------------------------------------------------------------------
{
	//alert( "tr_onload()" );

	resizeTo( 850, window.screen.availHeight );



	// Block CERTAIN Events

	document.oncontextmenu = blockEvents;


	// Some images are given default URLs initially, then given their actual URLs *after* the document has loaded

	cause_browser_to_load_actual_images();
}
//-----------------------------------------------------------------------------------------------------------

function tr_gallery_image_onload()

//-----------------------------------------------------------------------------------------------------------
{
	// Block CERTAIN Events

	document.oncontextmenu = blockEvents;



	// Some images are given default URLs initially, then given their actual URLs *after* the document has loaded

	cause_browser_to_load_actual_images();
}
//-----------------------------------------------------------------------------------------------------------

function draw_gallery_image( n )

//-----------------------------------------------------------------------------------------------------------
{
	// The Image Object

	// The Image Name is also the basis for the URL and HTML filename.
	// For the Gallery Image Window, this name can be found in "window.name".

	var tri = gallery_image_objects[n];

	// Determine the SIZE of the IMAGE and TEXT elements

	if( tri )
	{
		var wha = get_inner_dimensions();

		var wmax = wha[ 0 ];
		var hmax = wha[ 1 ];

		wmax = Math.floor( wmax * 0.95 );	// helps with text formatting on the same page
		hmax = Math.floor( hmax * 0.95 );	// helps with text formatting on the same page


		//alert( "wmax = " + wmax + ", hmax = " + hmax );

		var gi_html = tri.get_ga_img_tag_for( wmax, hmax );

		//alert( gi_html );

		//var bdrcol = trcol_lighter;
		//var gi_style = 'BORDER-TOP: '+bdrcol+' 1px solid; BORDER-RIGHT: '+bdrcol+' 1px solid; BORDER-LEFT: '+bdrcol+' 1px solid; BORDER-BOTTOM: '+bdrcol+' 1px solid';

		document.write( "<span style='align:center;margin:4px'>" + gi_html + "</span>" );
	}

	else
	{
		alert( "Could Not Find Image Name '" + n + "'" );
	}
}
//-----------------------------------------------------------------------------------------------------------

function cause_browser_to_load_actual_images()

//-----------------------------------------------------------------------------------------------------------
{
	// Ensure that images only start loading after all the HTML has loaded

	var my_imgs = document.getElementsByTagName( 'img' );

//	var estr = '';

	for( var ix=0; ix < my_imgs.length; ix ++ )
	{
		var my_tag = my_imgs[ ix ];

//		estr += ix.toString() + ': ';
//		estr += my_tag.src;
//		estr += '(';
//		estr += my_tag.name;
//		estr += ')';
//		estr += '\n';

		var img_name = my_tag.name;

		//alert( "cause_browser_to_load_actual_images() - " + img_name );

		if( img_name )
		{
			if( img_name.indexOf( 'JPG' ) > 0 )
			{
				my_tag.src = my_tag.name;
			}

			if( img_name.indexOf( 'jpg' ) > 0 )
			{
				my_tag.src = my_tag.name;
			}

			if( img_name.indexOf( 'GIF' ) > 0 )
			{
				my_tag.src = my_tag.name;
			}

			if( img_name.indexOf( 'gif' ) > 0 )
			{
				my_tag.src = my_tag.name;
			}

			if( img_name.indexOf( 'PNG' ) > 0 )
			{
				my_tag.src = my_tag.name;
			}

			if( img_name.indexOf( 'png' ) > 0 )
			{
				my_tag.src = my_tag.name;
			}
		}
	}

//	alert( estr );

}
//-----------------------------------------------------------------------------------------------------------

function blockEvents( evt )

//-----------------------------------------------------------------------------------------------------------
{
    evt = (evt) ? evt : event;

    var blockit = false;

    var elem = (evt.target) ? evt.target : ((evt.srcElement) ? 
        evt.srcElement : null);

    if (elem && elem.tagName && elem.tagName.toLowerCase( ) == "img") {
        if (evt.cancelBubble) {
            evt.cancelBubble = true;
        }
        alert( "All Images (c) 2009 Terrie Reddish" );
        return false;
    }
}
//-----------------------------------------------------------------------------------------------------------

function highlight_border( id )
{
	var el = document.getElementById( id );

	if( el )
	{
		el.style.borderColor = trcol_dark;
	}
}

function lowlight_border( id )
{
	var el = document.getElementById( id );

	if( el )
	{
		el.style.borderColor = trcol_lighter;
	}
}
//-------------------------------------------------------------------------------------------------------------------------

function get_unique_id()

//-------------------------------------------------------------------------------------------------------------------------

// Returns Next Unique ID In Sequence
// Only use this for elements that, e.g. are changed by JavaScript clicks in the current document, e.g. to edit a field

//-------------------------------------------------------------------------------------------------------------------------
{
	var next_unique_id = "uniqid" + UniqueIDIndex;

	UniqueIDIndex ++;

	return( next_unique_id );
}
//-------------------------------------------------------------------------------------------------------------------------

function get_inner_dimensions()

//-------------------------------------------------------------------------------------------------------------------------
{
	if( window.innerWidth )
	{
		iw = window.innerWidth;
		ih = window.innerHeight;
	}

	else if( document.documentElement && document.documentElement.clientWidth )
	{
		iw = document.documentElement.clientWidth;
		ih = document.documentElement.clientHeight;
	}

	else if( document.body )
	{
		iw = document.body.clientWidth;
		ih = document.body.clientHeight;
	}

	return( new Array( iw, ih ) );
}
//-------------------------------------------------------------------------------------------------------------------------

function which_browser()

//-------------------------------------------------------------------------------------------------------------------------
{
	var browser = "";

	if( navigator.userAgent.indexOf("Firefox") != -1 )
	{
		browser = "FF";
	}

	if( navigator.userAgent.indexOf("MSIE") != -1 )
	{
		browser = "IE";
	}

	return( browser );
}
//-------------------------------------------------------------------------------------------------------------------------


