(function($) {
	$.fn.extend({
		pi_gal:function(o){
			return this.each(function(index,dom){
				
				var $this = $(this),
					_timeChange = null,
					
					_settings ={
						id:0,
						datas:null, /*
							= {
								imgs:[
									{ src:'' },
									{ src:'' }
								];
							}; 
						*/
						onchange:null
					};
				
				if(o) $.extend(_settings,o);
				
				function _set () {					
					if (_timeChange) clearTimeout(_timeChange);
					$this.attr('src',_settings.datas.imgs[_settings.id].src);
					if (_settings.onchange) {
						_settings.onchange(_settings.id);
					}				
				}
				
				$this.bind('pi_gal_previous',function(e){
					_settings.id = _settings.id>0 ?_settings.id-1 :_settings.datas.imgs.length-1;
					_set();
				});
				$this.bind('pi_gal_next',function(e){
					_settings.id = _settings.id<_settings.datas.imgs.length-1 ?_settings.id+1 :0;
					_set();
				});
				
				_set();
				
			});
		}
	});	
})(jQuery);

(function($) {
	$.fn.extend({
		pi_popin_gal:function(o){
			return this.each(function(index,dom){
				
				var $this = $(this),
					
					_settings ={
						datas:null, /*
							datas = {
								texts:[
									{
										h1:'',
										h2:'',
										span:'',
										p:''
									}
								];
							}; 
						*/
						gallery:null
					};
				
				if(o) $.extend(_settings,o);
				
				$this.find('strong').html('<span></span>/'+_settings.datas.texts.length);
				$this.find('.previous, .next').click(function(){
					_settings.gallery.trigger('pi_gal_'+this.className);
				});
				
				$this.bind('pi_popin_gal_change',function(e,id){					
					var t = _settings.datas.texts[id];
					$this.find('h1').html(t.h1);
					$this.find('h2').html(t.h2+' <span>'+t.span+'</span>');					
					$this.find('p').html(t.p);
					$this.find('div').html(t.div);
					$this.find('strong span').html(Number(id)+1);
				});
				
			});
		}
	});	
})(jQuery);

(function($) {
	$.fn.extend({
		pl_gal:function(o){
			return this.each(function(index,dom){
				
				var $this = $(this),
					$see = $('.see')
								.each(function(i){
									$(this).data('id',i);
								})
								.click(_click)
								.pi_roll()
									.trigger('pi_roll_lock',[true]),
					_interval = null,
					_settings ={
						id:0,
						item:'item'
					};
				
				if(o) $.extend(_settings,o);
				
				function _click (e) {
					var o = $(e.target);
					if (_settings.id==o.data('id')) {
						return;
					}
					_unset();
					_settings.id = o.data('id');
					_set();				
				}
				
				function _unset (id) {	
					$($see[_settings.id]).trigger('pi_roll_force',[0]);					
					$('#'+_settings.item+_settings.id).css({ 'display':'none' });
				}
				
				function _set () {					
					$($see[_settings.id]).trigger('pi_roll_force',[1]);
					$('#'+_settings.item+_settings.id).css({ 'display':'block' });					
				}
				
				function _next (){
					_unset();
					_settings.id = _settings.id<$see.length-1 ?_settings.id+1 :0;
					_set();
				}
				
				
				function _setInterval () {
					if (_interval) clearInterval(_interval);
					_interval = setInterval(_next,6000);
				}
				
				$this.find('.previous').click(function(){
					_unset();
					_settings.id = _settings.id>0 ?_settings.id-1 :$see.length-1;
					_set();
				});
				
				$this.find('.next').click(function(){;
					_setInterval();
					_next();
				});
				
				_set();
				_setInterval();
				
			});
		}
	});	
})(jQuery);

