document.observe("dom:loaded", function() { /**    Event.observe(window,'load',fn);
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  removed from lightbox.js ...  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	document.observe('dom:loaded', function () { this.lightBox = new Lightbox(); }); this <== WindowObject
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  removed from lightbox.js ...  %%%%%%%%%%%%%%%%%%%%%%%%%%%**/
  try { var ui = {};

	if (document.location.hostname.indexOf('www.') != 0) {
		if ( document.location.port && document.location.port.match("10088")) ;
		else {
			document.domain = "jaguartrack.org";
			document.location.hostname = "www.jaguartrack.org";
			setInterimStatus("redirecting to: " + document.location.hostname);
	}   }

	
	var request =
	  new Ajax.Request('controller/getSingletonConfig.php'
		  ,{method:'post' ,asynchronous:false ,evalJSON:true ,evalScripts:false 
		    ,parameters: $H({arg:'framework'}).toQueryString()
		    ,onComplete: function(transport) {ui.framework = transport.responseJSON.singleton;
			 								  if (ui.framework != null && ui.framework.match(/lightbox/))
											  	  window.lightBox = new Lightbox();
											  else
											  if (! ui.framework ||
											  	  (! ui.framework.match(/lightbox/) && ! ui.framework.match(/shadowbox/))) {
											  	  throw new Error('Gallery framework not found in config file');
											  }
											  
											  ui.debug = transport.responseJSON.debug;
											  ui.ptblcols = transport.responseJSON.ptblcols;
		 								     }
		    ,onFailure:  function(transport) {setInterimStatus(transport.status+': '+transport.statusText);}
		   });
	if (request == null) throw new Error('1) Is JavaScript enabled in browser? 2) Check/Adjust domain name in address bar, http://<strong style="color:red">www.</strong>jaguartrack.org/');


	new Ajax.Request('controller/getPageComponents.php'
	   			     ,{method:'post' ,asynchronous:true ,evalJSON:true //,parameters: $H({}).toQueryString()
	   			       ,onCreate:    function()		     {var statusID = $('status');
													      statusID.defaultContent = statusID.innerHTML;
	   			       								      $('content').hide();
	   			       								     }
					   ,onSuccess:  (function() 		 {housekeeping.call(this);}).bind(ui)
	   			       ,onComplete: (function(transport) {handleJSONResponse.call(this,transport);}).bind(ui)
					   ,onFailure:   function(transport) {setInterimStatus(transport.status+': '+transport.statusText);}
	   			      });
  }
  
  catch( e ) {
  	$('status').innerHTML = e;
  }
  
});

