var timerBg = 0;

/* jQuery.preloader - v0.95 - K Reeve aka BinaryKitten
*
* v0.95
* 	# Note - keeping below v1 as really not sure that I consider it public usable.
* 	# But it saying that it does the job it was intended to do.
* 	Added Completion of loading callback.
* 	Main Reworking With Thanks to Remy Sharp of jQuery for Designers
*
*
* v0.9
* 	Fixed .toString being .toSteing
*
* v0.8
*		Fixed sheet.href being null error (was causing issues in FF3RC1)
*
* v0.7
*		Remade the preLoadImages from jQuery to DOM
*
* v0.6
* 		Fixed IE6 Compatability!
*		Moved from jQuery to DOM
*
* v0.5
* 		Shifted the additionalimages loader in the preLoadAllImages so it wasn't called multiple times
* 		Created secondary .preLoadImages to handle additionalimages and so it can be called on itself
*/

(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('/');spl.pop();path = spl.join('/')+'/';
			}
			else {
				path = './';
			}
			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) == '/') {
						var p2 = path.split('/');p2.pop();p2.pop();p2x = p2.join("/");
						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);

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

	backgroundResize();
	$('.scroll-pane:visible').jScrollPane();

	$(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);
		});
	}
	
	$('#imgBgTrame').css('height', $('#imgBgr').css('height'));

	$('#Pubs').cycle({
	    fx: 'scrollLeft',
		speed: 500,
	    timeout: 6000,
		random: true,
		cleartypeNoBg: true,
	    pager: '#PubsNav',
		// callback fn that creates a thumbnail to use as pager anchor
	    pagerAnchorBuilder: function(idx, slide) {
	        return '<li><a href="#">&nbsp;</a></li>';
	    }
	});
	
	var eve_scroll = new UIScrollView('#EveBloc', '#EveNavPrev', '#EveNavNext', 100);
	
	$('a#toggle_map, a#toggle_form').click(function() {
		$('a#toggle_map, a#toggle_form').toggle();
		$('#JoiForm').toggle();
		$('#JoiCarte').toggle();
	    return false;
	  });
	
	
	//Init the menu sections,
	$('.menu_section').click(function() {
		
		$('#MenusTop .on').removeClass('on');
		$(this).addClass('on');
		
		var link = this;
		
		$(".menu_container_on").fadeOut(function() {
			$(this).addClass("hidden");
			$(this).removeClass("menu_container_on");
		
			$('.menu_left .on').removeClass('on');
			$('#'+link.id+'_container .menu_left .menu_subsection:first').addClass('on');
		
			if($('.menu_content_on').length > 0) {

				$('.menu_content_on').css("display","none");

				classGestion(link);
			} else {
				classGestion(link);
			}
		});
		
		function classGestion(link) {	
			$('.menu_content_on').addClass("hidden");
			$('.menu_content_on').removeClass('menu_content_on');
			
			$('#'+link.id+'_container .menu_content:first').show();
			$('#'+link.id+'_container .menu_content:first').removeClass("hidden");
			$('#'+link.id+'_container .menu_content:first').addClass('menu_content_on');
		
			$('#'+link.id+'_container').fadeIn(function(){
				$(this).removeClass("hidden");
				$(this).addClass('menu_container_on');
			});
			
			var timerSection = setInterval(function(){
				$('#'+link.id+'_container .scroll-pane:visible').jScrollPane();
				clearInterval(timerSection);
			},50);
		}
	});
	
	//Init the sub menus sections,
	$('.menu_subsection').click(function() {
		
		$('.menu_container_on .menu_left .on').removeClass('on');
		
		$(this).addClass('on');
		
		var link = this;
		$('.menu_content_on:visible').fadeOut(function(){
			$(this).addClass("hidden");
			$(this).removeClass('menu_content_on');
			
			$('#'+link.id+'_container').fadeIn(function() {
				$(this).removeClass("hidden");
				$(this).addClass('menu_content_on');
			});
			
			var timerSub = setInterval(function(){
				$('#'+link.id+'_container .scroll-pane').jScrollPane();
				clearInterval(timerSub);
			},50);
			
		});
	});
});	//fin

//galleryGestion
function galleryGestion() {
	
	gallery = wp_gallery.init({
			handlers: $('div#gallery ul li a'),
			next: $('#images_next'),
			prev: $('#images_previous'),
			containers: {
				image: $('.EnContent div#viewContainer img#viewer')
			},
			defaultItem: $('#gallery li:first a')
		});
}

//ajaxGallery
function ajaxGallery() {
	var selector = new UISelectInput("#picChoice-select","#picChoice-option",{
		initCallback:function() {
			var val = this.getValue();
		},
		choiceCallback:function(text,el) {
			var chiffre = parseInt($(el).attr('id').charAt(2),10);
			$.post('assets/inc/ajax.php',{ id:chiffre,action:'ajaxCall'},function(data) {
				
				$('div#galleryContainer').hide().html(data.gallery).fadeIn('slow',function(){gallery.switch_image($('#gallery li:first a'));});
				
				$('.scroll-pane:visible').jScrollPane();
				galleryGestion();
			},
			'json');
		}
	});
}