(function($) {
	$.fn.extend({
		pl_box_actu:function(o){
			return this.each(function(index,dom){
				
				var $this = $(this),
					$link = o.link,					
					_open = false,
					_id = $link.attr('rel'),
					_content = null,
								
					_settings ={
						
					};
				
				if(o) $.extend(_settings,o);
				
				$this.prepend('<a name="'+_id+'" />');
				
				function _openHide (e) {
					
					e.preventDefault();
					
					if (!_open) {

						$.ajax({
							url: $this.find('a[rel="'+_id+'"]').attr('href'),
							success: function (o) {
								$link.parent().detach();
								
								if (!_content) {
									$this.append('<div id="b'+_id+'">'+o+'</div>')
										 .find('.close').click(_openHide);
									_content = $('#b'+_id);
									if ($this.next().length>0) {
										$this.find('.share').addClass('shareNoBg');
									}
								}
								else {
									_content.css({ 'display':'block' });
								}
								
							}
						});
						
					}
					else {
						$('#b'+_id).css({ 'display':'none' });
						$this.append($link.parent());
					}
					
					_open =! _open;
					
				}

				if (pl.actus.id==_id) {
					document.location = '#'+_id;
					_openHide();
				}
				
				$link.click(_openHide);
				
			});
		}
	});	
})(jQuery);

(function($) {
	$.fn.extend({
		pl_you_was:function(o){
			return this.each(function(index,dom){
				
				var $this = $(this),
					$input = $this.find('input');
					
				$this.pi_form().trigger('pi_form_inputText')
					 .find('a')
					 .click(function(){
						  if ($input.val()==''||$input.val()==$input.attr('title')) {
							  $this.find('.warning').remove();						
							  $this.append('<span class="warning">Votre numéro perso est obligatoire</span>')
							  	   .find('.warning')
								   .pi_alt();
						  }
						  else {
							  $.getJSON(pl.urls.retrieve_photos,{ num:$input.val() },_valid);
						  }
					 });
					 
				function _valid (o) {
					if (!o.response) {
						$this.find('.warning').remove();
						$this.append('<span class="warning">'+o.msg+'</span>')
							 .find('.warning')
							 .pi_alt();
					}
					else {
						document.location = o.redirect;
					}
				}
					
			});
		}
	});	
})(jQuery);

(function($) {
	$.fn.extend({
		pl_pagination:function(o){
			return this.each(function(index,dom){

				var $this = $(this),
					_time = null,
					$a,
					$anew;					
				
				function _setClick () {
					$pages = $this.find('.pagination');
					$pages
						 .find('a')
							 .click(function(e){
								 
								e.preventDefault();	
									
								$anew = $(this);
								if ($anew.parent().hasClass('on')) {
									clearTimeout(_time);
									return;
								}
								else if ($a&&$a.attr('href')==$anew.attr('href')) {
									return;
								}
								$a = $anew;
								
								if (!$a.parent().hasClass('on')) {								
									
									if (_time) clearTimeout(_time);
									
									_time = setTimeout(function(){
										
										$.ajax({
											url:$a.attr('href'),
											success: function (o) {
												$pages.remove();
												$this.find('.boxActuSmall').parent().remove();
												$this.append(o);
												_setClick();
												$('.boxActuSmall').each(pl.pl_box_actu_set);
											}
										});
										
									},500);	
																
								}
							});
				}
				
				_setClick();
					
			});
		}
	});	
})(jQuery);

(function($) {
	$.fn.extend({
		pl_google:function(o){
			
			var $this = $(this),
				_gm = google.maps,
				_marker = new _gm.Marker({
					icon:new _gm.MarkerImage(pl.urls.url_site+'images/google.png',new _gm.Size(39,37),new _gm.Point(0,0),new _gm.Point(13,35))
				}),
				_geocoder = new _gm.Geocoder();
			
			return this.each(function(index,dom){
				
				var $this = $(this),
					_location = null;
				
				function _init () {
					
					var _map = new _gm.Map($this[0],{
						center:new _gm.LatLng(46.75984,1.738281),
						mapTypeId:_gm.MapTypeId.ROADMAP
					}),
					_overlay = new _gm.OverlayView(),
					
					_settings = {
						zoom:4
					};
				
					if(o) $.extend(_settings,o);
					
					_map.setZoom(_settings.zoom);
					
					_overlay.setMap(_map);
					_overlay.draw = function(){};
					_overlay.onAdd = function () {
						_map.setCenter(_location);						
						_marker.position = _location;
						_marker.setMap(_map);
						_map.setZoom(10);
					};
					
				}	
				
				_geocoder.geocode( { 'address':$('#googleInfos').text() }, function(results, status) {
					if (status == _gm.GeocoderStatus.OK) {
						_location = results[0].geometry.location;
						_init();
					}
					else {
						$this.remove();
					}
				});		
				
			});
		}
	});
})(jQuery);

