/************************************************************************************************************/
/* Updates an html img with a new photo, using previous/next commands navigating in a series of pics.
/* The html img must have a name and id defined, eg "diapo"
/* The pictures to display have to be named "diapo_1.jpg", "diapo_2.jpg", "diapo_3.jpg" etc. or
/* "diapo_1_lg.jpg", "diapo_2_lg.jpg", "diapo_3_lg.jpg" where lg is the language of the pic, if the pic contains text.
/*
/* Variables path_photos and nb_photos MUST BE defined in document.diapo object.
/* If required, lg, ind_this_photo (rank of currently displayed picture) and tablabels can also be defined in the object.
/* tablabels should then be a table of strings containing nb_photos elements corresponding to labels that should
/* be displayed for each photo, in a span html field named <name fo the diapo>_label ("diapo_label" in our example).
/*
/* Example of code initiating the object:
document.slide1.tablabels = new Array('Το καμηλάρι','Το σπίτι μας','Το μπαλκόνι του σπιτιού','Ηλιοβασίλεμα στον κόλπο της Μεσαράς','Παραλία του Καλαμακίου');
document.slide1.nb_photos = 5;
document.slide1.path_photos = "./photos/";
/************************************************************************************************************/

// detects browser, to know if fading in / out is possible
function detectBrowser() {
  this.supportLayer = false;
  this.supportDOM   = false;
  this.supportAll   = false;
  this.isGecko      = false;
  this.isOpera      = false;
  this.isOpera5     = false;
  this.isOpera6     = false;
  this.isIE         = false;
  this.isIE4        = false;
  this.isIE5        = false;

  if( document.layers )         { this.supportLayer = true; }
  if( document.getElementById ) { this.supportDOM = true; }
  if( document.all )            { this.supportAll = true; }
  if(( navigator.userAgent ).indexOf( "Gecko" )!=-1 ) { this.isGecko = true; }
  else if(( navigator.userAgent ).indexOf( "Opera" )!=-1 ) {
    this.isOpera = true;
    if(( navigator.userAgent ).indexOf( "Opera 5" )!=-1 ) this.isOpera5 = true;
    else if(( navigator.userAgent ).indexOf( "Opera/5" )!=-1 ) this.isOpera5 = true;
    if(( navigator.userAgent ).indexOf( "Opera 6" )!=-1 ) this.isOpera6 = true;
    else if(( navigator.userAgent ).indexOf( "Opera/6" )!=-1 ) this.isOpera6 = true;
  }
  else if(( navigator.userAgent ).indexOf( "IE" )!=-1 ) {
    this.isIE = true;
    if(( navigator.userAgent ).indexOf( "MSIE 4" )!=-1 ) { this.isIE4 = true; }
	if(( navigator.userAgent ).indexOf( "MSIE 5" )!=-1 ) { this.isIE5 = true; }
	if(( navigator.userAgent ).indexOf( "MSIE 6" )!=-1 ) { this.isIE6 = true; }
  }
}

var browser = new detectBrowser();

// setting previous to true will update the img field named nom_diapo with the previous picture of the <nom_diapo>_*.jpg list.
function previous_next_photo(nom_diapo, previous) {
	tmp = eval("document.SL_" + nom_diapo + "_obj");
	if(tmp==null) {
		return;
	}
	if((tmp.lg==null)) {
    	tmp.lg = "";
	}
	if((tmp.ind_this_photo==null)) {
		tmp.ind_this_photo = (previous==true ? (tmp.nbslides -1) : 1);
	} else {
		tmp.ind_this_photo = (previous==true ? (tmp.ind_this_photo-1) : (tmp.ind_this_photo + 1));
	}
	if(tmp.ind_this_photo >= tmp.nbslides) {
		tmp.ind_this_photo = 0;
	}
	if(tmp.ind_this_photo < 0) {
		tmp.ind_this_photo = tmp.nbslides - 1;
	}
	// change photo, label and legend
	change_photo(nom_diapo,tmp.ind_this_photo);
}

