// JavaScript Document
// Copyright (c) 2011 Extrafine

// Mesures de références
var _$h = 19;
var _$z = 4/3*_$h;
// Position 0 des fenêtres par rapport à la gauche du body
var _$Left = Math.round(65/3*_$h);
// Position 0 des fenêtres par rapport au haut du body
var _$Top = Math.round(37/12*_$h);
var _$MenuParent = 0;
// Etat de la fonction Bureau
var _$EXTRA = false;
// Niveau de la fenêtre la plus haute
var _$zIndex = 5;
// Identifiant du timer de l'Apercu
var _$ApercuTimer = null;
// Tempo du changement d'image
var _$Next = false;
// Langue en cours
var _$Langue = "FR";
// Fenêtre en cours
var _$CellCurrent = 0;
// Temps de redimmensionnement du diaporama
var _$CycleResizeTime = 200;
// Fenetre en cours
var _$CellOpen = new Array();

String.prototype.trim = function (){ return this.replace(/(^\s*)|(\s*$)/g,""); }

function PageInit() {

	// ===== MENU =====
	$('#idMenu, .styInfo').disableSelection();
	// CLICK -> Entrer en FR
	$('#idMenuEnterLangFR').click( function() {
		funChangeLang("FR");
		$('#idMenuEnter').hide();
		$('#idAccueil').fadeOut("normal");
		$('#idMenuPart').show("slow");
	});
	// CLICK -> Entrer en EN
	$('#idMenuEnterLangEN').click( function() {
		funChangeLang("EN");
		$('#idMenuEnter').hide();
		$('#idAccueil').fadeOut("normal");
		$('#idMenuPart').show("slow");
	});
	// CLICK -> Changer de Lang en FR
	$('#idMenuLangFR').click( function() {
		funChangeLang("FR");
	});
	// CLICK -> Changer de Lang en EN
	$('#idMenuLangEN').click( function() {
		funChangeLang("EN");
	});
	// MOUSEDOWN -> Descendre le scroll de Part2
	$('#idMenuScrollDown').mousedown( function() {
		 $myMenuScroll.ScrollOn("+");
	});
	// MOUSEDOWN -> Monter le scroll de Part2
	$('#idMenuScrollUp').mousedown( function() {
		 $myMenuScroll.ScrollOn("-");
	});
	// MOUSEUP -> Stopper le scroll
	$('#idMenuScrollDown, #idMenuScrollUp').mouseup( function() {
		$myMenuScroll.ScrollOff();
	});
	// MOUSEOVER -> Afficher l'aperçu
	$('.styMenuObjet').live("mouseover", function(){
		$("#idApercu img").remove();
		$('#idApercu').css('top',$(this).offset().top-1 +"px");
		$('#idApercu').append($(this).children('img').eq(0).clone());
		clearTimeout(_$ApercuTimer);
		_$ApercuTimer = setTimeout(function(){ $('#idApercu').clearQueue().stop().fadeTo("fast",1) },300);
	});
	// MOUSEOUT -> Masquer l'aperçu
	$('.styMenuObjet').live("mouseout", function(){
		$("#idApercu img").remove();
		clearTimeout(_$ApercuTimer);
		$('#idApercu').clearQueue().stop().fadeTo("fast",0);
	});
		// Mise en place du changement d'état des liens
	$('#idMenuPart1 .styMenu, .styMenuLang').hover(
		function() { $(this).addClass('styMenuOn'); },
		function() { $(this).removeClass('styMenuOn'); }
	);
	// Mise en place du scroll de Part2
	$myMenuScroll = new ExtraScroll('idMenuScroll','$myMenuScroll');
	$myMenuScroll.Frequence = 100;
	$myMenuScroll.ScrollPas = _$h - 1 ; // h=19 -> -1 | h=15 -> 0
	$myMenuScroll.ScrollFondu = false;
	$myMenuScroll.ScrollPasHold = true;
	$myMenuScroll.ScrollTagUp.push('idMenuScrollUp');
	$myMenuScroll.ScrollTagDown.push('idMenuScrollDown');
	$myMenuScroll.ScrollInit();
	// Masque Part2
	$('#idMenuPart2').hide();


	//$('idExtrafine').
//	$('#idExtrafine').draggable({
//			cursor: 'crosshair',
//			distance: 20,
//			disabled: false
//		});

	// ===== BUREAU =====
	$('#idEXTRA, #idBorder').disableSelection();
	// CLICK -> Vider le Bureau
	$('#idBorderClear').live("click", function(){
		if(_$MenuParent == 1){
			$('#idEXTRA').html("");
		} else {
			$('#idEXTRAOther').html("");
		}
		_$CellOpen = new Array();
		funSurbrillance();
	});
	$('#idBorderSave').live("click", function(){
		$.cookie('desktop',escape($('#idEXTRA').html()));
	});
	// MOUSEOVER -> Afficher l'info d'EXTRA
	$('.styMenuBureau').live("mouseover", function(){
		if(!_$EXTRA)
			$('#idInfoEXTRA').clearQueue().stop().delay(800).fadeTo("slow",1);
	});
	// MOUSEOUT -> Masquer l'info d'EXTRA
	$('.styMenuBureau').live("mouseout", function(){
		$('#idInfoEXTRA').clearQueue().stop().delay(300).fadeTo("slow",0);
	});
	// MOUSEOVER -> Afficher l'info ViderBureau
	$('#idBorderClear').live("mouseover", function(){
		$('#idInfoClear').clearQueue().stop().delay(800).fadeTo("slow",1);
	});
	// MOUSEOUT -> Masquer l'info ViderBureau
	$('#idBorderClear').live("mouseout", function(){
		$('#idInfoClear').clearQueue().stop().delay(300).fadeTo("slow",0);
	});

	// ===== FENETRE =====
	$('.styCell').disableSelection();

	// MOUSEDOWN -> Mise à premier plan de la Cell
	$('.styCell').live("mousedown", function(){
		var $Id = $(this).attr("id");
		if(_$EXTRA){
			_$zIndex += 5;
			$('#'+ $Id).css('z-index',_$zIndex);
		}
		return false;
	});
	// MOUSEOVER -> Afficher la legende de la Cell
	$('.styCell .styCellContenu, .styCell .styCellLegende').live("mouseover", function(){
		var $Id = $(this).parent().attr("id");
		// Affiche la légende (si elle a du contenu)
		if(_$EXTRA && $('#'+ $Id +' .styCellLegende').text().trim() != "")
			$('#'+ $Id +' .styCellLegende').clearQueue().stop().delay(300).fadeTo("normal",1,function(){$(this).css('display','block')});
	});
	// MOUSEOUT -> Masquer la legende de la Cell
	$('.styCell .styCellContenu, .styCell .styCellLegende').live("mouseout", function(){
		var $Id = $(this).parent().attr("id");
		// Masque la légende (si elle a du contenu)
		//if(_$EXTRA && $('#'+ $Id +' .styCellLegende').text().trim() != "")
		if(_$EXTRA)
			$('#'+ $Id +' .styCellLegende').clearQueue().stop().delay(300).fadeTo("normal",0,function(){$(this).css('display','none')});
	});
	// CLICK -> Fermeture de la Cell
	$('.styCellCloseEntete, .styCellClose').live("click", function(){
		var $Id = $(this).parent().parent().attr("id");
		if($Id.search(/Image/) != -1)
			$Id = $(this).parent().parent().parent().parent().attr("id");
		$("#"+$Id).hide();
		var $Index = parseInt($Id.replace(/idCell/g,""));
		for(i=0; i<_$CellOpen.length; i++){
			if(_$CellOpen[i] == $Index){
				_$CellOpen.splice(i,1);
				i--;
			}
		}
		funSurbrillance();
		if(!_$EXTRA)
			_$CellCurrent = 0;

	});
	// CLICK -> Réduire/Agrandir la Cell via ExtraSize
	$('.styCellResize').live("click", function(){
		var $Id = $(this).parent().parent().parent().parent().attr("id");
		var $Height = 0;
		var $Temps = 0;
		var $Easing = '';

		if($("#"+$Id+" .styCellImage").height() == Math.round(_$h*14)){
			$Height = Math.round(_$h*27);
			$('#'+ $Id +' .styCellResize').attr("src",'img/resizesmall.png')
			$Temps = 1500;
			$Easing = 'easeOutQuint';

		} else {
			$Height = Math.round(_$h*14);
			$('#'+ $Id +' .styCellResize').attr("src",'img/resizebig.png');
			$Temps = 1600;
			$Easing = 'easeOutQuint';
		}

		var _Height = 0;
		var _Width = 0;

		var _Max14 = Math.round(_$h*14);
		var _Max27 = Math.round(_$h*27);

		var _Max = $(this).parent().parent().height() == _Max14 ? _Max27 : _Max14;
		var _ioImg = $(this).parent().children()[0];

		if(_ioImg.naturalHeight <= _Max && _ioImg.naturalWidth <= _Max){
			_Height = _ioImg.naturalHeight;
			_Width = _ioImg.naturalWidth;
		} else {
			if(_ioImg.height >= _ioImg.width){
				_Height = _ioImg.naturalHeight <= _Max ? _ioImg.naturalHeight : _Max;
				_Width = Math.round(_Max * _ioImg.width / _ioImg.height);

			} else {
				_Width = _ioImg.naturalWidth <= _Max ? _ioImg.naturalWidth : _Max;
				_Height = Math.round(_Max * _ioImg.height / _ioImg.width);
			}
		}
		_Height -= 1;
		$(this).parent().parent().parent().animate({height: _Height +'px', width: _Width +'px'}, $Temps, $Easing);

		eval("$"+ $Id +".SizeTo($Height,$Height,5);") // Redimensionnement via ExtraSize
		$('#'+ $Id +' .styCellPlay').css("bottom",(Math.round(1/2*_$h)-2)+'px'); // Recalage de l'icone Play
		$('#'+ $Id +' .styCellResize').css("bottom",(Math.round(1/2*_$h)-2)+'px'); // Recalage de l'icone Resize
	});
	// CLICK -> Play | Pause du diaporama d'une fenêtre
	$('.styCellPlay').live("click",function(){
		var $Id = $(this).parent().parent().parent().parent().attr("id");
		if($(this).attr("src").match(/play/g)){
			$('#'+ $Id +' .styCellImage').cycle('resume');
			$('#'+ $Id +' .styCellPlay').attr("src",'img/pause.png')
		} else {
			$('#'+ $Id +' .styCellImage').cycle('pause');
			$('#'+ $Id +' .styCellPlay').attr("src",'img/play.png')
		}
	});
	// MOUSEDOWN -> Activation du Timer pour le changement d'image du diaporama d'une fenêtre
	$('.styCellShadow').live("mousedown",function(){
		_$Next = true;
		setTimeout('ChangeNext()',400);
	});
	// MOUSEUP -> Changement de l'image du diaporama d'une fenêtre
	$('.styCellShadow').live("mouseup",function(){
		if(_$Next)
			$(this).parent().parent().cycle('next');
	});


	// ===== ACCEUIL =====
	// Choix de l'image d'accueil
	if(_$db.Accueil.length > 0){
		var $Limit = _$db.Accueil.length-1;
		var $Random = Math.floor(Math.random()*11);
		if($Limit == 0) {
			$Random = 0;
		} else {
			while($Random > $Limit){$Random = Math.floor(Math.random()*11);}
		}
		var $HtmlAccueil = "";
		for(i=0; i<_$db.Accueil.length; i++){
			$HtmlAccueil += '<img src="'+ _$dbImgPath + eval("_$db.Accueil["+ i +"]") +'" />';
		}
		$('#idAccueil').html($HtmlAccueil);
		$('#idAccueil').cycle({
			fx: 'fade',
			speed: 'normal',
			timeout: 1000,
			containerResize: 0,
			slideResize: 0
		});
		$('#idAccueil').cycle();
//		$('#idAccueil').html('<img src="'+ _$dbImgPath + eval("_$db.Accueil["+ $Random +"]") +'" />');
	}

	// ===== MAIN =====
	// Masque les éléments propres à EXTRA
	$('#idMenuExtraOff').hide();
	$('.styEXTRA').hide();
	// Par défaut en FR mais mettre en place un cookie
	var $CookieLang = $.cookie('lang');
	if($CookieLang != "" && $CookieLang != null)
		funChangeLang($CookieLang);
	else
		funChangeLang("FR");

	// Affichage de l'entrée du site
	$('#idAccueil').show("fast");
	$(".styMenuLoad").hide();
	$('.styMenuEnter').show();

//	var $CookieDesktop = $.cookie('desktop');
//	if($CookieDesktop != "" && $CookieDesktop != null){
//		if(confirm("Voulez-vous recharger le bureau sauvegardé ?")){
//			$CookieDesktop = unescape($CookieDesktop);
//			$('#idEXTRA').html($CookieDesktop);
//			$('#idMenuPart').show("slow");
//		} else {
//			$('#idAccueil').show("fast");
//			$('#idMenuEnter').show("fast");
//		}
//	} else {
//		$('#idAccueil').show("fast");
//		$('#idMenuEnter').show("fast");
//	}
}