(function($) {
	$.fn.extend({
		pl_group_infos:function(o){
			
			if (this.length==0) {
				if ($('#googleMap').length>0) {
					$('#googleMap').pl_google();
				}
				return;
			}
			
			return this.each(function(index,dom){
				
				var $this = $(this),
					_w = 0,
					_googleActive = false,
					_mediasWrite = false,
					_mediasActive = false,
					$allLiveForm = $('#allLiveForm'),
					_hasAllLiveForm = $allLiveForm.length>0 ?true :false,
					$allLiveFormEmail = null,
					$boxWithGallery = $('.boxWithGallery'),
					
					_settings ={
						id:0
					},
					
					$li = $this.find('li')
							   .each(function(i){	
							   		var li = $(this);								
									if (li.hasClass('on')) {
										_settings.id = i;
									}
									li.data('id',i);
								}),
					$on = null;
				
				if(o) $.extend(_settings,o);
				
				$li
					.each(function(){
						var o = $(this);
						o.css({ 'left':_w+'px' });					
						_w += o.outerWidth()+10;						
					 })
					 .click(function(){
						var $click = $(this);
						if (!$click.hasClass('on')) {
							_click($click);
						}
					 });
				
				function _click (o) {
					
					if ($on) {
						$on.removeClass('on');	
					}
					$on = o;
					o.addClass('on');
					
					_settings.id = o.data('id');
					
					if (_settings.id==0&&$('#googleMap').length>0) {
						$boxWithGallery.css({ 'display':'none' });
						$('.boxPracticalInfos').css({ 'display':'block' });
						if (!_googleActive) $('#googleMap').pl_google();
						_googleActive = true;
					}
					else if (_settings.id==1) {						
						
						$('.boxPracticalInfos').css({ 'display':'none' })
						
						if (_hasAllLiveForm) {							
							$boxWithGallery.css({ 'display':'block' });							
							if (!$allLiveFormEmail) _setLiveForm();							
							return;
						}
						if (_mediasWrite) {
							$boxWithGallery.css({ 'display':'block' });
							if (!_mediasActive) {								
								$('.mediasScroller').pi_scroll({ name: 'mediasScroller', toolsX: true, toolsBtnsX: true });
							}
							return;
						}
						
						$.ajax({
							url: pl.urls.group_gallery,
							success: function (o) {
								_setGallery(o);											
							},
							data: {  }
						});
						
						$.ajax({
							url: pl.urls.group_video,
							success: function (o) {
								_setGallery(o);											
							},
							data: {  }
						});
						
					}
									
				}
				
				function _setLiveForm () {
					$allLiveFormId    = $('#allLiveId');
					$allLiveFormEmail = $('#allLiveEmail');
					$allLiveForm.pi_form().trigger('pi_form_inputText');
					$allLiveForm.find('a').click(function(){
						if (!pl.valid_email($allLiveFormEmail.val())) {
							$allLiveForm.find('.warning').remove();
							$allLiveForm.append('<span class="warning">L\'email est obligatoire</span>')
								 .find('.warning')
								 .pi_alt();
						}
						else {										
							$.post(pl.urls.group_email, $("#allLiveFormFestival").serialize(),function(o){
																			
								if (!o.response) {
									$allLiveForm.find('.warning').remove();
									$allLiveForm.append('<span class="warning">'+o.msg+'</span>')
										 .find('.warning')
										 .pi_alt();
								}
								else {
									$allLiveForm.remove();
									$boxWithGallery.html('<strong class="success">'+o.msg+'</strong>');
								}
								
							}, "json");
						}
					});
				}
				
				function _setGallery (o) {
					$boxWithGallery
					   .prepend(o)
					   .css({ 'display':'block' })
					   .find('img')
							.each(function(i){
								$(this).data('id', $(this).attr('rel_media'));
								$(this).click(_setPopinGallery);
							});
					_mediasWrite = true;
					$('.mediasScroller').pi_scroll({ name: 'mediasScroller', toolsX: true, toolsBtnsX: true });	
					_mediasActive = true;
				}
				
				function _setPopinGallery (e) {										
					$('body').pi_popin({
						name:'popin_gallery',
						url:$(e.currentTarget).parent().hasClass('vid')?pl.urls.popin_video:pl.urls.popin_gallery,
						alpha:100,
						datas:{ id:$(e.currentTarget).data('id') },
						background:'#161616'
					});					
				}
				
				_click($($li[_settings.id]));
				
			});
		}
	});	
})(jQuery);

