
$(document).ready(function() {

	findImages(document);

	$("#form-formticket").each(function() {
		var form = PiCom.form.create({
			url: './content/soirees/formbilleterie.xml',
			onLoad: function() {
				$("#form-formticket").html(this.dom);
			}
		});
	});
	
	
	$("#form-formbenevoles").each(function() {
		var form = PiCom.form.create({
			url: './content/soirees/formbenevoles.xml',
			onLoad: function() {
				$("#form-formbenevoles").html(this.dom);
			}
		});
	});
	
	$("#form-formconcours").each(function() {
		var form = PiCom.form.create({
			url: './content/soirees/formconcours.xml',
			onLoad: function() {
				$("#form-formconcours").html(this.dom);
			}
		});
	});

	$(".nivoSlider").nivoSlider({ controlNav: false, pauseTime: 10000 });

	$("ul#Contests li a").bind('click', function() {
		
		$("ul#Contests li").removeClass('Selected');
		$(this).parent().addClass('Selected');
		
		$("#Event").animate({opacity: 0}, 400);
		$.get('./content/_ajax/getevent.ajax.php', {type: 'contest', event_id: $(this).data('event-id') }, function(data) {
			$("#Event").html(data);
			findGmaps('#Event');
			findImages('#Event');
			findBoxes($("#Event"));
			$("#Event").stop().animate({opacity: 1}, 300);
		});
	});


	$("ul#AGs li a").bind('click', function() {
		
		$("ul#AGs li").removeClass('Selected');
		$(this).parent().addClass('Selected');
		
		$("#Event").animate({opacity: 0}, 400);
		$.get('./content/_ajax/getevent.ajax.php', {type: 'ag', event_id: $(this).data('event-id') }, function(data) {
			$("#Event").html(data);
			findGmaps('#Event');
			findImages('#Event');
			findBoxes($("#Event"));
			$("#Event").stop().animate({opacity: 1}, 300);
		});
	});
	
	$("ul#Soirees li a").bind('click', function() {
		
		$("ul#Soirees li").removeClass('Selected');
		$(this).parent().addClass('Selected');
		
		$("#Event").animate({opacity: 0}, 400);
		$.get('./content/_ajax/getevent.ajax.php', {type: 'soiree', event_id: $(this).data('event-id') }, function(data) {
			$("#Event").html(data);
			findGmaps('#Event');
			findImages('#Event');
			findBoxes($("#Event"));
			$("#Event").stop().animate({opacity: 1}, 300);
		});
	});


	$(document).find("a[rel=openbox]").each(function() {
	
		$.fancybox($(this),
			{
				height: $(this).siblings().data('box-height'),
				width: $(this).siblings().data('box-width'),
				closeBtn: true,
				afterLoad: function(div) {
					
					findVideos(div);
					findGmaps();
				}
			}
		);
	});
	
	


	function findImages(sel) {
		
		$(sel).find(".MoreImages").bind('click', function() {
			$(this).parent().children('.Hidden').hide().removeClass('Hidden').show('slow');
			$(this).remove();
		});
		
	}
	
	var _pageNews = 1;
	$("#MoreNews").bind('click', function() {
		var _page;
		if((_page = $(this).attr('data-page-news')) == undefined)
			_page = _pageNews;
		
		var that = this;
		$.get('./content/_ajax/getnews.ajax.php', {page: _page}, function(content) {
			
			if(content == "") {
				$(that).hide();
				return;
			}
			var content = $("<div>" + content + "</div>");
			$(that).before(content);
			
			FB.XFBML.parse(content.get(0));
			
			content.hide().slideDown('slow');
			_pageNews++;
			$("#MoreNews").attr('data-page-news', _pageNews);	
		});
	});
	
	/* Google Map */
	$('textarea').growfield();

	findGmaps(document);
	findBoxes(document);
	
	/* Recherche d'events */
	$("form#SearchContent").submit(function(event) {
		event.preventDefault();
		$.get('./libs/ajax/earch_contest.ajax.php', $(this).serialize(), function(data) {
			$("#Page").html(data);
		});
	});
	
	///////////////////////////////////////////////////////////////////////
	/// AGENDA ////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////
	(function($) {
	
		$.fn.scrollNavigation = function(options) {
			
			return this.each(function() {
				
				if(options.url === undefined)
					return;
					
				var main = this;
				
				var bef = $('<div>').attr('id', 'PrevContent');
				var aft = $('<div>').attr('id', 'NextContent');
				var wr = $('<div>').attr('id', 'CurrentContent');
				var wrContent = $('<div>').attr('id', 'WrapperContent');
				var navPrev = $('<div>').addClass('Navigation').html('<div class="Prev"></div>');
				var navNext = $('<div>').addClass('Navigation').html('<div class="Next"></div>');
				
				$(this).wrap(wr);
				var wr = $(this).parent();
				$(wr).before(bef).after(aft).add(bef).add(aft).wrapAll(wrContent);
				var wrContent = $(wr).parent();
				$(wrContent).prepend(navPrev).append(navNext);
				
				$(navPrev).add(navNext).bind('click', function(event) {
					
					var wrap = $(this).parent();
					var current = $(this).data('nav-idpage');
					if(!current)
						var current = 0;
					var _class = $(this).children(0).attr('class');
					
					switch(_class) {
				
						case 'Prev':
							$.get(options.url, {page: current - 1}, function(data) {
								
								if(data.length == 0)
									return;
									
								wrap.children('#PrevContent').hide().html(data).slideDown('linear');
								wrap.children('#CurrentContent').slideUp('linear', function() {
									var newPrev = $('<div id="PrevContent"></div>');
									$(this).prev().attr('id', 'CurrentContent').before(newPrev);
									$(this).remove();
								});
								
																
								if($.isFunction(options.onClickPrev))
									options.onClickPrev.call(this, event);
									
								if($.isFunction(options.onClickNav))
									options.onClickNav.call(this, event);
									
								$(navNext).add(navPrev).data('nav-idpage', current - 1);
							});
							
						break;	
						
						case 'Next':
							$.get(options.url, {page: current + 1}, function(data) {
								
								if(data.length == 0)
									return;									
									
								wrap.children('#NextContent').hide().html(data).slideDown('linear');
								var height = wrap.children('#CurrentContent').height();
								wrap.children('#CurrentContent').parent().css('overflow', 'hidden').end().animate({marginTop: - + height + "px"}, 'linear', function() {
									$(this).parent().css('overflow', 'visible').end().remove();
									var newAft = $('<div id="NextContent"></div>');
									wrap.children('#NextContent').attr('id', 'CurrentContent').after(newAft);
								});
								
								if($.isFunction(options.onClickNext))
									options.onClickNext.call(this, event);
									
								if($.isFunction(options.onClickNav))
									options.onClickNav.call(this, event);
									
								$(navNext).add(navPrev).data('nav-idpage', current + 1);	
							});
						break;				
					}
				});
			});
		};
		
	}) (jQuery);
		

	$(".Event").moduleLoading({ module: $("#DetailsEvent") });
	$(".Group").moduleLoading({ module: $("#DetailsGroup") });
	$(".Group").moduleLoading({ module: $("#PhotosGroup"), url: './content/_ajax/loadphotosgroup.ajax.php' });
	$(".Member").moduleLoading({ module: $("#DetailsMember") });
	$(".Article").moduleLoading({ module: $("#DetailsArticle") });

});


