$(document).ready(function(){
	function chargeVignette(start)
	{
		$.each(lstImages,function(i,s){
			if (i%10 == 0 && i > start && i!=0) {
				return false;
			}
			
			var brow_ver = "N";if( jQuery.browser.safari){var brow_ver = "S";}if( jQuery.browser.opera){var brow_ver = "O";}if( jQuery.browser.msie){var brow_ver = "IE";}if( jQuery.browser.mozilla){var brow_ver = "M";}
			
			if ( i >= start ){
			s.VIGNETTE = s.VIGNETTE.split("\\/").join("/");
			s.VIGNETTE = s.VIGNETTE.split("%5c").join("");
			
			if(brow_ver != "N")
				{$('#deroulant .innerderoulant').append('<a title="'+s.TITRE+'"><img id="imgFolio'+i+'" src="'+s.VIGNETTE+'" /></a>');}
			}
		});
		$('#deroulant .innerderoulant').css('width',$('#deroulant a').length*150+'px');
	}

	function clickVignette(e)
	{
		imageCourante = parseInt(e.target.id.replace(/imgFolio/,''));
		chargeImage();
	}
	function chargeImage(){
		if ( imageCourante < 0 ) {
			imageCourante = 0;
			return false;
		}
		else if (imageCourante >= lstImages.length ) {
			imageCourante = lstImages.length-1;
			return false;
		}
		// Changement de la pub a chaque affichage de nouvelle image. 
		changementPub();
		$('#big_image').attr('src',lstImages[imageCourante].GRANDE_IMAGE).show();
		$('#port_leg p').html(lstImages[imageCourante].LEGENDE+' '+(lstImages[imageCourante].COPYRIGHT!=''?'('+lstImages[imageCourante].COPYRIGHT+')':''));
		$('#titre_description h1').html(lstImages[imageCourante].TITRE);
		
		if ( imageCourante%8 == 0 ) {
			chargeVignette($('#deroulant a').length);
		}
		$('#deroulant a img').removeClass();
		$('#deroulant a img:eq('+imageCourante+')').addClass('active');
		$('#number b').html(imageCourante+1);
	}
	if ( document.location.href.match(/\?ID=/) ){
		var idImage = parseInt(document.location.href.substr(document.location.href.indexOf('?ID=')+4));
		$.each(lstImages,function(i,n){
			if ( n.ID == idImage) {
				imageCourante = i;
				return false;
			}
		});
	}
	else if ( document.location.href.match(/\?NUM=/) ) {
		imageCourante = parseInt(document.location.href.substr(document.location.href.indexOf('?NUM=')+5));
	}
	if ( !imageCourante ) {
		imageCourante = 0;
	}
	$('#number').html('<b>'+parseInt(imageCourante+1)+'</b>/'+lstImages.length);
	
	chargeImage();
	
	chargeVignette($('#deroulant a').length);
	$('#deroulant a img').removeClass();
	$('#deroulant a img:eq('+imageCourante+')').addClass('active');
	
	$('#deroulant a').live('click',clickVignette);
	
	$('#bloc_scroll_self')
		.find('.fle_port_3').click(function(){
			if (parseInt($('#deroulant .innerderoulant').css('margin-left'))+300 > 0){
				$('#deroulant .innerderoulant').animate({'marginLeft': '0px'});
			}
			else {
				$('#deroulant .innerderoulant').animate({'marginLeft': '+=300px'});
			}
		}).end()
		.find('.fle_port_4').click(function(){
			if (Math.abs(parseInt($('#deroulant .innerderoulant').css('margin-left')))+300 > parseInt($('#deroulant .innerderoulant').css('width'))-parseInt($('#deroulant').css('width')) ){
				$('#deroulant .innerderoulant').animate({'marginLeft': '-'+(parseInt($('#deroulant .innerderoulant').css('width'))-parseInt($('#deroulant').css('width')))+'px'});
			}
			else {
				$('#deroulant .innerderoulant').animate({'marginLeft': '-=300px'});
			}
			if ( Math.abs(parseInt($('#deroulant .innerderoulant').css('margin-left'))) > parseInt($('#deroulant .innerderoulant').css('width'))-parseInt($('#deroulant').css('width'))-(4*150)) {
				chargeVignette($('#deroulant a').length);
			}
		}).end();
	$('#number_com')
		.find('.fle_port_1').click(function(){
			imageCourante--;
			chargeImage();
		}).end()
		.find('.fle_port_2').click(function(){
			imageCourante++;
			chargeImage();
		}).end()
		
	// Modification des pub 
		$('#carre_haut').loadPub('carre', 'carre_haut_tag', sas_pageid);
        //$('#sky_gauche').loadPub('sky', 'sky_gauche', sas_pageid);
        $('#megaban_bas').loadPub('megaban_bas', 'megaban_bas_tag', sas_pageid);
        //$('#megaban_haut').loadPub('megaban_haut', 'megaban_haut', sas_pageid);
		
		if (tag_cyber_url == undefined) tag_cyber_url = '/cyber_v5/Football/PORTFOLIO.html';
			// Chargement des tags de pub
		$('#bas_du_site').loadPagePub(tag_cyber_url,'bas_du_site_tag_pub');
            
			// Rafraichir toutes les pubs 
        refreshPub(180000); // Pour lancer le refresh des publicités
	
	
});


