var tabPanel = new Array();

$(window).load(function(){
	if ($('#technoslider').length > 0){
		var currentImg = $('#ex0').children('span').first().children('img').first();
		$("#technoslider").animate({ height: currentImg.css('height') }, 200, function() {});
	}
	
	$('.postLoad').each(function(){
		$(this).attr('src', $(this).attr('title'));
		$(this).attr('title', $(this).attr('alt'));
	});
	
	if ($('#sliderHome').length > 0){
		startHomeSlider();
	}
});

$(document).ready(function(){
	if ($('#slider').length > 0){
		$('#slider').orbit({
			 animation: 'horizontal-push',                  // fade, horizontal-slide, vertical-slide, horizontal-push
			 animationSpeed: 520,                // how fast animtions are
			 timer: true, 			 // true or false to have the timer
			 advanceSpeed: 5000, 		 // if timer is enabled, time between transitions 
			 pauseOnHover: false, 		 // if you hover pauses the slider
			 startClockOnMouseOut:false, 	 // if clock should start on MouseOut
			 startClockOnMouseOutAfter: 500, 	 // how long after MouseOut should the timer start again
			 directionalNav: false, 		 // manual advancing directional navs
			 captions: true, 			 // do you want captions?
			 captionAnimation: 'fade', 		 // fade, slideOpen, none
			 captionAnimationSpeed: 800, 	 // if so how quickly should they animate in
			 bullets: false,			 // true or false to activate the bullet navigation
			 bulletThumbs: false,		 // thumbnails for the bullets
			 bulletThumbLocation: '',		 // location from this file where thumbs will be
			 afterSlideChange: function(){} 	 // empty function 
		 });
		 $('#slider').css('width', '');
		 $('#slider').css('height', ''); 
		 $('.orbit-wrapper').css('width', $('#slider').css('width'));
		 $('.orbit-wrapper').css('height', $('#slider').css('height'));
		 $('#slider').css('visibility', 'visible');
	 }
	 
	 // POPUPS
	 $('div.popup').css('opacity','0');
	
	// societe  FR
	 $("#span4").mouseover(function(){
			$('div.popup#society').css('display','block');
			$('div#society.popup').animate({
									opacity: '1'},
									200,
									function(){
												})
		});	
		
	 $("#span4").mouseout(function(){
			$('div#society.popup').animate({
									opacity: '0'},
									200,
									function(){
												$(this).css('display','none');
												})
	});
	
		// societe  EN
	 $("#span38").mouseover(function(){
			$('div.popup#societyEN').css('display','block');
			$('div#societyEN.popup').animate({
									opacity: '1'},
									200,
									function(){
												})
		});	
		
	 $("#span38").mouseout(function(){
			$('div#societyEN.popup').animate({
									opacity: '0'},
									200,
									function(){
												$(this).css('display','none');
												})
	});	
		
	// references	FR
	$("#span5").mouseover(function(){
			$('div.popup#references').css('display','block');
			$('div#references.popup').animate({
									opacity: '1'},
									200,
									function(){
												})
		});	
	
	$("#span5").mouseout(function(){
			$('div#references.popup').animate({
									opacity: '0'},
									200,
									function(){
												$(this).css('display','none');
												})
	});	
	
		// references	EN
	$("#span39").mouseover(function(){
			$('div.popup#referencesEN').css('display','block');
			$('div#referencesEN.popup').animate({
									opacity: '1'},
									200,
									function(){
												})
		});	
	
	$("#span39").mouseout(function(){
			$('div#referencesEN.popup').animate({
									opacity: '0'},
									200,
									function(){
												$(this).css('display','none');
												})
	});	
		
		
	// FORMULAIRE CONTACT	
		$('input').click(function(){
				$('span.error').animate({
									opacity: '0'},
									200,
									function(){})
	});	
		
	 //TECHNOSLIDE
	if ($('#technoslider').length > 0){
		/*$('#technoslider').orbit({
			 animation: 'fade',                  // fade, horizontal-slide, vertical-slide, horizontal-push
			 animationSpeed: 800,                // how fast animtions are
			 timer: false, 			 // true or false to have the timer
			 advanceSpeed: 4000, 		 // if timer is enabled, time between transitions 
			 pauseOnHover: false, 		 // if you hover pauses the slider
			 startClockOnMouseOut: false, 	 // if clock should start on MouseOut
			 startClockOnMouseOutAfter: 1000, 	 // how long after MouseOut should the timer start again
			 directionalNav: false, 		 // manual advancing directional navs
			 captions: false, 			 // do you want captions?
			 captionAnimation: 'fade', 		 // fade, slideOpen, none
			 captionAnimationSpeed: 800, 	 // if so how quickly should they animate in
			 bullets: true,			 // true or false to activate the bullet navigation
			 bulletThumbs: false,		 // thumbnails for the bullets
			 bulletThumbLocation: '',		 // location from this file where thumbs will be
			 afterSlideChange: function(){} 	 // empty function 
		});
		$('#technoslider').css('width', '');
		$('#technoslider').css('height', '');
		$('.orbit-wrapper').css('width', $('#technoslider').css('width'));
		$('.orbit-wrapper').css('height', $('#technoslider').css('height'));
		$('#technoslider').css('visibility', 'visible');*/
		
		
		
		$('#technoslider').css('visibility', 'visible');
		var picpackCounter = 0;
		var technosliderUlBullets = '<div id="browseArrows"><a class="prev browse left">&nbsp;</a><a class="next browse right">&nbsp;</a></div><ul id="technosliderbullets">';		
		$('.picpack').each(function(){
			$(this).attr('id', 'ex' + picpackCounter);
			var hideLi = '';
			if (picpackCounter > 18) hideLi = ' style="display: none;"';
			technosliderUlBullets += '<li id="liEx' + picpackCounter + '"' + hideLi + '><a href="#' + $(this).attr('id') + '">' + (picpackCounter + 1) + '</a></li>';
			picpackCounter++;
		});
		technosliderUlBullets += '</ul>';

		$('#technoslider').before(technosliderUlBullets);
		$('#technosliderbullets').children('li').first().addClass('current');
		
		
		$("#technoslider").scrollable({
			onBeforeSeek: function(event, i){
				var liCount = $('#technosliderbullets').children('li').length;
				var showLiFrom = 0;
				if (i <= 9)	showLiFrom = 0;
				else if (i >= liCount -9) showLiFrom = liCount -18;					
				else showLiFrom = i - 9;
				var showLiTo = showLiFrom + 18;
				var currentLiCounter = 0;
				$('#technosliderbullets').children('li').each(function(){
					if (currentLiCounter < showLiFrom || currentLiCounter > showLiTo) $(this).css('display', 'none');
					else $(this).css('display', 'list-item');	
					currentLiCounter ++;
				});
				var nextImg = $('#ex' + i).children('span').first().children('img').first();
				$("#technoslider").animate({ height: nextImg.css('height') }, 200, function() {});
			}
		}).navigator({
			// select #flowtabs to be used as navigator
			navi: "#technosliderbullets",

			// select A tags inside the navigator to work as items (not direct children)
			naviItem: 'li',

			// assign "current" class name for the active A tag inside navigator
			activeClass: 'current'
		});
	}


	$('.mainNavLi.hasChild').each(function(){
		var subPanel = $(this).children('.navPanel').first();
		$(this).data('subPanelId', subPanel.attr('id'));
		subPanel.remove();	
		$('#navBelow').append(subPanel);
		subPanel.css('position', 'absolute');
		var offset = $(this).offset();
		offset.top -= 20;
		subPanel.offset(offset);		
		subPanel.css('left', '-50px');
		subPanel.css('z-index', 5);
		subPanel.data('opened', false);
		subPanel.css('opacity', '0');
		subPanel.css('visibility', 'visible');
	});
	
	/*
	$('.mainNavLi.hasChild').click(function(){
		return false;
	});
	*/
	$('#parentRow2 a, #parentRow3 a,  #parentRow102 a,  #parentRow119 a').css('cursor','default');
	$('#parentRow2, #parentRow3, #parentRow102,  #parentRow119').click(function(){
				return false;
		
	});
	
	// ENTER sur un lien parent
	$('li.mainNavLi').mouseover(function() {
		forceCloseAllPanel($(this).data('subPanelId'));
		if ($(this).hasClass('hasChild'))
			openPanel($(this).data('subPanelId'));			
	});	
	
	$('div.navPanel').mouseover(function() {
		if ($(this).data('opened'))
			openPanel($(this).attr('id'));
	});
	
		 
	// LEAVE hors d'un lien parent
	$('li.mainNavLi').mouseleave(function() {
		closePanelWithTimeOut($(this).data('subPanelId'));
	});		
	
	$('div.navPanel').mouseleave(function() {	 
		closePanelWithTimeOut($(this).attr('id'));
	});
	
	
	// SUB NAV HOVER
	
	$('li.subNavLi').mouseover(function(){
		$(this).children('span').children('a').css('color','#d6d300');
	
	});
	
	
	$('li.subNavLi').mouseleave(function(){
			$(this).children('span').children('a').css('color','#ffffff');
	});
	
	$('li.subNavLi span a').click(function(){
			$(this).css('color','#ffffff');
	});
	
	// SWITCH TECHNOSLIDE
	
	/*
		$('div#picswitch').click(function(){
			$('span#switch').click();
		});
	*/
/*
	if ($('span#switch').length > 0){
		$('span#switchOn').click(function(){
			$(this).css('background-position','top left');
			$('span#switchOff').css('background-position','top right');
			$('span.off').animate({opacity: '0'},1000,function(){});
		});
			
		$('span#switchOff').click(function(){
			$('span.off').animate({opacity: '1'},1000,function(){});
			$(this).css('background-position','bottom right');
			$('span#switchOn').css('background-position','bottom left');
		});	
		
		$('span#switch').data("state",false); // le SWITCH EST SUR OFF PAR DEFAUT
		$('span#switch').click(function(){
			$(this).data("state", !$(this).data("state"));		
			$(this).css('background-position', ($(this).data("state") ? 'top left' : 'bottom left'));
			SwitchPic($(this).data("state"));	
		});
	}
*/	

	
	if ($('span#switch').length > 0){
		$('span#switchOn').click(function(){
			$(this).css('background-position','top left');
			$('span#switchOff').css('background-position','top right');
			$('span.off').animate({opacity: '0'},1000,function(){});
		});
			
		$('span#switchOff').click(function(){
			$('span.off').animate({opacity: '1'},1000,function(){});
			$(this).css('background-position','bottom right');
			$('span#switchOn').css('background-position','bottom left');
		});	
		
		$('span#switch').data("state",false); // le SWITCH EST SUR OFF PAR DEFAUT
		$('div#picswitch').click(function(){
			$("span#switch").data("state", !$("span#switch").data("state"));		
			$("span#switch").css('background-position', ($("span#switch").data("state") ? 'top left' : 'bottom left'));
			SwitchPic($("span#switch").data("state"));	
		});
	}
	
	// DISTRIBUTEURS
	if ($('div#contactlist').length > 0){
		$("div#contactlist ul h5").click(function(){
			var currentLi = $(this).parent();
			var titleHeight = $(this).height();
			var contentHeight = currentLi.children('.distributeur_content').first().height();
			var alreadyOpened = currentLi.data('opened');	
		
			$("div#contactlist ul li").each(function(){
				if ($(this).attr('id') != currentLi.attr('id') && $(this).data('opened')){
					$(this).stop();
					$(this).data('opened', false);
					$(this).animate({
						height: 20
					}, 270 );
				}				
			});						
			
			if (!alreadyOpened){
				currentLi.stop();
				currentLi.data('opened', true);
				currentLi.animate({
					height: titleHeight + contentHeight + 20
				}, 270 );
			}			
		});
		
		
		/*
		
		$("div#contactlist ul li").mouseenter(function(){
			$(this).children('h5').first().click();			
		});
		
		$("div#contactlist ul li").mouseleave(function(){
			$("div#contactlist ul li").each(function(){
				if ($(this).data('opened')){
					$(this).stop();
					$(this).data('opened', false);
					$(this).animate({
						height: 20
					}, 270 );
				}				
			});				
		});
		*/
	}	

	
	
	
	if ($('a.myPlayer').length > 0){
		// VIDEO
		flowplayer(
			"a.myPlayer", 
			"http://releases.flowplayer.org/swf/flowplayer-3.2.7.swf", 
			{		
				// this is the player configuration. You'll learn on upcoming demos.
				plugins:  {
					controls:  {
						volume: false		
					}
				}
			}
		);
	}


	$('.aSecureZone').click(function(){
		if ($(this).data('activated')){
			$(this).data('activated', false);
			$(this).parent().css('background', '');
			$('#secureZoneForm').css('display', 'none');
		}else{
			$('.aSecureZone').each(function(){
				if ($(this).data('activated')){
					$(this).data('activated', false);
					$(this).parent().css('background', '');
				}				
			});
			$(this).data('activated', true);
			$(this).parent().css('background', 'none repeat scroll 0 0 #080808');
	
			$('#secureZoneForm').attr('action', $(this).attr('href'));
			$('#secureZoneForm').css('display', 'block');
		}		
		return false;
	});	
	
	
	$("div.gammeContainer").click(function(){
		$(this).css('height','auto');
	});
	
});

