var hoverColour = "#ff6600";
$(function(){
	$("a.hoverBtn").show("fast", function() {
		$(this).wrap("<div class=\"hoverBtn\">");
		$(this).attr("class", "");
	});
	
	//display the hover div
	$("div.hoverBtn").show("fast", function() {
		//append the background div
		$(this).append("<div></div>");
	
		//on link hover
		$(this).children("a").hover(function(){
			//store initial link colour
			if ($(this).attr("rel") == "") {
				$(this).attr("rel", $(this).css("color"));
			}
			//fade in the background
			$(this).parent().children("div")
				.stop()
				.css({"display": "none", "opacity": "1"})
				.fadeIn("fast");
			//fade the colour
			$(this)	.stop()
				.css({"color": $(this).attr("rel")})
				.animate({"color": hoverColour}, 350);
		},function(){
			//fade out the background
			$(this).parent().children("div")
				.stop()
				.fadeOut("slow");
			//fade the colour
			$(this)	.stop()
				.animate({"color": $(this).attr("rel")}, 250);
		});
	});
});
//preload images
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)
//adherer button
function MM_effectAppearFade(targetElement, duration, from, to, toggle)
{
	Spry.Effect.DoFade(targetElement, {duration: duration, from: from, to: to, toggle: toggle});
}
//fancy box + slide
$(document).ready(function() {
						   
	$("#hiddenclicker").fancybox({
		'titleShow'		: false,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'width'			: 500,
		'height'		: 402,
		'hideOnContentClick': false
	});
	$("a.fancy").fancybox({
		'titleShow'		: false,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'width'			: 500,
		'height'		: 402,
		'hideOnContentClick': false
	});
	$("a.fancysmall").fancybox({
		'titleShow'		: false,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'width'			: 380,
		'height'		: 380,
		'hideOnContentClick': false
	});
	
	
	$('#homeslide').cycle({ 
		fx:    'fade',
		next: '#nextButton',
		prev: '#prevButton',
		pause: true
	});
	//form handling
    var formoptions = { 
        beforeSubmit:  presubmit,  // pre-submit callback 
        success:       postsubmit,  
        dataType:  'json',         
    }; 
    $('#contact-form').ajaxForm(formoptions);
	$('#newsletter-form').ajaxForm(formoptions);
    var playeroptions = { 
        beforeSubmit:  presubmit,  // pre-submit callback 
        success:       playersubmit,  
        dataType:  'json',       
    }; 
	if($('#player-form').length > 0) loadplayer();
    $('#player-form').ajaxForm(playeroptions);
	
	//preload menu images
	jQuery.preLoadImages("/images/menu_accueil_on.png", "/images/menu_contact_on.png","/images/menu_happ_on.png","/images/menu_mobiles_on.png","/images/menu_offre_on.png","/images/menu_promos_on.png","/images/loading.gif");
	//userbox
	$('#login').focus(function(){
		$(this).val('');
	});
	$('#login').blur(function(){
		if(!$(this).val()) $(this).val('numéro play');
	});
	$('#password').focus(function(){
		$(this).val('');
	});
	$('#password').blur(function(){
		if(!$(this).val()) $(this).val('mot de passe');
	});
	//first login
	if($('#firstlogin').length > 0) $('#firstlogin').trigger('click');
	
	//date picker
	$( "input.date" ).datepicker({ dateFormat: 'yy-mm-dd', maxDate: '-5y', minDate: '-26y', changeYear: true, buttonImage: '/images/datepicker.gif', yearRange: '-26' });
	//auto complete
	$( "#campus" ).autocomplete({
			source: "/interact/campus.php",
			minLength: 2
	});

});
function presubmit(formData, jqForm, formoptions) {
	//show loading
	$('#formloading').show();
	//hide message
	$('#formmessage').hide();
	return true;
} 
function postsubmit(responseText, statusText, xhr, $form)  {
	//hide loading
	$('#formloading').hide();
	//show message
	$('#formmessage').html(responseText.message);
	$('#formmessage').show();
	//hide form
	if(responseText.success) $form.fadeOut();
	return true;
}
function playersubmit(responseText, statusText, xhr, $form)  {
	//hide loading
	$('#formloading').hide();
	//show message
	$('#formmessage').html(responseText.post.message);
	$('#formmessage').show();
	//hide form
	if(responseText.post.success) $form.fadeOut();
	return true;
}
function loadplayer(){
	$('#formloading').show();
	$('#formmessage').hide();
	$('#player-form').hide();
	$.get('/interact/player.php', function(data) {
		$('#formloading').hide();
		if(!data.user){
			$('#player-form').hide();
			$('#formmessage').html('inscris toi au  site play !');
			$('#formmessage').show();
			$('#login-form').show();
			return
		}
		else{
			//write number
			$('#uid').html(data.user);
			$('#prenom').val(data.first);
			$('#nom').val(data.last);
			$('#cin').val(data.cin);
			$('#campus').val(data.campus);
			$('#naissance').val(data.birth);
			$('#email').val(data.email);
			//check box
			if(data.newsletter) $('input[name=newsletter]').attr('checked', true);
			$('#player-form').show();
		}
	}, 'json');	
}
function callBoxFancy(my_href) {
	jl = document.getElementById('hiddenclicker');
	jl.href = my_href;
	$('#hiddenclicker').trigger('click');
}

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-19170466-2']);
_gaq.push(['_trackPageview']);

(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

