/* Choix de l'effet et temps entre les images */

var time = 3000;

var current = 0;
var next = 1;
var started = false;
var images = new Array();
var BODY = null;

var LINKS = {};
LINKS.all = ['next', 'prev', 'play'];
LINKS.play = function() {
	DIA.play();
	return false;
};

LINKS.pause = function() {
	DIA.pause();
	return false;
};

LINKS.next = function() {
	DIA.pause();
	DIA.nextImage();
	return false;
};

LINKS.prev = function() {
	DIA.pause();
	DIA.prevImage();
	return false;
};

LINKS.close = function() {

};

LINKS.open = function(no) {

};


var DIA = {};
DIA.img = null;
DIA.pc = null;
DIA.nums = null;
DIA.images = [];
DIA.loaded = [];
DIA.containers = [];
DIA.preloadImages = [
	'images/static/pictos/close-on.gif', 
	'images/static/pictos/next-on.gif', 
	'images/static/pictos/prev-on.gif', 
	'images/static/pictos/planche-on.gif', 
	'images/static/pictos/play-on.gif', 
	'images/static/pictos/pause-on.gif', 
	'images/static/pictos/pause-off.gif',
	'images/static/pictos/small/diapo-on.gif', 
	'images/static/pictos/small/planche-on.gif'
];
DIA.previous = null;

DIA.play = function() {
	if (!this.playing)
		this.pauseBT();
	this.nextImage();
	this.tid = setTimeout("DIA.play()", this.timeout * 1000);	
	this.playing = true;
};

DIA.pause = function() {
	if (this.playing) {
		this.startBT();
		clearTimeout(this.tid);
	}
	this.playing = false;
};

DIA.startBT = function() {
	var bt = $('pause');
	bt.id = 'play';
	bt.className = 'play';
	bt.parentNode.className = 'play';
	bt.onclick = LINKS.play;
};

DIA.pauseBT = function() {
	var bt = $('play');
	bt.id = 'pause';
	bt.className = 'pause';
	bt.parentNode.className = 'pause';
	bt.onclick = LINKS.pause;
};

DIA.nextImage = function() {
	this.previous = this.no;
	this.no++;
	this.move();
};

DIA.prevImage = function() {
	this.previous = this.no;
	this.no--;
	this.move();
};

DIA.move = function() {
	if (this.img) {
		if (this.no > this.images.length - 1) this.no = 0;
		if (this.no < 0) this.no = this.images.length - 1;
		
		var pr = this.containers[this.previous];
		var cu = this.containers[this.no];

		new Effect.Fade(pr.p); 
		new Effect.Appear(cu.p);
		

		if (this.nums) {
			this.detail = this.no + 1;
			this.nums.innerHTML = (this.detail + ' / ' + this.images.length);	
		}
		this.loadNeighbours();
	}
};

DIA.refresh = function() {
	if (this.isPresent()) {
		var oh = document.body.offsetHeight;
		
		var nt = oh / 2 - 300 - this.pni;
		if (nt < 0) nt = 0;
		this.pn.style.top = nt + 'px';
	}
};

DIA.preload = function() {
	if (document.preload == null) document.preload = [];
	for (i = 0; i < this.preloadImages.length; i++) {
		var im = new Image();
		im.src = this.preloadImages[i];
	}
	this.loadNeighbours();
};

DIA.getNeighbours = function() {
	var r = [];
	i = this.no;
	while (true) {
		if (r.length == 3) break;
		i = this.getNext(i);
		r.push(i);
	}
	i = this.no;
	while (true) {
		if (r.length == 5) break;
		i = this.getPrev(i);
		r.push(i);
	}
	return r;
};

DIA.loadNeighbours = function() {
	var nb = this.getNeighbours();
	for (i = 0; i < nb.length; i++) {
		var c = this.containers[nb[i]];
		if (typeof c.load !== 'undefined') // first container does not have a load function
			c.load();
	}
};

DIA.getNext = function(n) {
	
	if (n + 1 > this.images.length - 1) return 0;
	return n + 1;
};

DIA.getPrev = function(n) {
	if (n - 1 < 0) return this.images.length - 1;
	return n - 1;
};

DIA.init = function() {
	if (this.isPresent()) {

		this.pn = this.pc.parentNode;
		this.pni = Position.cumulativeOffset(this.pn)[1];
		this.refresh();

		for (i = 0; i < LINKS.all.length; i++) {
			var l = $(LINKS.all[i]);
			if (l) l.onclick = LINKS[LINKS.all[i]];
		}
		
		this.loaded.push(this.img.src);

		var modele = this.pc.cloneNode(true);
		modele.id = 'pc-auto-modele';
		modele.img = modele.getElementsByTagName('img')[0];
		modele.style.display = 'none';
		this.pc.parentNode.appendChild(modele);


		for (i = 0; i < this.images.length; i++) {			
			c = {};
			c.i = i;
			c.pc = modele;
			c.path = this.images[i];
			c.loaded = false;
			
			c.load = function () { 
				if (!this.loaded) {
					var p = this.pc.cloneNode(true);
					p.id = 'pc-auto-' + this.i;
					p.img = p.getElementsByTagName('img')[0];
					p.img.id = 'photo-auto-' + i;
					p.loaded = false;
					p.img.src = this.path;
					p = Element.extend(p);
					this.pc.parentNode.appendChild(p);
					this.p = p;
					this.loaded = true;
/*
					var a = p.getElementsByTagName('a');
                    for (var i = 0; i < a.length; i++) {
                        alert(a[i]);                        
                    }
*/
				}
			};
			
			if (i == this.no) {
				c.p = this.pc;
				c.loaded = true;
			}

			this.containers.push(c);
		}
		
		this.preload();

        if (/action=play/.test(window.location)) {
            setTimeout("DIA.play()", this.timeout * 1000);
        }
	}	
};