function closePanelWithTimeOut(panelId){
	var panel = $('#' + panelId);
	panel.data('closerTimeout', 
		setTimeout(function(){
			closePanel(panelId, false);
		}, 1000)
	);
}

function forceCloseAllPanel(ignorePanelId){
	$('div.navPanel').each(function(){
		if ($(this).data('opened') && $(this).attr('id') != ignorePanelId){	
			closePanel($(this).attr('id'), true);
		}
	});
}

function openPanel(panelId){
	var panel = $('#' + panelId);
	if (panel.data('closerTimeout') > 0) clearTimeout(panel.data('closerTimeout'));
	panel.data('closerTimeout', 0);
	if (!panel.data('opened')){	
		panel.data('opened', true);
		panel.stop();		
		panel.animate({
				left: '140px',
				opacity: '1'
				}, 600, function() {
			// Animation complete.
		  });
	}
}

function closePanel(panelId, force){
	var panel = $('#' + panelId);
	if (panel.data('closerTimeout') > 0) clearTimeout(panel.data('closerTimeout'));
	panel.data('closerTimeout', 0);
	if (force || panel.data('opened')){	
		panel.data('opened', false);		
		panel.stop();		
		panel.animate(
				{
					left: '10px',
					opacity: '0'
				}, 
				250, 
				function()  {}
		);
	  }
}

function SwitchPic(On) {
	if (On) {
		$('span.off').animate({opacity: '0'},300,function(){});
	} 
	else {
		$('span.off').animate({opacity: '1'},100,function(){});
	}
}



