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

// Functions

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

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

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

// Start: TRImage

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

function TRImage( n, w, h, t )

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

	this.get_img_wh_for		= tri_get_img_wh_for;
	this.get_ga_img_tag_for	= tri_get_ga_img_tag_for;
	this.get_ga_url			= tri_get_ga_url;
	this.get_ga_html		= tri_get_ga_html;
}
//-----------------------------------------------------------------------------------------------------------

function TRCardImage( n, w, h, t )

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

	this.get_img_wh_for		= tri_get_img_wh_for;
	this.get_ga_img_tag_for	= tri_get_ga_img_tag_for;
	this.get_ga_url			= tri_get_ga_url_card;
	this.get_ga_html		= tri_get_ga_html;
}
//-----------------------------------------------------------------------------------------------------------

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( 'lefoto/' + this.n + '.jpg' );
	return( this.n + '.jpg' );
}
//-----------------------------------------------------------------------------------------------------------

function tri_get_ga_url_card()

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

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

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

function tri_get_img_wh_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);
	}

	var who = new Object();
	who.w = w1;
	who.h = h1;

	return( who );
}
//-----------------------------------------------------------------------------------------------------------

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='"+url+"' width="+w1+" height="+h1+" style='border: 1px solid #b2bdbc'>" );
}
//-----------------------------------------------------------------------------------------------------------

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

//-----------------------------------------------------------------------------------------------------------
{
//	return( 'lefoto/' + n + '.htm' );
	return( 'foto.htm' );
}
//-----------------------------------------------------------------------------------------------------------

function open_gallery_window( 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];
	if( !tri ) { return; }

	var hfn = get_gallery_html_filename( n );

//	var xbdr = 25;
//	var ybdr = 10;
	var xbdr = 25;
	var ybdr = 100;

	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,toolbar=0,personalbar=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; }


	// If the image has dimensions less than these, further reduce the width & height


//	alert( "before: ww = " + ww + ", wh = " + wh );


//	alert( "tri.w = " + tri.w + ", tri.h = " + tri.h );


//	if( tri.w < ww ) { ww = tri.w; }
//	if( tri.h < wh ) { wh = tri.h; }

	var who = tri.get_img_wh_for( ww, wh );


//	alert( "who.w = " + who.w + ", who.h = " + who.h );


	if( ww > who.w ) { ww = who.w; }
	if( wh > who.h ) { wh = who.h; }


//	alert( "after: ww = " + ww + ", wh = " + wh );

	//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();
	}


	//alert( "hfn = " + hfn + "\nn = " + n + "\nargs = " + args );


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


	//alert( "global_wo = " + global_wo );



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


/*
	if( global_wo )
	{
		global_wo.focus();



		var wait_count = 0;

		while( global_wo.window.name != n )
		{
			wait_count ++;

			if( wait_count > 10000 ) { global_wo.window.name = n; }
		}


		//alert( "wait_count = " + wait_count );



		//global_wo.window.location.reload();
	}

	else
	{
		alert( 'global_wo is null' );
	}
*/

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

// End: TRImage

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

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

	var UniqueIDIndex = 0;

	var global_wo = 0;

	var browser = which_browser();

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

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

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

function draw_gallery_image( n )

//-----------------------------------------------------------------------------------------------------------
{

	//alert( "draw_gallery_image('" + n + "')" );
	//alert( "defined as gallery_image_objects = " + gallery_image_objects );


	// 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];



	//alert( "draw_gallery_image(" + tri + ")" );



	// Determine the SIZE of the IMAGE and TEXT elements

	if( tri )
	{
		//alert( "tri ok - draw_gallery_image(" + 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 );

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

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

function foto_onload()

//-----------------------------------------------------------------------------------------------------------
{
	// Block right-clicks on elements with image tags

	document.oncontextmenu = blockEvents;


	// Title

	var wn = window.name;


	//alert( "window.name = " + window.name );


	if( !wn )
	{
		alert( "no window.name" );

		window.location.reload();
	}



//	document.title = "Loading " + wn;


	//alert( "window.name = " + wn );


//	wn = 'DuomoFirenze';



	//alert( 'got here window.name = ' + window.name );



//	draw_gallery_image( wn );

	var elgi = document.getElementById( 'galleryimage' );


	//alert( "elgi = " + elgi );


	if( elgi )
	{
		//elgi.innerHTML = "Loading Image '" + n + "'...";


		//elgi.innerHTML = "test: window.name = " + wn;


		// 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[wn];



		// 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
			wmax = Math.floor( wmax * 0.90 );	// helps with text formatting on the same page
			hmax = Math.floor( hmax * 0.90 );	// 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 gi_html1 = "<span style='align:center;margin:1px;'>" + gi_html + "</span>";

			gi_html1 += "<br>" + "<span class='topmenu1'>" + tri.t + "</span>";

			//alert( gi_html1 );

			elgi.innerHTML = gi_html1;
		}

		else
		{
			elgi.innerHTML = "Could Not Find Image Name '" + n + "'";
		}


	}	// if( elgi )


	var ttt = wn; var tri = gallery_image_objects[wn]; if( tri && tri['t']) { ttt = tri.t; }
	document.title = ttt;



	//window.location.reload( false );

	//window.focus();

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

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) 2011 Helena Broome" );
        return false;
    }
}
//-----------------------------------------------------------------------------------------------------------

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()

//-------------------------------------------------------------------------------------------------------------------------
{
	var iw = 0;
	var ih = 0;

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

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

	else if( !iw && 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 );
}
//-------------------------------------------------------------------------------------------------------------------------


