/* ================================================================================ */
/* = fonctions = */
/* ================================================================================ */
var obj = null;
var lessVisible = 1;
var defaultVisible = 2;
var moreVisible = 4;
var evenMoreVisible = 6;
var itemsVisible = null;

function checkHover() {
	if (obj) {
		obj.find('ul').fadeOut('fast');	
	} //if
} //checkHover


function initMenus() {
	$('#page ul.menu ul').hide();
	$.each($('#page ul.menu'), function(){
		$('#' + this.id + '.expandfirst ul:first').show();
	});
	$('#page ul.menu li h2').mouseover(
		function() {
			var checkElement = $(this).next();
			var parent = this.parentNode.parentNode.id;

			if($('#' + parent).hasClass('noaccordion')) {
				$(this).next().slideToggle('normal');
				return false;
			}
			if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
				if($('#' + parent).hasClass('collapsible')) {
					$('#' + parent + ' ul:visible').slideUp('normal');
				}
				return false;
			}
			if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
				$('#' + parent + ' ul:visible').slideUp('normal');
				checkElement.slideDown('normal');
				return false;
			}
		}
	);
}

function calculateItemsToShow(item) {
	itemsVisible = defaultVisible;
	
	if( $(item).hasClass('quatre') )
		itemsVisible = moreVisible;
	else if( $(item).hasClass('six') )
		itemsVisible = evenMoreVisible;
	else if( $(item).hasClass('one'))
		itemsVisible = lessVisible;
}

function validate_email(field,alerttxt)
{
	apos = field.indexOf("@");
	dotpos = field.lastIndexOf(".");
	if (apos<1||dotpos-apos<2) {
	  alert(alerttxt);
	  return false;
	}
	else {
		return true;
	}
}