function change_photo(nom_diapo, idnewphoto) {
	tmp = eval("document.SL_" + nom_diapo + "_obj");
	if(tmp==null) {
		return;
	}
	if(tmp.transitiontype==null) tmp.transitiontype='cut';
	switch(tmp.transitiontype) {
		case 'fadeout':
			if(browser.isIE4 || browser.isIE5 || browser.isIE6)
				cut_change_photo(nom_diapo, idnewphoto);
			else
				fade_change_photo(nom_diapo, idnewphoto);
			break;
		case 'cut':
		default:
			cut_change_photo(nom_diapo, idnewphoto);
	}
}

function fade_change_photo(nom_diapo, indnewphoto) {
	tmp = eval("document.SL_" + nom_diapo + "_obj");
	tmp.ind_this_photo = indnewphoto;

	// a default value for fade or cut the legends, in case it is not defined in JS object.
	if(tmp.fadeoutlegends==null) tmp.fadeoutlegends=false;

	imgfg = eval("document." + nom_diapo + "_f");
	imgbg = eval("document." + nom_diapo + "_b");
	if((imgfg!=null) && (imgbg!=null) && (tmp.pixtab!=null)) {
		// moves foreground (current) image to background
		set_opacity(imgbg, 100);
		// here, should probably copy all the style of the image, not only border...
		imgbg.style.border = imgfg.style.border;
		imgbg.src = imgfg.src;

		// sets new pic in foreground, with opacity = 0
		set_opacity(imgfg, 0);
		imgfg.src = tmp.pixtab[indnewphoto].path;

		// alt and title tags of the image are put on the foreground image
		imgfg.alt = tmp.alt[indnewphoto];
		imgfg.title = tmp.title[indnewphoto];
		imgbg.alt = "";
		imgbg.title = "";

		reveal_foreground_image(nom_diapo);
	}

	// change img link is any
	imgfga = document.getElementById(nom_diapo + "_f_a")
	if((imgfga!=null) && (tmp.pixtab!=null)) {
		imgfga.href = tmp.pixtab[indnewphoto].a;
	}

	// for each text field accompanying the image. Same procedure.
	if(tmp.txtfields!=null && tmp.txtfields.length!=0) {
		for(f=0;f<tmp.txtfields.length;f++) {
			labelhtmlfield_bg = document.getElementById(nom_diapo + "_" + tmp.txtfields[f] + "_b");
			labelhtmlfield_fg = document.getElementById(nom_diapo + "_" + tmp.txtfields[f] + "_f");
			tablabels = eval("document.SL_" + nom_diapo + "_obj." + tmp.txtfields[f]);
			if((labelhtmlfield_bg!=null) && (labelhtmlfield_fg!=null) && tablabels!=null) {
				if(tmp.fadeoutlegends) { // fading out labels
					set_opacity(labelhtmlfield_bg, 100);
					labelhtmlfield_bg.className = labelhtmlfield_fg.className;
					labelhtmlfield_bg.innerHTML = labelhtmlfield_fg.innerHTML;
					set_opacity(labelhtmlfield_fg, 0);
					labelhtmlfield_fg.innerHTML = tablabels[indnewphoto];
				} else { // cutting labels
					labelhtmlfield_fg.innerHTML = tablabels[indnewphoto];
				}
			}
		}
	}
}

// function called every x ms to change the opacity of up and down divs
function reveal_foreground_image(nom_diapo) {
	tmp = eval("document.SL_" + nom_diapo + "_obj");
	imgfg = eval("document." + nom_diapo + "_f");
	imgbg = eval("document." + nom_diapo + "_b");

	// Some default values, if not initialized in JS object.
	if(tmp.opacity==null) tmp.opacity=100;
	if(tmp.fadeoutduration==null) tmp.fadeoutduration=1500; // in ms
	if(tmp.fadeoutsteps==null) tmp.fadeoutsteps=100; // must divide 100.

	// define new opacity of fading out image.
	tmp.opacity = tmp.opacity - (100/tmp.fadeoutsteps);
	if((imgfg!=null) && (imgbg!=null)) {
		set_opacity(imgbg, tmp.opacity);
		set_opacity(imgfg, 100 - tmp.opacity);
	}

	// fading out accompanying text fields
	// for each text field accompanying the image. Same procedure.
	if(tmp.fadeoutlegends && tmp.txtfields!=null && tmp.txtfields.length!=0) {
		for(f=0;f<tmp.txtfields.length;f++) {
			labelhtmlfield_bg = document.getElementById(nom_diapo + "_" + tmp.txtfields[f] + "_b");
			labelhtmlfield_fg = document.getElementById(nom_diapo + "_" + tmp.txtfields[f] + "_f");
			tablabels = eval("document.SL_" + nom_diapo + "_obj." + tmp.txtfields[f]);
			if((labelhtmlfield_bg!=null) && (labelhtmlfield_fg!=null) && (tablabels!=null)) {
				set_opacity(labelhtmlfield_bg, tmp.opacity);
				set_opacity(labelhtmlfield_fg, 100 - tmp.opacity);
			}
		}
	}

	// next round...
	if(tmp.opacity>0) {
		setTimeout('reveal_foreground_image("' + nom_diapo + '")',(tmp.fadeoutduration)/100);
	} else {
		tmp.opacity=100;
	}
}

