// projet.js
// author : dosites.com
// -------------------
window.addEvent('domready', function(){
	
	var jsLang = initPage();

	$('flech_back').src = "imgs/bt_img_back_end_off.gif";
	$('copyright').setStyle('top',window.getHeight()-20);

	// event But Back -------------------------------------------------------------
	$('imgBtBack').addEvent('mouseenter', function(e) {
		e = new Event(e).stop();
		this.src = "imgs/but_back_on.gif"; 
	});
	$('imgBtBack').addEvent('mouseleave', function(e) {
		e = new Event(e).stop();
		this.src = "imgs/but_back_off.gif"; 
	});
	
	// Slide -------------------------------------------------------------
	var mySlide = new Fx.Slide('projText');
		mySlide.hide();	
	
	var fx = new Fx.Styles('content', {duration:200, wait:false});

	$('titre').addEvent('click', function(e){
		e = new Event(e);
		isSlideOpen = !isSlideOpen;
		mySlide.toggle();
		// --------------------------------------
		if (isSlideOpen){
			$('imgBtOpenClose').src = "imgs/bt_close_on.gif";
			if (parseInt( $('projText').getStyle('height')) + 350 > window.getHeight()){
				var gotoPos = window.getHeight() - (parseInt( $('projText').getStyle('height'))+40);
				if (gotoPos < 0){
					gotoPos = 0;
				}
				fx.start({top:gotoPos});
			}
		}else{
			$('imgBtOpenClose').src = "imgs/bt_open_on.gif";	
			fx.start({top:350});
		}
		e.stop();
	});

	// event Titre -------------------------------------------------------------
	$('titre').addEvent('mouseenter', function(e) {
		e = new Event(e).stop();
		if(isSlideOpen){ $('imgBtOpenClose').src = "imgs/bt_close_on.gif"; }else{ $('imgBtOpenClose').src = "imgs/bt_open_on.gif"; }
	});
	$('titre').addEvent('mouseleave', function(e) {
		e = new Event(e).stop();
		if(isSlideOpen){ $('imgBtOpenClose').src = "imgs/bt_close_off.gif"; }else{ $('imgBtOpenClose').src = "imgs/bt_open_off.gif"; }
	});

	// event But flech_back -------------------------------------------------------------
	$('flech_back').addEvent('click', function(e) {
		e = new Event(e).stop();
		$('flech_next').src = "imgs/bt_img_next_off.gif"; 
		if (projectImgViewer.nSelImg == 0){
			document.location = projectImgViewer.addPrev;
		}else{
			projectImgViewer.loadPrevImg();
		}
	});
	$('flech_back').addEvent('mouseenter', function(e) {
		e = new Event(e).stop();
		if (projectImgViewer.nSelImg == 0){
			this.src = "imgs/bt_img_back_end_on.gif";
		}else{
			this.src = "imgs/bt_img_back_on.gif";
		}	 
	});
	$('flech_back').addEvent('mouseleave', function(e) {
		e = new Event(e).stop();
		if (projectImgViewer.nSelImg == 0){
			this.src = "imgs/bt_img_back_end_off.gif";
		}else{
			this.src = "imgs/bt_img_back_off.gif";
		}
	});

	// event But flech_next -------------------------------------------------------------
	$('flech_next').addEvent('click', function(e) {
		e = new Event(e).stop();
		$('flech_back').src = "imgs/bt_img_back_off.gif"; 
		if (projectImgViewer.nSelImg == projectImgViewer.allProjImg.length-1){
			document.location = projectImgViewer.addNext;
		}else{
			projectImgViewer.loadNextImg();
		}
	});
	$('flech_next').addEvent('mouseenter', function(e) {
		e = new Event(e).stop();
		if (projectImgViewer.nSelImg == projectImgViewer.allProjImg.length-1){
			this.src = "imgs/bt_img_next_end_on.gif"; 
		}else{
			this.src = "imgs/bt_img_next_on.gif"; 
		}
		
	});
	$('flech_next').addEvent('mouseleave', function(e) {
		e = new Event(e).stop();
		if (projectImgViewer.nSelImg == projectImgViewer.allProjImg.length-1){
			this.src = "imgs/bt_img_next_end_off.gif"; 
		}else{
			this.src = "imgs/bt_img_next_off.gif"; 
		}

	});
});

	var isSlideOpen = false;
	// --------------------------------------
	window.addEvent('resize',function(e){
		projectImgViewer.scaleImage();
		$('copyright').setStyle('top',window.getHeight()-15);

	});

	// --------------------------------------
	function checkFrom(){
		var lg="";
		if (projectImgViewer.lang != "fr"){
			lg = "_"+projectImgViewer.lang;
		}
		switch(Cookie.get("fromPage")){
			case"real":
				document.location ='realisations'+lg+'.php?real='+Cookie.get("fromReal");
			break;
			default:
				document.location ='index'+lg+'.php';
			break;		
		}
	}

