$(document).ready(function(){
	$('.google_plusone').each(function(){
		gapi.plusone.render($(this).attr('id'), {'size': 'medium', 'count': 'false'});
	});
	
	// Open external link in a new window
	$('a[rel="external"]').live('click', function(clickEvent){
		clickEvent.preventDefault();
		
		try {
			var pageTracker = _gat._getTrackerByName();			
			pageTracker._trackEvent('External Links', this.href);
		} catch(err){}
		
		window.open(this.href);
	});
	
	// Decode base64 "mailto"
	$('a[rel="mailto"]').each(function(){
		var _base64 = $(this).text();
		$(this).attr('href', 'mailto:' + $(this).text($.base64Decode(_base64)).text());
	});
	
	$('a[rel="email"]').each(function(){
		$(this).click(function(clickEvent){
			this.href = this.href.replace(/\[a\]/i, '@');
		});
	});
	
	$('form').each(function() {
		// Fix IE <button> bug
		$(this).ieFixButtons(); 

		// Display sample in forms
		className = 'hasExample';
		
		$(this).find('input[type="text"][title],textarea[title]').each(function(){
			$(this).focus(function(eventFocus){
				if($(this).hasClass(className)) {					
					$(this).val('');
					$(this).removeClass(className);
				}
			});
				
			$(this).blur(function(eventBlur){				
				if($(this).val() == '' || $(this).val() == $(this).attr('title')) {
					$(this).addClass(className);
					$(this).val($(this).attr('title'));
				}
			});
			
			$(this).change(function(eventChange){
				if ($(this).val() != '' && $(this).val() != $(this).attr('title')) {
					$(this).removeClass(className);
				}
			});
					
			if($(this).val() == '' || $(this).val() == $(this).attr('title')) {
				if(!$(this).hasClass(className)) {
					$(this).addClass(className);
					$(this).val($(this).attr('title'));
				}
			}
		});

		// "a" html entity to trigger form submission
		$(this).find('div.buttons a:not([rel="external"])').click(function(e){		
			e.preventDefault();
						
			// Set the 'rel' of the link to the hidden 'input[action]'
			$(this).parents('form').first().find('input[name="action"]').attr('value', $(this).attr('rel'));
			
			// Submit the parent form
			$(this).parents('form').trigger('submit');
		});
	
		$(this).submit(function(e){	
			// Remove any sample values for the form
			$('.' + className, this).val('');
	
			$.colorbox({
				close: '',
				html:'<div style="background:transparent url(/includes/themes/default/global/images/loading.gif) scroll no-repeat left top; padding-left:40px; line-height:33px; height:33px;">Traitement en cours ...</div>',
				overlayClose:false,
				width:400
			});
		});
	});

	// Animations
	if ($.support.opacity) {
		$('body#index #header .link-home').fadeTo(2000, 1.0);
	}
});

/*
http://www.alohatechsupport.net/webdesignmaui/maui-web-site-design/easy_jquery_auto_image_rotator.html
*/
function rotate(rotator) {	
	//Get the first image
	var current = ($(rotator).find('ul li.show') ? $(rotator).find('ul li.show') : $(rotator).find('ul li:first'));

  if (current.length == 0) current = $(rotator).find('ul li:first');

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $(rotator).find('ul li:first') : current.next()) : $(rotator).find('ul li:first'));
	
	//Un-comment the 3 lines below to get the images in random order	
	//var sibs = current.siblings();
  //var rndNum = Math.floor(Math.random() * sibs.length );
  //var next = $(sibs[rndNum]);
			
	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000).removeClass('show');
};
