/**
 *
 * @author	Benoit Asselin <benoit(at)agenceici(dot)com>
 * @version	javascript.js, 2011/08/26
 * @link	http://www.agenceici.com/
 *
 */


/**
 * jQuery est en mode noConflict
 */
jQuery(document).ready(function($) {
	
	$('#menu > li')
		.hover(function() {
			var $elt = $(this);
			if($elt.children('ul').is(':animated')) {return;}
			$elt	.children('a').addClass('active').end()
				.children('ul').slideDown(200);
		}, function() {
			var $elt = $(this);
			$elt	.children('a').removeClass('active').end()
				.children('ul').slideUp(100);
		});
	
	$('#form-search2')
		.submit(function() {
			var $input = $(this).find('input[type="text"]');
			if(!$.trim($input.val()) || $input.attr('title') == $input.val()) {
				$input.focus();
				return false;
			}
			return true;
		})
		.find('input[type="text"]')
			.focus(function() {
				if($(this).attr('title') == $(this).val()) {
					$(this).val('');
				}
			})
			.blur(function() {
				if(!$.trim($(this).val())) {
					$(this).val($(this).attr('title'));
				}
			});
	
	$('#popular-posts a.tab').click(function() {
		$('#popular-posts a.tab-active')
			.removeClass('tab-active')
			.addClass('tab-normal')
			.next('ul')
				.css({display:'none'});
		$(this)
			.removeClass('tab-normal')
			.addClass('tab-active')
			.next('ul')
				.css({display:'block'});
		return false;
	});
	
	if($('#slide-home').length) {
		var prevIdx = 0;
		var slideTimer = false;
		
		function slideNextIdx(p_idx) {
			var nextIdx = prevIdx + 1;
			
			if(typeof p_idx !== 'undefined') {nextIdx = p_idx;}
			if(nextIdx >= $('#slide-home .preview li').length) {nextIdx = 0;}
			
			$('#slide-home .preview li')
				.eq(prevIdx)
					.css({display:'none'})
					.end()
				.eq(nextIdx)
					.css({display:'block', opacity:0})
					.animate({opacity:1});
			
			$('#slide-home .buttons a')
				.eq(prevIdx)
					.removeClass('active')
					.end()
				.eq(nextIdx)
					.addClass('active');
			prevIdx = nextIdx;
			
			slideTimer = setTimeout(function() {slideNextIdx();}, 5000);
		}
		
		$('#slide-home .buttons a').bind('click mouseover', function() {
			clearTimeout(slideTimer);
			slideNextIdx($(this).parent('li').index());
			return false;
		});
		
		slideNextIdx(0);
	}
	
	$('#commentform')
		.submit(function() {
			var noError = true;
			$(this)
				.find('#author, #email, textarea')
					.each(function() {
						var $elt = $(this);
						if($elt.val() == $elt.attr('title') || !$elt.val()) {noError = false;}
					});
			
			if(!noError) {alert('Merci de bien remplir le formulaire: ECRIRE un commentaire');}
			return noError;
		})
		.find('input[type="text"], textarea')
			.each(function() {
				var $elt = $(this);
				if(!$elt.val()) {$elt.val($elt.attr('title'));}
			})
			.focus(function() {
				var $elt = $(this);
				if($elt.val() == $elt.attr('title')) {$elt.val('');}
			})
			.blur(function() {
				var $elt = $(this);
				if(!$elt.val()) {$elt.val($elt.attr('title'));}
			});
	
	$('#archives-monthly a.more').click(function() {
		$('#archives-monthly-more').slideToggle(400);
		return false;
	});
	
	// cgv
	$(':input[name="chkpaypal"]').each(function() {
		$(this).parents('form')
			.submit(function() {
				if(!$(this).find(':input[name="chkpaypal"]')[0].checked) {
					alert('Vous devez pr\xE9alablement accepter les Conditions G\xE9n\xE9rales de Vente en cochant la case ci-dessous !');
					return false;
				}
				return true;
			});
	});
	
});

(function($){
	
	$.theProducts = function() {
		var height = $('#bi-min a').height();
		var timeout = 0;
		var duration = 600;
		function nextA() {
			if($('#bi-min:animated').length) {return;}
			clearTimeout(timeout);
			$('#bi-min').css({bottom: 0}).animate({bottom: -height}, duration);
			$('#bi-min a:last-child').prependTo('#bi-min');
			$('#bi-min a:nth-child(3)').clone()
				.css({opacity: 0}).appendTo('#bi-max')
				.animate({opacity: 1}, duration, function() {
					if($('#bi-max a').length >= 2) {
						$('#bi-max a:eq(0)').remove();
					}
				});
			timeout = setTimeout(function(){nextA();}, 5000);
		}
		
		$('#bi-button').click(function(){nextA();});
		nextA();
		
		$('.bi-scroll').each(function() {
			var $li = $(this).find('li').eq(1);
			if($li.length) {
				var width = $li.width() + parseInt($li.css('marginLeft'));
				$(this).find('ul').width(width * $(this).find('li').length);
			}
		});
	};
	
})(jQuery);



/**
 * A ne plus utiliser
 * @deprecated
 */
function doCheckCgv(chkpal) {
	/*
	if(!chkpal.checked) {
		alert('Vous devez pr\xE9alablement accepter les Conditions G\xE9n\xE9rales de Vente en cochant la case ci-dessous !');
		return false;
	}
	return true;
	*/
}



