/*
This file controls the Modular slideshow on the bottom-right corner of the homepage.
At least for now, the selection of photos in the slideshow is hard-coded in this file;
it's not read dynamically from a directory.
*/

// Modular Slideshow
//--------------------

var mo_photos_ary = new Array();

var mo_disappearingIndex;   // index in the array of the picture that is about to 
                            // disappear (or disappearing, fading out) at any given moment.
var mo_appearingIndex;  // index in the array of the picture that is about to 
                        // appear (or appearing, fading in) at any given moment.

var mo_fadeOutTimer;    // rate at which the disappearing photo's opacity is changed when it's fading out.
var mo_fadeInTimer;     // rate at which the appearing photo's opacity is changed when it's fading in.

var mo_fadeOutID;   // the HTML ID representing the container of the disappearing photo
var mo_fadeInID;    // the HTML ID representing the container of the appearing photo

var mo_smoothOrder = 0;	//may be 0 or 1... changing this value reverses the order of the fading

var theBrowser = "";    //used below to detect which brower is being used by the visitor


initializeAllPhotosArrays();

/*
function startAllSlideshows()
{
    mo_startSmoothSlideshow();
}
*/

function initializeAllPhotosArrays()//This shouldn't be here. og_initializePhotosArray should be called directly above
{
    //alert('Initializing all photo arrays');
    mo_initializePhotosArray();
}

/*
Fill the array with the file names of the pictures in the slideshow and assign fading order
*/
function mo_initializePhotosArray()
{
	mo_photos_ary[0] = new Image();
	mo_photos_ary[1] = new Image();
	mo_photos_ary[2] = new Image();
	mo_photos_ary[3] = new Image();
	mo_photos_ary[4] = new Image();
	
	mo_photos_ary[0].src="../images/slideshows/modular/ss_mo1.JPG";
	mo_photos_ary[1].src="../images/slideshows/modular/ss_mo2.JPG";
	mo_photos_ary[2].src="../images/slideshows/modular/ss_mo3.JPG";
	mo_photos_ary[3].src="../images/slideshows/modular/ss_mo4.JPG";
	mo_photos_ary[4].src="../images/slideshows/modular/ss_mo5.JPG";

	//The above will be in a loop which reads the files in a specific folder.
	//Therefore, we'll need to check whether there's anything in the array after that
	//with something like: if(photos_ary.length > 1)
	//(the folder could be empty)

	mo_disappearingIndex = 0;
	mo_appearingIndex = 1;
}


//prepare the next picture in the slideshow
function mo_updateSlideshowIndex()
{
    //update the indices of the appearing and disappearing photos
	mo_disappearingIndex++;
	mo_appearingIndex++;

	if(mo_appearingIndex == mo_photos_ary.length)
	{
		mo_appearingIndex = 0;  //wrap around for the appearing photo
	}
	
	if(mo_disappearingIndex == mo_photos_ary.length)
	{
		mo_disappearingIndex = 0;   //wrap around for the disappearing photo
	}
}


//store the HTML ID representing the container of the picture that will fade out
function mo_setFadeOutID(theID)
{
//		alert("mo_fadeOutID = "+mo_fadeOutID);
	mo_fadeOutID = theID;
}

//store the HTML ID representing the container of the picture that will fade in
function mo_setFadeInID(theID)
{
//	alert("mo_fadeInID = "+mo_fadeInID);
	mo_fadeInID = theID;
}


/*
Fades one pair of pictures, prepares the next pair, sets a time delay and then calls itself
*/
function mo_startSmoothSlideshow()
{
    detectBrowser();
    
    document.getElementById("mo_topLayerPhoto").src = mo_photos_ary[mo_disappearingIndex].src;
	document.getElementById("mo_projShowCol").style.backgroundImage = "url("+mo_photos_ary[mo_appearingIndex].src+")";

    if(theBrowser == "Not IE")
    {
        document.getElementById("mo_topLayerPhoto").style.opacity = 1;
    }
    else
    {
        document.getElementById("mo_topLayerPhoto").filters.alpha.opacity = 100;
    }
	
    //Fade one pair of pictures
	if(mo_smoothOrder == 0)
	{
		mo_setFadeOutID('mo_topLayerPhoto');
		mo_fadeOut();
	}
	else
	{
    //
	}
	//prepare the next pair of pictures to fade
	mo_updateSlideshowIndex();
	
	//set time delay and make recursive call 
	setTimeout("mo_startSmoothSlideshow()",4000);
}
/********************/
//Stop the timer that fades out a picture
function mo_stopFadingOut()
{
	if(mo_fadeOutTimer)
	clearTimeout(mo_fadeOutTimer);
}

