
// PhotoShow 1.0 Coded by Christophe Braun / Netsquad, chris @t netsquad.ch

photoshow=true;

var slideImgs;
var curImg=0;
var slideDelay=5000;
var gallery;

function butState(name,state){
	obj=getObj(name);
	if (obj.style && obj.style.filter) {
	  	var curFilter=obj.style.filter;
	  	obj.style.filter=curFilter.replace(/_[ovf]+\./,"_"+state+".")
	} else {
  		var curSrc=obj.src;
  		obj.src=curSrc.replace(/_[ovf]+\./,"_"+state+".");
	}
}

function startSlideShow() {
	curImg=0;
	gallery=getObj("PhotoShowGallery");
	if (gallery && gallery.style) {
		gallery.style.backgroundColor='';
		slideImgs=gallery.getElementsByTagName("IMG");
		if (slideImgs && slideImgs.length>0) {
			showImg(slideImgs[0]);
			if (slideImgs.length>1) {
				setOpacity(slideImgs[1],0);
				showObj(slideImgs[1]);
				obj2Fade=slideImgs[curImg];
				slideThread++;
				if (slideThread>1) stopSlideThread=true;
				setTimeout("nextSlide()",slideDelay);
			}
		}
	}
}
var stopSlideThread=false;
var slideThread=0;

function nextSlide() {
	if (stopSlideThread || (getObj("PhotoShowGallery")==null)) {
		slideThread--;
		if (stopSlideThread && slideThread<2) stopSlideThread=false;
		//alert("Thread STopped"+slideThread);		
		return;
	}
	if (stopAll) {
		setTimeout("nextSlide()",slideDelay);
		return;
	}
	showImg(slideImgs[curImg]);
	hideObj(slideImgs[curImg]);
	nextImg=curImg+1;
	if (nextImg>=slideImgs.length) nextImg=0;
	showObj(slideImgs[nextImg]);		
	setOpacity(slideImgs[nextImg],0);
	obj2Fade=slideImgs[nextImg];
	fadeIn();
	curImg=nextImg;
	setTimeout("nextSlide()",slideDelay);	
}

function showImg(img) {
	gallery.style.backgroundImage="url("+img.src+")";
}

function setOpacity(obj, value){
	if (obj.style) {
		obj.style.filter="alpha(opacity="+Math.floor(value*100)+")";
		obj.style.opacity=value;
	}
}

curOpacity=0;
maxOpacity=1;
minOpacity=0;
stepFadeIn=.05;
speedFadeIn=20; // delay in ms between each Fade Step
var obj2Fade;

function fadeIn() {
	curOpacity+=stepFadeIn;
	setOpacity(obj2Fade,curOpacity);
	if (curOpacity<maxOpacity) {
		setTimeout('fadeIn()',speedFadeIn);	
	} else {
		curOpacity=minOpacity;
	}
}

function showObj(img) {
	img.style.visibility="visible";
	img.style.display="block";
}

function hideObj(img) {
	img.style.visibility="hidden";
	img.style.display="none";
}

function getObj(name) {
    var d=document;
  	if (d.all) {
  	  return d.all[name];
	  } else if(d.getElementById) {
	    return d.getElementById(name);
	  }
  }  

var imageArray =[];
var imageNum=0;
var galleryFrame;
var galleryCover;

function initGallery() {
		var anchors = document.getElementsByTagName('a');
		for (var i=0; i<anchors.length; i++){
				var anchor = anchors[i];
				var rel=anchor.getAttribute('rel');	
				if (rel && rel.indexOf('slideshow')==0){
						anchor.onclick =  function () {startGallery(this); return false;};//"startGallery(this);";
				}
		}
		galleryCover=getObj("galleryCover");
		galleryFrame=getObj("galleryFrame");
		if (galleryCover==null) {
			galleryCover=document.createElement("div");
			galleryCover.id="galleryCover";
			galleryCover.className="galleryCover";
			document.body.appendChild(galleryCover);
		}
		if (galleryFrame==null) {		
			galleryFrame=document.createElement("div");
			galleryFrame.id="galleryFrame";
			galleryFrame.className="galleryFrame";
			galleryFrame.innerHTML="<table border='0' width='550' cellpadding=0 cellspacing=0><tr><td colspan='3' width='550'><a href='javascript:closeGallery()'><img border='0' onLoad='centerGallery()' title=\"Cliquez sur l'image pour fermer\" id='galleryImage'></a></td></tr><tr><td width='60px'><div id='galleryPrev'><a href='javascript:prevImageGallery()' onmouseover=\"butState('back','ov');\" onmouseout=\"butState('back','of');\"><img src='/userfiles/image/generic/photoshow_nav_prev_of.gif' border='0' name='back' id='back' onload=''></a></div></td><td width='430'><div id='galleryPageNo'></div><div id='galleryImageDesc'></div></td><td align='right' width='60px'><div id='galleryNext' style='text-align: right;'><a href='javascript:nextImageGallery()' onmouseover=\"butState('next','ov');\" onmouseout=\"butState('next','of');\"><img src='/userfiles/image/generic/photoshow_nav_next_of.gif' border='0' name='next' id='next' onload=''></a></div></td></tr></table><div id='closebutt' style='z-index:99;position:relative;top:-604px;left:509px;width:45px;Height:45px;'><a href='javascript:closeGallery()' onmouseover=\"butState('close','ov');\" onmouseout=\"butState('close','of');\"><img src='/userfiles/image/generic/photoshow_nav_exit_of.png' alt='' name='close' width='45' height='45' onload='fixPNG(this,45,45)' border='0' id='close' onload='' /></a></div>";
			document.body.appendChild(galleryFrame);
		}
		window.onresize=new Function('centerGallery()');		
}

