$(document).ready(function(){initAjAX();});
function initAjAX(){//traverse all 'A' tags
  $('a[class!=skipMe]').each(initA);
}
function initA(_i, _a){//init this 'a' tag
  if(!$(this).hasClass("skipMe")){
    var href = String($(this).attr('href'));
    var substr = href.substring(0,7);
    if( substr== "http://")
      return false;

    $(this).click(function(_e){
      _e.preventDefault();
      processA($(this));
      //return false;
    });
  }
}
window.loading = false;
function processA(_a){
  var href = String(_a.attr('href'));
  loadContent(href);
}
function loadContent(_href){
  while(_href.substring(0, 5) == "#load")
    _href = _href.substring(6);
  if(_href != "")
    location.hash = "load|"+_href;

  window.lastHash = location.hash;
  if(window.addthis) window.addthis = null;
 	
	_href= _href.replace("http://www.","");
	_href= _href.replace(".php","");
	_href= _href.replace(".js","");
	_href= _href.replace("http://","");
  if(_href.indexOf(".html") == -1)
  	_href += ".html";
  if(_href.indexOf("?") == -1)
    _href += "?ajax=true";
  else
    _href += "&ajax=true";
  $("#myContent2").fadeOut();
  startWaiting();
  $('html, body').animate({scrollTop:0}, "fast");
  loading = true;
  $.get(_href, ajaxLoaded);
	
	
}

function startWaiting(){
  $("#waiter").css({display : 'block'});
  window.waitInterval = setInterval(wait, 200);
  $("#waiter").text("*");
}
function wait(){
  var content = $("#waiter").html();
  if(content == "* * *") $("#waiter").html("*");
  else $("#waiter").html(content + " *");
}
function ajaxLoaded(_data, _textStatus, _xhr){
  $("#waiter").css({display : 'none'});
  $('#myContent2').html(_data);
  $('#myContent2 a[class!=skipMe]').each(initA);
  var page = _xhr.getResponseHeader('menuItem');
  var isPortfolio = _xhr.getResponseHeader('isPortfolio') == "true";
  var index;
  switch(page){
    case "detail_post" :
    case "page" :
      index = !isPortfolio? 0 : 1;
      break;
    case "cv" :
    case "cv_en" :
      index = 1;
      break;
    case "telechargements" :
      index = 2;
      break;
    case "connexion" :
    case "compte" :
      index = 3;
      break;
    case "contact" :
      index = 4;
      break;
    default :
      index = 0;
      break;
  }
  $('#myMenu .activated').removeClass('activated');
  $('#menu' + index).addClass('activated');
  initMenu();
  initTitles();
  scrollAll();
  loading = false;

  $("#myContent2").fadeIn(function(){clearInterval(window.waitInterval);});
}
function scrollAll(){
  $(function(){$('#loginForm').hide();});

  $(function(){$('#add_comment_form').hide();});

  $(function(){$('#cv2').hide();});
  $(function(){$('#cv3').hide();});
  $(function(){$('#cv4').hide();});
  $(function(){$('#cv5').hide();});
  $(function(){$('#cv6').hide();});
  $(function(){$('#cv7').hide();});

  $(function(){$('#cv8').hide();});
  $(function(){$('#cv9').hide();});
  $(function(){$('#cv10').hide();});
  $(function(){$('#cv11').hide();});
  $(function(){$('#cv12').hide();});

  $(function(){$('#cv13').hide();});
  $(function(){$('#cv14').hide();});
  $(function(){$('#cv15').hide();});
  $(function(){$('#cv16').hide();});
  $(function(){$('#cv17').hide();});
  $(function(){$('#cv18').hide();});
}
function initMenu(){
  $('#myMenu a').each(function(){
    $(this).stop();
    $(this).attr("style", "");
		$(this).parent().css({'background':'none'});

		$(this).parent().next().css({'background':'none'});
		$(this).parent().prev().css({'background':'none'});

  });
  $("#myMenu .activated").each(function(){
	  $(this).css({'color':'#ffffff'});
  	$(this).parent().css({'background':'url(imgs/glow1.png) top center repeat-x'});

		$(this).parent().prev().css({'background':'url(imgs/glow0.png) top right no-repeat'});
		$(this).parent().next().css({'background':'url(imgs/glow2.png) top left no-repeat'});
  });
  $("#myMenu a").hover(function() {
     	if( $(this).hasClass( 'activated' ) == false){
        $(this).stop().animate({color:'#ffffff'},600);

    		$(this).parent().css({'background':'url(imgs/glow1.png) top center repeat-x'});

    		$(this).parent().prev().css({'background':'url(imgs/glow0.png) top right no-repeat'});
    		$(this).parent().next().css({'background':'url(imgs/glow2.png) top left no-repeat'});
    	}
    },function() {
    	if( $(this).hasClass( 'activated' ) == false){
    		$(this).stop().animate({color:'#00c6ff'}, 600);
    		$(this).parent().css({'background':'none'});

    		$(this).parent().next().css({'background':'none'});
    		$(this).parent().prev().css({'background':'none'});
    	}
    });

};
function initTitles(){
  $(".titre a").hover(function() {
      $(this).stop().animate({color:'#ffffff'},600);
    },function() {
  		$(this).stop().animate({color:'#00c6ff'}, 600);
    });
}
function init(){
  initMenu();
  initHM();
  initTitles();
  scrollAll();
  $.ajax({dataType : "html"});
}
$(document).ready(init);
