var SelectedImg = "";
var ThumbEffectDuration = 0.3;

function OpenWin(URL) {
	//window.open('Preloader.php5', 'MainPage', width=1000,height=700,toolbar=no,resizable=yes,location=no,status=no,menubar=no);
	window.open(URL, 'MainPage', 'width=1000,height=700,toolbar=no,resizable=yes,location=no,status=no,menubar=no');
}
function Open3mOPhoto() {
	OpenWin('Preloader.php5');
//	window.open('Preloader.php5', 'MainPage', 'width=1000,height=700,toolbar=no,resizable=yes,location=no,status=no,menubar=no');
}

function Open3mOWebDesign() {
	OpenWin('Construction.php5');
//	window.open('Construction.php5', 'MainPage', 'width=1000,height=700,toolbar=no,resizable=yes,location=no,status=no,menubar=no');
}

function titleOver(e) {	
	new Effect.Opacity(e, {duration:0.2, from:1.0, to : 0.5});	
}

function titleOut(e) {
	new Effect.Opacity(e, {duration:0.2, from:0.5, to : 1.0});	
}


function SetThumbTransparent(e) {

	var ThumbNb = $('CurSelectedThumb').value;	
	if ((e.id != "") && (e.id != ThumbNb )) {			
		var T = $('T'+e.id);	
		new Effect.Opacity(e, {queue:'front', duration:ThumbEffectDuration, from:1.0, to : 0.5});	
		new Effect.Opacity(T, {queue:'front', duration:ThumbEffectDuration, from:1.0, to : 0.5});	
	}
}



function SetPageTransparent(e) {
	var PageNb =  $('Page').value;	
	if ((PageNb >= 0 && e.id != PageNb) || (PageNb <0)) {
		new Effect.Opacity(e, {queue:{position:'front', scope:'test1'}, duration:ThumbEffectDuration, from:1.0, to : 0.5});
	}
}

function SetNotTransparent(e) {	
	if (e != null) {
		new Effect.Opacity(e, {queue:{position:'front', scope:'test2'}, duration:ThumbEffectDuration, from:0.5, to : 1.0});
	}
}

function SetThumbNotTransparent(e) {
	var ThumbNb = $('CurSelectedThumb').value;	
	if ((e.id != "") && (e.id != ThumbNb )) {		
		if (e != null) {
			var T = $('T'+e.id);
			new Effect.Opacity(e, {queue:{position:'front', scope:'test2'}, duration:ThumbEffectDuration, from:0.5, to : 1.0});
			new Effect.Opacity(T, {queue:{position:'front', scope:'test3'}, duration:ThumbEffectDuration, from:0.5, to : 1.0});
		}
	}
}

function ResetTransparent() {

	var ThumbNb = $('CurSelectedThumb').value;
	
	if ((ThumbNb != "") && (ThumbNb < 1000)) {			
		var ThumbUnit = $(ThumbNb);
		var T = $('T'+ ThumbNb);
		new Effect.Opacity(ThumbUnit, {queue:{position:'front', scope:'test3'}, duration:ThumbEffectDuration, from:1.0, to : 0.5});	
		new Effect.Opacity(T, {queue:{position:'front', scope:'test4'}, duration:ThumbEffectDuration, from:1.0, to : 0.5});	
	}
}

var SelectedImgTitle;
var SelectedImgLegend1;
var SelectedImgLegend2;

function SwitchTitle() {

	var ThumbId = $('CurSelectedThumb').value;
	
	$('ImageTitle').innerHTML = SelectedImgTitle;
	//$('ImageLegend1').innerHTML = SelectedImgLegend1;
	//$('ImageLegend2').innerHTML = SelectedImgLegend2;
}

function HighlightPage() {
	var PageDiv = $('Page');
	if (PageDiv != null) {
		if (PageDiv.value < 0) {		
			PageDiv.value = 0;
			var Pages = $$('div.SeriesUnit');
			if (Pages.length > 0) {
				SetNotTransparent(Pages[0]); //La première de la page devrait être la première page.			
			}
		}
	}
}

