$(function() {
	
	//fix embed transparent
	/*$('embed').each(function(i,e) {
		var $e = $(e), prt = $e.parent(), elClone = prt.html();
		elClone = $(elClone).attr('wmode','transparent');
		prt.html(elClone);
	});	*/
	
	//puces submainmenu
	$('#mainmenu ul:first>li.menu_active>ul>li:gt(0)').before('<li class="pM"></li>');
	//lavalamp style
	//$('#header li.menu_active>ul').lavaLamp();
	
	//breadcrumbs
	$('#breadcrumbs').append('<span class="lc"></span><span class="rc"></span>');
	
	//gest blocs de droite retractables
	$('#homecolright h2.oH2 a').click(function(e) {
		e.preventDefault();
	});
	
	var sOH2 = $('#homecolright h2.oH2'), $tO;
	sOH2.next().hide();
	sOH2.click(function() {
		$tO = $(this);
		if (!$tO.hasClass('active')) {
			sOH2.filter('.active').removeClass('active').next().hide(300);
			$tO.addClass('active').next().show(200);
		}
		else {
			$tO.removeClass('active').next().hide(300);
		}
	});
	
	// Gestion de l'espace personnel
 
 	$("#clientLogin").click (function () {
 		//$("#aggregator_main").html('<iframe height="800" frameborder="0" scrolling="auto" style="overflow: visible; width: 100%;" src="https://mail.tango.lu/selfservice"><p>Your browser does not support iframes.</p></iframe>');
 		window.open('https://mail.tango.lu/selfservice/provision.html','Moncompte','resizable=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,fullscreen=no,dependent=no,width=1024,height=800,status');

 		return false;
 	});

	 $("#numclient, #mdpclient, #inputsearch").focus(function () {
	 	if ($(this).val() == 'Num\351ro Client') {
	  		$(this).val('');
	 	}
	 	else if ($(this).val() == 'Mot de passe'){
	 		$("#mdpclient").remove();
	 		$("#mdp_div").append('<input type="password" id="mdpclient" name="password" value=""/>');
	 		$("#mdpclient").focus();
	 	}
	 	else if ($(this).val() == 'Recherche') {
	 		$(this).val('');
	 	}
	 });	 
	 
	$("#btnconnexion").click(function () {
		$("#hiddenFrame").html('<iframe style="display:none;" width="800" name="myIframe" id="myIframe" height="800" src="#" ></iframe>');
		$("#formWebBilling").attr('target','myIframe');
		$("#formWebBilling").submit();	
		$("body #myIframe").load( function(){
			window.location.href="http://www.tango.lu/page.php?url=particulier/espaceclients/monespacetango/mesfactures";
		});		 
		 
		 return false;
	});
	
	// Gestion de l'onglet Tango Mail
	 
 	$("#clientLogin").click (function () {
 		//$("#aggregator_main").html('<iframe height="800" frameborder="0" scrolling="auto" style="overflow: visible; width: 100%;" src="http://mail.tango.lu"><p>Your browser does not support iframes.</p></iframe>');
 		window.open('https://mail.tango.lu/selfservice/provision.html','Moncompte','resizable=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,fullscreen=no,dependent=no,width=1024,height=800,status');
 		return false;
 	});

	 $("#numclient_mail, #mdpclient_mail").focus(function () {
	
	 	if ($(this).val() == 'Adresse email') {
	  		$(this).val('');
	 	}
	 	else if ($(this).val() == 'Mot de passe'){
	 		$("#mdpclient_mail").remove();
	 		$("#mdp_div_mail").append('<input type="password" id="mdpclient" name="_pass" value=""/>');
	 		$("#mdpclient").focus();
	 	}
	 });
	 
	$("#btnconnexion_mail").click(function () {
		$("#hiddenFrame").html('<iframe style="display:none;" width="800" name="myIframe" id="myIframe" height="800" src="#" ></iframe>');
		$("#formTangoMail").submit();
		$("body #myIframe").load( function(){
			window.location.href="http://www.tango.lu/page.php?url=particulier/espaceclients/tangomail";
		});		 
		 return false;
	});
	
	$("iframe").each (function () {
		$(this).attr('frameborder', '0');
		$(this).frameBorder = 0;
	});
 	
 	
 	/******************* GESTION de LEA (...) ******************/
	
	var arrReturn 	 = new Array("0");
	var arrSaveQst 	 = new Array($('#yourqstVA').html());
	var saveHTMLVA 	 = '';
	
	function initLoaderVA() {
		$("#virtualadvisor a").click(function(e) {
			$("#ajaxloaderVA").ajaxStart(function(){
				$("#virtualadvisor #tangovalist").hide();
				$(this).show();
			});
			
			$("#ajaxloaderVA").ajaxStop(function(){
				$("#virtualadvisor #tangovalist").show();
				$(this).hide();
			});
		});
	}
	
	$.fn.initClickVA = function() {

		/* recuperation des questions */
		$("#virtualadvisor #tangovalist .hasDgt").click (function (e) {
			
			var idQst 		= this.id.substring(5, this.id.length);

			//gestion du lien de retour
			
			arrReturn.push(idQst);
			
			if (arrReturn.length > 1) {
				var blocReturn	= $("#virtualadvisor #yourqstVA");
				
				arrSaveQst.push($(this).html());
				blocReturn.html('<a href="" id="qstVA' + arrReturn[arrReturn.length-2] + '" class="return hasDgt">' + 
									arrSaveQst[arrSaveQst.length-1] + 
								'</a>');
				blocReturn.find(".return").initClickReturnVA();
			}
			
			//recuperation des donnees
			$.post("index.php/tangova/ajaxTangova?qstVA=" + idQst, function(data){
				
				//list de question/reponse
				$('#tangovalist').html(data);
				
				//reinitialisation des events
				$("#virtualadvisor #tangovalist a").initClickVA();
			});
 			
 			e.preventDefault();
 		});
 		
 		/* toggle des reponses */
 		$("#virtualadvisor #tangovalist .hasRsp").click (function (e) {
			$('.hasRsp +ul').hide();
			$(this).find('+ ul').show('slow');
			e.preventDefault();
 		});
	};
	
	//gestion du lien retour
	$.fn.initClickReturnVA = function() {
	
		$("#virtualadvisor .return").click (function (e) {
			
			var idQst 		= this.id.substring(5, this.id.length);
			var blocReturn	= $("#virtualadvisor #yourqstVA");
			
			//gestion du lien de retour (on le place avant l'appel serveur pour permettre le multi-cliquage)
			
			arrReturn.unset(arrReturn[arrReturn.length-1]);
			arrSaveQst.unset(arrSaveQst[arrSaveQst.length-1]);
			
			if (arrReturn.length > 1) {
				blocReturn.html('<a href="" id="qstVA' + arrReturn[arrReturn.length-2] + '" class="return hasDgt">' + 
									arrSaveQst[arrSaveQst.length-1] + 
								'</a>');
				$("#virtualadvisor .return").initClickReturnVA();
			}
			else {
				blocReturn.html(arrSaveQst[0]);
			}
			
			//on stop une quelconque requete ajax precedente
			// xhr.abort(); ?
			
			//recuperation des donnees
			$.post("index.php/tangova/ajaxTangova?qstVA=" + idQst, function(data){
				
				//list de question/reponse
				$('#tangovalist').html(data);
				
				//reinitialisation des events
				$("#virtualadvisor #tangovalist a").initClickVA();
			});
			
			e.preventDefault();
	 	});
	 };
 	
 	$('#leaconseil a').click(function(e){
		
		$('#leaconseil').animate({marginLeft:'-450px',opacity: 'toggle'},300);
		$('#getvirtualadvisor').animate({right:'229px', opacity: 'toggle'},200);
		
		e.preventDefault();
 	});
 	
 	$('#leaconseilArbo a').click(function(e){
		
		$('#leaconseilArbo').animate({marginLeft:'-450px',opacity: 'toggle'},300);
		$('#getvirtualadvisor').animate({right:'162px', opacity: 'toggle'},200);
		e.preventDefault();
		
 	});
 	
 	$('#virtualadvisor #closeVA').click(function(e){
		$('#getvirtualadvisor').animate({right:'0px', opacity: 'toggle'},200);
		if($('#leaconseilArbo a').html() == null){
			$('#leaconseil').animate({marginLeft:'0px',opacity: 'toggle'},200);
		}
		else{
			$('#leaconseilArbo').animate({marginLeft:'0px',opacity: 'toggle'},200);
		}
		e.preventDefault();
	 });
	 	
	
	//initialisation des differents events de VA
	initLoaderVA();
	$("#virtualadvisor a").initClickVA();


	/******************* GESTION menu de gauche ******************/
	
	//initialisation des blocs affiches suivant la page/arbo
	$.fn.initDisplayArboMenu = function() {
		$(this).find('ul ul ul').hide();
		$(this).find('ul:has(li[id*=menu_active_heading])').show();
		$(this).find('ul:has(li[id*=menu_active_heading]) #menu_active_heading > ul').show();
	
		
		// On regarde si le menu est composé d'éléments de niveau 2 sans enfants (niveau 3)
		var menuHasLevel3 = false;
		if ($(this).find('ul ul ul') == null)
			menuHasLevel3 = true;
		
		// S'il n'y a pas d'éléments de niveau 3 dans le menu, on classe les éléments de niveau 2
		// comme des éléments de niveau 2 de fin (menu_level2_end)
		if (!menuHasLevel3) {
			$(this).find('ul ul:not(:has(ul))').each(function (i) {
				$(this).find('a').attr('class', 'menu_level2_end');
			});
		}
	}
	
	$('#menu .menu_active').initDisplayArboMenu();
	
	// Supprimer décalage des images dans les H1
	$("#aggregator_main h1").each(function() {
	    if ($("img",this).length > 0) {
	    	$("img",this).css("float","right");
	    	$(this).css("float","left").css("min-width","70%").css("margin", "0");    
	    	img = $("img",this);
	    	$("img",this).remove();
	    	$(this).after(img);
	    }
	    else {
	    	$(this).css("margin", "0");
	    }
	});
	
	/****** gestion up/down fontsize ********/
	
	// up/down fontsize
	$('#font_up, #font_down').click(function(e){
		changeFontSize($(this).attr('id').substring(5));
		e.preventDefault();
	});
	//set fontsize
	if (getCookie('nbCkFF')) {
		changeFontSize('');
	}
	
	//up/down/set fontsize
	function changeFontSize(type) {
		
		var nbCkFF = parseInt(getCookie('nbCkFF')) || 0;
		var bSaveCk = true, setFS = true;
		var cpt = 0;

		if (type=='up'){
			if (nbCkFF < 7) {
				nbCkFF++;
				cpt = 1;
			}
		}
		else if(type=='down'){
			if(nbCkFF > 0) {
				nbCkFF--;
				cpt = -1;
			}
		}
		else {
			cpt = nbCkFF;
			bSaveCk = false;
		}
		if (nbCkFF == 0) {
			setFS = false;
		}
		//console.log('nbClick:'+nbCkFF+'== inc:'+cpt);
		$('#col2 #col2left #main h1, #col2 #col2left #main h2, #col2 #col2left #main p, #col2 #col2left #main li, #col2 #col2left #breadcrumbs p').each(function(){
			if (setFS) {
				$(this).css('font-size', function(i,v) {
					return (parseInt(v.replace('px','')) + cpt)+'px';
				});
			}
			else {
				$(this).css('font-size','');
			}
		});
		if (bSaveCk) setCookie('nbCkFF', nbCkFF);
	}
});

