$(document).ready(function(){		
	/* Send */
	$('#submit').click(function(){
		var verif = verif_form();
		var rules = verif_rules();
		if(verif == true && rules == true)
		{
			var id 			= $('#contest').val();
			var lastname 	= $('#lastname').val();
			var firstname 	= $('#firstname').val();
			var age 		= $('#age').val();
			var sexe 		= $('input[name=sexe]:checked').val();
			var city 		= $('#city').val();
			var zip 		= $('#zip').val();
			var country 	= $('#country').val();
			var phone 		= $('#phone').val() + $('#phone2').val() + $('#phone3').val();
			var cell 		= $('#cell').val() + $('#cell2').val() + $('#cell3').val();
			var email 		= $('#email').val();
			var activities 	= $('#field_select_value').val();
			var degree 		= $('#degree_select_value').val();
			var family_income = $('#family_income_select_value').val();
			var newsletter = $('#infos').val();
			
			$.ajax({
				url: '/administration/ajax.php',
				type: 'POST',
				data: 'p=contests&a=send_contest&plg=contest&id='+ id +'&lastname='+ lastname +'&firstname='+ firstname +'&age='+ age +'&sexe='+ sexe +'&city='+ city +'&zip='+ zip +'&country='+ country +'&phone='+ phone +'&cell='+ cell +'&email='+ email +'&activities='+ activities +'&degree='+ degree +'&family_income='+ family_income +'&newsletter='+ newsletter,
				success: function()
				{
					//alert('Merci!');
					//location.reload();
					$('.participation_succes').show();
					$('.participation').hide();
				}
			});
			return true;	
		}else
		{
			return false;
		}
	});
	
	$('#submit_contact').click(function(){
		var verif = verif_form();
		if(verif == true)
		{
			var lastname 	= $('#lastname').val();
			var firstname 	= $('#firstname').val();
			var age 		= $('#age').val();
			var sexe 		= $('input[name=sexe]:checked').val();
			var city 		= $('#city').val();
			var zip 		= $('#zip').val();
			var country 	= $('#country').val();
			var phone 		= $('#phone').val() + $('#phone2').val() + $('#phone3').val();
			var cell 		= $('#cell').val() + $('#cell2').val() + $('#cell3').val();
			var email 		= $('#email').val();
			var activities 	= $('#field_select_value').val();
			var degree 		= $('#degree_select_value').val();
			var family_income = $('#family_income_select_value').val();
			var newsletter = 1;
			
			$.ajax({
				url: '/administration/ajax.php',
				type: 'POST',
				data: 'p=fmdm&a=send_form&plg=fmdm&lastname='+ lastname +'&firstname='+ firstname +'&age='+ age +'&sexe='+ sexe +'&city='+ city +'&zip='+ zip +'&country='+ country +'&phone='+ phone +'&cell='+ cell +'&email='+ email +'&newsletter='+ newsletter,
				success: function()
				{
					$('.participation_succes').show();
					$('.participation').hide();
				}
			});
			return true;	
		}else
		{
			return false;
		}
	});
	
	$('input.filter').click(function(){
		var type = $(this).val();
		if($(this).attr('checked')){
			$('.item_'+ type).show();
			$('.'+ type +'_filter').attr('checked','checked');
			$('label.'+ type).addClass('checked');
		}else{
			$('.item_'+ type).hide();
			$('.'+ type +'_filter').removeAttr('checked');
			$('label.'+ type).removeClass('checked');
		}
		
	});
	
	
// GALLERY
	var current = 1;
	var current_over;
	var img_total = $('.ls_thumbs li').size();
	
	$('.ls_thumbs a.thumb').click(function(){
		var val = $(this).attr('_val');
		current = $(this).parent('li').attr('id');
		$('#view').attr('src',val);
		$('.ls_thumbs img.hover').removeClass('hover_active');
		$(this).find('img.hover').addClass('hover_active');
		
		if(current == img_total)
		{
			$('.bt_next').css('visibility','hidden');
		}else {
			$('.bt_next').css('visibility','visible');
		}
		
		
		if(current == 1)
		{
			$('.bt_previous').css('visibility','hidden');
		}else {
			$('.bt_previous').css('visibility','visible');
		}
		
	});
	
	// next
	$('.bt_next').click(function(){
		current++;
		var val = $('.ls_thumbs li#'+ current +' a.thumb').attr('_val');
		$('#view').attr('src',val);
		$('.ls_thumbs img.hover').removeClass('hover_active');
		$('.ls_thumbs li#'+ current +' a.thumb').find('img.hover').addClass('hover_active');
		
		$('.bt_previous').css('visibility','visible');
		if(current == img_total)
		{
			$('.bt_next').css('visibility','hidden');
		}
	});
	
	// previous
	$('.bt_previous').click(function(){
		current--;
		var val = $('.ls_thumbs li#'+ current +' a.thumb').attr('_val');
		$('#view').attr('src',val);
		$('.ls_thumbs img.hover').removeClass('hover_active');
		$('.ls_thumbs li#'+ current +' a.thumb').find('img.hover').addClass('hover_active');
		
		$('.bt_next').css('visibility','visible');
		if(current == 1)
		{
			$('.bt_previous').css('visibility','hidden');
		}
	});
	
	// overlay
	$('.bt_zoom').click(function(){
		current_over = current;
		$('.overlay').fadeIn(500,function(){
			
			var val = $('.ls_thumbs li#'+ current +' a.thumb').attr('_val');
			var width = $('.ls_thumbs li#'+ current +' a.thumb').attr('_ow');
			var height =  $('.ls_thumbs li#'+ current +' a.thumb').attr('_oh');
			var ratio = 1;
			
			
			var pos = $('.overlay .overlay_content').position();
			var image = new Image;
				$(image).load(function(){
					if(width > $(window).width())
					{
						window_width = $(window).width()- 40;
						ratio =  window_width / width;
						height = height * ratio;
						width = width * ratio;
					}
					else if( height > $(window).height())
					{
						window_height = $(window).height()- 40;
						ratio =  window_height / height;
						height = height * ratio;
						width = width * ratio;
					}
					$('.overlay .overlay_content').css({'margin-left':(width/2 + 10 )*-1,'margin-top':(height/2 + 10 )*-1, 'width': width, 'height': height}).show();
					$('.overlay .overlay_content').find('.image_view').html(this);
					$(this).attr({'width':width,'height':height});
				})
				.attr('src',val);
		});
	});
	
	$('.overlay_background').click(function(){
		$('.overlay_content').hide();
		$('.overlay').fadeOut(500);
	});
	
	// over next
	$('.zone_next').click(function(){
		current_over++;
		if(current_over > img_total) current_over = 1;
		var val = $('.ls_thumbs li#'+ current_over +' a.thumb').attr('_val');
		var width = $('.ls_thumbs li#'+ current_over +' a.thumb').attr('_ow');
		var height =  $('.ls_thumbs li#'+ current_over +' a.thumb').attr('_oh');
		var pos = $('.overlay .overlay_content').position();
		var ratio = 1;
		if(width > $(window).width())
		{
			window_width = $(window).width()- 40;
			ratio =  window_width / width;
			height = height * ratio;
			width = width * ratio;
		}
		else if( height > $(window).height())
		{
			window_height = $(window).height()- 40;
			ratio =  window_height / height;
			height = height * ratio;
			width = width * ratio;
		}
		$('.overlay .overlay_content').find('.image_view').html('');
		$('.overlay .overlay_content').animate({'width':width,'height':height,'margin-left':(width/2 + 10 )*-1,'margin-top':(height/2 + 10 )*-1},300,function(){
			var image = new Image;
				$(image).load(function(){
					
					$('.overlay .overlay_content').find('.image_view').html(this);
					$(this).attr({'width':width,'height':height});
				})
				.attr('src',val);
		});

	});
	
	// over previous
	$('.zone_previous').click(function(){
		current_over--;
		if(current_over == 0) current_over = img_total;
		var val = $('.ls_thumbs li#'+ current_over +' a.thumb').attr('_val');
		var width = $('.ls_thumbs li#'+ current_over +' a.thumb').attr('_ow');
		var height =  $('.ls_thumbs li#'+ current_over +' a.thumb').attr('_oh');
		var pos = $('.overlay .overlay_content').position();
		var ratio = 1;
		if(width > $(window).width())
		{
			window_width = $(window).width()- 40;
			ratio =  window_width / width;
			height = height * ratio;
			width = width * ratio;
		}
		else if( height > $(window).height())
		{
			window_height = $(window).height()- 40;
			ratio =  window_height / height;
			height = height * ratio;
			width = width * ratio;
		}
		$('.overlay .overlay_content').find('.image_view').html('');
		$('.overlay .overlay_content').animate({'width':width,'height':height,'margin-left':(width/2 + 10 )*-1,'margin-top':(height/2 + 10 )*-1},300,function(){
			var image = new Image;
				$(image).load(function(){
					
					$('.overlay .overlay_content').find('.image_view').html(this);
					$(this).attr({'width':width,'height':height});
				})
				.attr('src',val);
		});
	});
	
// ARCHIVES VIDEO
	$('.ls_video_thumbs .thumb').click(function(){
		var id = $(this).attr('_val');
		$('#video_player').attr('src','http://player.vimeo.com/video/'+ id +'?portrait=0');
	});
	
	$('.ls_ustream_thumbs .thumb_live').click(function(){
		var id = $(this).attr('_val');
		var html = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="610" height="370" id="utv293190"><param name="flashvars" value="autoplay=false&amp;brand=embed&amp;cid='+id+'"/><param name="allowfullscreen" value="true"/><param name="allowscriptaccess" value="always"/><param name="movie" value="http://www.ustream.tv/flash/viewer.swf"/><embed flashvars="autoplay=false&amp;brand=embed&amp;cid='+id+'" width="610" height="370" allowfullscreen="true" allowscriptaccess="always" id="utv293190" name="utv_n_379522" src="http://www.ustream.tv/flash/viewer.swf" type="application/x-shockwave-flash" /></object>'
		$('#video_player').html(html);
	});
	
	$('.ls_ustream_thumbs .thumb_video').click(function(){
		var id = $(this).attr('_val');
		var html = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="610" height="370" id="utv533941" name="utv_n_528698"><param name="flashvars" value="autoplay=true&amp;vid='+ id +'" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.ustream.tv/flash/viewer.swf" /><embed flashvars="autoplay=true&amp;vid='+ id +'" width="610" height="370" allowfullscreen="true" allowscriptaccess="always" id="utv533941" name="utv_n_528698" src="http://www.ustream.tv/flash/viewer.swf" type="application/x-shockwave-flash" /></object>'
		$('#video_player').html(html);
	});
	
///////////
// SHARE
//////////
	// TWITTER
	$('.ls_share .twitter').click(function(){
		var link = $(this).attr('_link');
		var txt = $(this).attr('_val');
		var lng = $(this).attr('_lng');
		
		window.open('http://twitter.com/share?text='+ txt + '&url='+ link);
	});

	// FACEBOOK
	$('.ls_share .facebook').click(function()
	{
		var link = $(this).attr('_link');
		var txt = $(this).attr('_val');
		var lng = $(this).attr('_lng');
		var img = $(this).attr('_img');
		FB.ui({
			method: 'stream.publish',
			message: txt,
			attachment: 
			{
				name: 'Festival Mode&Design Montr&eacute;al',
				caption: '',
				description: (txt),
				href: link,
				media: [{'type': 'image', 'src': img, 'href': link}]
			},
			action_links: [{ text: 'Festival Mode&Design Montr&eacute;al', href: 'http://www.facebook.com/SensationMode' }],
			user_message_prompt: txt
		}, 
		function(response) {});
		return false;
	});
});

function verif_rules()
{
	var val = $('input#rules').val();
	if($('input#rules').attr('checked')){
		return true;
	}else{
		$('input#rules').prev('label').addClass('error');
		return false;
	}
}
function verif_form()
{
	// Reset error
	$('.required').removeClass('error');
	var error = false;
	
	$('.required').each(function(){
	
		if($(this).val() == '')	{
			$(this).addClass('error');
			error = true;
		}
	});
	if(error == false){
		return verif_email();
	}
}	
function verif_email(){
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

	var email = $('input#email').val();
	if(reg.test(email) == false) 
	{
		$('#email').addClass('error');
 		return false;
	}else return true;
}

