

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-23920250-1']);

var pushAnalytics = function($name, $value){
	document.title = "Equilibre Agence - "+$value;
	_gaq.push(['_trackEvent', $name, $value]);
};
$(function(){
	/* Google Analytics */
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    
    /* Initialisation des variable d'environnements */
    var trackDefaultPage = true;
	var initialisationMenuService = true;

	/* Ouverture et fermeture du login */
	$('.nav-item.first').click(function(){
		$(this).toggleClass('open');
		$('.user').animate({opacity: 'toggle',height: 'toggle'}, 500, function() {});
	});
	
	/* Ouverture et fermeture du formulaire de contact */
	$('.nav-item.last').click(function(){
		$(this).toggleClass('open');
		$('.contact').animate({opacity: 'toggle',height: 'toggle'}, 500, function() {});
	});
	
	
	/* Diaporama de la home */
	$(".scrollable").scrollable({circular:true}).navigator().autoscroll({
		interval: 6000		
	});
	
	/* Diaporama des news */
	$oDiapoNews = $(".scrollable-news").scrollable({api: true});
	$('.mea-item').click(function(){
		$oDiapoNews.seekTo($(this).index());
		$('.mea-item ').removeClass('actif');
		$(this).addClass('actif');
	});

	$('.diapo-content-item').click(function(){
		switch($(this).index()){
		case 2:
			$('.menu-service-item.two').trigger('click',[2]);
			if(false == initialisationMenuService){
				$('.menu-context:eq(1)').children().children(":eq(2)").children().trigger('click');
			}
			
			break;
		case 3:
			$('.menu-service-item.two').trigger('click',[0]);
			if(false == initialisationMenuService){
				$('.menu-context:eq(1)').children().children(":eq(0)").children().trigger('click');
			}
			break;
		case 4:
			$('.menu-service-item.two').trigger('click',[1]);
			if(false == initialisationMenuService){
				$('.menu-context:eq(1)').children().children(":eq(1)").children().trigger('click');
			}
			break;
		/*case 4:
			//ouverture d'un popup
			myWindow = window.open('/templates/game/index.php',
								'Equilibre-Agence le jeux',
								'width=500,height=400,left=0,top=100px,location=0,menubar=0,resizable=0,scrollbars=0,status=0,titlebar=0,toolbar=0'
			);
			break;*/
		}
	});
	
	/* redimmenssionnement automatique en fonction des particularités de chaques pages*/
	if($('.show-news').length == 1){
		if($('.show-news .image').length != 1){
			$('.show-news .title').width(675);
			$('.show-news .subtitle').width(675);
			$('.show-news .title').width(675);
			if($('.show-news .text object').length == 1){
				$('.show-news .text object').attr('width',675);
				$('.show-news .text object').attr('height',380);
				$('.show-news .text object embed').attr('width',675);
				$('.show-news .text object embed').attr('height',380);
			}
		}
	};
	
	/* Positionnement automatique du footer de la home */
	var adaptFooterPosition = function(){
		var topMenu = $('.menu-service').offset().top + $('.menu-service').height() +20;
		var top = $('.main-contents').offset().top + $('.main-contents').height() +20;
		var posTop = (top > topMenu) ? top : topMenu;
		var args = {top: posTop, left: $('.footer').offset().left};
		$('.footer').offset(args);
	};
	if($('.main-contents').length >= 1){
		$('.footer').css('margin-top', '180px');
	}
	
	/* Menu des services */
	var selectServiceMenu = function($elementClick){
		
		$elementClick = $elementClick;
		
		$('.menu-context').animate({height:'hide'}, 300, function(){
			$('.menu-service-item').not($elementClick).animate({width: '220px'},200, function(){});
			$('.menu-service-item').not($elementClick).children('img.left').animate({width:'175px', height:'50px'},200, function(){});
		});
		
		$($elementClick).animate({width:"315px"}, 200, function(){
			$(this).next().animate({height:'show'}, 300, function(){
				adaptFooterPosition();
			});
		});
		
		$($elementClick).children('img.left').animate({width:'250px', height:'50px'},200, function(){});
		
	};
	
	/* Action de click sur le sous menu des contents */
	$('.menu-service-item').not('.services-news .menu-service-item').click(function(event, indexToDisplay){
		indexToDisplay = ( indexToDisplay == undefined) ? 0 : indexToDisplay;
		$elementClick = $(this);
		 
		//Déplacement des Menu
		if(initialisationMenuService){
			//Déplacement des blocs
			$(".bloc-service-item.first").animate({
				width: '200px',
				marginRight: '68px'
			}, 1000, function(){});
			$(".bloc-service-item.first img").animate({
				width: '210px',
				height: '440px'
			}, 1000, function(){});
			$('.bloc-service-item.agence').animate({
				marginLeft: $('.bloc-service-item.reference').css('margin-left')
			}, 500, function(){
				$(this).hide();
				$('.bloc-service-item.reference').animate({
					marginLeft: $('.bloc-service-item.difference').css('margin-left')
				}, 500, function(){
					$(this).hide();
					$('.bloc-service-item.difference').animate({
						marginLeft: $('.bloc-service-item.nous').css('margin-left')
					}, 500, function(){
						$(this).hide();
						
					});
				});
			});
			$('.menu-service-item').animate({marginLeft: '-10px'}, 1000, function(){
				
				if( $(this).index() == 6){
					selectServiceMenu($elementClick);
					
					var idToDisplay = $($elementClick).next().children().children(':eq('+indexToDisplay+')').children().attr('href');
					$('.main-contents').show();
					$(idToDisplay).animate({height:"show"}, 500, function(){
						adaptFooterPosition();
						pushAnalytics("Institutionnel",$(this).children('.title').html());
						//console.log('Si pas ouvert : '+$(this).children('.title').html());
					});
					$('.menu-service').addClass('grid_8').removeClass('grid_24');
					initialisationMenuService = false;
				}
				return true;
			});
		} else {
			selectServiceMenu($elementClick);
		}
		
	});
	
	/* Ouverture du menu en cours */
	$('.display-content').not('.footer .display-content').click(function(){
		$domElementClick = $(this);
		$('.contents-item:visible').animate({height:"hide"}, 500, function(){
			var idToDisplay = $($domElementClick).attr('href');
			idToDisplay = idToDisplay.replace(/index\.php/g, "");
			$(idToDisplay).animate({height:"show"}, 500, function(){
				adaptFooterPosition();				
				pushAnalytics("Institutionnel",$(this).children('.title').html());
				//console.log('Quand on clique : '+$(this).children('.title').html());
			});
		});
	});
	/* Ouverture du menu en cours Quand on clique sur le footer*/
	$('.footer .display-content').click(function(){
		$domElementClick = $(this);
		var domId = $($domElementClick).attr('href');
		domId = domId.replace(/index\.php/g, "");
		if(true == initialisationMenuService){
			var liDisplayContent= $('.menu-context-home a[href="'+domId+'"]').parent().index();
			var menuContext = $('.menu-context-home a[href="'+domId+'"]').parent().parent().parent().index('.menu-context');
			var $domParent= $('.menu-context-home a[href="'+domId+'"]').parent().parent().parent().prev();
			$($domParent).trigger('click',[liDisplayContent]);
			$('.menu-context:eq('+menuContext+')').children().children(":eq("+liDisplayContent+")").children().trigger('click');
			trackDefaultPage = false;
			initialisationMenuService = false;
		}else{
			$('.contents-item:visible').animate({height:"hide"}, 500, function(){
				$(domId).animate({height:"show"}, 500, function(){
					adaptFooterPosition();				
					pushAnalytics("Institutionnel",$(this).children('.title').html());
					//console.log('Quand on clique : '+$(this).children('.title').html());
				});
			});
		}
		
	});
	
	/* Chargement automatique des page contenu si l'url possède un # */
	if(window.location.hash != ""){
		Expression = new RegExp("(^#contents-[0-9][0-9]?)$","g");
		if(Expression.test(window.location.hash)){
			var domId = window.location.hash;
			var liDisplayContent= $('.menu-context-home a[href="'+domId+'"]').parent().index();
			var menuContext = $('.menu-context-home a[href="'+domId+'"]').parent().parent().parent().index('.menu-context');
			var $domParent= $('.menu-context-home a[href="'+domId+'"]').parent().parent().parent().prev();
			$($domParent).trigger('click',[liDisplayContent]);
			if(false == initialisationMenuService){
				$('.menu-context:eq('+menuContext+')').children().children(":eq("+liDisplayContent+")").children().trigger('click');
			}
			trackDefaultPage = false;
		}
	}
	
	/* Tracking de la page si aucun contenu de load */
	if(true == trackDefaultPage){
		_gaq.push(['_trackPageview']);
	}
});