function findGmaps(content) {


	$(content).find(".Build-GMap").each(function() {
		
		var name = $(this).data('gmap-name');
		var address = $(this).data('gmap-address');
		var zip = $(this).data('gmap-zip');
		var city = $(this).data('gmap-city');
		generateMap(this, name, address, zip, city);
	});
}



function findBoxes(content) {
	
	$(content).find("a.fancybox").fancybox({
		type: 'image'
	});	
	
	
	$(content).find("a.ajaxbox").fancybox({
			type: 'ajax',
			width: $(this).data('box-width'),
			height: $(this).data('box-height'),
			closeClick: false,
			afterShow: function() {
				
				findGmaps(document);
			}	
		});
		
}


function generateMap(div, name, address, zip, city) {

	var mlatlng = new google.maps.LatLng(0, 0);
    var myOptions = {
      zoom: 8,
      center: mlatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };

	var map = new google.maps.Map(div, myOptions);
	$(div).css('opacity', 0).data('map', map);
	
	geocoder = new google.maps.Geocoder();
	geocoder.geocode( { 'address':  address + ", " + zip + " " + city + " Suisse" }, function(results, status) {
		
		if (status == google.maps.GeocoderStatus.OK) {
			map.setCenter(results[0].geometry.location);
			map.setZoom(address == null ? 11 : 16);
			
			var marker = new google.maps.Marker({
				map: map, 
				position: results[0].geometry.location
			});	
			
		    var contentString = '<div class="GmapInfos"><h3>' + name + '</h3>' + (address != '' ? address + '<br />' : '') + zip + " " + city;
		    var infowindow = new google.maps.InfoWindow({
		        content: contentString
		    });
	     	infowindow.open(map,marker);
	     	
			$(div).css('opacity', 1);
		} 
	});
}