DIA.isPresent = function() {
	this.loadElements();
	if (this.pc && this.img) return true;
	return false;
};

DIA.loadElements = function() {
	this.pc = this.pc == null ? $('pc') : this.pc;
	this.img = this.img == null ? $('photo') : this.img;
	this.nums = this.nums == null ? $('nums') : this.nums;
};

function imgLoaded(img)
{
	//alert('loaded: '+ img.src);
}


var LOADER = {};
LOADER.show = function () { me.style.display = 'block'; };
LOADER.hide = function () { me.style.display = 'none'; };
LOADER.setParent = function (parent) { this.parent = parent; };
LOADER.init = function () { 
	if (!this.parent) this.parent = document.body;

	var img = document.createElement('img');
	img.src = 'images/static/loader.gif';
	img.style.position = 'absolute';
	
	this.parent.insertBefore(img, this.parent.firstChild);


	//this.hide();
};



window.onload = init;
window.onresize = refresh;

function init() 
{
	
	// get the first images (loaded in html)
	if (document.getElementsByClassName) {
		BODY = $('body');
		images = document.getElementsByClassName("image");
		for(var i = 1; i < images.length; i++) {
			images[i].style.display = "none";
		}
		
		//makePopOver();
		initDia();


		var sn = document.getElementsByClassName("on-off");
		for (var i = 0; i < sn.length; i++) {
		    var img = sn[i];
		    sn[i].parentNode.onmouseover = function() {
                this.firstChild.src = this.firstChild.src.replace(/off/, 'on');
		    };
		    sn[i].parentNode.onmouseout = function() {
                this.firstChild.src = this.firstChild.src.replace(/on/, 'off');
		    };
		}
	}
	
	
	// already start the diaporama if we have multiple images
	if( images.length > 1)  
		galerie();
	
	// only get other images if I have at least one image
	if( images.length > 0)  
		getAllImages();

}


function extendDia(body, jsonHeader) 
{
	var o = eval('(' + body.responseText + ')');
	
	DIA = Object.extend(DIA, o);
	DIA.init();
}

function initDia()
{
	if (DIA.isPresent()) {
		var a = new Ajax.Request(
						window.location.toString(),
						{
							method: 'get', 
							parameters: 'action=ajax', 
							onSuccess: extendDia
						});
	}
}

function galerie() 
{
	started = true;
	self.setTimeout("nextImage()",time);	
}

function getAllImages()
{
	var a = new Ajax.Request(
					window.location.toString(),
					{
						method: 'get', 
						parameters: 'action=images', 
						onSuccess: addImages
					});
}

function addImages(body, jsonHeader) 
{
	var imgs = eval(body.responseText);
	
	if (imgs) {
		// only add images we don't already have
		var toAdd = new Array();
		for (var j = 0; j < imgs.length; j++) {
			var found = false;
			for (var i = 0; i < images.length; i++) 
				if (images[i].src == imgs[j]) {
					found = true;
					break;
				}
			if (!found)
				toAdd.push(imgs[j]);
		}
		
		for (var i = 0; i < toAdd.length; i++)
			shiftImage(toAdd[i]);
		
		if (!started)
			galerie();
	}
}

function shiftImage(ad)
{
	var i = images[0].cloneNode(true);
	i.src = ad;
	i.style.display = 'none';
	
	images.push(i);
	images[0].parentNode.appendChild(i);
}

function nextImage() 
{
	new Effect.Fade(images[current]); 
	new Effect.Appear(images[next]);
	
	if (next == (images.length - 1)) {
		current = next;
		next = 0;
	} else {
		current = next;
		next++;
	}
	galerie();
}

Position.GetWindowSize = function(w) {
	var width, height;
        w = w ? w : window;
        this.width = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
        this.height = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
        
        return this;
}

function refresh()
{
	DIA.refresh();
}

function createBackground()
{
	// create the bg element
	var bg = document.createElement('div');
	var s = bg.style;

	var _docHeight = document.height || document.body.offsetHeight;

	// position it
	s.width = '100%';
	s.height = getOffsetDimensions().height + 'px';
	s.position = 'absolute';
	s.top = '0';
	s.left = '0'; // ie needs it
	
	// style it
	s.backgroundColor = '#000';
	setOpacity(bg, 100);
	bg.onclick = bgClick;
	return bg;
}

function bgClick() 
{
	POP.close();
}

function setOpacity(el, o)
{
	var s = el.style;
	s.opacity = (o / 100); 
    s.MozOpacity = (o / 100); 
    s.KhtmlOpacity = (o / 100); 
    s.filter = "alpha(opacity=" + o + ")"; 
}

function getOffsetDimensions()
{
	
	if( window.innerHeight && window.scrollMaxY ) { // firefox
		pageWidth = window.innerWidth + window.scrollMaxX;
		pageHeight = window.innerHeight + window.scrollMaxY;
	} else if ( document.body.scrollHeight > document.body.offsetHeight ) { // all but Explorer Mac
		pageWidth = document.body.scrollWidth;
		pageHeight = document.body.scrollHeight;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		pageWidth = document.body.offsetWidth + document.body.offsetLeft; 
		pageHeight = document.body.offsetHeight + document.body.offsetTop; 
	}
	return { height: pageHeight, width: pageWidth };
}


