/*
Name: galleryViewer.js
Author: Daniel Pan -- http://www.dowebbiz.com
Description: contains javascript functions used for displaying pictures
Date: 3/5/2004
*/

/*-------------------------------
Player variables
--------------------------------*/

var slideShow = 0;
var pause = 2000;	//this controls the flow speed of the autoplay procedure
var currentIdx=0;
var imageList;
var size = 0;
var prev=0;
var next=0;

//function showPhotos
//it lets the user to browse all images of the activity selected
//Parameters: direction, it is tells whether goes forward or backward
	
function showPhotos(direction){
        currentIdx = getIndex(currentIdx, size, direction);
 
        source = imageList[currentIdx];
        //alert(currentIdx);
        //alert(imageList[currentIdx]); 
 
        document.photo.src = source;

        //----------> show friendly message to user if message reach to the end or the begining
        if(direction==1){//going forward
                if(currentIdx==(size-1)){
			setTimeout("stopIt()", pause);
                        if(slideShow==0){                       
                                alert("This is the last image. Click Next to restart from the begining.");
                        }
                        else{
                                alert("This is the last image.");
                                //stopIt();
                        }
                }
        }
        else{
                if(currentIdx==0){
                        alert("This is the first image. Click Previous will go the last image.");
                }
        }
}

/*
  findObj
  Params:
	- name, the id or object name as a string
	- doc, page document object
  Return:
	the Object found, otherwise false 
*/

function findObj(name, doc) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

/*
  preloadImages
  It retrieves all arguments using the JavaScript function arguments built-in array		
*/

	
function preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.pic) d.pic=new Array();
    var i,j=d.pic.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.pic[j]=new Image; d.pic[j++].src=a[i];}}
}

//function to stop the autoplay process


function stopIt(){
	slideShow = 0;
}
	
//function to start an autoplay procedure
//It uses a recursive call to accomplish this
	
function autoPlay(){
	slideShow =1;
	play();
}
	
function play(){
	if(slideShow == 1){
		showPhotos(1);
		setTimeout("play()", pause);
	}
}
	
//function to return the right image index
	
function getIndex( curr, size, val){
	curr = curr + val;
	if(curr < 0){
		curr = size - 1;
	}
	else if( curr == size){
		curr = 0;
	}
	return curr;
}

/*
showFirstPhoto
to display the first photo
*/

function showFirstPhoto(){
        source = imageList[0];
 
        document.photo.src = source;
        currentIdx =0;
}

/*
showLastPhoto
to display the last photo
*/

function showLastPhoto(){
        source = imageList[(size-1)];
 
        document.photo.src = source;
        currentIdx = size-1;        
}

/*
getCurrentImage
set image source
*/
function setImage(){
	
	if(size>0) document.photo.src=imageList[currentIdx];
}

/*
showViewer
writes the viewer gui code to the document object
*/

function showViewer(){

var viewer=new String();
viewer += '<div id="viewer">';
viewer += '<table bgcolor="#F2F5F6" border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#5D5D5E" width="500">';
viewer += '<tr>';
viewer += '<td align="center">';
viewer += '<img src="../css/playerControls.jpg" width="450" height="41" usemap="#Map" border="0">';
viewer += '<map name="Map">';
viewer += '<area onclick="showFirstPhoto()" shape="rect" coords="44,11,95,40" href="#point" alt="Display to first image" title="Display to first image">';
viewer += '<area onclick="showLastPhoto()" shape="rect" coords="99,11,141,43" href="#point" alt="Display last image" title="Display last image">';
viewer += '<area onclick="autoPlay()" shape="rect" coords="150,12,228,40" href="#point" alt="Start AutoPlay" title="Start AutoPlay">';
viewer += '<area onclick="stopIt()" shape="rect" coords="230,12,270,40" href="#point" alt="Stop AutoPlay" title="Stop AutoPlay">';
viewer += '<area onclick="showPhotos(-1)" shape="rect" coords="281,12,360,43" href="#point" alt="Display Previous image" title="Display Previous image">';
viewer += '<area onclick="showPhotos(1)" shape="rect" coords="361,12,409,40" href="#point" alt="Display Next image" title="Display Next image">';viewer += '</map>';
viewer += '</td>';
viewer += '</tr>';
viewer += '<tr bgcolor="#F2F5F6">';
viewer += '<td align="center" bgcolor="#F2F5F6">';

viewer += '<img align="center" src="../css/blank.jpg" name="photo"/>';
viewer += '</td>';
viewer += '</tr>';
viewer += '</table>';
viewer += '</div>';

document.write(viewer);

setImage();
}