function housekeeping() {
	this.SB = window.Shadowbox;
	this.settings = { containerID		:'pFrame',
	                ix                  : 0,
	                galleryCnt          : 0,
	                infoBlock           : null,
					statusID			:'status',
					processUrl			:'controller/getAnotherImage.php',
					statusSuccessColor	:'#99ff99',
					statusErrorColor	:'#ff9999' };
//	setInterimStatus('Retrieving ...');
}
function handleJSONResponse (response) {
	var local = response.responseJSON;
	// local.rightContainer.scan(/<li>[\s\S]+?<\/li>/i,function(m){alert(m[0]);});
    // local.nav.scan(/<li id="submenu1_\d"[^>]*?blackbox.*?>.*?<ul.*?<.ul>/i,function(x){alert(x[0]);});
	var el 	  = Builder.node('ul',{id:'feedBoxList'});
	el.innerHTML = '<li id="feedHead" class="fold"><h4>Articles</h4></li>'+local.element2;

	$('nav').            update( local.nav );
	$('left-container'). update( local.leftContainer );
	$('right-container').update( local.rightContainer );
	$('element1').       update( local.element1 );

	$('feedBox').		 insert(el);

	this.SB.setup( "a.jump" ,{} ); // search document for anchors w/ class{jump} ... and display in shadowbox iframe
	if (this.framework.match(/shadowbox/))
		this.SB.setup( 'a[rel^="lightbox"]', {} );

	setOnloadBehavior.call(this);
	setStatus('Done ...');
}
function setOnloadBehavior() {

	addCommonHandler.call(this);

	manageFrontImage.call(this);

	if(!NiftyCheck())  return;
	$$("div#box"	   ).each( function() {Rounded(             "div#box","#000"   ,"#0f0f0f"	);});
	$$("div#messageBd" ).each( function() {Rounded(       "div#messageBd","#1a1a1a","#fc3"		);});
	$$("div#pWrap"     ).each( function() {Rounded(           "div#pWrap","#0f0f0f","#000"		);});
	$$("div#storyBoard").each( function() {Rounded(      "div#storyBoard","#1a1a1a","#424242"	);});
	$$("div.gradient"  ).each( function() {Rounded(        "div.gradient","#1a1a1a","#b8b8b8"	);});
	$$("div#feedBox"   ).each( function() {Rounded(         "div#feedBox","#000"   ,"#0f0f0f"	);});
}
function manageFrontImage() {
	
	this.pFrame 	= this.settings.containerID;
	this.theImage	= null;
	var el  		= $(this.pFrame);
	this.infoBlock  = $('iBlock');

	if (this.updater)  { this.updater.stop(); }

	new Ajax.Request('controller/getSingletonConfig.php'
		,{method:'post' ,asynchronous:false ,evalJSON:true ,evalScripts:false 
		  ,parameters: $H({arg:'image_retrieval_mode'}).toQueryString()
		  ,onComplete: (function(transport) {transport.responseJSON.singleton.scan(/last(\d+)/i,(function(m){this.settings.galleryCnt=m[1];}).bind(this));
											 if (this.settings.galleryCnt>0) {
											   var min=0, max=this.settings.galleryCnt;
											   this.settings.ix=Math.floor(Math.random() * (max - min + 1)) + min;
											 }  
											}).bind(this)
		  ,onFailure:  (function(transport) {this.settings.ix=0;this.settings.galleryCnt=-1;}).bind(this)
		 });

	if (this.settings.galleryCnt>0) {
	    //var color = '#1a1a1a';
	    //this.infoBlock.setStyle( {border:'1px solid'+color} );
		this.infoBlock.update("<h6>"+(this.settings.ix+1)+"/"+this.settings.galleryCnt+"</h6>");
	}

	new Ajax.Updater( el ,this.settings.processUrl
	   			     ,{method:'post' ,asynchronous:true 
                       ,parameters: $H({arg:this.settings.ix++}).toQueryString()
	   			       ,onSuccess: function(){ $('content').show(); }
	   			       ,onComplete:handleImageGetSuccess.bind( this )
	   			      } );

	this.updater = new PeriodicalExecuter( 
						(function() {
						   if (this.settings.galleryCnt>0) {
							 this.infoBlock.update("<h6>"+((this.settings.ix % this.settings.galleryCnt)+1)+"/"+this.settings.galleryCnt+"</h6>");
						   }
						   new Ajax.Updater( el ,this.settings.processUrl
											  ,{method:'post' 
												,parameters: $H({arg:this.settings.ix++}).toQueryString()
												,asynchronous:true 
												,onComplete:handleImageGetSuccess.bind( this )} );
						}).bind(this) ,8);
/** callback functions are called BEFORE the DOM is updated -not good ... in this case!
	this.updater = 
	 new Ajax.PeriodicalUpdater( el ,this.settings.processUrl
							    ,{method:'post'
							      ,asynchronous:true
							      ,frequency: 8
							      ,onComplete: handleImageGetSuccess.bind( this )
							      ,onFailure: handleImageGetFailure} 
							   );
**/
}
function releaseTimer( timer_id ) {

	window.clearInterval(timer_id);
}
function handleImageGetSuccess( ) {

	var watch 		= this.DOMObserver;
	var pFrame		= $(this.pFrame);
	var box 		= pFrame.up().getDimensions();
	var theImage	= pFrame.down('img');
	var xFileN 		= theImage.identify();
	var fudge 		= 6;
	var scaleFactor = 0.90;
	var fileInfo	= [];
	var ii 			= {};

	/**%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%% Each new slide pic needs to be added to the element watchlist ... %%%%%%%%%%%%%%%
	%%%% ... remove previous pic %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%**/
	if (this.theImage) watch.removeItem(this.theImage);
	this.theImage = new DocImage(theImage.up('A'),watch);

	xFileN.scan( /(.*?)\?(\d+)X(\d+)/
				,function(match) { for (i=0; i<3; i++) {fileInfo.push( match[i+1] );} } );	
	ii.image = fileInfo[0];
	ii.width = fileInfo[1];
	ii.height= fileInfo[2];
	ii.aspect= ii.width/ii.height;

	var scale,nW,nH;
	var vector = ii.width;  
	var vectorMax = box.width; 
	var shrink=scaleFactor*vectorMax/vector;
	if ( (scale=vector/vectorMax) > scaleFactor ) {
		nW = Math.round( ii.width*shrink );
		nH = Math.round( nW / ii.aspect);
		ii.width=nW; ii.height=nH;
	}
	vector	= ii.height;  vectorMax = box.height; shrink=scaleFactor*vectorMax/vector;
	if ( (scale=vector/vectorMax) > scaleFactor ) {
		nH = Math.round( ii.height*shrink);
		nW = Math.round( nH * ii.aspect );
		ii.width=nW; ii.height=nH;
	}
	
	var hrzMargin  = Math.round( (box.width -ii.width)/2  );
	var vertMargin = Math.round( (box.height-ii.height)/2 );
	theImage.setStyle( {margin:vertMargin+'px '+hrzMargin+'px',width:nW+'px', height:nH+'px'} );

	new Effect.Opacity( theImage, {duration:0.9,from:0.0,to:1.0} );
	setTimeout("new Effect.Opacity($('pFrame').down('img'), {duration:2.5,from:1.0,to:0.0} );", 5490);
	setStatus( ii.image + ' retrieved' );

/** %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% **/
/** %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% **/
/** %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% **/
/** %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% **/
/** %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% **/
/** %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	window.lightBox.updateImageList();
/** %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% **/
/** %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% **/
/** %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% **/
/** %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% **/
/** %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% **/

}
function handleImageGetFailure( ) {	setStatus( 'Error retrieving image', true ); }

