// JavaScript Document
var g_defaultNewsLabel;
$(document).ready(function() {
	g_defaultNewsLabel = "Votre adresse email";	
	/*setFooterToPos();
	$(window).resize(function(e) {
		setFooterToPos();
	});*/
	
	/* ! marges droites */
	$('#homemaincontent .onethird:nth-child(3)').css('margin-right',0);
	$('#homemaincontent .onethird .entry:last-child').css('background','none');
	$('#gallery-wrapper .gallery-img:nth-child(3n)').css('margin-right',0);
	
	
	/* !newsletter */
	$('#email').val(g_defaultNewsLabel);
	$('#email').focus(function(e) {
		if ($(this).val()==g_defaultNewsLabel) $(this).val("").css('color','#000');
	});
	$('#email').blur(function(e) {
		if ($(this).val()=="") $(this).val(g_defaultNewsLabel).css('color','#aaa');
	});
	$('#frm_newsSubscribe').click(function(e){
		m = $('#email').val();
		if (m.indexOf("@")==-1 || m.indexOf(".")==-1) alert("Veuillez entrer votre adresse email");
		else {
			$.post( "/_services/news.php", {todo:"subscribe", e : m}, function(data){
				if (data) {
					$('#newsMsg').html(data).fadeIn();
					setTimeout(hideMsg, 1000);
				}
			});	
		}
		return false;		
	});
	$('#frm_newsUnsubscribe').click(function(e){
		m = $('#email').val();
		if (m.indexOf("@")==-1 || m.indexOf(".")==-1) alert("Veuillez entrer votre adresse email");
		else {
			$.post( "/_services/news.php", {todo:"unsubscribe", e : m}, function(data){
				if (data) {
					$('#newsMsg').html(data).fadeIn();
					setTimeout(hideMsg, 1000);
				}
			});	
		}
		return false;		
	});
	/* !drop down archives expos */
	$('#archives').change(function() {
		$('#archivesexpo').submit();
	});
	/* !order form (books) */
	if ($('#orderbook').length) {
		$('#submitformbook').click(function(e) {
			var arrBooksSelected = checkBookSelected();
			if (arrBooksSelected.length==0) {
				alert("Vous devez au-moins sélectionner un livre");
				return false;
			} else if (checkBookForm()) {
				var arrCoord = new Array();
				arrCoord[0] = $('#lastname').val();
				arrCoord[1] = $('#firstname').val();
				arrCoord[2] = $('#address1').val();
				arrCoord[3] = $('#address2').val();
				arrCoord[4] = $('#zip').val();
				arrCoord[5] = $('#city').val();
				arrCoord[6] = $('#country').val();
				arrCoord[7] = $('#orderemail').val();
				arrCoord[8] = $('#message').val();
				
				
				$.post("/_services/order.php", {'todo':"order", 'books' : arrBooksSelected, 'coord' : arrCoord}, function (data) {
					//console.log(data);
					if (data=="ok") {
						$('#orderbook').animate({
							height: 'toggle'
						}, 500, function() {
							$('#msgsuccess').fadeIn();
						});
					}
					
					/*$.each(data, function(key, val) {
						
					});	*/				
				});
			}
			return false;
		});
	}	
	
	setTimeout(resizeColumn, 50);	
});
function checkBookForm() {
	
	if (!$('#lastname').val()
	|| !$('#firstname').val()
	|| !$('#address1').val()
	|| !$('#zip').val()
	|| !$('#city').val()
	|| !$('#country').val()
	|| !$('#orderemail').val()
	
	) {
		alert("Veuillez remplir tous les champs obligatoires");
		return false;
	} 
	return true;
}
function checkBookSelected() {
	var arrBooksSelected=new Array();
	$('.orderthisbook').each(function(e) {
		if ($(this).attr("checked")=="checked") arrBooksSelected.push($(this).attr('id'));
	});
	return arrBooksSelected;
}
function resizeFooter() {
	var vpHeight = $(window).height();
	var zoneHeight = $('#zone3').innerHeight();
	if (zoneHeight+80 < vpHeight) {
		var footerHeight = vpHeight - zoneHeight;
		$('#footer-wrapper').height(footerHeight);
	}
}
function resizeColumn () {
	var leftH = $('#left-wrapper').innerHeight();
	var rightH = $('#right-wrapper').innerHeight();
	if (leftH>rightH) {
		
		var cssObj = {
		  'margin-bottom' : '0',
		  'padding-bottom' : '0'
		}
		//console.log(leftH);
		$('#right-wrapper').css(cssObj);
		$('#right-wrapper').height(leftH-17);
		//console.log($('#right-wrapper').innerHeight()+"H"+leftH);
	}
}
function hideMsg() {
	$('#newsMsg').fadeOut();
}
function setFooterToPos() {
	var dHeight = $('#wrapper').innerHeight();
	var wHeight = $(window).height();
	//console.log(dHeight);
	var y = dHeight - $('#footer-wrapper').height();
	//if (y>$('#wrapper').outerHeight()) {
		var cssObj = {
		 'position' : 'absolute',
		 'top' : y
		}
		$('#footer-wrapper').css(cssObj);
		//console.log("h");
	//}
}