// Construire d'une fenêtre
function funCellBuild(pId) {

	$IdGo = !_$EXTRA ? 'idCell0' : pId;
	$CellClass = 'styCell';

	// Génération du Diaporama
	var $CellImage = "";
	var $CellNbImage = eval("_$db."+pId+".Image.length");
	if($CellNbImage > 0){
		$CellImage = '<div id="'+ $IdGo +'Image" class="styCellImage" style="position:relative;">';
		if($CellNbImage == 1){
			$CellImage += '<div style="position:absolute; z-index:3;"><img src="'+ _$dbImgPath + eval("_$db."+pId+".Image[0]") +'" class="styCellShadow" /><img src="img/closeimg.png" class="styCellClose styEXTRA" /><img src="img/resizesmall.png" class="styCellResize styEXTRA" /></div>';
		} else {
			for(i=0; i<$CellNbImage; i++) {
				$CellImage += '<div><img src="'+ _$dbImgPath + eval("_$db."+pId+".Image["+ i +"]") +'" class="styCellShadow" /><img src="img/play.png" class="styCellPlay" /><img src="img/closeimg.png" class="styCellClose styEXTRA" /><img src="img/resizesmall.png" class="styCellResize styEXTRA" /></div>';
			}
		}
		$CellImage += '</div>';
	}

	// Génération de Contenu HTML
	var $CellContenuHTML = "";
	var $CellContenu = eval("_$db."+pId+".Contenu");
	if(typeof($CellContenu) != 'undefined'){
		$CellContenuHTML = '<div id="'+ $IdGo +'Contenu" class="styCellContenuHTML" style="position:relative;"><span class="langFR">'+ $CellContenu.FR +'</span><span class="langEN">'+ $CellContenu.EN +'</span></div>';
		$CellClass = 'styCellHTML';
	}

	// Destruction & Création de la fenêtre
	$('#'+ $IdGo).remove();
	switch($CellClass){
		case 'styCell' :
			if(_$MenuParent == 1){
				$('#idEXTRA').append('<div id="'+ $IdGo +'" class="'+ $CellClass +'">'+ $('#idCell_id').html() +'</div>');
			} else {
				$('#idEXTRAOther').append('<div id="'+ $IdGo +'" class="'+ $CellClass +'">'+ $('#idCell_id').html() +'</div>');
			}
			break;
		case 'styCellHTML' :
			$('#idEXTRAText').append('<div id="'+ $IdGo +'" class="'+ $CellClass +'">'+ $('#idCell_id').html() +'</div>');
			break;
	}

	// Remplissage des champs de la fenêtre
	$('#'+ $IdGo +' .langFR .styCellTitre').text(eval("_$db."+pId+".Titre.FR") +" ");
	$('#'+ $IdGo +' .langFR .styCellLib').text(eval("_$db."+pId+".Lib.FR") +" ");
	$('#'+ $IdGo +' .langFR .styCellInfo').text(eval("_$db."+pId+".Info.FR") +" ");
	$('#'+ $IdGo +' .styCellLegende .langFR').html(eval("_$db."+pId+".Legende.FR"));

	$('#'+ $IdGo +' .langEN .styCellTitre').text(eval("_$db."+pId+".Titre.EN") +" ");
	$('#'+ $IdGo +' .langEN .styCellLib').text(eval("_$db."+pId+".Lib.EN") +" ");
	$('#'+ $IdGo +' .langEN .styCellInfo').text(eval("_$db."+pId+".Info.EN") +" ");
	$('#'+ $IdGo +' .styCellLegende .langEN').html(eval("_$db."+pId+".Legende.EN"));

	// Ajouter du "target" pour les liens des légendes
	$(".styCellLegende a").attr('target','_blank');

	// Destruction du diaporama
	$('#'+ $IdGo +' .styCellImage').cycle('destroy');
	$('#'+ $IdGo +' .styCellContenu').html('');



	// Remplisage de CellContenu
	$('#'+ $IdGo +' .styCellContenu').html($CellContenuHTML + $CellImage);
	funChangeLang(_$Langue);

	// Mise en place du diaporama
	if($CellImage != ""){
	//if($CellNbImage > 0){
		$('#'+ $IdGo +' .styCellImage').cycle({
			fx:     'fade',
			speed:   'fast',
			timeout: 5000,
			before: funOnBeforeCycle,
			//after: funOnAfterCycle,
			//next:   '#'+ $IdGo +' .styCellShadow',
			containerResize: 0,
			slideResize:0
		});
		// Mise en pause par défaut du diaporama
		$('#'+ $IdGo +' .styCellImage').cycle('pause');

		// Création d'ExtraSize
		eval("$"+ $IdGo +" = new Extra('"+ $IdGo +"Image','$"+ $IdGo +"');")
		eval("$"+ $IdGo +"._Size = true;")
		eval("$"+ $IdGo +".Constructor();")
		eval("$"+ $IdGo +".SizeChild = 4;")
		eval("$"+ $IdGo +".SizeNot.push('styCellPlay');")
		eval("$"+ $IdGo +".SizeNot.push('styCellClose');")
		eval("$"+ $IdGo +".SizeNot.push('styCellResize');")
		eval("$"+ $IdGo +".Init();")
		eval("$"+ $IdGo +".Width = Math.round(_$h*27);")
		eval("$"+ $IdGo +".Height = Math.round(_$h*27);")

		setTimeout('funInitPosLegende("'+$IdGo+'")',60);
	}

	// Mise en forme selon Mode Actif (EXTRA ou pas)
	if(!_$EXTRA){
		$('#'+ $IdGo +' .styEXTRA').hide();
	} else {
		$('.styCellShadow').addClass('styEXTRAShadow');
	}
}