/*********************** FONCTIONS DIVERSES **********************/

//fonctions generiques pour les cookies

	function getCookie( name ) {
		var start = document.cookie.indexOf( name + "=" );
		var len = start + name.length + 1;
		if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
			return null;
		}
		if ( start == -1 ) return null;
		var end = document.cookie.indexOf( ';', len );
		if ( end == -1 ) end = document.cookie.length;
		return unescape( document.cookie.substring( len, end ) );
	}
	
	function setCookie( name, value, expires, path, domain, secure ) {
		var today = new Date();
		today.setTime( today.getTime() );
		if ( expires ) {
			expires = expires * 1000 * 60 * 60 * 24;
		}
		var expires_date = new Date( today.getTime() + (expires) );
		document.cookie = name+'='+escape( value ) +
			( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
			( ( path ) ? ';path=' + path : '' ) +
			( ( domain ) ? ';domain=' + domain : '' ) +
			( ( secure ) ? ';secure' : '' );
	}
	
	function deleteCookie( name, path, domain ) {
		if ( getCookie( name ) ) document.cookie = name + '=' +
				( ( path ) ? ';path=' + path : '') +
				( ( domain ) ? ';domain=' + domain : '' ) +
				';expires=Thu, 01-Jan-1970 00:00:01 GMT';
	}

//fonctions generiques type PHP pour les tableaux
	
	//pour verifier si presence d'une valeur
	Array.prototype.has = function(value) {
		return this.indexOf(value) != -1;
	};

	//pour supprimer un element d'un tableau	
	Array.prototype.unset = function(value) {
		this.splice(this.indexOf(value),1);
	};
	
	//IE ne reconnait pas indexOf, on cree la notre
	if (document.selection) {
		Array.prototype.indexOf = function(value){
			for(i=0 ; i < this.length; i++) {
				if(this[i] == value) {
					return i;
				}
			}
			return -1;
		 };
	}
	
function displayZone(elem) {
      if (document.getElementById(elem).style.visibility == "hidden") 
            document.getElementById(elem).style.visibility="visible";
}

//*** fonctions Jquery perso ***//

$.fn.initClickTab  = function(func) {
	return this.each(function() {
	
		var prt = $(this);
		
		prt.find('.zononglet li').click(function(e) {
			var idT = $(this).attr('id').substring(1);
			$(this).parent().children('.active').removeClass('active');
			$(this).addClass('active');
			
			//var newUrl = document.location.pathname+document.location.search+'#tab'+idT;
			//document.location.replace(newUrl);
			
			prt.find('>.zTab').hide().filter('#tab'+idT).show();
			
			if (typeof func != 'undefined') {
				func();
			}
			
			e.preventDefault();
		});
	});
};

