// JavaScript Document
//jQuery.noConflict();
$(document).ready(initialiser);

function initialiser(){
	/*var btnsMenu = $('.sectionTitre');
	var numberOfBtnsMenu = btnsMenu.length;
	var listWidth = [];
	var totalWidth = 0;
	btnsMenu.each(function(){listWidth.push($(this).innerWidth(true));});
	$.each(listWidth,function(){totalWidth += this;});
	alert(listWidth);
	alert(totalWidth);
	$("#nav-one").css("width", totalWidth);*/
			
	$("#nav-one").dropmenu();
	$("#infosTKD").delegate('a.videoForme', 'click', function() { $(".videoForme").colorbox({iframe:true, innerWidth:425, innerHeight:349});});
	
	//move the image in pixel
	var move = -15;
	//zoom percentage, 1.2 =120%
	var zoom = 1.2;
	$('#infosTKD').delegate('.item','hover', function(event) {
		if(event.type === 'mouseenter'){
			//Set the width and height according to the zoom percentage
			width = $('.item').width() * zoom;
			height = $('.item').height() * zoom;
			 
			//Move and zoom the image
			$(this).find('img').stop(false,true).animate({'width':width, 'height':height, 'top':move, 'left':move}, {duration:200});
			 
			//Display the caption
			$(this).find('div.caption').stop(false,true).fadeIn(200);
		} 
		else{
			//Reset the image
			$(this).find('img').stop(false,true).animate({'width':$('.item').width(), 'height':$('.item').height(), 'top':'0', 'left':'0'}, {duration:100});   
	 
			//Hide the caption
			$(this).find('div.caption').stop(false,true).fadeOut(200);
		}
	});

	$("#btnLogin").colorbox({width:"600px", inline:true, href:"#loginBox"});
	
	//Normes Ados
	$("#exerAdos01").colorbox({width:"600px", inline:true, href:"#tabNormesAdos1"});
	$("#exerAdos02").colorbox({width:"600px", inline:true, href:"#tabNormesAdos2"});
	$("#exerAdos03").colorbox({width:"600px", inline:true, href:"#tabNormesAdos3"});
	$("#exerAdos04").colorbox({width:"600px", inline:true, href:"#tabNormesAdos4"});
	$("#exerAdos05").colorbox({width:"600px", inline:true, href:"#tabNormesAdos5"});
	$("#exerAdos06").colorbox({width:"600px", inline:true, href:"#tabNormesAdos6"});
	
	var arr = ["Ecole1","Ecole2","Ecole3","Ecole4","Ecole5"];
	jQuery.each(arr, function() {
		var lienEcole = $("#lien" + this);
		var divEcole = $("#div" + this);
		lienEcole.click(function(){$.scrollTo(divEcole, 1000, {easing:'easeOutExpo'} );});
	});
				
	//News Slider
	var currentPosition = 0;
	var slideWidth = 600;
	var slides = $('.slide');
	var numberOfSlides = slides.length;

	// Remove scrollbar in JS
	$('#slidesContainer').css('overflow', 'hidden');
	
	// Wrap all .slides with #slideInner div
	slides.wrapAll('<div id="slideInner"></div>')	// Float left to display horizontally, readjust .slides width
	.css(
	{
		'float': 'left',
		'width': slideWidth
	});
	
	// Set #slideInner width equal to total width of all slides
	$('#slideInner').css('width', slideWidth * numberOfSlides);
	
	// Insert controls in the DOM
	$('#slideshow').prepend('<span class="control" id="leftControl"></span>').append('<span class="control" id="rightControl"></span>');
	
	// Hide left arrow control on first load
	manageControls(currentPosition);
	
	// Create event listeners for .controls clicks
	$('.control').bind('click', function()
	{
		// Determine new position
		currentPosition = ($(this).attr('id') == 'rightControl') ? currentPosition + 1 : currentPosition - 1;
		
		// Hide / show controls
		manageControls(currentPosition);
		// Move slideInner using margin-left
		$('#slideInner').animate(
		{
			'marginLeft': slideWidth * (-currentPosition)
		});
	});
	
	// manageControls: Hides and Shows controls depending on currentPosition
	function manageControls(position)
	{
		// Hide left arrow if position is first slide
		if (position == 0) 
		{
			$('#leftControl').hide()
		}
		else 
		{
			$('#leftControl').show()
		}
		// Hide right arrow if position is last slide
		if (position == numberOfSlides - 1) 
		{
			$('#rightControl').hide()
		}
		else 
		{
			$('#rightControl').show()
		}
	}
};