function envoi_mail() {
	
	if( validate_email( $("#input_newsletter").val(), "Le courriel n'est pas valide" ) ) {
	
		var dateVar = new Date();
		var dateFR = dateVar.getDate() + '/' + dateVar.getMonth()+1 + '/' + dateVar.getFullYear() + ' ' + dateVar.getHours() + ':' + dateVar.getMinutes() + ':' + dateVar.getSeconds();
		var mail_value = $("#input_newsletter").val();
		$.ajax({
			type: 'GET',
			url: 'http://mediacd.richmailer.com/commun/pages/dialog_WEB/save_abonneUTF.asp?ID_lib=95&NB=1&email='+ mail_value+'&C1='+dateFR,				
			dataType: 'json',
			jsonp:'jsonp_callback',
			success: function() {
				//alert('http://mediacd.richmailer.com/commun/pages/dialog_WEB/save_abonneUTF.asp?ID_lib=95&NB=1&email='+ mail_value+'&C1='+dateFR);
			}
		});
		
		/*$.get('http://mediacd.richmailer.com/commun/pages/dialog_WEB/save_abonneUTF.asp?ID_lib=95&NB=1&email='+ mail_value+'&C1='+dataVar,				
			function(){
				alert("GET OK");
			}, "html"
			);*/
		$("#newsletter_form").hide();
		$('<div class="message_newsletter">Votre inscription a &eacute;t&eacute; envoy&eacute;e</div>')
		.insertAfter( $("#newsletter_form") )
		.fadeIn('slow')
		.animate({opacity: 1.0}, 5000)
		.stop(function() {
			$(".message_newsletter").remove();
		});
		
	}
}


	$(document).ready(function() {
		// pour coins arrondis
		$('#defilement .rounded').prepend('<div class="tl"></div><div class="tr"></div><div class="bl"></div><div class="br"></div>');
		
		//hover du menu sur sous-pages
		//initMenus();
		
		$('.niv2').hide();
		
		//hover du menu sur accueil
		$('#home #menu li').hover(function() {
			if (obj) {
				obj.find('ul').fadeOut('fast');
				obj = null;
			} //if
			$(this).find('ul').fadeIn('fast');
			$(this).addClass('current');
		}, function() {
			obj = $(this);
			if($(this).hasClass('current'))
				$(this).removeClass('current');
			setTimeout(
			"checkHover()",
			600);
		});
		
		//affiche seulement ce parti du menu 'current'
		$('#page #menu li.current').find('ul').show();
		$('#reference #menu li.current').find('ul').show();
		
		
		//references
		$('#naviCaptures a').click(function(event){
			event.preventDefault();
			var img = $(this).find('div.toto');
			$.get($(img[0]).html(), function(data) {
				$('#ref_img div').html(data);
			});
		});
		
		
		/*
		 * PLUS ET MOINS LISTS 
		 */
		
		//show hidden toolblocks
		$('.tool_block').show();
		//hide all elements in the plusetmoins divs
		$('.plusetmoins div').not('.img_container').hide();
		$('.moins').hide();
		
		//show two elements in each plusetmoins div
		parents = $('.plusetmoins');
		for(i = 0; i < parents.length; i++) {
			
			calculateItemsToShow($(parents[i]));
			
			children = $(parents[i]).children('div').not('.clear');
			
			for(j=0; j < itemsVisible; j++) {
				if(children[j] != undefined){
					$(children[j]).show();
					$(children[j]).children().show();
				}
			}
			
			if(children.length <= itemsVisible) {
				//hide plus
				grandpere = $(parents[i]).parent();
				plus = $(grandpere[0]).find('.plus');
				$(plus[0]).hide();
			}
			
			grandpere = 'joske';
		}
		
		//show one element more in the right plusetmoins div if any
		$('a.plus').click( function(event) {
			event.preventDefault();
			aPlusChildren = $(this).parents();
			pEtMoins = $(aPlusChildren[2]).children('.plusetmoins');
			
			calculateItemsToShow($(pEtMoins[0]));
			
			pEMHidden = $(pEtMoins[0]).children('div:hidden').not('.clear');
			
			if(pEMHidden.length > 0) {
				$(pEMHidden[0]).show();
				$(pEMHidden[0]).children().show();
			}
			
			pEMVisible = $(pEtMoins[0]).children('div:visible').not('.clear');
			if(pEMVisible.length >= itemsVisible) {
				papa = $(this).parents();
				moins = $(papa[1]).find('.moins');
				if(pEMVisible.length > itemsVisible)
					$(moins).show();
			}
			
			if(pEMVisible.not('.clear').length == $(pEtMoins[0]).children('div').not('.clear').length)
				$(this).hide();
			
		});
		
		
		//show one element less in the right plusetmoins div if any
		$('a.moins').click( function(event) {
			event.preventDefault();
			aPlusChildren = $(this).parents();
			pEtMoins = $(aPlusChildren[2]).children('.plusetmoins');
			
			calculateItemsToShow($(pEtMoins[0]));
			
			//chercher plus
			papa = $(this).parents();
			plus = $(papa[1]).find('.plus');
			
			if($(pEtMoins[0]).children('div:visible').length > itemsVisible) {
				pEMLast = $(pEtMoins[0]).children('div:visible:last');
				$(pEMLast[0]).hide();
				$(plus).show();
			}
			
			pEMVisible = $(pEtMoins[0]).children('div:visible');
			if(pEMVisible.length == itemsVisible) {
				$(this).hide();
			}
			
			if(pEMVisible.length == $(pEtMoins[0]).children('div').length) {
				$(plus).hide();
			}
			
		});
		
		
		// Tableau une ligne sur deux
	    $('table tr:even', this).addClass('pair');
	
	
		// RICHMAILER :	
		$("#btn_newsletter").click( function(event)  {
			
			envoi_mail();
			
		});
		$("#input_newsletter").keyup( function(event)  {
			
			if(event.keyCode == 13) {
				envoi_mail();
				event.preventDefault();
			}
			
		});

	});
		