var _Height = 0;
var _Width = 0;


function funInitPosLegende($IdGo){
	var _Max = Math.round(_$h*27);
   	var _ioImg = $('#'+ $IdGo +' .styCellImage img')[0];

    if(_ioImg.naturalHeight <= _Max && _ioImg.naturalWidth <= _Max){
        _Height = _ioImg.naturalHeight;
        _Width = _ioImg.naturalWidth;
    } else {
        if(_ioImg.height >= _ioImg.width){
            _Height = _ioImg.naturalHeight <= _Max ? _ioImg.naturalHeight : _Max;
            _Width = Math.round(_Max * _ioImg.width / _ioImg.height);
        } else {
            _Width = _ioImg.naturalWidth <= _Max ? _ioImg.naturalWidth : _Max;
            _Height = Math.round(_Max * _ioImg.height / _ioImg.width);
        }
    }
	$('#'+ $IdGo +' .styCellContenu').height(_Height);
    $('#'+ $IdGo +' .styCellContenu').width(_Width);
}

function funOnAfterCycle(){
	if(_Height == 0 && _Width == 0){
		var _Max14 = Math.round(_$h*14);
		var _Max27 = Math.round(_$h*27);

		var _Max = $(this).parent().height() == _Max14 ? _Max14 : _Max27;
		var _ioImg = $(this).children()[0];

		if(_ioImg.naturalHeight <= _Max && _ioImg.naturalWidth <= _Max){
			_Height = _ioImg.naturalHeight;
			_Width = _ioImg.naturalWidth;
		} else {
			if(_ioImg.height >= _ioImg.width){
				_Height = _ioImg.naturalHeight <= _Max ? _ioImg.naturalHeight : _Max;
				_Width = Math.round(_Max * _ioImg.width / _ioImg.height);
			} else {
				_Width = _ioImg.naturalWidth <= _Max ? _ioImg.naturalWidth : _Max;
				_Height = Math.round(_Max * _ioImg.height / _ioImg.width);
			}
		}
		$(this).parent().parent().height(_Height);
		$(this).parent().parent().width(_Width);
	}
}

