
var isIE8 = ($.browser.msie && parseInt($.browser.version.substr(0,1)) >= 8);
var isIE7 = ($.browser.msie && parseInt($.browser.version.substr(0,1)) == 7);
var isIE6 = ($.browser.msie && parseInt($.browser.version.substr(0,1)) < 7);
var isIE = (isIE8 || isIE7 || isIE6) ;

function log(thingToLog){
	if(window.console){
		console.log(thingToLog);
	}
}

$(document).ready(function(){
	// ajoute les smoothScroll automatiquement
	constructSmoothScrollLink();
	constructExternalLink();
	constructEmptyField();
	constructDefaultAction();
	constructFancyBox();
	showInfoBox();
	disableAutoComplete();

	ajaxLoadScript(function(){
		constructSmoothScrollLink();
		constructExternalLink();
		constructEmptyField();
		constructDefaultAction();
		disableAutoComplete();
		constructFancyBox();
	});

});

/**
 * Function constructSmoothScrollLink 
 * 
 * @description : Construit les liens à défilement progressif 
 * @access : public
 * @return : void
 * @date : 2011-01-26
 * @author : emmanuel <e.gauthier@hegyd.com>
 */
function constructSmoothScrollLink(){

	$('a.jsSmoothScroll[href^=#]').each(function(){
		var linkEl = $(this);
		var destination = linkEl.attr('href');
		if( destination != '#' && $(destination).length >= 1){
			linkEl.unbind('click').click(function(){
				smoothScroll($(destination).offset().top ,function(){
					window.location.hash = destination;
				},700);
				return false;
			});
		}
	});
}

/**
 * Function constructExternalLink 
 * 
 * @description : target=_new workaround  
 * @access : public
 * @return : void
 * @date : 2011-01-26
 * @author : emmanuel <e.gauthier@hegyd.com>
 */
function constructExternalLink(){
	$('a[rel="external"]').attr('target','_new');
}

/**
 * Function constructEmptyField 
 * 
 * @description : Field with predefined text will hide and show this text  
 * @access : public
 * @return : void
 * @date : 2011-01-26
 * @author : emmanuel <e.gauthier@hegyd.com>
 */
function constructEmptyField(){
	$('.jsEmpty').each(function(){
		var inputEl = $(this);
		var defaultText = inputEl.val(); 

		inputEl.attr('rel',defaultText);

		inputEl.focus(function(){
			if(inputEl.val() == defaultText){
				inputEl.val('');
			}
		}).blur(function(){
			if(inputEl.val() == ''){
				inputEl.val(defaultText);
			}
		});
		inputEl.removeClass('jsEmpty').addClass('jsEmptyDone');
	});
}


/**
 * Function disableAutoComplete 
 * 
 * @description : Disable browser suggest box  
 * @access : public
 * @return : void
 * @date : 2011-01-26
 * @author : emmanuel <e.gauthier@hegyd.com>
 */
function disableAutoComplete(){
	$('.jsAutoCompleteOff').attr('autocomplete','off');	
}

/**
 * Function smoothScroll 
 * 
 * @description : smooth scroll to an y destination 
 * @param :  destinationY $destinationY 
 * @param :  callbackFunction $callbackFunction 
 * @param :  timing $timing 
 * @access : public
 * @return : void
 * @date : 2011-01-26
 * @author : emmanuel <e.gauthier@hegyd.com>
 */
function smoothScroll(destinationY,callbackFunction,timing){
	
	if(timing == undefined || timing == '') timing = 550 ;

	var MaxScroll = window.innerHeight || document.documentElement.clientHeight;
	MaxScroll = $('body').height() - MaxScroll;
	if(MaxScroll > 1 && destinationY > MaxScroll) destinationY = MaxScroll ;
	
	if(destinationY >= 0){
		$('html,body').animate({
			scrollTop: destinationY
		},timing,'easeOutExpo',function(){
			if( $.isFunction(callbackFunction) ) callbackFunction() ;
		});
	}else{
		if( $.isFunction(callbackFunction) ) callbackFunction() ;
	}
	return false;
}

/**
 * Function constructDefaultAction 
 * 
 * @description : if you want an element to have a default action on dblclicking 
 * @access : public
 * @return : void
 * @date : 2011-01-26
 * @author : emmanuel <e.gauthier@hegyd.com>
 */
function constructDefaultAction(){
	$('.jsDefaultAction').each(function(){
		var blockEl = $(this);
		var defaultLink = blockEl.find('jsDefaultLink');
		if(defaultLink.length != 1){
			defaultLink = blockEl.find('a:first');
		}

		if(defaultLink.length == 1){
			blockEl.dblclick(function(e){
				window.location = defaultLink.attr('href');
			}).hover(function(){
				blockEl.css('cursor','pointer');
			},function(){
				blockEl.css('cursor','');
			});
		}
	});
}


/**
 * Function showInfoBox 
 * 
 * @description : Show information box on top of the design  
 * @param :  text $text 
 * @param :  b $b 
 * @param :  s $s 
 * @param :  a $a 
 * @param :  h $h 
 * @access : public
 * @return : void
 * @date : 2011-01-26
 * @author : emmanuel <e.gauthier@hegyd.com>
 */

var beforeShowDuration = 500;
var showDuration = 500;
var aliveDuration = 4000;
var hideDuration = 500;

var infoBoxTimeout = null;

function showInfoBox(text,b,s,a,h){

	if(b == 0 || b == undefined ) b=beforeShowDuration;
	if(s == 0 || s == undefined ) s=showDuration;
	if(a == 0 || a == undefined ) a=aliveDuration;
	if(h == 0 || h == undefined ) h=hideDuration;
	
	var infoBox = $('#infoBox');
	log(infoBox);
	if(infoBox.length){
		if(infoBoxTimeout) window.clearTimeout(infoBoxTimeout);
		
		infoBox.css({ display: 'none'}).stop();

		if(text || (text = infoBox.find('.callbackText').html()) ){
			infoBox.find('.message').html(text);
			var infoBoxHeight = infoBox.height();
			infoBox.css({
					'top': -(infoBoxHeight)
			});

			infoBoxTimeout = window.setTimeout(function(){
				infoBox.css({display:'block'});
				infoBox.animate({
					top: 0 
				},s,function(){					
					infoBoxTimeout = window.setTimeout(function(){
						infoBox.animate({top: -(infoBoxHeight+10)}, h);
					},a);
				});
			},b);

		}

	}
}


/**
 * Function constructFancyBox 
 * 
 * @description : Construit les fancybox de la page 
 * @access : public
 * @return : void
 * @date : 2011-01-27
 * @author : emmanuel <e.gauthier@hegyd.com>
 */

var fancyBoxGalleryCount = 1;

function constructFancyBox(){

	$('a.jsFancyBox').each(function(){ $(this).fancybox() });
	
	$('.jsFancyBoxGallery').each(function(){
		var galleryEl = $(this);
		galleryEl.find('a.jsFancyBoxGalleryLink').attr('rel','FancyBoxGallery-'+ fancyBoxGalleryCount++ ).fancybox() ;
	});
}






