﻿var TripHarmonic = {
  
  Start: function(){
    if($('navigation')) TripHarmonic.CountryList();
    TripHarmonic.Newsletter();
    if($('top')) TripHarmonic.ScrollToTop();
    if($('s')) TripHarmonic.InputSearch();
    if($('element')) TripHarmonic.Planisphere();
    if($('pratique') || $('liens')) TripHarmonic.SlideBlock();
    if($('new-list-pays')) TripHarmonic.CountrySlider();
    if(navigator.appVersion.indexOf("Mac",0)>0){TripHarmonic.DebugMacOs();}
    if(navigator.appVersion.indexOf("Mac",0)>0 && window.gecko && $('podcast')){
      $$('#new-list-pays ul')[0].style.marginTop = '-2px';
    }
  },
  
  CountryList: function(){
  
    var nav = $('navigation');    
    var oULNav = $$('#navigation ul')[0];	
  	var oListNav = $$('#navigation li'); 
  	var oBg = $('transparent');	
  	var oLink = $('openclose');
   
  	var oBtn = $$('#openclose img')[0];
    
  	ULNavEffects = new Fx.Style(oULNav, 'height',{duration:750}).set(100);
    BgEffects = new Fx.Style('transparent', 'height',{duration:750}).set(100);
    LinkEffects = new Fx.Style('openclose', 'top', {duration:750}).set(80);
    PushmapEffects = new Fx.Style('pushmap', 'top', {duration:750}).set(538);
    
    for(var i=5;i<oListNav.length;i++){
      oListNav[i].style.display = 'none';
    }
    	
    oBtn.src = 'http://www.trip-harmonic.com/wp-content/themes/tripharmonic/img/openlist.gif';
    
    oLink.href = 'javascript:TripHarmonic.ActionCtryList()';
    
  },
  
  ActionCtryList: function(){
    
    var nav = $('navigation');    
    var oULNav = $$('#navigation ul')[0];	
  	//var oListNav = $$('#navigation li'); 
  	var oListNav = document.getElementById('navigation').getElementsByTagName('li'); 
  	var oBg = $('transparent');	
  	var oLink = $('openclose');
  	
  	if(window.ie){var oBtn = document.getElementById('openclose').firstChild;}
    else{var oBtn = $$('#openclose img')[0];}
    
    ULNavEffects = new Fx.Style(oULNav, 'height',{duration:750});
    BgEffects = new Fx.Style('transparent', 'height',{duration:750});
    LinkEffects = new Fx.Style('openclose', 'top', {duration:750});
    PushmapEffects = new Fx.Style('pushmap', 'top', {duration:750});
    
    if(oBtn.src == 'http://www.trip-harmonic.com/wp-content/themes/tripharmonic/img/openlist.gif'){
    
    setTimeout(function(){
                  for(var i=5;i<oListNav.length;i++){
                    oListNav[i].style.display = 'block';
                  }
                 }, 750);
        
  		 
      oBtn.src = 'http://www.trip-harmonic.com/wp-content/themes/tripharmonic/img/closelist.gif';
      
      ULNavEffects.start(100, 310);
      BgEffects.start(100, 310);
      LinkEffects.start(80, 290);
      PushmapEffects.start(538, 748);
  			
      }
      
      else{TripHarmonic.CloseCtryList(oListNav,oULNav,oBg,oLink,oBtn);}
  
  },
  
  CloseCtryList: function(oListNav,oULNav,oBg,oLink,oBtn){
  	
    ULNavEffects = new Fx.Style(oULNav, 'height',{duration:750});
    BgEffects = new Fx.Style('transparent', 'height',{duration:750});
    LinkEffects = new Fx.Style('openclose', 'top', {duration:750});
    PushmapEffects = new Fx.Style('pushmap', 'top', {duration:750});
    
    for(var i=5;i<oListNav.length;i++){
      oListNav[i].style.display = 'none';
    }
    	
    ULNavEffects.start(310, 100);
    BgEffects.start(310, 100);
    LinkEffects.start(290, 80);
    PushmapEffects.start(748, 538);
    
    oBtn.src = 'http://www.trip-harmonic.com/wp-content/themes/tripharmonic/img/openlist.gif';
  
  },
  
  ScrollToTop: function(){
    var scroll = new Fx.Scroll(window, {duration:750,fps:150,transition: Fx.Transitions.Quad.easeInOut});
    $('top').onclick = function(){scroll.toTop();return false;}
  },
  
  InputSearch: function(){
    $('s').onfocus = function(){
      if(this.value == 'mot(s)-clé') this.value = '';
    }
    $('s').onblur = function(){
      if(this.value == '') this.value = 'mot(s)-clé';
    }
  },
  
  Planisphere: function(){

  	var oPlan = $('element');
  	if(oPlan.className != 'planisphere') return;
  	
  	for(var i=1;i<22;i++){tripPlanMove(i);}
  	
  	$('pays022').onclick = function (){return false;}

    function tripPlanMove(i){
    	
      var oInfobul = $('map0'+i);
    	var oPaysbul = $('pays0'+i);
    	
    	if(oPaysbul.className == 'redbox on'){
    		oInfobul.style.display = 'block';
    	} else{
    		oPaysbul.onmouseover = function(){oInfobul.style.display = 'block';}
    		oPaysbul.onmouseout = function(){oInfobul.style.display = 'none';}
    	}
      
    }
    
  },
  
  SlideBlock: function(){
    
    var oH2 = $$('h2');
    
    if(window.gecko && $('pratique')){
      for(var i=0;i<oH2.length;i++){
        
        oH2[i].chiffre = i;
        
        if(oH2[i].className == 'derouler'){
          oH2[i].statut = 'fermer';
          $('txt-0'+i).style.display = 'none';
          oH2[i].onclick = function(){
            ActionPratiqueFF(this);
          }
        } 
        
        else {
          oH2[i].statut = 'ouvert';
          oH2[i].onclick = function(){
            ActionPratiqueFF(this);
          }
        }
        
      }

    }
    else{
    
      for(var i=0;i<oH2.length;i++){
        
        oH2[i].chiffre = i;
        oH2[i].slider = new Fx.Slide('txt-0'+i, {duration: 650});
        
        if(oH2[i].className == 'derouler'){
          oH2[i].statut = 'fermer';
          oH2[i].slider.slideOut()
          oH2[i].onclick = function(){
            SlideAction(this);
          }
        } 
        
        else {
          oH2[i].statut = 'ouvert';
          oH2[i].onclick = function(){
            SlideAction(this);
          }
        }
        
      }
    }
    
    function SlideAction(obj){
      
      if(obj.statut == 'fermer'){
        obj.statut = 'ouvert';
        obj.style.background = 'url(/wp-content/themes/tripharmonic/img/masquer.gif)';
        
        obj.slider.slideIn()
        
        var oH2 = $$('h2');
        
        for(var i = 0;i<oH2.length;i++){
          if(i != obj.chiffre){
            CloseBlock(oH2[i])
          }
        }
        
      }
      
      else{CloseBlock(obj);}
    
    }
    
    function CloseBlock(obj){
      obj.statut = 'fermer';
    	obj.style.background = 'url(/wp-content/themes/tripharmonic/img/afficher.gif)';
      obj.slider.slideOut()
    }
    
    function ActionPratiqueFF(obj){
      
      if(obj.statut == 'fermer'){
        obj.statut = 'ouvert';
        obj.style.background = 'url(/wp-content/themes/tripharmonic/img/masquer.gif)';
        
        $('txt-0'+obj.chiffre).style.display = 'block';
        console.log(obj.chiffre);
        var oH2 = $$('h2');
        
        for(var i = 0;i<oH2.length;i++){
          if(i != obj.chiffre){
            ClosePratiqueFF(oH2[i])
          }
        }
        
      }
      
      else{ClosePratiqueFF(obj);}
    
    }
    
    function ClosePratiqueFF(obj){
      obj.statut = 'fermer';
    	obj.style.background = 'url(/wp-content/themes/tripharmonic/img/afficher.gif)';
      $('txt-0'+obj.chiffre).style.display = 'none';
    }
    
  },
  
  PopUp: function(page,nom,zewidth,zeheight){
  	var zetop1 = screen.height/2;
  	var zetop2 = zetop1-(zeheight/2);
  	var zeleft1 = screen.width/2;
  	var zeleft2 = zeleft1-(zewidth/2);
  	
  	window.open(page,nom,'resizable=no, location=no, width='+zewidth+', height='+zeheight+', left='+zeleft2+', top='+zetop2+', menubar=no, status=no, scrollbars=no, menubar=no');
  },
  
  Newsletter: function(){
    
    var oFormulaire = '<form method="post" action="#" style="width:190px;height:29px;position:absolute;top:368px;left:18px;">';
    oFormulaire += '<label style="display:block;background:#000;width:185px;height:13px;font-weight:bold;font-size:11px;color:#00C4FC;padding-left:5px;">newsletter :</label>';
    oFormulaire += '<input type="text" id="email-newsletter" value="Envoyez votre adresse email" onfocus="TripHarmonic.FocusNewsletter(this);" onblur="TripHarmonic.BlurNewsletter(this);" style="position:absolute;top:15px;height:13px;width:160px;font-size:11px;padding-left:4px;" />';
    oFormulaire += '<a href="#" onclick="javascript:TripHarmonic.submitNews(); return false;">';
    oFormulaire += '<img src="/wp-content/themes/tripharmonic/img/btn-ok.gif" alt="Envoyer" style="position:absolute;left:167px;top:15px;width:23px;height:13px;cursor:pointer;" />';
    oFormulaire += '</a>';
    oFormulaire += '</form>';
    
    $('container').innerHTML += oFormulaire;
    
  },
  
  submitNews: function(){
      
      if($('email-newsletter').value == '' || $('email-newsletter').value == 'Envoyez votre adresse email'){
        alert('Entrez une adresse email.');
      }
      
      else if($('email-newsletter').value.indexOf('@') == -1){
        alert('Adresse email invalide.');
      }
      
      else{
        
        var oEmail = $('email-newsletter').value;
        
        //Création de l'objet Ajax
        var requete = null;
        try{requete = new XMLHttpRequest();}
        catch(essaimicrosoft){
          try{requete = new ActiveXObject("Msxml2.XMLHTTP");}
          catch(autremicrosoft){
            try{requete = new ActiveXObject("Microsoft.XMLHTTP");}
            catch(echec){requete = null;}
          }
        }
        
        if (requete == null){alert("Objet non créé");}
        
        //Url du Fichier a appeler
        var url = '/newsletter/news.php?email=' + oEmail + "&killcache=" + new Date().getTime();
        	
        //Paramètrage Requete
        requete.open("get", url, true);
        requete.send(null);
      
        alert('Votre email a bien été envoyé. Merci.');
        
      }
        
  },
  
  FocusNewsletter: function(obj){
    if(obj.value == 'Envoyez votre adresse email'){
      obj.value = '';
    }
  },
  
  BlurNewsletter: function(obj){
    if(obj.value == ''){
      obj.value = 'Envoyez votre adresse email';
    }
  },
  
  CountrySlider: function(){
  
    var BtnAction = $$('#new-list-pays ul li a')[0];
    var FirstList = $$('#new-list-pays ul')[0];
    var SubList = $$('#new-list-pays ul li ul')[0];
    
    BtnAction.slider = new Fx.Slide(SubList, {duration: 500});
    BtnAction.slider.slideOut();
    BtnAction.statut = 'close';
    
    BtnAction.onclick = function(){
      
      if(this.statut == 'close'){
        this.statut = 'open';
        this.slider.slideIn();
      }
      
      else{
        this.statut = 'close';
        this.slider.slideOut();
      }
      
      return false;
      
    }
    
    FirstList.addEvent('mouseleave', SliderAction);
    
    function SliderAction(){
    
      BtnAction.statut = 'close';
      BtnAction.slider.slideOut();
    
    }
    
    
  },
  
  DebugMacOs: function(){
  	
    dbugNav();
  	dbugHome();
  	dbugVoyage();
  	dbugPodcast();
  	dbugAtelier();
  	dbugPratik();
  	dbugLiens();
  	dbugPartenR();

    function dbugNav(){
    	var navPartenR = $('navpartenair');
    	navPartenR.getElementsByTagName('a')[0].style.borderRight = '8px solid #000;';
    }

    function dbugHome(){
    	var bodyHome = $('home');
    	if(!bodyHome) return;
    	
    	var navPartenR = $('navpartenair');
    	navPartenR.getElementsByTagName('a')[0].style.borderRight = '4px solid #000;';
    	
    }

    function dbugVoyage(){
    	var bodyCarnet = $('carnet');
    	if(!bodyCarnet) return;
    	
    	var navPartenR = $('navpartenair');
    	navPartenR.getElementsByTagName('a')[0].style.borderRight = '8px solid #000;';
    }

    function dbugPodcast(){
    	var bodyPodcast = $('podcast');
    	if(!bodyPodcast) return;
    	
    	var navPartenR = $('navpartenair');
    	navPartenR.getElementsByTagName('a')[0].style.borderRight = '3px solid #000;';
    }

    function dbugAtelier(){
    	var bodyAtelier = $('atelier');
    	if(!bodyAtelier) return;
    	
    	var navPartenR = $('navpartenair');
    	navPartenR.getElementsByTagName('a')[0].style.borderRight = '23px solid #000;';
    }

    function dbugPratik(){
    	var bodyPratik = $('pratique');
    	if(!bodyPratik) return;
    	
    	var navPartenR = $('navpartenair');
    	navPartenR.getElementsByTagName('a')[0].style.borderRight = '4px solid #000;';
    }

    function dbugLiens(){
    	var bodyLiens = $('liens');
    	if(!bodyLiens) return;
    	
    	var navPartenR = $('navpartenair');
    	navPartenR.getElementsByTagName('a')[0].style.borderRight = '2px solid #000;';
    }

    function dbugPartenR(){
    	var bodyPartenR = $('partenaire');
    	if(!bodyPartenR) return;
    	
    	var navPartenR = $('navpartenair');
    	navPartenR.getElementsByTagName('a')[0].style.borderRight = '27px solid #000;';
    }
  
  }
  
};

if(window.opera) window.addEvent('load', TripHarmonic.Start);
else window.addEvent('domready', TripHarmonic.Start);