	// ----------------------------------------------------------------------------------------------------------------------------
//  SCRIPT Emanence.net
//		- pilote l'affichage du site
//		- nécessite jquery et le(s) plugin(s) suivant(s)
//					dimension
//
//	Auteur : Philippe Piveron	
// ----------------------------------------------------------------------------------------------------------------------------
if (typeof(src)=='undefined'){
	var src = null;
} 

var ieMaxVer		= null;

var aMem							= new Array();
	aMem['refreshPortfolio']		= true;							// rafraichissement du portfolio (true/false)
	aMem['refreshPortfolioDelay']	= 30;							// delai en secondes avant le rafraichissement du portfolio
	aMem['hideFlashContentDelay']	= 10;							// delai en secondes avant la suppression éventuelle du message flash
	aMem['fadeInPortfolioDelay']	= 600;							// delai en millisecondes pour afficher en fadein les images
	// Ne toucher aux variables ci-dessous qu'n toute connaissance de cause.
	aMem['goldNumber']				= 1.618;						// interne, réservé : Nombre d'or ((racine(5)-1)/2)
	aMem['freqCheck']				= 50;							// interne, réservé : frequence de check des changements intervenant sur la page
	aMem['showMeDelay']				= 750;							// interne, réservé : 
	aMem['counterCheck']			= 0;							// interne, réservé :
	aMem['screenWidth']				= 0;							// interne, réservé :
	aMem['screenHeight']			= 0;							// interne, réservé :
	aMem['oldScreenWidth']			= 0;							// interne, réservé :
	aMem['oldScreenHeight']			= 0;							// interne, réservé :
	aMem['handleMainReInitDelay']	= 120;							// réinitialise le handle toutes les n secondes
	aMem['handleMain']				= null;							// interne, réservé : handle du gestionnaire d'interruption prinicipal
	aMem['portfolioHome']			= null;							// interne, réservé : layer portfolio
	aMem['portFolioHeight']			= 64;							// interne, réservé : layer portfolio
	aMem['mainBoxWidth']			= 800;							// interbe, réservé : layer principale.
	aMem['logoBoxWidth']			= 242;							// interbe, réservé : layer logo.
	//aMem['flashNewsHeight']			= 300;							// interbe, réservé : layer falsh/actualités.
	// scripts sollicités via Ajax 
	aMem['scriptGetPortfolio']		= 'spip.php?page=portfolio';

// --- Execution des scripts sur document.ready
$(document).ready(
	function(){
		aMem['handleMain'] = setInterval(fctCheckChange,aMem['freqCheck']);
	}
)

// --- Initialisation des variables globales selon les navigateurs
function fctInit(){
	if($.browser.msie){
		var reg = /MSIE ([0-9])/gi;
		var aMatch = reg.exec(navigator.appVersion);
		ieMaxVer = aMatch[1];
	} else  {
		ieMaxVer = 0;
	}
	aMem['screenWidth'] 	= $(window).width();
	aMem['screenHeight'] 	= $(window).height();
}

