$(document).ready(function() {

	// home videos
	var video_list = $('#video-list');
	var video_player = $('#video-player');
	var video_links = video_list.find('a');
	if(video_links.length > 0) {
		video_links.each(function(i) {
			var $this = $(this);
			var href = this.href;
			if(i==0) {
				video_player.html(YouTubeVideoHtml(href, false));
				$this.addClass('current');
				YouTubeVideoPrevNext(video_links, i);
			}
			$this.attr('title', $('img', this).attr('alt'));
			$('img', this).attr('src', 'http://i1.ytimg.com/vi/'+this.href.replace(/^.*\=/, '')+'/default.jpg');
			$this.click(function() {
				video_links.removeClass('current');
				video_player.html(YouTubeVideoHtml(href, true));
				$this.addClass('current');
				//YouTubeVideoPrevNext(video_links, i);
				return false;
			});
		});
	}
	$('#videoleft').click(function() {
		YouTubeVideoIncr(video_links, -1);
		return false;
	});
	$('#videoright').click(function() {
		YouTubeVideoIncr(video_links, 1);
		return false;
	});
	
	
	// Player youtube pages not:home 
	
	var settings = {
		videoContainer: 'videoPlayer',
		width: 660,
		height: 400
	}
	
	$url = $('#'+settings.videoContainer+' a').attr('href'); // R�cup�ration de l'url de la vid�o

	/*~�
	 * Le player
	 */

	$player = '<object type="application/x-shockwave-flash" width="'+settings.width+'" height="'+settings.height+'" data="'+$url+'">';
	$player += '<param name="movie" value="'+$url+'"/>';
	$player += '<a href="http://get.adobe.com/flashplayer/" onclick="window.open(this.href); return false;">';
	$player += '<img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get flash player to play to this file" width="88" height="31" /></a><br /></object>';

	$('#'+settings.videoContainer).html($player) // On l'injecte dans le html du bloc conteneur
	
	// dropdown
	$("#nav").superfish({ 
		delay:       500,                            // one second delay on mouseout 
		animation:   {opacity:'show',height:'show'},  // fade-in and slide-down animation 
		speed:       'normal',                          // faster animation speed 
		autoArrows:  false,                           // disable generation of arrow mark-up 
		dropShadows: false                            // disable drop shadows 
	});
	
	// change banner
	//if (home.length) {
		home_animation.init();
	//}
	// xhtml valid external links
	$('a[rel="external"]').attr('target', '_blank');
	
	if ($('#slideshow2').length) {
		$('#slideshow2').s3Slider({
			timeOut: 4000
		});
	}

	var videos = [];
	var videos_alt = [];
	$('#surface-videos a').each(function() {
		var width = 549;
		var height = 335;
		var href = this.href+'';
		videos.push(href);
		videos_alt.push($(this).find('img').get(0).alt+'');
		$(this).click(function() {
			$('#surface-videos a.current').removeClass('current').css('opacity', 0.8);
			$(this).addClass('current').css('opacity', 1);
			$('#surface-videos > div').html('<object width="'+width+'" height="'+height+'"><param name="movie" value="'+href+'&hl=fr&fs=1&rel=0&autoplay=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="'+href+'&hl=fr&fs=1&rel=0&autoplay=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'+width+'" height="'+height+'"></embed></object>');
			return false;
		}).hover(function() {
			if(this.className != 'current') {
				$(this).css('opacity', 1);
			}
		},function() {
			if(this.className != 'current') {
				$(this).css('opacity', 0.8);
			}
		});
	}).css('opacity', 0.8);
	if(videos.length == 3) {
		$('#surface-videos > div area').each(function(i) {
			this.href = "javascript:void(0);";
			this.alt = videos_alt[i];
			$(this).click(function() {
				return $('#surface-videos a').eq(i).click();
			}).hover(function() {
				$('##surface-videos a').eq(i).css('opacity', 1);
			},function() {
				$('##surface-videos a').eq(i).css('opacity', 0.8);
			});
		});
		$('#surface-videos > div > img').attr('usemap', '#map-video');
	}
	
	// disable border around checkbox in IE
	$("input:checkbox").addClass('checkbox');
	//$('#mycarousel').jcarousel();
	setInterval(slideSwitch, 2000 );
	
	// player mp3
	var audio = jQuery("#audio1").eq(0);
	if (audio.length == 1) {
		audio_player('#audio1', 290, 24, '/upload/files/press/audio/BFM-atelier_numerique.mp3');
	}
	
	// slide videos + tooltip
	$(".scrollable").scrollable({size: 4}).find("a").tooltip({
		tip: '#tooltip',
		effect: 'slide'
	});

	/*~�
	Nos References : colorbox
	*/
	$(".references").colorbox({iframe:true, innerWidth:650, innerHeight:510});
	
});

// Cufon
Cufon.replace('.fontified, #content h1, #content h2');