//Stop the timer that fades in a picture
function mo_stopFadingIn()
{
	if(mo_fadeInTimer)
	clearTimeout(mo_fadeInTimer);
}


//fades out a picture
function mo_fadeOut()
{
    if(theBrowser == "Not IE")
    {
        //reduce opacity
		document.getElementById(mo_fadeOutID).style.opacity += 0.001;
		document.getElementById(mo_fadeOutID).style.opacity *= 0.9;        
		
		//if opacity is negligible
		var checkFF = document.getElementById(mo_fadeOutID).style.opacity;
		if(checkFF < 0.05)
		{
			//make the opacity equal to zero
			document.getElementById(mo_fadeOutID).style.opacity = 0;
			checkFF = 0;
			
			//stop the fade-out process
			mo_stopFadingOut();
			return;
		}
    }
    else
    {
        //reduce opacity
		document.getElementById(mo_fadeOutID).filters.alpha.opacity += 0.001;
		document.getElementById(mo_fadeOutID).filters.alpha.opacity *= 0.9;
		
		//if opacity is negligible
		var checkIE = document.getElementById(mo_fadeOutID).filters.alpha.opacity;
		if(checkIE < 5)
		{
			//make the opacity equal to zero
			document.getElementById(mo_fadeOutID).filters.alpha.opacity = 0;
			checkIE = 0;
			
			//stop the fade-out process
			mo_stopFadingOut();
			return;
		}
        
    }
        //time delay and recursive call
		mo_fadeOutTimer = setTimeout("mo_fadeOut()",50);
}
/*********************/

//fades in a picture
function mo_fadeIn()
{
        //increase opacity
		document.getElementById(mo_fadeInID).style.opacity += 0.01;
		document.getElementById(mo_fadeInID).style.opacity *= 1.1;
		
		//if opacity is close to 1 (100%)
		var checkFF = document.getElementById(mo_fadeInID).style.opacity;
		if(checkFF > 0.99)
		{
		    //make the opacity equal to 1
			document.getElementById(mo_fadeInID).style.opacity = 1;
			checkFF = 1;
			
			//stop the fade-in process
			mo_stopFadingIn();			
			return;
		}
		
		
		//increase opacity
		document.getElementById(mo_fadeInID).filters.alpha.opacity += 1;//this one is different (not 0.001) on purpose
		document.getElementById(mo_fadeInID).filters.alpha.opacity *= 1.1;
		
		//if opacity is close to 100 (%)
		var checkIE = document.getElementById(mo_fadeInID).filters.alpha.opacity;
		if(checkIE > 99)
		{
		    //make the opacity equal to 100 (%)
			document.getElementById(mo_fadeInID).filters.alpha.opacity = 100;
			checkIE = 100;
			
			//stop the fade-in process
			mo_stopFadingIn();			
			return;
		}
		//time delay and recursive call
		mo_fadeInTimer = setTimeout("mo_fadeIn()",50);
}


/*
    starts a slideshow that has a hard transition, instead of a smooth one.
    
    (photos don't fade in and out... the just swap instantaneously)
*/
function mo_startSwapSlideshow()
{
    document.getElementById("mo_topLayerPhoto").src = mo_photos_ary[mo_disappearingIndex].src;
	document.getElementById("mo_projShowCol").style.backgroundImage = "";

	mo_setFadeOutID('mo_topLayerPhoto'); 

	mo_updateSlideshowIndex();
	setTimeout("mo_startSwapSlideshow()",4000);
}


/*
    Detect whether the visitor is using Internet Explorer or a different browser.
*/
function detectBrowser()
{
    if(navigator.appVersion.indexOf("MSIE") == -1)
    {
        theBrowser = "Not IE";
    }
    else
    {
        theBrowser = "IE";
    }
}