$("html").addClass("hasJS"); // AJOUT pour bug accordion dans IE (affichait le contenu avant initialisation)

CB_AllowedToRun='off'; // Desactivation de la clearbox pour empecher l'ouverture par click sur les vignettes

// INIT THE MAIN NAVIGATION (accordion)
function initMainMenu() {

	$('.rub_content').hide();
	$('.rub_content.current').show();
	$('#main-content h2 a').click(
		function() {
			// Si le menu était déjà ouvert, on le referme :
			// if the menu was already open, we close it :
			if ($(this).parent().next(".rub_content:visible").length != 0) {
				
				$(this).parent().next(".rub_content").slideUp("normal");
				$(this).removeClass('current');
			}
			// Si le menu est caché, on ferme les autres et on l'affiche :
			// If menu is hidden, we close the others and display it :
			else {
				$(".rub_content").slideUp("normal");
				$(this).parent().next(".rub_content").slideDown("normal");
				$("#main-content h2 a").removeClass('current');
				$(this).addClass('current');
			}
			// On empêche le navigateur de suivre le lien :
			// We prevent the browser from following the link:
			return false;  
		}
			
	);	
}

// INIT THE PANELS TABS (slider) AND THE ARTICLE BROWSER BUTTONS (href)
function initArticleBrowser() {
	
	var nb_panels = $('#articles').children().length; // number of panels
	var panelNum = $.cookies.get('flypixPortBrowse');
	if(! panelNum){panelNum=0;}
	var panelTabId = $("#articles").children().eq(panelNum);
	
	$('#portfolio #nav-n2 a:eq(' + panelNum + ')').addClass('current');
	//alert(panelTabId.attr('id'));
	// Apply styles to #articles and wrap it in #articles-wrapper
	$('#articles')
        .css({
           // height: '163px', // 163 is the height of each portfolio piece 
            width: ($(this).width() * nb_panels) + 'px', // i.e. 303x2 = 606 , but if we trace, the result is much more greater (strange!, seems to be window or document width)
            position: 'relative',
			left: -(panelNum * 303) + 'px'
        })
        .wrap('<div id="articles-wrapper" style="width:303px;height:400px;overflow:hidden;position:relative;"></div>'); // Wrap in new DIV element (required for scroll to work properly)
	
	//$('#articles').offset().left = $('#articles').offset().left-$('#articles #' + panelTabId.attr('id')).width();
	
	
	//Set the click events for <a> tags in order to animate #articles container
	var iAButton=0;
	$('#articles').children().each(function() {
		$(this).addClass('fleft');
		var panel_id = $(this).attr('id');
		var panel_num =  iAButton ;
		$('#portfolio #nav-n2 a:eq(' + iAButton + ')').click(
			function() {
				// Animate
				var difference = $('#articles').offset().left-$('#articles #' + panel_id).offset().left; // leftOffset of #articles minus leftOffset of selected article Browser panel
                $('#articles').animate({left: difference}, 500); // Animate to the value of different over 500 milliseconds
				// Set correct selected element's style
				//alert($(this).parents('ul:first').attr('id'));
				$(this).parents('ul:first').find('a').removeClass('current');
				$(this).addClass('current');
				$.cookies.set('flypixPortBrowse',  panel_num );
				
                return false; // prevent default action of links
			});
		iAButton++;
	});
	
}
// Initialization of the tool tip for the display of the article thumb

// Tooltip with jQuery Tools (flowplayer.org)
/*
function initProjListToolTip() {
	$("#articles-list li img").addClass('png'); // for IE6
	$("#articles-list li a").tooltip({
		tip: ".projListTip",
		relative: true,
		position: "center left",
		offset: [10, 288],
		effect: "fade"
	});
}
*/

// Tooltip with QTIP 1
/*
function initProjListToolTip() {
	$('#articles-list li a').each(function() {
		$(this).qtip(
		{
			content: '<img src="' + $(this).attr('rel') + '" />',
			style: {
				width: 116,
				height: 70,
				padding: 2,
				background: '#FFF',
				color: '#333',
				tip: true, // Apply a speech bubble tip to the tooltip at the designated tooltip corner
				border: {
				  width: 1,
				  radius: 2,
				  color: '#e6e6e6'
				}
			},
			position: {
				corner: {
				   target: 'topRight', // Position the tooltip above the link
				   tooltip: 'bottomLeft'
				},
				adjust: {
				   screen: true // Keep the tooltip on-screen at all times
				}
			},
			show: { delay: 0 }
		});
	});
}
*/

// Tooltip with QTIP 2.0 (Beta - http://craigsworks.com/projects/qtip2)
function initProjListToolTip() {
	$('#articles-list li a').each(function() {
		$(this).qtip(
		{
			content: '<img src="' + $(this).attr('rel') + '" class="vAlignTB" />',
			style: {
				width: 114,
				/*height: 70,*/
				classes: 'ui-tooltip-light ui-tooltip-rounded2'
		    },
			position: {
				my: 'left top', // Position the tooltip above the link
				at: 'right top',
				adjust: {
					/*x: -10,*/
					method: 'shift'
				},
				viewport: $(window)
			},
			show: { delay: 0 }
		});
	});
}

// Make sonme links to be open in a blank window
function makeBlankWindowLinks() {
	$('p.voirsite a, .external').click(
		function() {
			window.open(this.href);
			return false;
		})
}