function slideSwitch() {
	var $active = $('#slideshow img.active');

	if ( $active.length == 0 ) $active = $('#slideshow img:last');

	var $next =  $active.next().length ? $active.next()
		: $('#slideshow img:first');
	$active.addClass('last-active');

	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 1000, function() {
			$active.removeClass('active last-active');
		});
}

	// tabs
	var SlidingTabs = jQuery('.SlidingTabs');
	var SlidingTabsView = jQuery('.SlidingTabsView');
	var SlidingTabsList = jQuery('.SlidingTabsList');
	if(SlidingTabs.length == 1 && SlidingTabsView.length == 1 && SlidingTabsList.length == 1) {
		var offsetList = SlidingTabsList.offset();
		SlidingTabs.find('li a').each(function() {
			var id = (this.href+'').replace(/^.*#/, '');
			var obj = jQuery('#'+id);
			jQuery(this).click(function() {
				SlidingTabsList.find('.SlidingTabsItem').css({display: 'none'});
				jQuery('#'+id).css({display: 'block'});
				SlidingTabs.find('li a').removeClass('selected');
				jQuery(this).addClass('selected');
				return false;
			});
		}).eq(0).click();
	};
	
	
// one pixel out player
function audio_player(selector, width, height, data){
	jQuery(selector).append('<object type="application/x-shockwave-flash" data="/upload/audio/player.swf" height="'+height+'" width="'+width+'"><param name="movie" value="/upload/audio/player.swf"><param id="player-play" name="FlashVars" value="soundFile='+data+'"><param name="quality" value="high"><param name="menu" value="false"><param name="wmode" value="transparent"></object>');
}
function YouTubeVideoPrevNext(list, a) {
	if(a > 0 && a < list.length - 1) {
		$('#videoright, #videoleft').removeClass('disabled');
	}
	else if(a <= 0) {
		$('#videoleft').addClass('disabled');
		$('#videoright').removeClass('disabled');
	}
	else {
		$('#videoleft').removeClass('disabled');
		$('#videoright').addClass('disabled');
	}
}

function YouTubeVideoIncr(list, incr) {
	var a = -1;
	list.each(function(i) {
		if(this.className.indexOf('current') >= 0) {
			a = i;
		}
	});
	var b = a + incr;
	if(a != b) {
		if(b >= 0 && b < list.length) {
			$(list[b]).trigger('click');
		}
	}
	YouTubeVideoPrevNext(list, b);
}

function YouTubeVideoHtml(href, autoplay) {
	var width = 630;
	var height = 382;
	var link = href+'';
	/**
	 * transform link http://www.youtube.com/watch?v=u1zgFlCw8Aw
	 * to             http://www.youtube.com/v/u1zgFlCw8Aw
	 */
	link = link.replace('/watch?v=', '/v/');
	if(autoplay) {
		link += '?autoplay=1';
	}
	var html = '<object style="height: '+height+'px; width: '+width+'px">';
	html += '<param name="movie" value="'+link+'">';
	html += '<param name="allowFullScreen" value="true">';
	html += '<param name="allowScriptAccess" value="always">';
	html += '<param name="wmode" value="transparent">';
	html += '<embed src="'+link+'" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="'+width+'" height="'+height+'" wmode="transparent">';
	html += '</object>';
	return html;
}

// change images in banner

var home_animation = {
	time_fade: 500,
	time_pause: 2000,
	banner_id: 'wrapper',
	sprite_id: 'defilement',
	items:[
		{
			top: 80,
			left: 450,
			width: 383,
			height: 320,
			banner: '/upload/design/banner-home-bg.jpg',
			sprite: '/upload/design/home/sprite.jpg',
			sprite_nb: 7
		}
		// {
			// top: 78,
			// left: 524,
			// width: 250,
			// height: 150,
			// banner: 'banner2.jpg',
			// sprite: 'banner2-sprite.jpg',
			// sprite_nb: 3
		// }
	],
	image: null,
	index: 0,
	sprite_index: 0,
	mode: 0,
	index_changed: false,
	next: function() {
		var item = home_animation.items[home_animation.index];
		home_animation.sprite_index++;
		home_animation.index_changed = false;
		if(home_animation.sprite_index >= item.sprite_nb) {
			home_animation.sprite_index = 0;
			home_animation.index++;
			if(home_animation.index >= home_animation.items.length) {
				home_animation.index = 0;
			}
			home_animation.index_changed = true;
			item = home_animation.items[home_animation.index];
		}
		home_animation.mode++;
		return item;
	},
	timer: function() {
		var item = home_animation.next();
		var oBanner = document.getElementById(home_animation.banner_id);
		var oSprite = document.getElementById(home_animation.sprite_id);
		var pos = - item.width * home_animation.sprite_index;
		if(home_animation.index_changed) {
			oBanner.style.backgroundImage = 'url(' + item.banner + ')';
			oSprite.style.backgroundImage = 'url(' + item.sprite + ')';
			oSprite.style.backgroundPosition = '0px 0px';
			oSprite.style.top = item.top + 'px';
			oSprite.style.left = item.left + 'px';
			oSprite.style.width = item.width + 'px';
			oSprite.style.height = item.height + 'px';
			home_animation.image.src = item.sprite;
			home_animation.image.style.left = '0px';
			home_animation.mode = 0;
			jQuery(home_animation.image).fadeIn(home_animation.time_fade, home_animation.reload);
		}
		else {
			if(home_animation.mode % 2 == 1) {
				oSprite.style.backgroundPosition = pos + 'px 0px';
				jQuery(home_animation.image).fadeOut(home_animation.time_fade, home_animation.reload);
			}
			else {
				home_animation.image.style.left = pos + 'px';
				jQuery(home_animation.image).fadeIn(home_animation.time_fade, home_animation.reload);
			}
		}
	},
	reload: function() {
		window.setTimeout(home_animation.timer, home_animation.time_pause);
	},
	init: function() {
		var sprite = document.getElementById(home_animation.sprite_id);
		if(sprite) {
			var item = home_animation.items[home_animation.index];
			home_animation.image = document.createElement('img');
			home_animation.image.src = item.sprite;
			home_animation.image.style.position = 'absolute';
			home_animation.image.style.left = '0px';
			home_animation.image.style.top = '0px';
			sprite.appendChild(home_animation.image);
			home_animation.reload();
		}
	}
};
function flash_player(file_flv, width, height){
document.write('<object type="application/x-shockwave-flash" data="/upload/files/flash/player_flv_maxi.swf" width="'+width+'" height="'+height+'">\n');
document.write('<param name="movie" value="/upload/files/flash/player_flv_maxi.swf" />\n');
document.write('<param name="allowFullScreen" value="true" />\n');
document.write('<param name="FlashVars" value="flv='+file_flv+'&amp;configxml=/upload/files/flash/param.xml" />\n');
document.write('<param name="wmode" value="transparent" />\n');
document.write('<param name="height" value="'+height+'" />\n');
document.write('<param name="width" value="'+width+'" />\n');
document.write('</object>\n');
}

/*~�
Diaporama logos PARTENAIRES
*/
var timerAnim;
    $(function(){
        var images = $('.imgSlide');
        var preImageWidth = 0;
        for(var i=0; i<images.length; i++){
            if(i==0){
                var width = 0 - parseInt($(images[i]).css('width').replace("px",""));
                $(images[i]).css('right',width+'px');
            } else if(i==1){
                preImageWidth = parseInt($(images[i]).css('width').replace("px",""));
                $(images[i]).css('right','0px');
            } else {
                $(images[i]).css('right',preImageWidth+'px');
                preImageWidth = preImageWidth + parseInt($(images[i]).css('width').replace("px",""));                
            }
            $(images[i]).mouseover(function(){
                 clearInterval(timerAnim);
            });
            $(images[i]).mouseout(function(){
                 timerAnim = setInterval("startScroll()", 10);
            });
        }
        timerAnim = setInterval("startScroll()", 10);
    });

    function startScroll(){
        var images = $('.imgSlide');       
        for(var i=0; i<images.length; i++){
            var right = parseInt($(images[i]).css('right').replace("px",""))+1;
            var width = parseInt($(images[i]).css('width').replace("px",""));           
            if(right>=640){
                 right = 0 - width;
            }
            $(images[i]).css('right',right+'px');         
        }
    }
	
$(document).ready(function(){
	//Add class to the selected path
	$("#path a:last").addClass("selected-path");
	if ($("#path a:last:contains('Accueil')").length) {
		$("#path").hide();
	} else if($("#path a:last:contains('Home')").length) {
		$("#path").hide();
	} else if($("#path a:last:contains('Willkommen')").length) {
		$("#path").hide();
	} else if($("#path a:last:contains('inicio')").length) {
		$("#path").hide();
	}
	// Replace Accueil by Home for the US pages
	
	$link_lang = $('#path a:eq(1)');
	
	if($link_lang.is(':contains("US")'))
	{
		$('#path a:first').html('Home').attr('href', '/us/index.html');
	} else if ($link_lang.is(':contains("DE")'))
	{
	$('#path a:first').html('Willkommen').attr('href', '/de/index.html');
	} else if ($link_lang.is(':contains("ES")'))
	{
		$('#path a:first').html('P&aacute;gina de inicio').attr('href', '/es/index.html');
	} else if ($link_lang.is(':contains("IT")'))
	{
		$('#path a:first').html('Home').attr('href', '/it/index.html');
	}
	
	// Redirect people clicking on the News link (in #path) to the current news they were reading before clicking 
	if ($("#path a:contains('News')").length) {
		$link_news = $('#path a:contains("News")');
		$link_news_link = $link_news.next('a').attr('href');
		$link_news.attr('href', $link_news_link);
	}
	
	//Transform target="_blank" in js equivalent
	$('a.blank').attr('target', '_blank');
	
});