// --- Fonction définissant la taille des boxes en fonction de la résolution de l'écran.
function fctSetMainBoxes(){	
	src = (src!=null)?src:'home'
	var mainBoxHeight 	= Math.floor(aMem['mainBoxWidth']/aMem['goldNumber']);
	var widthMargins 	= Math.floor((aMem['screenWidth']-aMem['mainBoxWidth'])/2);
	var heightMargins 	= Math.floor((aMem['screenHeight']-mainBoxHeight)/2);
	
	switch(src){
		// page rubrique
		case 'feature':
			var lWidthMargins 	= (ieMaxVer==6)?widthMargins/2:widthMargins 		// Corrige un bug de positionnement sous IE 6
			intBoxWidth = aMem['mainBoxWidth']-20
			var widthRightBoxes = intBoxWidth-(2*10);
			
			oMainBox 				= $('#box_'+src);
			oQuickAccess			= $('#box_'+src+' .quickAccess'); 
			oTitleBox 				= $('#box_'+src+' .title');
			oZoneGalleryList		= $('#box_'+src+' .zoneGalleryList');
			aMem['portfolioHome']	= $('#box_'+src+' .portfolioHome');
			oContent				= $('#box_'+src+' .content');
			oCopyBox 				= $('#box_'+src+' .copyRight');
			
			oMainBox.css('width',intBoxWidth);
			oMainBox.css('height',mainBoxHeight);
			//alert(oMainBox.css('width'))
			oMainBox.css('margin-left',lWidthMargins);
			oMainBox.css('margin-right',lWidthMargins);
			oMainBox.css('margin-top',heightMargins);
			
			oQuickAccess.css('height',14);
			oQuickAccess.css('width',widthRightBoxes);
			//oQuickAccess.css('width',widthRightBoxes);
			oTitleBox.css('height',60);
			oTitleBox.css('width',229);
			//alert(oTitleBox.css('width'))
			aMem['portfolioHome'].css('width',intBoxWidth-20-fctGetValue(oTitleBox.css('width')));
			aMem['portfolioHome'].css('height',aMem['portFolioHeight']);
		
			oTitleBox.css('height',fctGetValue(aMem['portfolioHome'].css('height'))-2);
					
			oCopyBox.css('height',15);
			oCopyBox.css('width',widthRightBoxes);

			oContent.css('height',mainBoxHeight-(20+fctGetValue(oQuickAccess.css('height'))+fctGetValue(aMem['portfolioHome'].css('height'))+fctGetValue(oCopyBox.css('height'))));
			
			if(oZoneGalleryList!=null){
				oZoneGalleryList.css('width',fctGetValue(oTitleBox.css('width'))+4)
			}
			break
		// home
		default:
			var lWidthMargins 	= (ieMaxVer==6)?widthMargins/2:widthMargins 		// Corrige un bug de positionnement sous IE 6
			var widthRightBoxes = aMem['mainBoxWidth']-aMem['logoBoxWidth']-(3*10);
			
			oMainBox 				= $('#box_'+src);
			
			oLogoBox 				= $('#box_'+src+' .logo');
			oQuickAccess			= $('#box_'+src+' .quickAccess'); 
			oTitleBox 				= $('#box_'+src+' .title');
			oFlashNews				= $('#box_'+src+' .flashNews');
			oFlashNewsContent		= $('#box_'+src+' .flashNews .flashNewsContent');
			aMem['portfolioHome']	= $('#box_'+src+' .portfolioHome');
			oEpiCopyRight	= $('#box_'+src+' .epiCopyRight');
			
			oCopyBox 		= $('#box_'+src+' .copyRight');
			oLangBox 		= $('#box_'+src+' .setLang');
			
			oMainBox.css('width',aMem['mainBoxWidth']);
			oMainBox.css('height',mainBoxHeight);
			//alert(oMainBox.css('width'))
			oMainBox.css('margin-left',lWidthMargins);
			oMainBox.css('margin-right',lWidthMargins);
			oMainBox.css('margin-top',heightMargins);
			
			oQuickAccess.css('height',14);
			oQuickAccess.css('width',widthRightBoxes);
			oTitleBox.css('height',60);
			oTitleBox.css('width',widthRightBoxes);
			
			aMem['portfolioHome'].css('width',widthRightBoxes);
			aMem['portfolioHome'].css('height',aMem['portFolioHeight']);
		
			oEpiCopyRight.css('height',15);
			oEpiCopyRight.css('width',widthRightBoxes);
			
			oLangBox.css('width',aMem['logoBoxWidth']);
		
			oCopyBox.css('height',15);
			oCopyBox.css('width',widthRightBoxes);
			
			oFlashNews.css('width',widthRightBoxes);
			oFlashNews.css('height',oMainBox.height()-(2*fctGetValue(oQuickAccess.css('margin-top')))-(fctGetValue(aMem['portfolioHome'].css('height'))+fctGetValue(oCopyBox.css('height'))+fctGetValue(oEpiCopyRight.css('height'))+fctGetValue(oTitleBox.css('height'))+fctGetValue(oQuickAccess.css('height'))));
		
			oFlashNewsContent.css('height',fctGetValue(oFlashNews.css('height'))-(2*fctGetValue(oQuickAccess.css('margin-top'))));
			oFlashNewsContent.css('width',Math.floor(fctGetValue(oFlashNews.css('width'))/2));
			break
	}
}

// --- Fonction Générique de détection des changements globaux
function fctCheckChange(){
	src = (src!=null)?src:'home'
	if($('#box_'+src)!=null){
		if(fctCheckChangeOnSize()){
			fctSetMainBoxes();
			$('#box_'+src+':hidden').fadeIn	(
												3000,
												function()	{
																fctDspPortFolio(src);
															}
											);		
		}
		// Reload du portfolio si actif
		aMem['counterCheck']++
		if(aMem['refreshPortfolio']&&!((aMem['counterCheck']*aMem['freqCheck'])%(aMem['refreshPortfolioDelay']*1000)))fctDspPortFolio(src);
		if(aMem['hideFlashContentDelay']&&!((aMem['counterCheck']*aMem['freqCheck'])%(aMem['hideFlashContentDelay']*1000)))fctHideLayer('.flashNewsContent');
	}
}