// Repositionnement de la légende lors d'une transition
function funOnBeforeCycle(){

	_Height = 0;
	_Width = 0;

	var _Max14 = Math.round(_$h*14);
	var _Max27 = Math.round(_$h*27);

	var _Max = $(this).parent().height() == _Max14 ? _Max14 : _Max27;
	var _ioImg = $(this).children()[0];

	if(_ioImg.naturalHeight <= _Max && _ioImg.naturalWidth <= _Max){
		_Height = _ioImg.naturalHeight;
		_Width = _ioImg.naturalWidth;
	} else {
		if(_ioImg.height >= _ioImg.width){
			_Height = _ioImg.naturalHeight <= _Max ? _ioImg.naturalHeight : _Max;
			_Width = Math.round(_Max * _ioImg.width / _ioImg.height);

		} else {
			_Width = _ioImg.naturalWidth <= _Max ? _ioImg.naturalWidth : _Max;
			_Height = Math.round(_Max * _ioImg.height / _ioImg.width);

		}
	}
	if(this.cycleH == 0 && this.cycleW == 0){
		$(this).parent().parent().height(_Height);
		$(this).parent().parent().width(_Width);
	} else {
		$(this).parent().parent().animate({height: _Height +'px', width: _Width +'px'}, _$CycleResizeTime);
	}
}


