
$(document).ready(function() {
	
	$('textarea').growfield();

	findSlider(document);
	findBoxes(document);
	findNews();
	
	$(document).trigger('pageLoaded');	
});

function findNews() {
	
	$("#MoreNews").unbind('click').bind('click', function(event) {

		var div = this;
		$(div).html('Chargement...');
		$.get('./content/_ajax/getnews.ajax.php', { page: $(this).attr('rel') }, function(data) {

			var data = $(data);
			$(div).before(data.hide().slideDown());
			$(div).attr('rel', parseInt($(div).attr('rel')) + 1);
			
			$(data).each(function() {
				try {
				FB.XFBML.parse(this);
				} catch(e) {}
			});
			
			$(div).html('+ de news');
			
			findBoxes(data);
		//	findAudioVideo();
		});
	});
}

function findSlider(content) {
	
	  $(content).find('#Slider').nivoSlider({
        effect:'fade', // Specify sets like: 'fold,fade,sliceDown'
        directionNav: true,
        animSpeed: 100,
        pauseTime: 30000,
        controlNav: false
    });
	
}
function findBoxes(content) {

	$(content).find("a[rel=ajaxbox]").box({
		speed :700,
		bg_alpha : 0.5,
		scroll : false,
		drag :false,
		nav_pos: 'bottom',
		mode: 'content',
		onLoad: function(div) {
			findVideos(div);
		}
	}).bind('click', function(event) {
		event.stopPropagation();
	});
	
	$(content).find("a[rel=imagebox]").box({
		speed :700,
		bg_alpha : 0.5,
		scroll : false,
		drag :false,
		nav_pos: 'bottom',
		mode: 'image'
	});
}

function findRubriques(div) {
	
	$(div).find('ul.Rubriques li').bind('click', function() {
		
		var id = $(this).data('rub-id');
		if(id === undefined)
			return;
		
		var rub = this;
		$(this).data('page', 0);
		
		$(this).parent().children().removeClass('Selected');
		$(this).addClass('Selected');
		
		$(this).parent().parent().children('div').not('.Spacer').slideUp('slow');
		$(this).parent().parent().children('div[rel=' + id + ']:lt(2)').slideDown('slow').each(function() {
				$(this).find(".FBLike:visible").each(function() {
					FB.XFBML.parse(this);
				});
			});;
		
		$.url._urlInfos['rubrique'] = [id, 0];
		$.url.rebuild();

		$(this).parent().parent().data('rub', id); 
		
		if($(this).parent().parent().children('div[rel=' + id + ']:gt(1):not(".Seemore")').length > 0)
			$(this).parent().parent().find('.Seemore').fadeIn();
		
		else
			$(this).parent().parent().find('.Seemore').fadeOut();
	});
	
	$(div).find('.Seemore').bind('click', function() {
		
		var id = undefined;
		var rub = $(this).parent().children('.Rubriques').children('.Selected');
		
		if(rub.length > 0) {
			var page = (page = $(rub).data('page')) !== undefined ? page + 1 : 1;
			var id = $(rub).data('page', page).data('rub-id');
			
			$.url._urlInfos['rubrique'] = [id, page];
			$.url.rebuild();
		
		} else {
			var page = $(this).data('page');
			if(page == undefined)
				page = 1;
			else
				page++;
			$(this).data('page', page);
		}
		
		if(id !== undefined) {
			$(this).parent().children('div[rel=' + id + ']:lt(' + ((page + 1) * 3) + ')').slideDown('slow').each(function() {
				
				$(this).find(".FBLike:visible").each(function() {
					FB.XFBML.parse(this);
				});
			});	
			if($(this).parent().children('div[rel=' + id + ']:gt(' + ((page + 1) * 3 - 1) + ')').length == 0)
				$(this).fadeOut();
		} else {
			$(this).parent().children('div:lt(' + ((page + 1) * 3) + ')').slideDown('slow').each(function() {
				$(this).find(".FBLike:visible").each(function() {
					FB.XFBML.parse(this);
				});
			});	
			
			
			if($(this).parent().children('div:gt(' + ((page + 1) * 3 - 1) + '):not(".Seemore")').length == 0)
				$(this).fadeOut();
		}
	});
}

$(document).bind('url.set-rubrique', function() {
	
	if(rub = $.url._urlInfos['rubrique']) {
		
		$("[data-rub-id=" + rub[0] + "]").trigger('click').parent().parent().children('.Seemore').each(function() {
			
			var page = rub[1];
			for(var i = 0; i < page.length; i++) {
				$(this).trigger('click');
			}
		});
	}
		
});


$(document).bind('pageLoaded', 	function() {
	
	
	$("#FormGuestlist").submit(function() {
		
		var that = this;
		$.post('./content/_modules/guestlist.module.php', $(this).serialize(), function(data) {
			$(that).replaceWith($(data));
		});
		
		return false;
	});
	
	try {
		$(".FBLike:visible").each(function() {
			FB.XFBML.parse(this);
		});
	} catch(e) {
		console.info("Failed to parse XFBML tags");
	}
	
	
	
	var content = $("#PageContent");
	findLinks(content);
	
	findBoxes(content);
	findNews(content);			
	findRubriques(content);
	
	
	findSlider(content);
	findCaptcha(content);	
	
	
	$(".Shop .ImageShop").bind('click', function(e) {
		$(this).parent().children('.Inside').fadeIn();
	});
});


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

	var myLatlng = new google.maps.LatLng(0, 0);	
	var myOptions = {
	  zoom: 8,
	  center: myLatlng,
	  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 }, function(results, status) {
		
		if (status == google.maps.GeocoderStatus.OK) {
			map.setCenter(results[0].geometry.location);
			map.setZoom(13);
			
			var marker = new google.maps.Marker({
				map: map, 
				position: results[0].geometry.location
			});	
			
		    var contentString = '<div class="GmapInfos"><h3>' + name + '</h3>' + address + '<br />' + zip + " " + city;
		    var infowindow = new google.maps.InfoWindow({
		        content: contentString
		    });
	     	infowindow.open(map,marker);
	     	
			$(div).css('opacity', 1);
		} 
	});
}



function findVideos(content) {
	$(content).find(".build_video").each(function() {
		$(this).buildVideo({ platform: $(this).data('video-platform'), id: $(this).data('video-id') });								
	});	
}


(function($) {
	
	$.fn.buildVideo = function(opts) {
		
		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: 559,
						height: 418,
						hasVersion: 8
					});
				break;
				
			}
		});
	}
}) (jQuery);


function findVideos(content) {
	$(content).find(".build_video").each(function() {
		$(this).buildVideo({ platform: $(this).data('video-platform'), id: $(this).data('video-id') });								
	});	
}

(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);


function findLinks(dom) {
	
	$(dom).find('a.Ajax').not('.NoAjax').bind('click', function(e) {
		$(this).addClass('Selected').parent().siblings().children('a').removeClass('Selected');
		e.preventDefault();
		var href = $(this).attr('href');
		$("#PageContent").loadPage({url: href, mode: 'page'});
		
		var url = document.location.href.split("#");
		url[1] = href;
		
		document.location.href = url[0] + "#" + url[1];
	});
}