// browser independent opacity set. Opacity between 0 and 100
function set_opacity(obj, opacity) {
	obj.style.filter = 'alpha(opacity=' + opacity + ')';
	norm_opacity = opacity/100;
	norm_opacity = (opacity==0?0.01:norm_opacity);
	norm_opacity = (opacity==100?0.99:norm_opacity);
//	-khtml-opacity: 0.5;
//	-moz-opacity = norm_opacity;
	obj.style.opacity = norm_opacity;
}

// starts automatic change of pictures
function start_diapo(nom_diapo) {
	tmp = eval("document.SL_" + nom_diapo + "_obj");
	if(tmp.changeevery==null) tmp.changeevery=5000;

	strcmd = "previous_next_photo(\"" + nom_diapo + "\",false)";
//	alert(strcmd);
	tmp.idInterval = setInterval(strcmd,tmp.changeevery);
}

// pauses automatic change of pictures
function pause_diapo(nom_diapo) {
	tmp = eval("document.SL_" + nom_diapo + "_obj");
	if((tmp.idInterval==null)) {
		return;
	}

	clearInterval(tmp.idInterval);
	tmp.idInterval = "";
}

// toggles between pause and run slideshow, and updates the pause/play icon if found
// NEEDS PATHs TOWARDS <<, >>, pause andf play icons
// in the html doc, this image must ber named <nom_diapo> + "_startpauseimg
var pause_icon_path = "./resources/pause.jpg";
var play_icon_path = "./resources/play.jpg";

function toggle_pause_start(nom_diapo) {
	tmp = eval("document." + nom_diapo);
	// switch slideshow on
	if((tmp.idInterval==null) || (tmp.idInterval =="")) {
		start_diapo(5000, nom_diapo);
		tmp2 = eval("document." + nom_diapo + "_startpauseimg");
		if((tmp2!=null)) {
			tmp2.src = pause_icon_path;
		}
  } else { // switch slideshow off
		pause_diapo(nom_diapo);
		tmp2 = eval("document." + nom_diapo + "_startpauseimg");
		if((tmp2!=null)) {
			tmp2.src = play_icon_path;
		}
  }
}

function cut_change_photo(nom_diapo, indnewphoto) {
	tmp = eval("document.SL_" + nom_diapo + "_obj");
	tmp.ind_this_photo = indnewphoto;
//alert(nom_diapo);
	// change photo
	img = eval("document." + nom_diapo + "_f");
	if((img!=null) && (tmp.pixtab!=null)) {
		img.src = tmp.pixtab[indnewphoto].path;
		img.alt = tmp.alt[indnewphoto];
		img.title = tmp.title[indnewphoto];
	}

	// change img link if any
	imgfga = document.getElementById(nom_diapo + "_f_a")
	if((imgfga!=null) && (tmp.pixtab!=null)) {
		imgfga.href = tmp.pixtab[indnewphoto].a;
	}

	// label and legend of the image, if fields exist.
	if(tmp.txtfields!=null && tmp.txtfields.length!=0) {
		for(f=0;f<tmp.txtfields.length;f++) {
			tablabels = eval("document.SL_" + nom_diapo + "_obj." + tmp.txtfields[f]);
			labelhtmlfield = document.getElementById(nom_diapo + "_" + tmp.txtfields[f] + "_f");
			if((labelhtmlfield!=null) && (tablabels!=null)) {
				document.getElementById(nom_diapo + "_" + tmp.txtfields[f] + "_f").innerHTML
					= tablabels[indnewphoto];
			}
		}
	}
}