// Changer de langue
function funChangeLang(pLang) {
	switch(pLang){
		case "FR" :
			_$Langue = "FR";
			$('.langEN').hide();
			$('.langFR').show();
			$.cookie('lang',"FR");
			break;
		case "EN" :
			_$Langue = "EN";
			$('.langFR').hide();
			$('.langEN').show();
			$.cookie('lang',"EN");
			break;
	}
}

// Activer / Désactiver le mode EXTRA
function funEXTRA(pVal) {
	if(pVal) {  // -> Activer EXTRA
		_$EXTRA = true;
		// Rennome l'id de la Cell0
		if(_$CellCurrent != 0){
			$('#idCell0').remove();
			funMenuClick(3,1,_$CellCurrent);
			_$CellCurrent = 0;
		}
		// Modification de l'apparence EXTRA
		$('#idMenuExtraOn').hide();
		$('#idMenuExtraOff').show();
		$('.styEXTRA').fadeTo(1500,1);
		$('.styCellShadow').addClass('styEXTRAShadow');
		// Masque les légendes (qui ont du contenu)
		$('.styCellLegende').fadeTo(1500,0);
		// Activation du déplacement des fenêtres
		$('.styCell').draggable({
			cursor: 'crosshair',
			handle: '.styCellShadow, .styCellEntete',
			distance: 13,
			disabled: false
		});
		$('#idEXTRA, #idEXTRAOther').draggable({
			cursor: 'crosshair',
			disabled: false
		});
		// Masque la bulle informative
		$('#idInfoEXTRA').clearQueue().stop().delay(300).fadeTo("slow",0);

	} else {  // -> Désactiver EXTRA
		_$CellOpen = new Array();
		funSurbrillance();
		_$EXTRA = false;
		_$zIndex = 5;
		// Modification de l'apparence EXTRA
		$('#idMenuExtraOff').hide();
		$('#idMenuExtraOn').show();
		$('.styEXTRA').fadeTo("slow",0);
		$('.styCell:not(#idCell0)').hide();
		$('.styCellShadow').removeClass('styEXTRAShadow');
		// Affiche la légende (si elle a du contenu)
		$('.styCellLegende').fadeTo("slow",1);
		/*$('.styCellLegende').each( function(){
			if($(this).text().trim() != "")
				$(this).fadeTo("slow",1);
		});*/
		//$('.styCellLegende').fadeTo("slow",1);
		// Désactivation du déplacement des fenêtres
		$('.styCell').draggable({
			disabled: true
		});
		$('#idEXTRA, #idEXTRAOther').draggable({
			cursor: 'crosshair',
			disabled: true
		});
		// Repositionnement de la fenêtre zéro
		$('#idEXTRA, #idEXTRAOther').css("left",'-5000px');
		$('#idEXTRA, #idEXTRAOther').css("top",'-5000px');
		var $Left = 0;
		var $Top = 0;
		if(_$MenuParent == 1){
			$Left = _$Left + (-1 * parseInt($('#idEXTRA').css("left")));
			$Top = _$Top + (-1 * parseInt($('#idEXTRA').css("top")));
		} else {
			$Left = _$Left + (-1 * parseInt($('#idEXTRAOther').css("left")));
			$Top = _$Top + (-1 * parseInt($('#idEXTRAOther').css("top")));
		}
		$('#idCell0').css('left',$Left +'px');
		$('#idCell0').css('top',$Top +'px');
		$('#idCell0').css('z-index',_$zIndex);
		// Redimensionnement via ExtraSize
		$Height = Math.round(_$h*27);
		if(typeof($idCell0) != 'undefined')
			$idCell0.SizeToH($Height);
	}
}