// -- Détection d'un changement de taille écran 
function fctCheckChangeOnSize(){
	var changed = false;
	fctInit();
	if(aMem['screenWidth'] != aMem['oldScreenWidth']||aMem['screenHeight'] != aMem['oldScreenHeight'])changed = true;
	aMem['oldScreenWidth'] = aMem['screenWidth'];
	aMem['oldScreenHeight'] = aMem['screenHeight'];
	return changed;	
}

// -- Fonctions de gestion de l'affichage des portfolios
// 		Aiguillage
function fctDspPortFolio(sType){
	sType = typeof(sType)=='string'?sType:'';
	switch(sType){
		// -- affichage du portfolio de la homepage
		case 'home':
			fctSetPortFolio(aMem['portfolioHome']);
			break;
		case 'feature':
			fctSetPortFolio(aMem['portfolioHome']);
			break;
	}
}

//  	Setting
function fctSetPortFolio(oB){
	var hZ 	= oB.height(); 								// hauteur zone (portfolio)
	var wZ 	= oB.width()-2;								// largeur zone (décompte d'un pixel à droite et à gauche)
	var hB 	= oB.height() - 2;							// hauteur des vignettes dans la zone
	var wB 	= Math.floor(hB * aMem['goldNumber']);		// largeur des vignettes dans la zone (on applique le produit du nombre d'or)
	var sHV = 3	;										// espace horizontal entre les vignettes
	var nV 	= Math.floor(wZ/wB);						// détermine le nombre maximum de vignettes 'insérables' 
	while(((nV-1)*sHV)+(nV*wB)>wZ){	
		nV--;									// supérieur à la largeur de la zone ! 
	}
	inport = $('#box_import').empty().html(fctAjaxModule(aMem['scriptGetPortfolio'],''));
	
	// Extraction des logo a afficher dans la zone
	var obj = inport.find('img');
	if(!obj.size())return;
	nV = (obj.size()<nV)?obj.size():nV;
	html =''
	for(var i=0; i < nV ;i++){					// génération et insertion des vignettes
	 	html += '<span class="showMe" style="display:none;">';
		//alert(obj.eq(i).parent().attr('href'))
		html += '	<a href="'+(obj.eq(i).parent().attr('href'))+'"><img src="'+obj.eq(i).attr('src')+'" style="width:'+wB+'px;height:'+hB+'px;" alt="" /></a>';
		html += '</span>';
	}
	oB.empty().append(html);
	cssTA = oB.css('text-align');
	cssMP = (cssTA=='left')?'right':'left';
	oB.find('span').css('border','1px solid lightgrey');
	oB.find('span').css('margin-'+cssMP,sHV);
	oB.find('span').css('float',cssTA).width(wB).height(hB);
	oB.find('span').css('text-align','center');
	aMem['wB'] = wB;
	aMem['hB'] = hB;
	fctDspShowMe();
}

function fctHideLayer(sName){
	var oTrg = $(sName)
	if(oTrg==null)return;
	oTrg.fadeOut(6000,function(){oTrg.html('')});
}

// -- Méthode chargée d'opérer un fadeIn sur tous les objets de class = showMe
function fctDspShowMe(){
	var oT = $(".showMe:eq(0)");
	if(oT==null)return;
	oT.removeClass('showMe').addClass('hideMe').fadeIn(aMem['fadeInPortfolioDelay'],function(){oT.width(aMem['wB']);oT.height(aMem['hB']);});
	setTimeout(fctDspShowMe,aMem['showMeDelay']);
}

// -- Méthode AJAX HttpRequest
function fctAjaxModule(script,data){
	/*
	alert($.ajax(
				{
					type: 'POST',
					async: false,
					url: script,
					dataType: 'html',
					error: function(obj,msg){},
					success: function(msg){},
					data: data
				}
			).responseText)
	*/
	return  $.ajax(
				{
					type: 'POST',
					async: false,
					url: script,
					dataType: 'html',
					error: function(obj,msg){},
					success: function(msg){},
					data: data
				}
			).responseText;
	}
	
function fctGetValue(str){
	if(typeof(str)=='undefined')return 0;
	var reg = /[0-9]+/
	return 1*str.match(/[0-9]+/)
}
