/* ****************************************************

	@file		  global.js
	@description  Comportements globaux
	@author		  remi (ixmedia.com)
	@version	  20090527

***************************************************** */

(function($){

	$(function() {

		var searchWidth = $('#s').css('width');
		$('#entete .recherche a').click(function() {

			if ( $(this).parent().parent().parent().is('.active') ) {

				var self = this;
				$('#s').stop().animate({
					width: 10
				},{
					duration: 300,
					complete: function() {
						$(self).parent().next().hide();
						$(self).parent().parent().parent().removeClass('active');
						$(this).width( searchWidth );
					}
				});

			} else {

				$('#s').width(1);
				$(this).parent().next().show();
				$('#s').stop().animate({
					width: searchWidth
				},{
					duration: 300
				});

				$('#s').focus();
				$(this).parent().parent().parent().addClass('active');
			}
			return false;
		});


		if ( $('.featured').length ) {

			var $featuredItems = $('.featured-item');
			$featuredItems.not(':first-child').hide();

			$('.featured').append('<div class="featured-nav"><ul class="clearfix"></ul></div>');
			var ulContent = '';
			var compteur = 0;

			if ( $featuredItems.length > 1 ) {
				$featuredItems.each(function() {
					compteur++;
					var href = $(this).find('h3 a').attr('href');
					ulContent += '<li'+(compteur==1?' class="active"':'')+'><a href="'+href+'">'+compteur+'</a></li>';
				});
			}

			$('.featured-nav ul').append(ulContent);
			$('.featured-nav a').click(function() {

				if ( $(this).parent().is('.active') ) {
					return false;
				}
			
				var index = $(this).parent().parent().find('li').index( this.parentNode );
				$(this).parent().addClass('active').siblings().removeClass('active');

				$('.featured').stop().animate({
					height : ($('.featured-item').eq(index).height())+'px'
				},{
					duration: 300
				})

				$('.featured-item:visible').css('position', 'absolute').stop().animate({
					opacity: 0
				},{
					duration: 300,
					complete: function() {
						$(this).hide();
						$(this).css('position', 'relative');
					}
				});

				$featuredItems.eq(index).css('position', 'absolute').stop().css('opacity', 0).show().animate({
					opacity: 1
				},{
					duration: 300,
					complete: function() {
						$(this).css('position', 'relative');
						$('.featured').css('height', 'auto');
					}
				});

				return false;

			});

		}

	});

})(jQuery);
