/*
jquery.comercia.js - created by Mike Croteau 
Comercia web template
8 Dec 2009

Global Utility functions:
1. setupViewAdminLinks(); = creates the button at the bottom of the page that links to the login page
2. setupSubscribeValidation(); = sets up the subscribe area with validation and binds the click event to the subscribe button
3. setupContactValidation(); = sets up the contact form with validation and binds the click event to contact button
4. setupLoginValidation(); = sets up the login page with validation and binds the click event to login button


Action functions:
1. performLogin(); =  performs an ajax call to php/perform_login.php.  It is a makeshift attempt at creating a more secure way to view the pages comments and subscribers.  Login credentials are held in the php/perform_login.php.  This is a quick work around without creating a complete php application.  No database required

2. performSubscribe(); = performs an ajax call to php/subscribe.php.  It saves the data to the php/csv/subscribers.csv file.  It can be viewed once you have passed through the login.  I did it this way to make it super simple to setup.  No database is required.

3. performSubscribe(); = performs an ajax call to php/contact_form.php.  Data gets saved from the contact form into the php/csv/contacts.csv file. It can be viewed once you have passed through the login.  I did it this way to make it super simple to setup.  No database is required.


Page functions: Each page has its own javascript file located in the js/pages directory which sets up needed elements for validation, font replacement, ui stuff... etc for the individual page.
 
*/


/*
'replacefont' is a global variable 

Determines whether or not you would like to have the font replaced.  

To turn it off, set it to false.
*/
var replacefont = true;
var disableRightClick = false;



var setupSuperfishMenu = function(){
   $('ul.mainmenu').superfish({
	delay: 0,
	hoverClass: 'menuhover'
   });
}



//login validation setup
var setupLoginValidation = function(){

   $("a#login-button").click(function(){
      performLogin();
   });

   $("#loginForm").validate({
      errorLabelContainer: $("div#loginerror"),
	rules: {
	   username: { required: true },
	   password: { required: true}
	},
	messages:{
	   username: "Merci de saisir votre nom d'utilisateur",
	   password: "Merci de saisir votre mot de passe"		
	}
   });

}


//contact form validation setup
var setupContactValidation = function(){

   $("a#submit-contact").click(function(){
      performContactSubmit();
   });

   $("#commentForm").validate({
	errorLabelContainer: $("div#errors"),
	rules: {
	   prenom: { required: true },
	   name: { required: true },
   	   societe: { required: true },
   	   invite: { required: true },
	   email: { required: true, email: true  },
	   comment: { required: false }
	},
	messages:{
	   name: "Le champ NOM est requis",
	   prenom: "Le champ PRENOM est requis",
	   societe: "Le champ SOCIETE est requis",
	   invite: "Le champ INVITE PAR est requis.",
   	   email: "Merci de saisir une adresse email valide",
	   comment: "Merci de saisir un commentaire"
	}
   });

}


//subscribe validation setup
var setupSubscribeValidation = function(){

   $("a#subscribe-button").click(function(){
	performSubscribe();
   });


   $("<div id=\"subscribeError\"></div>").prependTo("div#footer");

   $("div#subscribeError").css({
	'position':'absolute',
	'top':'0px',
	'right':'90px',
	'display':'none'
   });

   $("#subscribeForm").validate({
      errorLabelContainer: $("div#subscribeError"),
	rules: {
	   email: { required: true, email: true  }
	},
	messages:{
	   email: "Please enter a valid email address"
	}
   });
}



//performs login
var performLogin = function(){ 

   if($("#loginForm").valid()){ 
      var username = $("input#username").val();  
	var password = $("input#password").val();  
      var dataString = 'username='+ username + '&password=' + password;  
      
      //alert(dataString);return false;  
	$.ajax({  
   	   type: "POST",  
   	   url: "../php/perform_login.php5",  
   	   data: dataString, 
	   success: function(html){
	      $("div#loginresult").html(html);
	   }
	}); 
	
      return false;
 
   }else{
      $(".errors").prependTo("div#loginerror");
   }

}



//performs subscribe submission
var performSubscribe = function(){ 
       
   if($("#subscribeForm").valid()){ 
	var email = $("input#subscribeEmail").val();  
      var dataString = '&email=' + email;  
      //alert(dataString);  return false;
      $.ajax({  
   	   type: "POST",  
   	   url: "../php/subscribe.php5",  
   	   data: dataString, 
	   success: function(html){
	      $("<div id=\"subscribemessagebox\"></div>").prependTo("div#subscribe-area-wrapper");
		$("div#subscribemessagebox").css({
		   'position':'absolute',
		   'top':'0px',
		   'display':'none',
		   'color':'#efefef'
		});
		
		$("div#subscribe-area").slideUp("", function(){
               $("div#subscribemessagebox").html(html);
		   $("div#subscribemessagebox").slideDown("slow");
		});
	   }

      }); 

      return false;

   }else{
       $("div.errors").prependTo("div#content");
   }

}



//performs contact submission
var performContactSubmit = function(){

   if($("#commentForm").valid()){
    var name = $("input#cname").val();  
    var prenom = $("input#cprenom").val();
    var societe = $("input#csociete").val(); 
    var invite = $("input#cinvite").val();
	var email = $("input#cemail").val();  
	var comment = $("textarea#ccomment").val();  
    var dataString = '&prenom='+ prenom + '&name='+ name + '&email=' + email + '&societe=' + societe + '&invite=' + invite + '&comment=' + comment;  
      //alert(dataString);return false;  

	$.ajax({  
   	   type: "POST",  
   	   url: "../php/contact_form.php5",  
   	   data: dataString, 
	   success: function(html){
	      $("<div id=\"messagebox\"></div>").prependTo("div#contactform-wrapper");
		$("div#messagebox").css({'display':'none'});
		$("div#contactform").slideUp("slow", function(){
     		   $("div#messagebox").html(html);
               Cufon.replace('#messagebox h1');
		   $("div#messagebox").slideDown("slow");				
		});
	   }

	}); 

      return false;


   }else{
      $("div.errors").prependTo("div#errors");
   }

}




//sets up the view contacts & subscribers link
var setupViewAdminLinks = function(){
	
   $("<a href=\"login.html\" id=\"view-contacts\" title=\"View Contacts & Subscribers\">Admin</a>").prependTo("div#footer-wrapper");

   $("#view-contacts").css({
	'font-size':'14px',	
      'height':'25px',
	'padding':'10px',
	'color':'#5e5e5e', 
	'background':'#3e3e3e', 
	'font-weight':'bold',
	'position':'absolute',
	'text-transform':'uppercase', 
	'bottom':'0px', 
	'left':'50%',  
	'z-index':'11',
	'border-top':'solid 2px #5e5e5e',
	'border-left':'solid 2px #5e5e5e',
	'border-right':'solid 2px #5e5e5e'
   });



   $("#view-contacts").hover(function(){
      $(this).animate({'height': 45}, 'fast');
	$(this).css({
	   'color':'#efefef',
    	   'border-top':'solid 2px #efefef',
    	   'border-left':'solid 2px #efefef',
    	   'border-right':'solid 2px #efefef'
	});
   }, function(event) {
      $(this).animate({'height': 25}, 'fast');
	$(this).css({
	   'color':'#5e5e5e',
    	   'border-top':'solid 2px #5e5e5e',
    	   'border-left':'solid 2px #5e5e5e',
    	   'border-right':'solid 2px #5e5e5e'
	});
   });

}


var setFancyBoxSettings = function(){

   $("#portfolio-slides a").fancybox({				
      'zoomSpeedIn' : 500,
	'zoomSpeedOut': 500
   });

}

