// JavaScript Document

/* Mosaic Projects */
/* --------------- */

var goroll = {
	elts : null, fadeLevel: 70,
	set : function(div){
		this.elts = document.getElementById(div).getElementsByTagName("A");

		for(var i=0;i<this.elts.length;i++ ){
			this.alpha(this.elts[i], this.fadeLevel );
			this.elts[i].ivar = i;
			this.elts[i].onmouseover = goroll.showThis;
			this.elts[i].onmouseout = function (){
				goroll.hideAll(this.ivar);
			}
			
		}


	},
	hideAll : function(n){
		clearInterval(this.timer);
		for(var i=0;i<this.elts.length;i++ ){
	        this.alpha(this.elts[i], this.fadeLevel);
		}
	},
	showThis : function(n){
        goroll.alpha(this, 100);
	},
	
	alpha : function (o, value){
		if(isIE8) {
			//return this.imagetofade.filters.alpha.opacity = value+10;
			return o.filters.item("DXImageTransform.Microsoft.Alpha").Opacity = value; 

		} else if(isIE) {
            return o.filters.alpha.opacity = value;

		}else {
            return o.style.opacity = value/100;
		}
	}
}


/* Slide Publication */
/* ----------------- */

var mediamanager = {
	slideShow : null, intervalTime: 0,
	aWidth : null,
	flashUrl : null, link : null, saveFlashLink :  null, linkTitle : '',
	set: function(DivId, intervalTime){
	   this.intervalTime = intervalTime;
		if (document.getElementById(DivId)!=undefined) {
			var mediadiv = document.getElementById(DivId);
			var numDiv = document.getElementById(numDiv);
			
			if (mediadiv.innerHTML !='') {
				this.slideShow = mediadiv.getElementsByTagName('img');//imgroll.js needed

			//	this.aWidth = Math.floor(mediadiv.offsetWidth/this.slideShow.length)-2;

				if (this.slideShow.length>0) {// slideshow
					for(var i=0; i< this.slideShow.length; i++){
						this.slideShow[i].style.zIndex = (100+this.slideShow.length-i);
						this.slideShow[i].style.display = 'block';
					//	this.cAblock((i+1), numDiv);
					}
					this.slideGallery();
				} else {
					return;
				}
			}
		}
	},
	slideGallery : function (){
		setgallery.intervalTime = this.intervalTime;
		setgallery.set('slideImage');
	},
	cAblock : function (data, o) {
		var sp = document.createElement('A');
		o.appendChild(sp);

		if (data>1) sp.className  = 'fade';
		sp.style.width = this.aWidth+'px';
		sp.onclick = function(){
			inputform.set(this)
			go.editMode = true;
		}
		if (data<10) data = "0"+data;
		sp.innerHTML = data;
		return sp;
	}
}

var setgallery = {
	timevar : null, timevar2 : null, goingTo : 100, intervalTime : 0,
	fadevar : null,
	formsArray : [],
	aTab : [],
	actualImage : 0,previousImage: 0,
	shoimgid : 0,
	imagetofade : null,
	alpha : 0,
	set : function (divId) {
		this.formsArray = document.getElementById(divId).getElementsByTagName("img");
		this.aTab = document.getElementById(divId).getElementsByTagName("A");
		for(var i=0; i<this.formsArray.length; i++){
			this.formsArray[i].id = "imsl"+i;


		}
		setgallery.nextimg(this.actualImage);
		setgallery.play();
	},
	play : function(){
		this.timevar = setInterval('setgallery.nextimg()', this.intervalTime);
	},
	pause : function(){
		clearInterval(this.timevar);
		clearInterval(this.timevar2);
		this.timevar2 = null;
	},
	nextimg : function(force) {
		this.previousImage = this.actualImage;
		if (force!=undefined) {
			this.actualImage = force;
			setgallery.pause();
			this.shoimgid = force;
		} else {
			this.actualImage++;
			if ( this.actualImage == (this.formsArray.length) ) this.actualImage = 0;//r.a.z
			this.shoimgid = this.actualImage;
		}
		setgallery.showImg();
	},
	showImg : function(){
		for(var i=0; i<this.formsArray.length; i++){
			if (i==this.shoimgid) {
				this.formsArray[i].style.visibility = 'visible';//
				this.imagetofade = this.formsArray[this.shoimgid];
				if (this.previousImage!=this.shoimgid) setgallery.alpha(true, 0);
				this.imagetofade.style.zIndex = this.formsArray.length;
				this.timevar2 = setInterval('setgallery.fadePic();', 50);
				
				this.aTab[i].className = '';
				
			} else if ((i==this.previousImage)) {
				this.formsArray[i].style.zIndex = (this.formsArray.length-1);
				this.aTab[i].className = 'fade';

			} else if (i!=this.shoimgid) {
				this.formsArray[i].style.zIndex = "1";
				this.formsArray[i].style.visibility = 'hidden';
				this.aTab[i].className = 'fade';
			}
		}
	},
	fadePic : function(){
		this.actual =  setgallery.alpha(false,false);
		var dx =  (100 - this.actual);
		var doMath = Math.ceil(this.actual+(0.1*dx));

		if (this.actual<this.goingTo) {
			setgallery.alpha(true, doMath);
		} else {
			clearInterval(this.timevar2);
			this.timevar2 = null;
		}
	},
	alpha : function (set, value){
		if(isIE8) {
			if (set==true) {
				//return this.imagetofade.filters.alpha.opacity = value+10;
				return this.imagetofade.filters.item("DXImageTransform.Microsoft.Alpha").Opacity = value+10; 
			} else {
				//return this.imagetofade.filters.alpha.opacity;
				return this.imagetofade.filters.item("DXImageTransform.Microsoft.Alpha").Opacity;
			}
		} else if(isIE) {
			if (set==true) {
				return this.imagetofade.filters.alpha.opacity = value+10;
			} else {
				return this.imagetofade.filters.alpha.opacity;
			}
		} else {
			if (set==true) return this.imagetofade.style.opacity = value/100;
			else return (getComputedStyle(this.imagetofade, null).opacity)*100;
		}
	}
}

