// JavaScript Document
$(document).ready( function () {
	// Fix for hover of main menu item
	$("#header ul.main_menu li a").hover(
		function(){
			$(this).parent().addClass("over");
		},
		function(){
			$(this).parent().removeClass("over");
		}
	);
	
	// Submit of newsletter form
	/*
	$(".newsletter a").click(function(){
		$("#newsletter").submit();
	});
	*/
	
	// Remove field text when selected
	$("#email").click(function(){
		if($("#email").val() == 'Adresse courriel')
		{
			$("#email").val('');
		}
	});
	// Remove field text when selected
	$("#email").blur(function(){
		if($("#email").val() == '')
		{
			$("#email").val('Adresse courriel');
		}
	});
	
	
	
	
	$("#news div.light,#news div.dark").each(function(){
		short_text = $(this).find("div.content").attr("title");
		$(this).find("div.content").removeAttr("title");
		$(this).find("div.content").attr("rel", short_text);
	});

	// Open and close news on HOME
	jQuery.easing.def = "easeInOutQuad";
	var iMinHeight = 123;
	$("#news div.light, #news div.dark,").each(function(){
		$(this).attr('rel', $(this).find("div.content").height());
	});
	$("#news div.light, #news div.dark").find("div.content").css('height', iMinHeight+'px');
	$("#news div.light, #news div.dark").find("div.content").css('overflow', 'hidden');
	$("#news div.light, #news div.dark,").each(function(){
		new_text = $(this).find("div.content").attr("rel");
		old_text = $(this).find("div.content div.text").html();
		$(this).find("div.content").attr("rel", old_text);
		$(this).find("div.content div.text").html(new_text);
	});
	$("#news div.light, #news div.dark").click(function(){
		var iFullHeight = $(this).attr('rel');
		
		if($(this).find("div.content").height() == iFullHeight)
		{
			var iNewHeight = iMinHeight;
			change = 'end';
		}
		else
		{
			var iNewHeight = iFullHeight;
			change = 'start';
		}
		
		element = $(this).find("div.content div.text");
		new_text = $(this).find("div.content").attr("rel");
		old_text = $(this).find("div.content div.text").html();
		$(this).find("div.content").attr("rel", old_text);
		
		if(change == 'start')
		{
			element.html(new_text);
		}
		
		$(this).find("div.content").animate({height:iNewHeight}, 1000, function(){
			if(change == 'end')
			{
				element.html(new_text);
			}
		});
		$(this).find("div.content a.more").click(function(e) {
			//e.preventDefault();
		});
		
		$(function() {
			$('a.popup').fancybox({overlayOpacity:0.1, titlePosition:"inside"});
					 }
		);	
		
	});
	


			
	if($.flash.available)
	{
		// Find active menu
		var current_section = "";
		if($("#header ul.main_menu li").is(".active"))
		{
			current_section = $("#header ul.main_menu li.active").attr("class").split(' ').slice(0,1);
			if(current_section == 'presse')
			{
				current_section = 'salledepresse';
			}
		}

		// Insert SWF header
		$('#header').html('');
		$('#header').flash({
			swf: 'swf/header.swf',
			height: '300',
			width: '100%',
			flashvars: {
				section : current_section
			},
			params: {
				scale: 'noScale',
				bgcolor: '#0b0b0b'
			}
		});
		
		// Insert Video Player HOME
		$('#video_container').flash({
			swf: 'swf/videoplayer.swf',
			width: '480',
			height: '310',
			flashvars: {
				video_uri: '../flv/lpdlb_3.flv',
				image_uri: 'images/preview/lpdlb_3.jpg',
				autoplay: 'false'
			},
			params: {
				bgcolor: '#0b0b0b',
				scale: 'noScale',
				wmode: 'transparent',
					allowfullscreen:true
			}
		});
		
		// Insert Video Player
		$('#video_player').flash({
			swf: 'swf/videoplayer_large.swf',
			width: '640',
			height: '400',
			flashvars: {
				video_uri: '../flv/lpdlb_3.flv',
				image_uri: 'images/preview/bande_annonce_3_big.jpg',
				autoplay: 'false'
			},
			params: {
				wmode: 'transparent',
				allowfullscreen:true
			}
		});
		
		/*
		// Insert Video Player
		$('#video_player_coulisses1').flash({
			swf: 'swf/videoplayer_large.swf',
			width: '640',
			height: '400',
			flashvars: {
				video_uri: '../flv/Coulisses_MCPERRON.flv',
				image_uri: 'images/preview/coulisses_mcperron.jpg',
				autoplay: 'false'
			},
			params: {
				wmode: 'transparent',
				allowfullscreen:true
			}
		});
		
		// Insert Video Player
		$('#video_player_coulisses2').flash({
			swf: 'swf/videoplayer_large.swf',
			width: '640',
			height: '400',
			flashvars: {
				video_uri: '../flv/Coulisses_MDUBREUIL.flv',
				image_uri: 'images/preview/coulisses_mdubreuil.jpg',
				autoplay: 'false'
			},
			params: {
				wmode: 'transparent',
				allowfullscreen:true
			}
		});
		*/
		
		//Launch new page
		$('.launch').click ( function ()
		{
			$url = $(this).attr("url");
			
			window.location = $url;
		});
		
		
	    $('.subscribe_newsletter').click( function()
	    {
	    	var strEmail = $('#email').val();
	    	
	    	if (!isValidEmailAddress(strEmail))
	    	{
	    		//$('#error').removeClass('hide');
	    		$(".newsletter").addClass("newsletter_error");
	    		return false;
	    		
	    	}
	    	else
	    	{
	    		//$(".newsletter").removeClass("newsletter_error");
	    	}
	    	
	        $.ajax({
	            url: $(this).attr('href'),
	            method: 'post',
	            data: "email=" + strEmail,
	            success: function(resp)
	            {
	        		$(".newsletter").addClass("hide");
	        		$(".newsletter_thx").removeClass("hide");
	            }
	        });
	        
	        return false;
	        
	    });
	    	    
	    function isValidEmailAddress(emailAddress) 
	    {
	    	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	    	return pattern.test(emailAddress);
	    }
	}
	
});

