if (typeof js == "undefined"){
	js = {};
}
 
toutou.require("toutou.js.object");
toutou.require("toutou.html.event");
toutou.require("toutou.event.manager");
toutou.require("toutou.html.dom");
toutou.require("toutou.html.layout");
toutou.require("toutou.js.ajax");
toutou.require("toutou.js.closure");
toutou.require("toutou.utils.forms");
toutou.require("toutou.frame.defaultFrame");

js.belem = new function() {
	
	/**
	* Popover actualité
	**/
	
	this.currentPosDiapo 	= null;
	this.photosDiapo 		= null;
	
	this.popoverFrame = function(photos,j) {
		this.currentPosDiapo 	= j-1;
		this.photosDiapo 		= photos;
		
// construction de l'objet html
		if(!photos || !j) { return ; }
		
		this.id = 'diaporama';
		this.constructFrame();
		this.frame = tt$(this.id);
		
// construction des attributs 
		this.frame.tt_container 	= document.body;
		this.frame.tt_fadeDuration	= 1000;

		toutou.frame.defaultFrame.extend(this.frame);
		
		this.frame.tt_properties.resizeContentTextHeight = true;
		this.frame.tt_properties.isModal = true;

		toutou.frame.defaultFrame.initEvents(this.frame);
// init title and content elements
		toutou.frame.defaultFrame.initTitleAndContent(this.frame);
// init controls
		toutou.frame.defaultFrame.initControls(this.frame);
// fade
	if (this.frame.getAttribute("tt_fadeDuration")) {
		toutou.require("toutou.effect.fade");
		toutou.effect.fade.extend(this.frame);
	}
	
// affichage données
		this.refreshDiapo();
		
// ouverture et positionnement	
		this.frame.tt_open();
		var size  = this.frame.tt_getBorderSize();
		var position  = {left:0, top:0};
	    position.left = Math.round( (document.body.offsetWidth - size.width) / 2 ); 
	    position.top  = Math.round( (document.body.clientHeight-669)/2+document.body.scrollTop+25);
		if (position.top < 0){position.top = 0;}
		this.frame.tt_setBorderPosition(position); 

// gestion de la fermeture 
		var closure = toutou.js.closure.bind(this, this.frameClosed);
		this.frame.tt_eventManager.addListener("close", closure);
		
		return false;
	}
		
	this.constructFrame = function() {
	
		var html = '' +
						'<div id="popover">' +
						'	<div id="popoverTop">' +
						'		<p class="popoverClose">' +
						'			<a tt_type="CloseControl" class="closeControl">' +
						'				Fermer' +
						'			</a>&nbsp;' +
						'		</p>' +
						'		<p class="popoverNumeroImage" id="photo_number">' +
						'		</p>' +
						'		<p class="popoverTitre" id="title">' +
						'		</p>' +
						'	</div>' +
						'	<div id="popoverImage">' +
						'		<table width=792 border=0 cellspacing=0 cellpadding=0>' +
						'			<tr>' +
						'				<td colspan=3 align=center width=792>' +
						'					<img id="photo_src" src="" />' +
						'				</td>' +
						'			</tr>' +
						'			<tr>' +
						'				<td width=78>&nbsp;</td>' +
						'				<td width=636 align=center>' +
						'					<span id="photo_legende" class="textedesc">' +
						'					</span>' +
						'				</td>' +
						'				<td width=78>&nbsp;</td>' +
						'			</tr>' +
						'		</table>' +
						'	</div>' +
						'	<div id="popoverFooter">' +
						'		<span class="boutonsuiv">' +
						'			<a id="btn_suiv" onClick="js.belem.onClickSuiv()">' +
						'				<img src="imgT/btn_popover_suiv.gif" />' +
						'			</a>' +
						'		</span>' +
						'		<span class="boutonprec">' +
						'			<a id="btn_prec" onClick="js.belem.onClickPrec()">' +
						'				<img src="imgT/btn_popover_prec.gif" />' +
						'			</a>' +
						'		</span>' +
						'	</div>' +
						'</div>' +
						'';
					
		var div = document.createElement("div");
		div.id = this.id;	
		div.className = "frameContainer";
		div.style.display = "none";
		div.style.position = "absolute";
		div.innerHTML = html;
		document.body.appendChild(div);
		toutou.html.dom.extend(div);
	}

	this.onClickPrec = function() {
		this.currentPosDiapo--;
		if(this.currentPosDiapo < 0) 
			this.currentPosDiapo = 0;
			
		this.refreshDiapo();
	}
	
	this.onClickSuiv = function() {
		var lastPhoto = this.photosDiapo.length;
		this.currentPosDiapo++;
		if(this.currentPosDiapo > lastPhoto) 
			this.currentPosDiapo = lastPhoto;
			
		this.refreshDiapo();
	}
	
	this.frameClosed = function() {
		this.frame.parentNode.removeChild(this.frame);
		tt$("navigation").style.display = 'block';	
	}
	
	this.refreshDiapo = function() {
// preparation des données
// copyright 		
		if (!this.photosDiapo[this.currentPosDiapo].photo_photographe) {
			var copy = '' ;
		}
		else {
			var copy = '<br/>&copy;&nbsp;'+this.photosDiapo[this.currentPosDiapo].photo_photographe;
		}

		tt$("photo_src").src 			= 'bases/'+this.photosDiapo[this.currentPosDiapo].photo_src;
		tt$("photo_legende").innerHTML 	= this.photosDiapo[this.currentPosDiapo].photo_legende+copy;
		tt$("photo_number").innerHTML	= 'Photo&nbsp;'+(this.currentPosDiapo+1)+'/'+this.photosDiapo.length;
		tt$("title").innerHTML			= this.photosDiapo[this.currentPosDiapo].photo_rubrique;
		
// gestion du probleme de zindex du select sur IE6
		tt$("navigation").style.display = 'none';
		
		if (this.currentPosDiapo == (this.photosDiapo.length-1)) {
			tt$("btn_suiv").style.display		= 'none'
		}
		else {
			tt$("btn_suiv").style.display		= 'block'
		}
		if (this.currentPosDiapo == 0) {
			tt$("btn_prec").style.display		= 'none'
		}
		else {
			tt$("btn_prec").style.display		= 'block'
		}
// Preload des images precedentes et suivantes
		this.precPos = this.currentPosDiapo-1;
		this.suivPos = this.currentPosDiapo+1;
		
		if(this.photosDiapo[this.precPos]){toutou.html.event.preloadImages('bases/'+this.photosDiapo[this.precPos].photo_src);}
		if(this.photosDiapo[this.suivPos]){toutou.html.event.preloadImages('bases/'+this.photosDiapo[this.suivPos].photo_src);}	}
	
	//Toggle on panier
	this.toggleNuit = function(nuit /* avant ou apres*/,id_stage,id_stagiere){
		//launch request
		var url='/php/view_reservation_modif.php';
		var reponse = toutou.js.ajax.request(url+'?method=modif&nuit='+nuit+'&id_stage='+id_stage+'&id_stagiere='+id_stagiere);
		//Eval reponse
		eval(reponse); 
	}
	
	//Handle text boxes
	this.showHelp = function(visible /*bool*/,id){
		if (visible){
			tt$(id).style.display='block';
		}else{
			tt$(id).style.display='none';
		}
	}
}
