/*
 * Copyright 2010 by Robert Viens<viens@pubnix.net>
 *
 * This program is Private for now!
 *
 * Versions: 0.1    2010-04-23  Initial release
 *                 
 */

function centrer () {
  var winH = $(window).height();  
  var winW = $(window).width();  
  var topPos =  parseInt((winH/2)-$('#contenant').height()/2);
  var LeftPos =  parseInt((winW/2)-$('#contenant').width()/2);
  $('#contenant').css({
    'position': 'absolute',
	'top': (topPos < 0 ? 0:topPos),
	'left':(LeftPos < 0 ? 0:LeftPos)
  });  
}
 
function initFormulaire() {
   $(window).resize(function() {  
 		centrer();
		 if(TO !== false)
    		clearTimeout(TO);
 		 TO = setTimeout('centrer()', 1200);
 	});

	$("#formulaire").validate({
		rules: {
		 nom: "required",
		 sujet: "required",
		 message: "required",
		 courriel: {
		   required: true,
		   email: true
		 }
	   },
	   messages: {
		 nom: 'Votre nom  s.v.p.',
		 sujet: 'Il serait bien d’indiquer pourquoi vous voulez me contacter',
		 message: 'Laissez-moi un cours message s.v.p.',
		 courriel: {
		   required: 'Nous avons besoins d’une adresse courriel pour vous contacter.',
		   email: 'Votre adresse de courriel doit avoir ce format : nom@domaine.com.'
		 }
	   }
	});

	$('#fermerFormulaire').click(function () {
		fermerFormulaire();
		});

	$('.contact').click(function () {
		OuvreFormulaire();
	});
	
	$('#envoyer').mouseover(function () {
		$('#envoyer').find('img').attr({"src" :"images/btnEnvoyer_f2.gif"});
	}).mouseout(function () {
		$('#envoyer').find('img').attr({"src" :"images/btnEnvoyer.gif"});
	});
	
	$('#envoyer').click(function () {
		if($('#formulaire').valid()) {
			$('#formulaire').submit();
		}
	});
	
	$("#loading")
		.ajaxStart(function(){
			$(this).show();
		})
		.ajaxComplete(function(){
			$(this).hide();
		});


$('#formulaire').submit(function () {
	var dataString = $('#formulaire').serialize();
	$.ajax({
	   type: "POST",
	   url: "contactinstant.php",
	   data: dataString,
	   success: function(msg){
		 $('#contactForm').animate({opacity: 'hide'}, 500);
		 $('#retourMessage').delay(500).css({'padding': '50px 0 35px 0'}).html(msg); 
		 $('#retourMessage').addClass('active');
	     }
	 });
 	  return false;
 	});
}

function OuvreFormulaire()
{
//Get the screen height and width  
        var maskHeight = $(document).height();  
        var maskWidth = $(window).width();  
      
//        $('#centrage').addClass('estomper');
//		$('#centrage').animate({opacity : 0.5},300); 
 //		$('#contenant').animate({opacity : 0.5},300); 

 //Set height and width to mask to fill up the whole screen  
        $('#mask').css({'width':maskWidth,'height':maskHeight});  
          
        //transition effect       
        $('#mask').fadeIn(1000);      
        $('#mask').fadeTo("slow",0.8);
        var winH = $(window).height();  
        var winW = $(window).width();  
                
        $('#sPopup').css('top',  winH/2-$('#sPopup').height()/2);  
        $('#sPopup').css('left', winW/2-$('#sPopup').width()/2);  
      
      $('#sPopup').fadeIn(500);       
	 // 	$('#sPopup').show(); 
	
 }
 
function fermerFormulaire() {
//	$('#centrage').removeClass('estomper');
//	$('#centrage').animate({opacity : 1.0},300); 
// 		$('#contenant').animate({opacity :1.0},300); 
		$('#contactForm').removeClass("ouvert");
		$('#contactForm').addClass("ferme");
		$('#contactForm').delay(600).animate({opacity: 'show'}, 300);
	if($('#retourMessage').is('.active')) { 
		$('#retourMessage').removeClass('active').animate({opacity: 'hide'}, 500);
		}
	$('#sPopup').animate({opacity : 'hide'},300); 
	$('#mask').hide();  
}
var acloseBox = '<div style="position:absolute;top: 4px;right: 4px;"><a href="#" id="closeBoxInvi"><span style="position:relative;top:-3px;font-size: 0.8em">Fermer/Close</span>&nbsp;<img src="css/img/close.png" alt="Close - fermer" width="18" height="18" border="0" class="pngToFix" /></a></div>';

function showInvitation(invitationurl) {
    var winW = $(window).width(); 
    
	var leftDepl = parseInt((winW/2)-(860/2) );
	var topDepl = parseInt($('#sPopup').css('top')) + 62;
		$('#debug').html(topDepl);

	$('#previewInvitation').css({'position':'absolute','top':'60px','left':leftDepl,'width':'860px','height':'550px','padding':'24px 12px 12px 12px', 'background':'#fff ','border':'#900 2px solid','text-align':'left','z-index': 10000 });  
	$('#previewInvitation').html(acloseBox);
	$("#previewInvitation")
		.ajaxStart(function(){
			$(this).show().removeClass('ferme');
		})
		.ajaxComplete(function(){
			var referant = $('#inscrAmiFormulaire [name=nomRef]').val();
//$('#referantFr,#referantEn').each().html("test");
			if(referant.length == 0) {
				referant = $('#previewInvitation #referantFr').html();
			}
			$('span#referantFr').html(referant);
			$('span#referantEn').html(referant);
			var message = $('#inscrAmiFormulaire [name=messageami]').val();
			$('#previewInvitation #commentsfromfriend').html(message);
			
			var currentTimeAndDate = new Date();
			$('#previewInvitation span#dateheure').html(currentTimeAndDate.toLocaleDateString() + '&nbsp;'+currentTimeAndDate.toLocaleTimeString() );
			$('#previewInvitation').unbind('ajaxStart');
			$('#previewInvitation').unbind('ajaxComplete');
	});
	
	$.ajax({
		   type: "get",
		   url: invitationurl,
		   data: "",
		   success: function(msg){
			 $('#previewInvitation').append(msg);
			}
		 });
	$('#closeBoxInvi').hover(function () {
		$(this).find('img').attr({'src':'css/img/closehover.png'});
	}, function () {
		$(this).find('img').attr({'src':'css/img/close.png'});
	});
		 
	$('#closeBoxInvi').click(function () {
		hideInvitation();
	});
}

function hideInvitation() {
	$('#previewInvitation').fadeOut('fast').addClass('ferme');
}