(function($) {
	$.fn.extend({
		pl_likes:function(o){
			
			return this.each(function(index,dom){
				
				var $this = $(this), s, i, prepend = '';
				
				$.getJSON(pl.urls.like,function(o){
					s = o.likes.toString();	
					for (i=s.length-1;i>=0;i--) {
						prepend += '<div class="num" style="margin-right:'+( i%3==0&&i>0 ?13 :6 )+'px"><span>'+s.charAt(((i-s.length)*-1)-1)+'</span></div>';
					}					
					$this.prepend(prepend);					
				});	
				
			});
		}
	});
})(jQuery);

(function($) {
	$.fn.extend({
		pl_facebook:function(o){
 
			return this.each(function(index,dom){
				
				var $this = $(this),
					$on = $($this.find('.boxVid')[0]),
					//$vid = $('#iframeYouthude'),
					//$date = $vid.find('.date'),
					_time = null,
					_url,
					$boxVid,
					//_dateInfos,
					_link;
				
				$this.find('.boxVid').click(function(e){
					
					e.preventDefault();
					$boxVid = $(this);
					
					if (_time) clearTimeout(_time);
					_link = $boxVid.find('a');
					_url = _link.attr('href');
					
					/*if (_url!=$vid.find('iframe').attr('src')) {
						_time = setTimeout(function(){
							$vid.find('iframe').remove();
							$vid.append('<iframe title="YouTube video player" width="640" height="390" src="'+_url+'" frameborder="0" allowfullscreen></iframe>');
						},500);
					}*/
					
					/*_dateInfos = _link.attr('rel').split(' ');
					$date.html('<strong>'+_dateInfos[0]+'</strong> <span>'+_dateInfos[1]+'</span> '+_dateInfos[2]);*/
					$on.removeClass('boxVidOn');
					$on = $boxVid;
					$on.addClass('boxVidOn');
					
					$.ajax({
						url:_url,
						evalScripts: true,
						success: function (o) {
						   $('#facebookVideo').html(o);
						}
					});
					
				});
				
			});
		}
	});
})(jQuery);

pl.pl_box_actu_set = function () {
	var f = $(this).find('.bt .ajax');
	if (f.length>0) {
		$(this).pl_box_actu({ link:f });
	}
};

pl.valid_email = function (email) {
	var reg = /^[A-Za-z0-9._-]+@[A-Za-z0-9.-]{2,}[.][A-Za-z]{2,3}$/;
  	return (reg.exec(email)!=null);
};

$(document).ready(function () {
    
	$('.legals').click(function(){
		$('body').pi_popin({
			name:'popin_legals',
			url:pl.urls.popin_legals,
			alpha:40
		});
	});
	
	$('#home .panel').pl_gal();
	$('#photos .panel').pl_gal({ item:'photo' });
	$('#iWasHome, #iWas').pl_you_was();
	
	$('.boxActu').each(pl.pl_box_actu_set);
	$('.boxActuSmall').each(pl.pl_box_actu_set);
	
	$('#left').pl_pagination();
	
	$('.boxInfosNav').pl_group_infos();	
	$('#Contact .warning').pi_alt();
	$('#facebookFans').pl_likes();
	$('#what').pl_facebook();
	
});