// --------------------------------------------------------------------------------------
var projectImgViewer = {

	allProjImg:null,
	nSelImg:0,
	mediaWidth:1200,
	mediaHeight:900,
	bgImgloader:null,
	addNext:"",
	addPrev:"",
	lang:"fr",

	// --------------------------------------
	initialize:function(data){
		this.allProjImg = data;
		this.loadImg();
	},

	// --------------------------------------
	setStartIndex:function(startIndex){
		this.nSelImg = startIndex;
	},

	// --------------------------------------
	loadNextImg:function(){
		var test = this.nSelImg+1;
		if (test == this.allProjImg.length){ this.nSelImg = 0; }else{ this.nSelImg++;}
		this.loadImg();
	},

	// --------------------------------------
	loadPrevImg:function(){
		var test = this.nSelImg-1;
		if (test < 0){ this.nSelImg = this.allProjImg.length-1; }else{ this.nSelImg--;}
		this.loadImg();
	},
	
	// --------------------------------------
	loadImg:function(){
		projectImgViewer.setNavInfo();
		$('loader').style.display='block';
		$('bgImageContent').empty();
		new Fx.Style('bgImageContent', 'opacity').set(0);
		projectImgViewer.bgImgloader = new Asset.image(projectImgViewer.allProjImg[projectImgViewer.nSelImg][0], {onload:function(){ projectImgViewer.imageisLoad();}});
		projectImgViewer.bgImgloader.injectInside('bgImageContent');
		// ###
		//$('copyright').setHTML(projectImgViewer.allProjImg[projectImgViewer.nSelImg][1]);
	},

	// --------------------------------------
	imageisLoad:function(){
		$('loader').style.display='none';
		this.scaleImage();
	},

	// --------------------------------------
	scaleImage:function(){
		// ###
		//$('bgImage').setStyle('clip', [0, 0, window.getWidth(), window.getHeight()]);
		var scale = this.getFitMinScaleRatio(projectImgViewer.mediaWidth,projectImgViewer.mediaHeight,window.getWidth(),window.getHeight());
		var imgWidth = Math.round(projectImgViewer.mediaWidth * scale);
		var imgHeight = Math.round(projectImgViewer.mediaHeight * scale);
		var imgTop = Math.round((window.getHeight() - imgHeight)/2);
		var imgLeft = Math.round((window.getWidth() - imgWidth)/2);

		//$('debug').setHTML("imgWidth = " + imgWidth +"  <br> imgHeight = "+imgHeight+"  <br> imgTop = "+imgTop+"  <br> imgLeft = "+imgLeft+"  <br> getWidth = "+window.getWidth()+"  <br> getHeight = "+window.getHeight());
		//$('bgImage').setStyles({top:imgTop,left:imgLeft});,clip:[imgLeft, imgTop, window.getWidth(), window.getHeight()]
		//$('bgImage').setStyles({top:imgTop,left:imgLeft});

		projectImgViewer.bgImgloader.setProperties({width: imgWidth,height: imgHeight});
		new Fx.Style('bgImageContent', 'opacity').start(0,1); 
	},
	
	// --------------------------------------
	setNavInfo:function(){
		switch (this.lang){
			case "fr":
				var info = 'image <strong>'+(this.nSelImg+1)+'</strong>/'+this.allProjImg.length;
			break;
			case "en":
				var info = 'picture <strong>'+(this.nSelImg+1)+'</strong>/'+this.allProjImg.length;
			break;
			case "cn":
				var info = '图片 <strong>'+(this.nSelImg+1)+'</strong>/'+this.allProjImg.length;
			break;
		}
		$('navInfo').setHTML(info);
	},

	// --------------------------------------
	getFitMinScaleRatio:function( w, h, fw, fh){
		var s;
		if( w < fw || h < fh ) {
			s = Math.max( fw / w,  fh / h );
		} else if( w > fw || h > fh ){
			s = 1 / Math.min( w / fw,  h / fh );
		} else {
			s = 1;
		}
		return s;
	}
}