function SelectFirstImage(obj) {	
	var SelBorder = $('SelectedImageBorder');
	var SelImage = $('SelectedImageSrc');
	
	HighlightPage();
	
	SelBorder.style.opacity = 0;
	SelBorder.style.filter = 'alpha(opacity=0)';
	SelBorder.style.visibility = 'visible';		
	SelImage.src = SelectedImg;			
	SelImage.onload = function(image) {
		var TotalHeight = 533;
		var TotalWidth = 800;
		var NewWidth = 0;
		var NewHeight = 0;	
		var NewMarginTop = 0;
		var NewMarginLeft = 0;
	
		NewHeight = this.height;
		NewWidth = this.width;
		NewMarginTop = (TotalHeight - NewHeight) / 2 ;	
		NewMarginLeft  = (TotalWidth - NewWidth) / 2;				
		
		SelBorder.style.width = NewWidth + 'px';
		SelBorder.style.height = NewHeight + 'px';
		SelBorder.style.marginTop = NewMarginTop + 'px';
		SelBorder.style.marginLeft = NewMarginLeft + 'px';
		
		new Effect.Opacity(SelBorder, {queue:'front', afterFinish: SwitchTitle, duration:1.0, from:0.0, to : 1.0});
	}
}

var GbTotalHeight = 533;
var GbTotalWidth = 800;

function MorphBorder(obj) {		
	var Img = $('SelectedImageSrc');	
	var OldHeight = Img.height;
	var OldWidth = Img.width;						
	Img.src = SelectedImg;
	Img.onload = function(image) {					
		var NewHeight = this.height;
		var NewWidth = this.width;
		
		if ( (OldHeight != NewHeight) || (OldWidth != NewWidth)) {
		
			NewMarginTop = (GbTotalHeight - NewHeight) / 2 ;	
			NewMarginLeft  = (GbTotalWidth - NewWidth) / 2;				
			new Effect.Morph('SelectedImageBorder', {queue:'front', style:'opacity:1.0;position:absolute; margin-left:' + NewMarginLeft + 'px; width: ' + NewWidth +'px; height: ' + NewHeight + 'px;	margin-top: ' + NewMarginTop + 'px;'});		
		}
		new Effect.Opacity('SelectedImageSrc', {queue:'end', afterFinish: SwitchTitle, duration:0.5, from:0.0, to:1.0});
	}	
}

function SwitchImage(ThumbNb, FileName, Title, Legend1, Legend2) {
	
	if ($('CurSelectedThumb').value != ThumbNb) {		
		ResetTransparent();
		SelectedImg = FileName;
		SelectedImgTitle = Title;
		SelectedImgLegend1 = Legend1;
		SelectedImgLegend2 = Legend2;
		$('CurSelectedThumb').value = ThumbNb;
		//SetNotTransparent($(ThumbNb));
		//SetNotTransparent($('T'+ThumbNb));
		if ($('DefaultImage').visibility != 'hidden') {				
			new Effect.Opacity('DefaultImage', {queue:'front', afterFinish: SelectFirstImage, duration:0.5, from:1.0, to:0.0});
			$('DefaultImage').visibility = 'hidden';
			$('DefaultImage').display = 'none';
		} else {
			new Effect.Opacity('SelectedImageSrc', {queue:'front', afterFinish: MorphBorder, duration:0.5, from:1.0, to:0.0});			
		}					
	}
	
}

function NotifyUpdateSuccessful(transport) {

	Def = transport.responseText.evalJSON(true);
}

function DoGoPage(Page, CategoryId) {
	var URL = 'DisplayThumbnail.php5?page=' + Page + '&categoryId='+ CategoryId;

	$('Page').value = Page;
	$('CurSelectedThumb').value = 1000; //deselection
	new Ajax.Updater('Thumbnail', URL);
}

function GoNextPage(Page, CategoryId) {
	var NewPage = +Page + 1;
	DoGoPage(NewPage, CategoryId);
}

function GoPreviousPage(Page, CategoryId) {
	var NewPage = +Page - 1;
	DoGoPage(NewPage, CategoryId);		
}

function ShowBio(Content) {
	$('AllText').innerHTML = $(Content).innerHTML;	
}

function ShowBioFr() {
	ShowBio('BioFr');
}

function ShowBioEn() {
	ShowBio('BioEn');	
}
