(function ($) {
	$.preLoadImages = function(imageList,callback) {
		var pic = [], i, total, loaded = 0;
		if (typeof imageList != 'undefined') {
			if ($.isArray(imageList)) {
				total = imageList.length; // used later
					for (i=0; i < total; i++) {
						pic[i] = new Image();
						pic[i].onload = function() {
							loaded++; // should never hit a race condition due to JS's non-threaded nature
							if (loaded == total) {
								if ($.isFunction(callback)) {
									callback();
								}
							}
						};
						pic[i].src = imageList[i];
					}
			}
			else {
				pic[0] = new Image();
				pic[0].onload = function() {
					if ($.isFunction(callback)) {
						callback();
					}
				};
				pic[0].src = imageList;
			}
		}
		pic = undefined;
	};

	$.preLoadCSSImages = function(callback) {
		var pic = [], i, imageList = [], loaded = 0, total, regex = new RegExp("url\((.*)\)",'i'),spl;
		var cssSheets = document.styleSheets, path,myRules,Rule,match,txt,img,sheetIdx,ruleIdx;
		for (sheetIdx=0;sheetIdx < cssSheets.length;sheetIdx++){
			var sheet = cssSheets[sheetIdx];
			if (typeof sheet.href == 'string' && sheet.href.length > 0) {
				spl = sheet.href.split('index.html');spl.pop();path = spl.join('index.html')+'/';
			}
			else {
				path = 'index.html';
			}
			myRules = sheet.cssRules ? sheet.cssRules : sheet.rules;
			for (ruleIdx=0;ruleIdx < myRules.length;ruleIdx++) {
				Rule = myRules[ruleIdx];
				txt = Rule.cssText ? Rule.cssText : Rule.style.cssText;
				match = regex.exec(txt);
				if (match != null) {
					img = match[1].substring(1,match[1].indexOf(')',1));
					if (img.substring(0,4) == 'http') {
						imageList[imageList.length] = img;
					}
					else if ( match[1].substring(1,2) == 'index.html') {
						var p2 = path.split('index.html');p2.pop();p2.pop();p2x = p2.join("index.html");
						imageList[imageList.length] = p2x+img;
					}
					else {
						imageList[imageList.length] = path+img;
					}
				}
			};
		};

		total = imageList.length; // used later
		for (i=0; i < total; i++) {
			pic[i] = new Image();
			pic[i].onload = function() {
				loaded++; // should never hit a race condition due to JS's non-threaded nature
				if (loaded == total) {
					if ($.isFunction(callback)) {
						callback();
					}
				}
			};
			pic[i].src = imageList[i];
		}

	};
	$.preLoadAllImages = function(imageList,callback) {
		if (typeof imageList != 'undefined') {
			if ($.isFunction(imageList)) {
				callback = imageList;
			}
			else if (!$.isArray(imageList)) {
				imageList = [imageList];
			}
		}
		$.preLoadCSSImages(function(){
			if (imageList.length > 0) {
				$.preLoadImages(imageList,function(){
					callback();
				});
			}
			else {
				callback();
			}
		});
	};
})(jQuery);

jQuery(function($){
	if ($('#page-bg').length) {
		$.ezBgResize({
		    img : $('#page-bg').attr('src'), 
		    width : 960, 
		    height : 678,  
		    center : true
		}); 	
	}
	
	$('.ginput_container input, .contact-info .ginput_container textarea').each(function() {
		$(this).attr('title', $(this).val());
	});

	$('.blink, .contact-info .ginput_container input, .contact-info .ginput_container textarea')
		.focus(function(){
			if( $(this).val() == $(this).attr('title') ) {
				$(this).val('');
			}
		})
		.blur(function(){
			if( $(this).val() == '' ) {
				$(this).val( $(this).attr('title') );
			}
		});
		
	$('.btn-plus').colorbox({
		inline:true
	});
	
	/* job */
	
	$(".detailJob").click(function() {
		var txt = $(this).next(".jobContent").is(':visible') ? 'Afficher les détails »' : 'Masquer les détails »';
		$(this).text(txt);
		$(this).next('.jobContent').slideToggle();
		$(this).next('.jobContent').show();
		return false;
	});
	
	/*slider*/
	var total = 0;
	$('.slider li').each(function (index) {
		total += $(this).outerWidth(true);
	});
	$('.slider ul').width(total);
	
	$('.slider').jScrollPane();
	
	if ($('.slider').length > 0) {
		var api = $('.slider').data('jsp');
		var throttleTimeout;
		$(window).bind('resize', function() {
				
			if ($.browser.msie) {
				if (!throttleTimeout) {
					throttleTimeout = setTimeout(function() {
							api.reinitialise();
							throttleTimeout = null;
						}, 50);
					}
			} else {
				api.reinitialise();
			}
		});
	}
	
	//hiding home page from navigation when on home page
	$('.page-template-page-home-php #navigation > .current-menu-item').hide();
	
	$('.f-nav li span:last').remove();
	
	//if the main navigation item has a submenu, going on the first subpage upon click on it
	$('#navigation > li > a').click(function() {
		var ul = $(this).parent().find('ul');
		if (ul.length) {
			var href = ul.find('a:eq(0)').attr('href');
			window.location.href = href;
			return false;
		}
	});
});

//Background
var gallery = null;
jQuery(document).ready(function($) {

	backgroundResize();
	
	$(window).bind('resize', backgroundResize);
	
	if($('#imgBgr2_container')) {
		var rand_number = Math.floor(Math.random()*5);
		$.preLoadImages(bgArray, function() {
			$('#imgBgr_container').css('display', 'block');
			switchBg(rand_number);
		});
	}
	
	$('#squareoverpix').css('height', $('#imgBgr').css('height'));


});
