// Set up the slideshow
var stillxfading = false;

// Speed in ms (1000 ms = 1 sec)
var slidespeed=6000;
// Set to false for starters, since we change this in the first call
var keepplaying=false;

// Ie wasn't happy with these in a function. ;-) 
var playpause=new Array();
var first=0;
var current=0;

var playbutton=document.getElementById('playbttn');

var imagenames = new Array();
// Kick it all off here
function startup(inarray){
    stillxfading = true;
    imagenames=inarray;
    imagearray=new Array();
    swappy=document.getElementById('slidy');
    playbutton=document.getElementById('playbttn');

    // Fade in magic
    setOpacity(swappy,100);
    swappy.style.visibility = 'visible';
    fadeIn('slidy',0);

    //for (var i=0; i < imagenames.length; i ++){
    for (var i=0; i < 3; i ++){ // Load three 
	loadimg(i);
    }
    last=imagearray.length - 1;

    // Load play and pause buttons into array
    var tempobj=new Image();
    tempobj.onload=function(){
	playpause[0]=tempobj; 
    }
    tempobj.src="http://Elements-by-Caroline-Photography.com/magic/controls//play.png";

    var tempobj2=new Image();
    tempobj2.onload=function(){
	playpause[1]=tempobj2; 
    }
    tempobj2.src="http://Elements-by-Caroline-Photography.com/magic/controls/pause.png";

    play();
}

// Load each image
function loadimg(whichimg){ 
    // Preload the images into the object array
    var tempobj=new Image();

    tempobj.onload=function(){
	imagearray[imagearray.length]=tempobj;
    }
    tempobj.src=imagenames[whichimg];
}

function back() {
    keepplaying=false;
    if(playpause[1] != null) playbutton.src=playpause[1].src; 
    last=imagearray.length - 1;
    current=current - 1;
    if (current < first){
	current=last;
    }
    if (imagearray[current] != null) crossfade(imagearray[current].src);
}

function fwd() {
    keepplaying=false;
    if(playpause[1] != null) playbutton.src=playpause[1].src; 
    last=imagearray.length - 1;
    current=current + 1;
    if (current > last){
	current=first;
    }
    if (imagearray[current] != null) crossfade(imagearray[current].src);
}



// Set the show to play or pause
function play(){
    keepplaying = !(keepplaying);
    if(keepplaying){
	slide();
	if(playpause[0] != null) playbutton.src=playpause[0].src; 
    }
    else{
	if(playpause[1] != null) playbutton.src=playpause[1].src; 
    }
}

// Next four are fade magic functions
function setOpacity(obj, opacity) {
    opacity = (opacity == 100)?99.999:opacity;

    // Safari<1.2, Konqueror
    obj.style.KHTMLOpacity = (opacity/100);

    // Older Mozilla and Firefox
    obj.style.MozOpacity = (opacity/100);

    // Safari 1.2, newer Firefox and Mozilla, CSS3
    obj.style.opacity = (opacity/100);

    // IE/Win
    obj.style.filter = "alpha(opacity="+opacity+")";
}

function fadeIn(objId,opacity) {
    if (document.getElementById) {
	obj = document.getElementById(objId);


	if (opacity <= 100) {
	    setOpacity(obj, opacity);
	    opacity += 10;
	    window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 100);
	}
	else{
	    stillxfading = false;
	}
    }
}

function fadeOut(objId,opacity) {
    if (document.getElementById) {
	obj = document.getElementById(objId);
	if (opacity >= 0) {
	    setOpacity(obj, opacity);
	    opacity -= 10;
	    window.setTimeout("fadeOut('"+objId+"',"+opacity+")", 100);
	}
	else{
	    swappy.src=imagearray[current].src; 
	    fadeIn('slidy',0);
	}
    }
}

function crossfade(newsrc){
    // we don't want two fades going on at once. It gets confusing and flickers
    if(!stillxfading){
	stillxfading = true;
	fadeOut('slidy',99.99);
    }
}

function slide(){

    if(keepplaying){
	// load another image if all not loaded
	if(imagearray.length != imagenames.length){
	    loadimg(imagearray.length); 	
	} 

	last=imagearray.length - 1;
	current=current + 1;
	if (current > last){
	    current=first;
	}
	if (imagearray[current] != null) crossfade(imagearray[current].src);
	setTimeout('slide()',slidespeed);
    }
}

// On fade in only pages
function fadeinonly(){
    swappy=document.getElementById('slidy');
    setOpacity(swappy,100);
    swappy.style.visibility = 'visible';
    fadeIn('slidy',0);
}

// Menu stuff
latestmenu =  "";
pushplaying = false;
function hovermenu(whatmenu){
    //Swap HTML
    latestmenu = whatmenu + 'menu';
    tmpmenu = document.getElementById(latestmenu);
    tmpmenu.style.visibility = 'visible';

    // Stop slideshow to prevent baddness
    pushplaying = keepplaying;
    keepplaying = false;
}
function menunormal(){
    //Swap HTML
    tmpmenu = document.getElementById(latestmenu);
    tmpmenu.style.visibility = 'hidden';

    //Resume playing
    if(pushplaying){
	play();
    }
}

var infobox;
var infoopen = false;

function expand(){
    if(infoopen){
	shrink();
    } else {
	infobox = document.getElementById("container");
	var curwidth = infobox.offsetWidth;
	if(curwidth < 880){
	    infobox.style.width = curwidth + 10 + "px";
	    setTimeout("expand()",1);
	} else {
	    infoopen = true;
	    document.getElementById('slidecontrol').src = "http://elements-by-caroline-photography.com/magic/controls/close.png"; 
	}
    }
}

function shrink(){
    var curwidth = infobox.offsetWidth;
    if(curwidth > 619){
	infobox.style.width = curwidth - 10 + "px";
	setTimeout("shrink()",1);
    } else {
	infoopen = false;
	document.getElementById('slidecontrol').src = "http://elements-by-caroline-photography.com/magic/controls/open.png"; 
    }
}
