/*************************************************************************
*                         COPYRIGHT NOTICE                               *
*                                                                        *
*   The contents of this file is protected under the United States       *
*   copyright laws as an unpublished work, and is confidential and       *
*   proprietary to Planetography.  Its use or disclosure in whole or in  *
*   part without the expressed written permission of Planetography. is   *
*   prohibited.                                                          *
*                                                                        *
*   (c) Copyright 2003-2007 by Planetography.  All rights reserved.      *
**************************************************************************/

var IMAGE_COUNT = 8; // number of images, must be more than 1
var VIEW_TIMER = 3000;
var FADE_OUT_TIMER = 20;
var FADE_IN_TIMER = 20;
var OPACITY_INCREASE = 5;
var OPACITY_DECREASE = 7;

var ie5 = (document.getElementById&&document.all);
if (!ie5) {
/*
OPACITY_INCREASE = 100;
OPACITY_DECREASE = 100;
VIEW_TIMER = 3000;
*/
}
var MAX_OPACITY = 96;  // Netscape flashes at 98

var ANIMATE_DELAY = 1500;  // delay before start of first animation

var currentTimer;
var currentOpacity = 0;

// Randomize start image
var visible_image_number = -1; // a seed that random will not choose

var old_visible_image_number;

// put in platform dependent some time
var isIE = navigator.appName.indexOf("Microsoft") != -1;
var isNetscape = navigator.appName.indexOf("Netscape") != -1;



function start_fadeout_timer ()
{
//alert ("start fadeout timer");
	currentTimer = setTimeout ("reduce_opacity ()", FADE_OUT_TIMER)
}

function start_fadein_timer ()
{
//alert ("start fadein timer");
	currentTimer = setTimeout ("increase_opacity ()", FADE_IN_TIMER)
}

function start_view_timer ()
{
//alert ("start view timer");
	currentTimer = setTimeout ("start_fadeout_image ()", VIEW_TIMER)
}

function stop_interval_timer ()
{
//alert ("stop interval timer");
//clearTimeout (currentTimer);

//clearInterval (currentTimer);
}

function select_image ()
{
	old_visible_image_number = visible_image_number;
	while (true) {
		visible_image_number = Math.round(Math.random() * (IMAGE_COUNT-1));
		if (visible_image_number != old_visible_image_number) {
			break;
		}
	}
//visible_image_number = 0;
//alert ("Image is "+visible_image_number);

// Netscape does not set the initial opacity correctly in the CSS file, 
// set the image no opacity before the show
// In IE, if the show() comes after the set_opacity(), for some reason the images are not visible on the first
// pass through them.
	currentOpacity = 0;
	setTopPosition("Div" + visible_image_number,"0px");
	show ("Image"+visible_image_number);
	set_opacity ("Image" + visible_image_number,currentOpacity);
	start_fadein_timer ();
}


function reduce_opacity ()
{
	if (currentOpacity <= 0){
//		stop_interval_timer ();
//alert ("opacity reduced to zero div #: "+visible_image_number);
		setTopPosition("Div" + visible_image_number,"-640px");
//setTopPosition("Div" + visible_image_number,"50px");

		hide ("Image" + visible_image_number);
		select_image ();
		return;
	}
	currentOpacity -= OPACITY_DECREASE;
	if (currentOpacity < 0) {
		currentOpacity = 0;
	}
	set_opacity ("Image" + visible_image_number, currentOpacity);
	start_fadeout_timer ();
}

//var objectToSet;
//function SO (id,op) {
//		objectToSet.style.opacity = (op/100); 
//}

//function noop ()
//{
//	set_opacity ("Image" + visible_image_number,currentOpacity);
//}

function increase_opacity ()
{
	if (currentOpacity >= MAX_OPACITY){
//		stop_interval_timer ();
//alert ("reached full opacity");
		start_view_timer ();
		return;
	}
	currentOpacity += OPACITY_INCREASE;
	if (currentOpacity > 100) {
		currentOpacity = 100;
	}
	set_opacity ("Image" + visible_image_number,currentOpacity);
//	setTimeout ("noop ()", 1);
	start_fadein_timer ();
}

function start_fadeout_image ()
{
//	stop_interval_timer ();
	start_fadeout_timer ();
}


// This function is needed to avoid the jerky motion of the planetography globe loading.  It
// sets the "src" address for the globe ("logo") once this particular frame is loaded.  We are
// assuming that this is the last frame loaded, since it is the biggest.
//
// After setting the 'src' directory, it calls prepare_scroller to get the small images to start
// appearing.
function body_loaded ()
{
//alert ('inside body_loaded');
	trigger_logo ();
	// Set a mouse click handler on each of the images.
	set_onclick_handler ("Image0");
	set_onclick_handler ("Image1");
	set_onclick_handler ("Image2");
	set_onclick_handler ("Image3");
	set_onclick_handler ("Image4");
	set_onclick_handler ("Image5");
	set_onclick_handler ("Image6");
	set_onclick_handler ("Image7");
	window.setTimeout('delay_complete ();',ANIMATE_DELAY);
//alert ('inside body_loaded - after trigger_logo');
}

function delay_complete  () {
	select_image ();
}