(function($) {
	
	$.fn.moduleLoading = function(opts) {
		

		if(!opts.module)
			return;
			
		if(opts.reset)
			opts.module.animate({ marginTop: 0});
		
		var all = this;
		this.bind('click', function() {
			var pos = $(this).position();
			var top = pos.top - 230;
			
			$(all).removeClass('Selected');
			$(this).addClass('Selected');
			
			var parentHeight = $(this).parent().height();
			var modulesHeight = opts.module.parent().height();
			var currentHeight = opts.module.height();
			
			function processLoading(html) {
				
				$(opts.module).children('.Content').children('.Inside').animate({opacity: 0}, 'fast', function() {
					var that = this;
					$(that).parent().css('overflow', 'hidden').css('height', $(this).height() + "px").end().html(html);
					var height = $(that).height();
					findBoxes(opts.module);
					$(that).parent().animate({ height: (height + 20) + "px"}, 'slow', function() {
						$(this).css({height: 'auto'}).children().animate({ opacity: 1}, 'fast');
					});
					
				/*	if(recompute) {
						var top = parentHeight - (height + modulesHeight - currentHeight);
						$(opts.module).parent().stop().animate({ marginTop: top + 'px'}, 'fast', function() {
							$(opts.module).animate({ opacity: 1}, 'fast');
						});
					}
					*/
				}).end().end().stop().parent().animate({ marginTop: top + 'px'});
			}
			
			recompute = false;
			if(modulesHeight + top > parentHeight)
				recompute = true;
			
			var html = $(this).find('.Informations').html();
			
			if(opts.url) {
				$.get(opts.url, {id: $(this).data('el-id') }, function(html) {
					processLoading(html);
				})
			} else {
				processLoading(html);
			}
		});
		
		return this;
	}; 
}) (jQuery);


$(document).ready(function() {
	
	/*
	$("div#ContentBourse").scrollNavigation({
		url: './content/_ajax/bourse.ajax.php',
		onClickNav: function() {
			$(".Article").moduleLoading({ module: $("#DetailsArticle"), reset: true });			
		}
	})*/
	
	
	$("div#ContentAgenda").scrollNavigation({
		url: './content/_ajax/agenda.ajax.php',
		onClickNav: function() {
			$(".Event").moduleLoading({ module: $("#DetailsEvent"), reset: true });			
		}
	})
	
});



function findVideos(content) {
	$(content).find(".build_video").each(function() {
		var options = { 
			platform: $(this).data('video-platform'), 
			id: $(this).data('video-id') 
		};
		
		if($(this).width() > 0)
			options.width = $(this).width();
		
		if($(this).height() > 0)
			options.height = $(this).height();
			
		$(this).buildVideo(options);								
	});	
}

(function($) {
	
	$.fn.buildVideo = function(opts) {
		
		var width = (opts.width == undefined || !opts.width) ? 540 : opts.width;
		var height = (opts.height == undefined || !opts.height) ? 400 : opts.height;
		
		return this.each(function() {
			if(!opts.platform || !opts.id)
				return false;
			
			switch(opts.platform) {
				
				case 'youtube':
					$(this).flash({
						swf: 'http://www.youtube.com/v/' + opts.id,
						flashvars: {
							enablejsapi: 1,
							playerapiid: 'ytplayer'
						},
						width: width,
						height: height,
						hasVersion: 8
					});
				break;
				
				case 'vimeo':
				
					$(this).flash({
						swf: 'http://vimeo.com/moogaloop.swf',
						flashvars: {
							clip_id: opts.id,
							show_portrait: 1,
							show_byline: 1,
							show_title: 1,
							js_api: 1,
							js_onLoad: null,
							js_swf_id: 'Video'
						},
						width: width,
						height: height,
						allowfullscreen: true
					});
				
				break;
				
				case 'wat':
				
					$(this).flash({
						swf: 'http://wat.tv/swf2/' + opts.id,
						flashvars: {
						},
						width: width,
						height: height,
						
						allowfullscreen: true
					});
				break;
				
				case 'dailymotion':
				
					$(this).flash({
						swf: 'http://www.dailymotion.com/swf/' + opts.id,
						flashvars: {
							enableApi: 1,
							playerapiid: 'dmplayer'
						},
						width: width,
						height: height,
						allowfullscreen: true
					});
					
				break;
			}
		});
	}
}) (jQuery);