var homeFlash = {
	flashID: 'flashcontent',
	footerHeight: 135,
	set: function(){
		if( isIE || Browser.Engine.webkit || Browser.Engine.presto ){
			homeFlash.setHeight();
			$(window).addEvent('resize', function(){
// console.log( 'resize' );
				homeFlash.setHeight();
			});
		}
	},
	setHeight: function(){
		var _minHeight = parseInt( $(homeFlash.flashID).getStyle('min-height') ) - homeFlash.footerHeight;
		var _height = $(window).getSize().y - homeFlash.footerHeight;
		var _falshHeight = ( _height >= _minHeight )? _height : _minHeight;

		$(homeFlash.flashID).setStyle('height',_falshHeight+'px');
	}
}

/*   Collections       */
/* ------------------- */

var rollImage = {
	slideShow : null,
	aWidth : null,
	arrayimages : [],
	set: function(DivId, toimg, images){
		if (document.getElementById(DivId)!=undefined) {
			var mediadiv = document.getElementById(DivId);
			var image = document.getElementById(toimg);
			var arrayimages = images;
			
			if (mediadiv.innerHTML !='') {
				this.slideShow = mediadiv.getElementsByTagName('a');

				if (this.slideShow.length>0) {// slideshow
					for(var i=0; i< this.slideShow.length; i++){
					    this.slideShow[i].saveVarI = arrayimages[i]
						this.slideShow[i].onmouseover = function(){
						image.src = this.saveVarI ;
          			   }
					}
				} else {
					return;
				}
			}
		}
	}
}

var load4Images = {
	allImages : [], imageToLoad: [], timevar2: [],
	goingTo : 100,
	set : function(){
		this.allImages = document.getElementById('droite').getElementsByTagName('img');
		if (this.allImages.length>0) {
			for(var i=0;i<this.allImages.length; i++){//boucle sur images
				//images non chargées
//				if (!this.allImages[i].complete) {
					this.imageToLoad.push(this.allImages[i]);
/*				} else {
					this.alpha(this.allImages[i], true, 100);
				}*/
			}
			this.firstLoad();
		}
	},
	firstLoad : function(){
		if (this.imageToLoad.length>0) {
			for(var i=0;i<this.imageToLoad.length; i++){//boucle sur images
				//images non chargées
				if (!this.imageToLoad[i].complete) {
					setTimeout('load4Images.firstLoad()', 100);
					return;
				}
				this.timevar2[i] = setInterval('load4Images.fadePic('+i+');', 25);
			}
		}
	},
	fadePic : function(i){
		this.imageToLoad[i].actual =  load4Images.alpha(this.imageToLoad[i], false,false);
		var dx =  (100 - this.imageToLoad[i].actual);
		var doMath = Math.ceil(this.imageToLoad[i].actual+(0.1*dx));

		if (this.imageToLoad[i].actual<this.goingTo) {
			load4Images.alpha(this.imageToLoad[i], true, doMath);
		} else {
			clearInterval(this.timevar2[i]);
			this.timevar2[i] = null;
		}
	},
	alpha : function (o, set, value){
		if(isIE8) {
			if (set==true) {
				//return this.imagetofade.filters.alpha.opacity = value+10;
				return o.filters.item("DXImageTransform.Microsoft.Alpha").Opacity = value+10; 
			} else {
				//return this.imagetofade.filters.alpha.opacity;
				return o.filters.item("DXImageTransform.Microsoft.Alpha").Opacity;
			}
		} else if(isIE) {
			if (set==true) {
				return o.filters.alpha.opacity = value+10;
			} else {
				return o.filters.alpha.opacity;
			}
		} else {
			if (set==true) return o.style.opacity = value/100;
			else return (getComputedStyle(o, null).opacity)*100;
		}
	},
	end:0
}

function domFunction(f, a){
	var n = 0;
	var t = setInterval(function() {
		var c = true;
		n++;
		if(typeof document.getElementsByTagName != 'undefined' && (document.getElementsByTagName('body')[0] != null || document.body != null))	{
			c = false;
			if(typeof a == 'object') {
				for(var i in a) {
					if((a[i] == 'id' && document.getElementById(i) == null) || (a[i] == 'tag' && document.getElementsByTagName(i).length < 1)) { 
						c = true; 
						break; 
					}
				}
			}
			if(!c) { f(); clearInterval(t); }
		}
		if(n >= 60)	{
			clearInterval(t);
		}
	}, 250);
};















