var alertBox = 
{
	element: $('div#alertBox'),
	content: $('div#alertBoxContent'),
	bottom: $('div#alertBoxBottom'),
	overlay: $('div#overlay'),
	unload: false,

	setTopContent: function(content) {
		this.top.html(content);
	},
		
	setBottomContent: function(content) {
		this.bottom.html(content);
	},
	
	/*
	 * @method alertBox.start
	 */
	start: function(obj)
	{
		// Bouton de fermeture de l'alertBox
		if(obj.cancelButton) {
			$(obj.cancelButton).click(function() {
				alertBox.close(); return false;
			});
		}
		
		// Placement de l'alertBox au centre de la page
		this.element.css('top',(($(window).height()-this.element.height())/2)+$(window).scrollTop());
		this.element.css('left',($(window).width()-this.element.width())/2);
		this.overlay.height($(document).height()).fadeIn(100);
		this.element.fadeIn(150);
	},
	
	startError: function(msgHTML)
	{
		this.content.html('<h3 id="alertbox-error"></h3>'+msgHTML);
		this.bottom.html('<a id="ok" class="submitCenter" href="#" title=""></a>');
		
		// Bouton de fermeture de l'alertBox
		if(obj.cancelButton) {
			$(obj.cancelButton).click(function() {
				alertBox.close(); return false;
			});
		}
		this.unload = function(){alertBox.bottom.find('a#ok').unbind('click')};
		
		// Placement de l'alertBox au centre de la page
		this.element.css('top',(($(window).height()-this.element.height())/2)+$(window).scrollTop());
		this.element.css('left',($(window).width()-this.element.width())/2);
		this.overlay.height($(document).height()).fadeIn(100);
		this.element.fadeIn(150);	
	},
	
	close: function()
	{
		this.element.fadeOut(150,function(){alertBox.content.empty();});
		this.overlay.fadeOut(100);
	}
};

$(document).ready(function()
{
	var annoncesSlider = 
	{
		interval:'',
		annonceWrapper:$('#annonces'),
		annoncesList:$('#annonces').children('.annonce'),
		defilBtns: $('#annonces').children('#annonces-nb').children(),
		
		init: function() {
			this.annoncesList.not(":first").hide();
			this.defilBtns.click(this.goToAnnonce);
			this.start();
		},
		start: function() {
			this.interval = setInterval(this.next,10000);
		},
		stop: function() {
			clearInterval(this.interval);
		},
		next: function() 
		{
			var currAnnonce = annoncesSlider.getCurrentAnnonce();
			currAnnonce.fadeOut(512, function() 
			{
				if(currAnnonce.is(':last-child'))
					currAnnonce.parent().children('.annonce:first').fadeIn(512);
				else 
					currAnnonce.next().fadeIn(512);
					
				var currDefilBtn = annoncesSlider.getActiveDefilBtn().removeClass('active');
				if(currDefilBtn.is(':last-child'))
					annoncesSlider.defilBtns.eq(0).addClass('active');
				else
					currDefilBtn.next().addClass('active');
			});
		},
		getActiveDefilBtn: function() {
			return annoncesSlider.defilBtns.filter('.active');
		},
		getCurrentAnnonce: function() {
			return annoncesSlider.annoncesList.filter(':visible');
		},
		goToAnnonce: function(e)
		{
			annoncesSlider.stop();
			var id = $(this).attr('id');
			var nb = parseInt(id.substring(id.length-1),10);
			var currAnnonce = annoncesSlider.getCurrentAnnonce();
			
			annoncesSlider.getActiveDefilBtn().removeClass('active');
			$(this).addClass('active');
			
			currAnnonce.fadeOut(512, function() {
				annoncesSlider.annoncesList.eq(nb-1).fadeIn(512,function(){annoncesSlider.start();});
			});
			return false;
		}
	}
	
	if($('#alertBox').length > 0) {alertBox.start({cancelButton:'#ok'});}
	
	// Lien contact
	$('#contact').click(function(){window.open($(this).attr('href')); return false;});
	
	// Targets _blank
	$('#contact').add('#social a').add('#footer a').click(function(){window.open($(this).attr('href')); return false;});

	// Défilement
	annoncesSlider.init();
	
	$('#submit-preinscription').hover(
		function(e){$(this).css('background-position','0 -42px');},
		function(e){$(this).css('background-position','0 0');}
	);
});