$(document).ready(function() {
	if (jQuery.fn.dataTableExt) {
		jQuery.fn.dataTableExt.oSort['category-asc']  = function(x,y) {
			x = $(x).attr('title');
			y = $(y).attr('title');
		    return ((x < y) ? -1 : ((x > y) ?  1 : 0));
		};

		jQuery.fn.dataTableExt.oSort['category-desc'] = function(x,y) {
			x = $(x).attr('title');
			y = $(y).attr('title');
		    return ((x < y) ?  1 : ((x > y) ? -1 : 0));
		};

		$('#references #references_page').dataTable({
			"bPaginate": false,
			"bLengthChange": false,
			"bFilter": false,
			"bSort": true,
			"bInfo": false,
			"bAutoWidth": false,
			"aaSorting": [[0,"desc"]],
			"sScrollY": "550px",
			"aoColumns": [
				null,
				{ "sType": 'category' },
				{ "sType": 'html' },
				null,
				null,
				null
			]
		});
	}
});

// Function called on all pages
function pageClose() {	
	
	if ( $("body").attr("id") == "metiers"){
	    $('.subnav').show();
	}
		
	// nav with subsection
   
    $('.expandable').click(function(){
    	$('.subnav').slideToggle();
    	$('.expandable').toggleClass('active');
    	$('#contactform').slideUp(150);
    });
	// Contact Form is hidden if JS is on
	$('#contactform').hide();
	// Show/hide contactform and hide/show subnav (to limit total height of sidebar)
	$('#contactme').click(function(){
		$('#showmessage').slideDown(150);
		$('.subnav').slideUp(150);
		$('.expandable').removeClass('active');
		$('#contactform').slideToggle(300);
	});
	
	$("#video_home").click(function() {
			$.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'width'			: 400,
				'height'		: 300,
				'href'			: this.href.replace(new RegExp("([0-9])","i"),'moogaloop.swf?clip_id=$1'),
				'type'			: 'swf'
			});

			return false;
		});
	
	// jQuery placeholders for older browsers
	$('input,textarea').placeholder();
	// fancybox for all links with a ".fancy" class
	$('a.fancy').fancybox();
	// fancybox for videos (add a ".video" class to link)
	$('a.project').fancybox({
			'padding': 0,
			'width': 880,
			'height': 520,
			'type':'iframe',
			'titleShow':false
	});
	// fancybox for references (table)
	$('a.readmore').fancybox({
			'padding': 10,
			'width': 910,
			'height': 580,
			'type':'iframe',
			'titleShow':false,
			'scrolling':'auto'
	});
	// scroll to top
	$('a[href=#header]').click(function(){$('html,body').animate({scrollTop:0},'slow');return false;});
	
	// // Contact Form validation
	// $('#quick_hello').validate({
	// 		onkeyup: false,
	// 		rules: {			
	// 			name: {required:true,minlength:2},
	// 			email: {required:true,email:true},						
	// 			message: {required:true,maxlength:240}
	// 			},
	// 		messages: {				
	// 			name: '',
	// 			email: '',
	// 			message: '',
	// 			
	// 		},
	// 		errorLabelContainer: $('#errorContainer'),
	// 		
	// 		 invalidHandler: function(form, validator) {
	// 		      var errors = validator.numberOfInvalids();
	// 		
	// 		      if (errors) {
	// 		        var message = errors == 1
	// 		          ? 'Veuillez remplir le champ manquant'
	// 		          : ' Veuillez remplir les champs manquants';
	// 		        $("#errorContainer").html(message);
	// 		        $("#errorContainer").slideToggle();
	// 		      } else {
	// 		        $("#errorContainer").slideToggle();
	// 		      }
	// 		    },
	// 			submitHandler: function(form, validator) {			
	// 
	// 				var lname = $("#form_game").serialize();
	// 				$.ajax({
	// 					type: "POST",
	// 					url: '/main/sendForm',
	// 					data: lname,
	// 					success: function(result){
	// 						if(result == "ok"){
	// 							jQuery("#showmessage").html("<p>Votre requête ou message nous a bien été envoyé.</p>");
	// 							jQuery("#showmessage").slideToggle("slow");							
	// 							jQuery("#contactform").slideToggle("slow");							
	// 						}else{
	// 							jQuery("#showmessage").html("<p>Une erreur est survenue votre message n'a pas été envoyé</p>");
	// 							jQuery("#showmessage").slideToggle("slow");							
	// 							jQuery("#form_game").slideToggle("slow");
	// 						}
	// 
	// 					}
	// 				});
	// 			}
	// });
	
}
function popupClose() {
	
	// Project slideshow
	var toggle = $('#navpause').click(function() {
		var paused = slideshow.is(':paused');
		slideshow.cycle(paused ? 'resume':'pause',true);
	});
	function onAfter(curr,next,opts) {
        var caption = $(next).find('figcaption').html();
        $('#caption').html(caption);
    }
	var slideshow = $('#slideshow ul').cycle({
		timeout:4000,
		pause:false,
		fx:'fade',
		speed:500,
		timeout: 0,
		next:'#navnext',
		prev:'#navprev',
		// paused:function(cont,opts,byHover){!byHover && toggle.html('Resume');},
		resumed:function(cont,opts,byHover){!byHover && toggle.html('Pause');},
		after:onAfter
	});
	
	$('#slideshow ul li').show(); 
	
}