(function($){
	$.fn.dropmenu = function(custom) {
		var defaults = {
		  	openAnimation: "slide",
			closeAnimation: "slide",
			openClick: false,
			openSpeed: 300,
			closeSpeed: 300,
			closeDelay: 300,
			onHide: function(){},
			onHidden: function(){},
			onShow: function(){},
			onShown: function(){}
		  };
		var settings = $.extend({}, defaults, custom);
		
		var menu = this;
		var currentPage = 0;
		var delayTimer = "";
		
		// Trigger init
		init();
		
		/**
		 * Do preparation work
		 */
		function init(){

			// Add open button and class to parent of a submenu
			var items = menu.find(":has(li,div) > a").append('<span class="arrow"></span>');
			$.each(items, function(i, val) {
				if(items.eq(i).parent().is("li")){
					items.eq(i).next().addClass("submenu").parent().addClass("haschildren");
				}else{
					items.eq(i).parent().find("ul").show();
				}
			});
			
			if(settings.openClick){
				menu.find(".submenu").css("display", "none");
				menu.find(":has(li,div) > a").parent().bind("mouseleave",handleHover).bind("mouseenter",function(){ window.clearInterval(delayTimer); });
				menu.find("a span.arrow").bind("click", handleHover);
			}else{
				menu.find(":has(li,div) > a").bind("mouseenter",handleHover).parent().bind("mouseleave",handleHover).bind("mouseenter",function(){ window.clearInterval(delayTimer); });
			}
			
			
		}
		
		/**
		 * Handle mouse hover action
		 */
		function handleHover(e){
			if(e.type == "mouseenter" || e.type == "click"){
				window.clearInterval(delayTimer);
				var current_submenu = $(e.target).parent().find(".submenu:not(:animated):not(.open)");
				if(current_submenu.html() == null){
					current_submenu = $(e.target).parent().next(".submenu:not(:animated):not(.open)");
				}
				if(current_submenu.html() != null){
					settings.onShow.call(current_submenu);
					closeAllMenus();
					current_submenu.prev().addClass("selected");
					current_submenu.css("z-index", "90");
					current_submenu.stop().hide();
					openMenu(current_submenu);
				}
			}
			if(e.type == "mouseleave" || e.type == "mouseout"){
				current_submenu = $(e.target).parents(".submenu");
				if(current_submenu.length != 1){
					var current_submenu = $(e.target).parent().parent().find(".submenu");
					if(current_submenu.html() == null){
						var current_submenu = $(e.target).parent().find(".submenu");
						if(current_submenu.html() == null){
							var current_submenu = $(e.target).parent().parent().parent().find(".submenu");
						}
					}
				}
				if(current_submenu.html() != null){
					if(settings.closeDelay == 0){
						current_submenu.parent().find("a").removeClass("selected");
						closeMenu(current_submenu);
					}else{
						window.clearInterval(delayTimer);
						delayTimer = setInterval(function(){
							window.clearInterval(delayTimer);
							closeMenu(current_submenu);
						}, settings.closeDelay);	
					}
				}
			}
		}
		
		function openMenu(object){
			switch(settings.openAnimation){
				case "slide":
					openSlideAnimation(object);
					break;
				case "fade":
					openFadeAnimation(object);
					break;
				default:
					openSizeAnimation(object);
					break;
			}
		}
		
		function openSlideAnimation(object){
			object.addClass("open").slideDown(settings.openSpeed, function(){ settings.onShown.call(this); });
		}
		
		function openFadeAnimation(object){
			object.addClass("open").fadeIn(settings.openSpeed, function(){ settings.onShown.call(this); });
		}
		
		function openSizeAnimation(object){
			object.addClass("open").show(settings.openSpeed, function(){ settings.onShown.call(this); });
		}
		
		function closeMenu(object){
			settings.onHide.call(object);
			switch(settings.closeAnimation){
				case "slide":
					closeSlideAnimation(object);
					break;
				case "fade":
					closeFadeAnimation(object);
					break;
				default:
					closeSizeAnimation(object);
					break;
			}
		}
		
		function closeSlideAnimation(object){
			object.slideUp(settings.closeSpeed, closeCallback);
		}
		
		function closeFadeAnimation(object){
			object.fadeOut(settings.closeSpeed, function(){ $(this).removeClass("open"); $(this).prev().removeClass("selected"); });
		}
		
		function closeSizeAnimation(object){
			object.hide(settings.closeSpeed, function(){ $(this).removeClass("open"); $(this).prev().removeClass("selected"); });
		}
		
		function closeAllMenus(){
			var submenus = menu.find(".submenu.open");
			$.each(submenus, function(i, val) {
				$(submenus[i]).css("z-index", "1");
				closeMenu($(submenus[i]));
			});
		}
		
		function closeCallback(object){
			$(this).removeClass("open"); 
			if($(this).prev().attr("class") == "selected") settings.onHidden.call(this);
			$(this).prev().removeClass("selected");
		}
			
		// returns the $ object to allow for chainability.
		return this;
	}
	
})(jQuery);