function funSurbrillance(){
	$('#idMenuPart2 .styMenuObjet').removeClass('styMenuOn');
	$('#idMenuPart2 .styMenuObjet').hover(
		function() { $(this).addClass('styMenuOn'); },
		function() { $(this).removeClass('styMenuOn'); }
	);

	for(i=0; i<_$CellOpen.length; i++){
		$('#idMenuObjet'+_$CellOpen[i]).addClass('styMenuOn');
		$('#idMenuObjet'+_$CellOpen[i]).hover(
			function() { $(this).addClass('styMenuOn'); },
			function() { $(this).addClass('styMenuOn'); }
		);
	}
}


function funRealisation(pId,pSlide){
	// Vide le bureau
	$('#idEXTRA').hide("normal").css('display','none');

	//_$CellOpen = new Array();
	//funSurbrillance();

	// Masque la Part2
	$('#idMenuPart2').hide("slow");

	// Fermeture sous-menu
	if(pSlide){
		$('#idMenuSous1').slideUp();
		_$MenuParent = 0;
	}
}


// Cliquer sur Menu
function funMenuClick(pType,pAction,pId) {
//	pType : 1= Partie Gauche
//			2= Partie Droite
//			3= Appel de funEXTRA
//	pAction : 	1= Ouvrir d'une entité
//				2= Remplir la Part2
//				3= Dérouler sous-menu
//	pId : Identifiant
//
	// SI :
	//	- Origine "Part1"
	//	- Parent "Part1"
	//	- Accordeon Ouvert
	if(pType == 1 && $("#idMenu"+ pId).parent().attr("id") == "idMenuPart1" && $('#idMenuSous1').css('display') == 'block') {
		funRealisation(pId,pAction == 3 ? false : true);
	}
	switch(pAction) {
		case 1 :  // -> Ouvrir d'une entité
			// Masque la Part2
			if(pType == 1)
				$('#idMenuPart2').hide("slow");

			if(_$MenuParent == 0)
				$('#idEXTRAOther').show("normal");

			$('#idEXTRAText').html('');
			if(_$EXTRA) {
				if(eval("_$CellOpen.join().search(/"+pId+"/g)") == -1)
					_$CellOpen.push(pId);
				funSurbrillance();
				// Vérification que la fenêtre n'est pas déjà ouverte
				if($('#idCell'+ pId).css('display') != "block") {
					// Analyse de l'ouverture en Cascade de la fenêtre
					var $Left = 0;
					var $Top = 0;
					if(_$MenuParent == 1){
						$Left = _$Left + (-1 * parseInt($('#idEXTRA').css("left")));
						$Top = _$Top + (-1 * parseInt($('#idEXTRA').css("top")));
					} else {
						$Left = _$Left + (-1 * parseInt($('#idEXTRAOther').css("left")));
						$Top = _$Top + (-1 * parseInt($('#idEXTRAOther').css("top")));
					}
					$('.styCell').each( function(){
						$('.styCell').each( function(){
							//if($(this).offset().left == $Left && $(this).offset().top == $Top){
							if($(this)[0].offsetLeft == $Left && $(this)[0].offsetTop == $Top){
								$Left += Math.round(1/2*_$z);
								$Top += Math.round(1/2*_$z);
								return false;
							}
						});
					});
					// Construction et Remplissage de la fenêtre
					funCellBuild('idCell'+ pId);
					// Positionnement de la fenêtre au coordonnées analysées
					if($('#idCell'+ pId).attr('class').search(/styCellHTML/g) != -1){
						$('#idCell'+ pId).css('left',_$Left +"px");
						$('#idCell'+ pId).css('top',_$Top +"px");
						// Positionnement de la fenêtre au premier plan
						$('#idCell'+ pId).css('z-index',8000);
					} else {
						$('#idCell'+ pId).css('left',$Left +"px");
						$('#idCell'+ pId).css('top',$Top +"px");
						// Positionnement de la fenêtre au premier plan
						_$zIndex += 5;
						$('#idCell'+ pId).css('z-index',_$zIndex);
					}
				} else {
					// Positionnement de #idEXTRA
					if(_$MenuParent == 1){
						$('#idEXTRA').css("left",'-'+ ($('#idCell'+ pId)[0].offsetLeft - _$Left) +'px');
						$('#idEXTRA').css("top",'-'+ ($('#idCell'+ pId)[0].offsetTop - _$Top) +'px');
					} else {
						$('#idEXTRAOther').css("left",'-'+ ($('#idCell'+ pId)[0].offsetLeft - _$Left) +'px');
						$('#idEXTRAOther').css("top",'-'+ ($('#idCell'+ pId)[0].offsetTop - _$Top) +'px');
					}
					// Positionnement de la fenêtre au premier plan
					_$zIndex += 5;
					$('#idCell'+ pId).css('z-index',_$zIndex);
				}
				// Activation du déplacement des fenêtres
				$('.styCell').draggable({
					cursor: 'crosshair',
					handle: '.styCellShadow, .styCellEntete',
					distance: 13,
					disabled: false
				});



				$('#idCell'+ pId +' .styCellLegende').css('z-index',0);
				// Affichage de la fenêtre
				if(pType == 3){
					$('#idCell'+ pId).show();
				} else {
					$('#idCell'+ pId).fadeIn("fast");
				}
				// Mise en Surbrillance du Menu
				if(pType == 2){
					$('#idMenuObjet'+pId).addClass('styMenuOn');
					$('#idMenuObjet'+pId).hover(
						function() { $(this).addClass('styMenuOn'); },
						function() { $(this).addClass('styMenuOn'); }
					);
				} else if(pType == 1){
					$("#idMenuPart1 > .styMenu").removeClass('styMenuOn');
					$("#idMenuPart1 > .styMenu").hover(
						function() { $(this).addClass('styMenuOn'); },
				    	function() { $(this).removeClass('styMenuOn'); }
					);
					$('#idMenu'+pId).addClass('styMenuOn');
					$('#idMenu'+pId).hover(
						function() { $(this).addClass('styMenuOn'); },
						function() { $(this).addClass('styMenuOn'); }
					);
				}
			} else {
				_$CellCurrent = pId;
				var $Left = 0;
				var $Top = 0;
				if(_$MenuParent == 1){
					$Left = _$Left + (-1 * parseInt($('#idEXTRA').css("left")));
					$Top = _$Top + (-1 * parseInt($('#idEXTRA').css("top")));
				} else {
					$Left = _$Left + (-1 * parseInt($('#idEXTRAOther').css("left")));
					$Top = _$Top + (-1 * parseInt($('#idEXTRAOther').css("top")));
				}

				// Masquage la fenêtre
				$('.styCell').hide();
				// Construction et Remplissage de la fenêtre
				funCellBuild('idCell'+ pId);
				// Positionnement de la fenêtre au coordonnées analysées
				if($('#idCell0').attr('class').search(/styCellHTML/g) != -1){
					$('#idCell0').css('left',_$Left +"px");
					$('#idCell0').css('top',_$Top +"px");
				} else {
					$('#idCell0').css('left',$Left +"px");
					$('#idCell0').css('top',$Top +"px");
				}

				// Désactivation du déplacement des fenêtres
				$('.styCell').draggable({
					cursor: 'crosshair',
					disabled: true
				});
				//// Positionnement de la fenêtre au coordonnées initiale
				//$('#idCell0').css('left',$Left +"px");
				//$('#idCell0').css('top',$Top +"px");
				// Positionnement de la fenêtre au niveau initial
				$('#idCell0').css('z-index',_$zIndex);
				$('#idCell'+ pId +' .styCellLegende').css('z-index',0);
				// Affichage de la fenêtre
				$('#idCell0').fadeIn("fast");
				// Réinitialisation des changements d'état des liens de Part2
				$('#idMenuPart2 .styMenuObjet').removeClass('styMenuOn');
				$('#idMenuPart2 .styMenuObjet').hover(
					function() { $(this).addClass('styMenuOn'); },
					function() { $(this).removeClass('styMenuOn'); }
				);
				// Mise en Surbrillance du Menu
				if(pType == 2){
					$('#idMenuObjet'+pId).addClass('styMenuOn');
					$('#idMenuObjet'+pId).hover(
						function() { $(this).addClass('styMenuOn'); },
						function() { $(this).addClass('styMenuOn'); }
					);
				} else if(pType == 1){
					$("#idMenuPart1 > .styMenu").removeClass('styMenuOn');
					$("#idMenuPart1 > .styMenu").hover(
						function() { $(this).addClass('styMenuOn'); },
				    	function() { $(this).removeClass('styMenuOn'); }
					);
					$('#idMenu'+pId).addClass('styMenuOn');
					$('#idMenu'+pId).hover(
						function() { $(this).addClass('styMenuOn'); },
						function() { $(this).addClass('styMenuOn'); }
					);
				}
			}

			//Masquer l'aperçu
			$("#idApercu img").remove();
			clearTimeout(_$ApercuTimer);
			$('#idApercu').clearQueue().stop().fadeTo("fast",0);
			break;
		case 2 :  // -> Remplir la Part2
			// Masque la Part pour réinitialisé l'animation
			$('#idMenuPart2').hide();
			$.ajax({
				type: 'POST',
				url: 'ajax.php',
				dataType: 'html',
				data: 'id='+pId,
				success: function(html) {
					// Si c'est un lien d'un sous-menu
					if(_$MenuParent == 0) {
						$('#idEXTRAOther').show("normal");
						$("#idMenuPart1 > .styMenu").removeClass('styMenuOn');
						$("#idMenuPart1 > .styMenu").hover(
							function() { $(this).addClass('styMenuOn'); },
							function() { $(this).removeClass('styMenuOn'); }
						);
						$("#idMenuPart1 #idMenu"+ pId).addClass('styMenuOn');
						$("#idMenuPart1 #idMenu"+ pId).hover(
							function() { $(this).addClass('styMenuOn'); },
							function() { $(this).addClass('styMenuOn'); }
						);
					} else {
						// Suppression de la surbrillance des liens du sous-menu
						$('#idMenuSous'+ _$MenuParent +' .styMenuSous').removeClass('styMenuOn');
						// Mise en place des changements d'état de TYPE 2 des liens du sous-menu
						$('#idMenuSous'+ _$MenuParent +' .styMenuSous').hover(
							function() { $(this).addClass('styMenuOn'); },
							function() { $(this).removeClass('styMenuOn'); }
						);
						// Mise en surbrillance du lien concerné du sous-menu
						$('#idMenuSous'+ _$MenuParent +' #idMenu'+pId).addClass('styMenuOn');
						// Désactivation du changement d'état du lien du sous-menu concerné
						$('#idMenuSous'+ _$MenuParent +' #idMenu'+pId).hover(
							function() { $(this).addClass('styMenuOn'); },
							function() { $(this).addClass('styMenuOn'); }
						);
					}
					// Récupération du contenu de la balise du lien cliqué pour titre Part2
					$('#idMenuPart2 .styMenuTitre').html($('#idMenu'+pId).html());

					// Remplit le contenu de Part2 avec 'IdMemoMenu' préchargé
					//$('#idMenuPart2 .styMenuScrollPart2').html($('#idMemoMenu'+pId).html());
					$('#idMenuPart2 .styMenuScrollPart2').html(html);

					// Mise en place des changements d'état des liens de Part2
					$('#idMenuPart2 .styMenuObjet').hover(
						function() { $(this).addClass('styMenuOn'); },
						function() { $(this).removeClass('styMenuOn'); }
					);
					// Mise à jour de la langue de Part2
					funChangeLang(_$Langue);
					// Affiche la Part2
					$('#idMenuPart2').show("slow");
					// Mise à jour des flèches indicatives
					$myMenuScroll.ScrollMaj(true);
					funSurbrillance();
				}
			});
			break;
		case 3 :  // -> Dérouler sous-menu
			// Masque la Part2
			$('#idMenuPart2').hide("slow");

			if(pId == 1 && $('#idMenuSous'+pId).css('display') == 'none'){
				$('#idEXTRA').show("normal");
				$('#idEXTRAOther').hide("normal");
				$('#idEXTRAText').html('');
			}

			$("#idMenuPart1 > .styMenu").removeClass('styMenuOn');
			$("#idMenuPart1 > .styMenu").hover(
				function() { $(this).addClass('styMenuOn'); },
		    	function() { $(this).removeClass('styMenuOn'); }
			);

			//if($('#idMenu'+pId).hasClass('styMenuOn')) {  // -> Sous-menu fermé
			if($('#idMenuSous'+pId).css('display') == 'none') {  // -> Sous-menu fermé
				// Désactivation du changement d'état du lien du menu concerné
				$('#idMenu'+pId).hover(
					function() { $(this).removeClass('styMenuOn'); },
					function() { $(this).removeClass('styMenuOn'); }
				);
				_$MenuParent = pId;
			} else {  // -> Sous-menu ouvert
				// Re-Mise en place du changement d'état du lien du menu concerné
				$('#idMenu'+pId).hover(
					function() { $(this).addClass('styMenuOn'); },
					function() { $(this).removeClass('styMenuOn'); }
				);
				_$MenuParent = 0;
			}
			// Inversion de la surbrillance du menu
			$('#idMenu'+pId).toggleClass('styMenuOn');

			// Mise en surbrillance des liens du sous-menu
			$('#idMenuSous'+pId+' .styMenuSous').addClass('styMenuOn');
			// Mise en place des changements d'état de TYPE 1 des liens du sous-menu
			$('#idMenuSous'+pId+' .styMenuSous').hover(
				function() { $(this).removeClass('styMenuOn'); },
				function() { $(this).addClass('styMenuOn'); }
			);

			// Ouverture / Fermeture sous-menu
			$('#idMenuSous'+pId).slideToggle();
			break;
	}
}

// Désactivation du changement d'image
function ChangeNext(){
	_$Next = false;
}

