/* Author: 
Guillaume Meunier
*/
$(document).ready( function() {
	//EFFACEMENT AUTOMATIQUE DU MENU
	lanceClosing = function(t){
		if (t == null) t = "12s";
		$(document).everyTime(t, "closing", function() {
			if ($('#menu').css('z-index')>10) {
				menuClick();
				stopClosing();
			}
		});
	};
	restartClosing = function(t) {
		stopClosing();
		lanceClosing(t);
	};
	stopClosing = function() {
		$(document).stopTime("closing");
	};
	//CHANGE LA CLASSE
	$('#contact').removeClass('hidden').addClass('menuClass');
	$('div[id*="agence_text"]').removeClass('hidden').addClass('agenceTextClass');
	$('div[id*="agence_link"]').removeClass('hidden').addClass('agenceLinkClass');
	$('.hidden').removeClass('hidden').addClass('agenceImgClass');
	//INITIALISATION	
	var s = 10;//size of box
	$.flash.expressInstaller = 'swf/expressInstall.swf';
	var randomNumber = Math.floor( (Math.random()*101)-50 );
	$('#box').offset({ top: $(window).height()/2+randomNumber, left: $(window).width()/2+randomNumber });
	if ($(window).height()-$('#box').offset().top<95) $('#box').offset({ top: $(window).height()-130, left: $(window).width()/2+randomNumber });
	$('div[id*="agence"]').hide();//cache agence
	var idDivText = '#agence_texten';//par defaut le bon div est l'anglais
	var francaisText = $('#agence_textfr').html();//sauvegarde le texte francais
	var englishText = $('#agence_texten').html();//sauvegarde le texte anglais
	$('#agence_texten').text("");
	$('#agence_textfr').text("").css('width','0px');
	var t = 500;//temps des transitions
	var oMin = .5;//opacity when mouseout
	var dist = 92;//distance entre bloc
	$('#contact').css({'opacity':'0', 'right':"+="+dist*4+"px",'bottom':"-="+$('#contact').height()+"px"}).hide();//GERE L'IMAGE CONTACT
	//rajoute les tooltip
	$('#box').tooltip();
	$('#menu').tooltip();
	$('#contact').tooltip();
	$('#agence_link').tooltip();
	$('div[id*="menu_"]').css('opacity', 0).tooltip();
	$('#menu').css('opacity', 0).fadeTo( 1500, oMin );
	var bottomPos = 0;
	var mobile = false;
	var iphone = false;
	var flashRunning = false;
	//////////////////////
	//GESTION DES TOUCHPAD
	//////////////////////
	//IPHONE
	var ww = $(window).width();
	if(ww<1024) mobile=true;
	if(mobile){
		bottomPos = 92;
		$('#contact').css({'opacity':'0', 'right':'0px','bottom':"-="+$('#contact').height()+"px"}).hide();//GERE L'IMAGE CONTACT sur touch
		$('div[id*="agence_img"]').css({'width':'30px',});
		//$('#agence_textfr').css({'display':'block', 'top':'-200px', 'left':'0px'}).hide();
		$('#agence_textfr').css('vertical-align','top');
		$('#agence_texten').css('width','0px');
		$('#agence_textfr').css('width','190px');
		idDivText = '#agence_textfr';
	};
	if (ww<500) {
		$('#contact').css({'left':'0px'});
		iphone = true;
	}
	//TEXTE ANGLAIS ONOVER
	$(idDivText).mouseover(function () {
		$(this).html(francaisText);
	}).mouseout(function () {
		$(this).html(englishText);
	});
	//MENU ONOVER ONOUT
	$('div[id*="menu"]').mouseover(function () {
		$(this).fadeTo( 200, 1 );
	}).mouseout(function () {
		$(this).fadeTo( 200, oMin );
	});
	//HIDE CONTACT
	hideContact = function(){
		$('#contact').animate({
			opacity: 0,
			bottom: "-="+$('#contact').height()+"px",
			}, t, function() { $(this).hide();}
		);
	};
	//BOX ONCLICK FLASHHHHHHHHHHHH
	$('#box').click(function(){
		$('#flash').hide();
		$('div[id*="agence"]').hide("fade");
		if ($('#menu').css('z-index')>10) {
			menuClick();
		}
		if ($.flash.available) {};
		if ($.flash.hasVersion(10.2) && !iphone) {//CAS NORMAL
			$('#box').hide();
			$('#flash').show();
			$('#flash').width(700).height(700).css({'padding-top':'0px', 'top':'0px', 'margin-top':'5px'});
			//log($("menu").offset().top);
			//$("menu").offset({ top: 800});
			$('#flash').flash({swf:'swf/sbbt.swf',height:700,width:700, bgcolor:'#000000'});
			flashRunning = true;
			$('#box').hide();
		} else if ($.flash.hasVersion(10.2) && iphone) {//CAS PETIT ECRAN
			$('#flash').css({'width':'300px','background':'#FF6600'});
			$('#flash').text("votre ecran est trop petit (min 700px)");
			$('#flash').show();
		} else if ($.flash.available) {//CAS MISE A JOUR FLASH
			$('#flash').show();
			$('#flash').flash($.flash.expressInstaller);
		} else {//IPHONE ARF ARF
			$('#flash').hover( function(){
				$(this).children('a').css({'color':'#FFF'});},
				function(){
					$(this).children('a').css('color', '#000');});
			$('#flash').html("<a href='liste_projets.html' title='Project's list'>Vous devez avoir flash pour voir l'animation<br>mais vous avez la liste des projets...</a>");
			$('#flash').css({'width':'300px','background':'#FF6600','padding-top':'20px','padding-bottom':'20px'});
			$('#flash').children('a').css({'text-decoration':'none'});
			$('#flash').show();
		}
	});
	//AGENCE Toggle
	toggleAgence = function(){
		t="12s";
		if (iphone) t="2s";
		restartClosing(t);
		if ( flashRunning ) $('#flash').flash().remove();
		flashRunning = false;
		if ( !Modernizr.touch ){
			$('#box').show("puff");
		};
		$('#flash').hide();
		$(idDivText).html(englishText);
		$('div[id*="agence"]').each(function(){
			var randomNumber = Math.floor( (Math.random()*201) );
			$(this).css('padding-top', randomNumber).toggle("fade");
		});
		if(mobile){
			$('#agence_textfr').css({'padding-top':'10px', 'top':'0px', 'margin-top':'0px'});
			$('#agence_link').removeClass('agenceLinkClass');
			$('#agence_link').css({'padding-top':'10px', 'top':'0px', 'margin-top':'0px','float':'right', 'text-align':'right', 'padding-right':'10px',});
			if ($('#contact').css('opacity')=="1") hideContact();
		};
	};
	//AGENCE ONCLICK
	$('#menu_img1').click(function(){
		toggleAgence();
	});
	//CONTACT ONCLICK
	$('#menu_img3').click(function(){
		t="12s";
		if (iphone) t="2s";
		restartClosing(t);
		if ($('#contact').css('opacity')=="0") {
		$('#contact').show().animate({
					opacity: 1,
					bottom: bottomPos,
				}, t, function() {
				})
		} else if ($('#contact').css('opacity')=="1") hideContact();
		if ($('#agence_img4').is(':visible') && mobile) toggleAgence();
	});
	//MENU ONCLICK
	menuClick = function(){
		lanceClosing();
		if ( !Modernizr.touch && !flashRunning ){
			$('#box').show("puff");
		};
		if ($('#contact').css('opacity')=="1" && !iphone) hideContact();
		if ($('#menu').css('z-index')<10) {//OPEN MENU
			$('#menu').css('z-index', 10);
			$('#menu_img1').animate({
				opacity: oMin,
				right: "+="+dist+"px",
			}, t, function() {
				// Animation complete.
				$('#menu_img2').css('right', "+="+dist+"px").animate({
					opacity: oMin,
					right: "+="+dist+"px",
				}, t, function() {
					// Animation complete.
					$('#menu_img3').css('right', "+="+dist*2+"px").animate({
						opacity: oMin,
						right: "+="+dist+"px",
					}, t, function() {
						// Animation complete.
						//$('#menu').fadeTo( t, oMin );
						$('#menu').css('z-index', 11);
					});
				});
			});
		} else if ($('#menu').css('z-index')>10) {//CLODE MENU
			stopClosing();
			$('#menu').css('z-index', 10);
			$('#menu_img3').animate({
				opacity: 0,
				right: "-="+dist+"px",
			}, t, function() {
				// Animation complete.
				$('#menu_img3').css('right', 0);
				$('#menu_img2').animate({
					opacity: 0,
					right: "-="+dist+"px",
				}, t, function() {
					// Animation complete.
					$('#menu_img2').css('right', 0)
					$('#menu_img1').animate({
						opacity: 0,
						right: "-="+dist+"px",
					}, t, function() {
						// Animation complete.
						//$('#menu').fadeTo( t, oMin );
						$('#menu').css('z-index', 9);
					});
				});
			});
		}
	};
	//MENU ONCLICK
	$('#menu').click(function(){
		menuClick();
	});
});