// INIT THE IMAGE SLIDER (thumbs) AND SET THE DEFAULT IMAGE
function initThumbsSlider() {
	//alert(defaultArticleImage);
	$('#thumbnails_main div.scrollable')
        .css({
           
            width: '553px',
			height: '71px',
			overflow: 'hidden',
            position: 'relative'
        });
	
	$('#thumbnails_main div.scrollable div.items')
        .css({
            width: '2000px',
            position: 'absolute',
			clear:'both'
        });
	
	$("div.scrollable").scrollable();
	// Evenement click des elements
	$("div.scrollable div.items div").click(function() { 
        // perform the effect 
		$(this).siblings().fadeTo(400, 1);
        $(this).fadeTo(400, 0.33);
		
		// Test sur la chaine url
		var url = $(this).find('a').attr("href");
		var currentPath = $('#loader').find('img').attr("src");
		// Fuck IE qui ne veut pas se satisfaire d'un undefined
		if(! currentPath) {
			currentPath = "bidon";
		}
		/*alert("Thumb linkHrefURL : " + url
			  +"\nLoader>PhotoImgSRC : "+ currentPath);*/
		
		
		/*
		//TEST avec regexp
		//var url = $(this).find('img').attr("src");
		var reg = new RegExp("(_thumb)", "g");
		var allNewPath = url.replace(reg,"_photo");
		
		// Test si chargement de la même image (compare l'attribut src de l'image)
		if ( allNewPath != $('#loader').find('img').attr("src") ) {
			//loadImage(allNewPath);
			changeImage(allNewPath);
			//alert($(this).hasClass("active"));
		}
		*/
		//var currentPath = $('#loader').find('img').attr("src");
		// Test si chargement de la même image (compare l'attribut href du lien)
		if ( url != currentPath ) {
			changeImage(url);
		}
		return false;
    });
	// Click et Chargement de la 1ere image par défaut
	$("div.scrollable div.items div:eq(0)").click();
	defaultArticleImage = $("div.scrollable div.items div:eq(0)").find('a').attr("href");
	changeImage(defaultArticleImage);
}

function initAboutBrowser() {

	var nb_panels = $('#apropos_bloc').children().length; // number of panels
	// Apply styles to #articles and wrap it in #articles-wrapper
	$('#apropos_bloc')
        .css({
           // height: '163px', // 163 is the height of each portfolio piece 
            width: ($(this).width() * nb_panels) + 'px', // i.e. 303x2 = 606 , but if we trace, the result is much more greater (strange!, seems to be window or document width)
            position: 'relative'
        })
        .wrap('<div id="apropos_bloc-wrapper" style="width:940px;overflow:hidden;position:relative;"></div>'); // Wrap in new DIV element (required for scroll to work properly)
	
	//Set the click events for <a> tags in order to animate #articles container
	var iAButton=0;
	$('#apropos_bloc').children().each(function() {
		$(this).addClass('fleft');
		var panel_id = $(this).attr('id');
		$('#apropos #nav-apropos a:eq(' + iAButton + ')').click(
			function() {
				// Animate
				var difference = $('#apropos_bloc').offset().left-$('#apropos_bloc #' + panel_id).offset().left; // leftOffset of #articles minus leftOffset of selected article Browser panel
                $('#apropos_bloc').animate({left: difference}, 500); // Animate to the value of different over 500 milliseconds
				// Set correct selected element's style
				//alert($(this).parents('ul:first').attr('id'));
				$(this).parents('ul:first').find('a').removeClass('current');
				$(this).addClass('current');
                return false; // prevent default action of links
			});
		iAButton++;
	});
	
	$('#apropos #nav-apropos a:first').addClass('current');
}

function changeImage(pict) {
	//alert("changeImage() > LAUNCHED!!!")
	var fullPict = getFullPict(pict);
	if ( $('#loader img').length == 0) {
		
		$('#loader').append(new Image());
		$('#loader img').hide();
		$('#loader img').wrap('<a href="'+ fullPict + '" title="cliquer pour agrandir" style="display:block;width:100%;height:100%;outline:none;"></a>');
		
		/*
		$('#loader').append('<a href="'+ pict +'"></a>');
		$('#loader a').append(new Image());
		$('#loader a img').hide();
		*/
		
		$('#loader a').click(function() {
			CB_AllowedToRun='on';
			/* Ajout pour recuperation du titre de l'article (important, suit la meme logique que pour les items du "scrollable". Voir attribut "a rel").
			Nécessaire pour attribuer dynamiquement le nom de la gallerie "gallery" */
			var articleTitle = $('#article-infos h3').text();
			CB_Open('gallery=' + articleTitle + ',,href=' + $(this).attr('href'));
			CB_AllowedToRun='off';
			return false;
		});
		
	}
	
	$("#loader img").fadeOut("normal", function(){
		$('#loader').addClass('loading');
		$("#loader img").attr({src : pict});
		$("#loader a").attr({href : fullPict});
		
    	$("#loader img").load( function(){
			$('#loader').removeClass('loading');
        	$("#loader img").fadeIn("normal");
	    });
	});
}

/*	Recuperation de l'url de l'image taille réele (transformation de la chaine de caractere href de la photo "déco")
	>>> Mauvaise approche :
		- Obligation de nommer ses images avec la meme nomenclature (préférable dans tous les cas, mais obligatoire ici)
		- Impossibilité d'utiliser les images mises en cache de spip aprés transformation/manipulation d'image.
		  (chemin + nom d'image différents de l'original uploadé) */
function getFullPict(pictURL) {
	var reg = new RegExp("(_photo)", "g");
	var fullPictPath = pictURL.replace(reg,"_imgfull");
	return fullPictPath;
}

/*	Recuperation de l'url de l'image taille réele (extraction du href de l'attribut rel, dans la balise A de la vignette)
	>>> Moyenne approche : à tester plus tard
*/