function closeGallery() {
	imageArray =[];
	imageNum=0;	
	hideObj(getObj("galleryCover"));
	hideObj(getObj("galleryFrame"));
	var docZone=getObj("DOC_ZONE");
	if (docZone) docZone.style.visibility="visible";
	stopAll=false;
}

var stopAll=false;

function startGallery(imageLink) {
	stopAll=true;
	var docZone=getObj("DOC_ZONE");
	if (docZone) docZone.style.visibility="hidden";	
		imageArray = [];
		imageNum = 0;		
		//alert(imageLink);
		if (!document.getElementsByTagName){ return; }
		var anchors = document.getElementsByTagName('a');

		// if image is NOT part of a set..
		if((imageLink.getAttribute('rel') == 'slideshow')){
			// add single image to imageArray
			imageArray.push(new Array(imageLink.getAttribute('href'), imageLink.getAttribute('title')));			
		} else {
		// if image is part of a set..

			// loop through anchors, find other images in set, and add them to imageArray
			for (var i=0; i<anchors.length; i++){
				var anchor = anchors[i];
				if (anchor.getAttribute('href') && (anchor.getAttribute('rel') == imageLink.getAttribute('rel'))){
					imageArray.push(new Array(anchor.getAttribute('href'), anchor.getAttribute('title')));
				}
			}
			//imageArray.removeDuplicates();
			while(imageArray[imageNum][0] != imageLink.getAttribute('href')) { imageNum++;}
		}
		getObj('galleryFrame').style.display="block";
		showGalleryImage(imageArray[imageNum]);
		resizeCover();
		showObj(getObj("galleryCover"));
		//showObj(getObj("galleryFrame"));
	}
	
	function showGalleryImage(img) {
		getObj("galleryImage").src=img[0];
		getObj("galleryImageDesc").innerHTML=img[1];
		getObj("galleryPageNo").innerHTML= (imageNum+1)+" / "+imageArray.length;
//		centerGallery();		
	}
	
	function nextImageGallery() {
		if (++imageNum>=imageArray.length) imageNum=0;
		showGalleryImage(imageArray[imageNum]);
	}

	function prevImageGallery() {
		if (--imageNum<0) imageNum=imageArray.length-1;
		showGalleryImage(imageArray[imageNum]);
	}
	
function centerGallery() {
		var	doc=document;//.getElementById("ContentFrame").contentWindow.document;	
		var w,h,sT,sL;
		if (doc.innerHeight) // all except Explorer
		{
			w = doc.innerWidth;
			h = doc.innerHeight;
			sT = doc.scrollTop;
			sL = doc.scrollLeft;			
		}
		else if (doc.documentElement && doc.documentElement.clientHeight)
			// Explorer 6 Strict Mode
		{
			w = doc.documentElement.clientWidth;
			h = doc.documentElement.clientHeight;
			sT = doc.documentElement.scrollTop;
			sL = doc.documentElement.scrollLeft;
		}
		else if (doc.body) // other Explorers
		{
			w = doc.body.clientWidth;
			h = doc.body.clientHeight;
			sT = doc.body.scrollTop;
			sL = doc.body.scrollLeft;
		}
		if (window.innerHeight && (typeof doc.body.scrollTop!= undefined)) {
			w=window.innerWidth;
			h=window.innerHeight;
			sT = doc.body.scrollTop;
			sL = doc.body.scrollLeft;
		}
		var obj,s;
    if ((obj=getObj('galleryFrame'))!=null && obj.style.display=="block") {
			//if (obj.style.visibility=="hidden") obj.style.display="block";
			var popUpWidth=obj.offsetWidth;
			var popUpHeight=obj.offsetHeight;
  		if ((s=obj.style)) {
	      		s.position="absolute";
	      		s.left=sL+((w-popUpWidth)/2)+"px";
	      		s.top=sT+((h-popUpHeight)/2)+"px";
						obj.style.visibility="visible";
       }
    }
    resizeCover();		
	}

function resizeCover() {
	var w,h;
	var	doc=document;//.getElementById("ContentFrame").contentWindow.document;	
	var test1 = doc.body.scrollHeight;
	var test2 = doc.body.offsetHeight
	if (test1 > test2) // all but Explorer Mac
	{
	w = doc.body.scrollWidth;
	h = doc.body.scrollHeight;
	}
	else // Explorer Mac;
	   //would also work in Explorer 6 Strict, Mozilla and Safari
	{
	w = doc.body.offsetWidth;
	h = doc.body.offsetHeight;
	}	
	obj=getObj("galleryCover");
	obj.style.height = h + 'px';
	obj.style.width = w + 'px';
}

function initphotoshow() {
	//alert("init");
	initGallery();
	startSlideShow();
}

function ajax_initphotoshow() {
		//stopSlide();
		initphotoshow();
}

if (document && document.body) initphotoshow();
//window.onload=initphotoshow;