var sector_animation;
var original_z_index=20;
var height_z_index=32;
var original_src='';

$(document).ready(function(){
  setup();
  $('a.pages_parent').click(replaceContent);
  //$('a.home_quick_link').click(handleQuickLinkContent);
  printPage();
  fontResizing();
});

function replaceContent() {
  if(!$(this).attr('href') || $(this).attr('href').length==0 || (typeof(root_url)!== 'undefined' && !root_url )) {
    return false;
  }

  //on reste sur la page d'accueil si le valeur is_root_page=true
  if(is_root_page) {
    getAndReplacePageContent($(this).attr('href'));
    selectMenuItemActive($(this));
  }
  else {
    var val=$(this).parents('span:first').attr('class');

    var form_el=$('<form>');
    form_el.attr('method', 'post');
    form_el.attr('action', root_url);

    var input=$('<input/>');
    input.attr('type', 'hidden');
    input.attr('name', 'dpid');
    input.attr('value', val);
    input.appendTo(form_el);

    form_el.appendTo($(this).parents(':first'));
    form_el.submit();
  }
  return false;
  
}

function getAndReplacePageContent(page_url) {
  $.ajax({
    url: page_url,
    global: true,
    type: "GET",
    processData: false,
    dataType: "html",
    beforeSend: function() {$('.colonne_gauche').before('<div id="loading_main"></div>');},
    complete: function() {$('#loading_main').remove();},
    error:function (xhr){
      alert('Error : '+xhr.status+' '+xhr.statusText);
    },
    success: function(data) {
      var sub_navigation=$(data).find('#horizontal_navigation');
      var sub_html=$(data).find('#exportable_content');
      //navigation si le contenu n'est pas vide
      if(sub_navigation.html() && sub_navigation.html().length>0) {
        $('#horizontal_navigation').html(sub_navigation.html());
        if(!$('#horizontal_navigation').hasClass('no_bg')) {
          $('#horizontal_navigation').addClass('no_bg');
        }
      }
      //contenu si le contenu n'est pas vide, on remplace le contenu actuelle
      if(sub_html.html() && sub_html.html().length>0) {       
        $('#root_slide_cont').html(sub_html);

        $('.colonne_gauche').html(sub_html);
        $('.colonne_gauche').css('height', 'auto');
        $('.colonne_gauche').css('overflow', 'visible');        
      }
      $('#loading_main').remove();
    }
  });
  return false;
}

//loads link content into the home page
function handleQuickLinkContent() {
  if(!$(this).attr('href') || $(this).attr('href').length==0) {
    return false;
  }

  $.ajax({
    url: $(this).attr('href'),
    global: true,
    type: "GET",
    processData: false,
    dataType: "html",
    beforeSend: function() {$('.colonne_gauche').before('<div id="loading_main"></div>');},
    complete: function() {$('#loading_main').remove();},
    error:function (xhr){
      alert('Error : '+xhr.status+' '+xhr.statusText);
    },
    success: function(data) {
      //var sub_html=$(data).find('#exportable_content').html();
      //$('.slideshow').html(sub_html);
      var sub_html=$(data).find('#exportable_content');
      //si le contenu n'est pas vide, on remplace le contenu actuelle
      if(sub_html.html() && sub_html.html().length>0) {
        $('.slideshow').html(sub_html);
        $('.slideshow').css('height', 'auto');
        $('.slideshow').css('overflow', 'visible');
      }
      $('#loading_main').remove();
    }
  });
  return false;
}

//animate sector icons by hover
function sectorOver(el) {
  var img_obj;
  var description_title='';
  var description_text='';
  //store original z-index
  original_z_index=$(el).parent().css('z-index');
  img_obj=$(el).find('img');
  original_src = img_obj.attr("src").match(/[^\.]+/);

  $(el).parent().css({'z-index' : height_z_index});
  img_obj.attr('src', original_src+'_over.gif');
  
  //add description
  description_title=$(el).next('p.description_title').html();
  description_text=$(el).next().next('p.description').html();

  if(description_title) {$('#sector_description p strong').html(description_title);}
  if(description_text) {$('#sector_description p span').html('.&nbsp;'+description_text);}
}

function sectorOut(el) {
  $(el).parent().css({'z-index' : original_z_index});
  var img_obj=$(el).find('img');
  img_obj.attr('src', original_src+'.gif');
}

function showPopup(){
  $('#popup_container').lightbox_me({
    centered: true,
    lightboxSpeed: 'normal',
    closeSelector: '.btn_fermer'
    });
  return false;
}

function getTestimony(el) {
  //on remplace le nom du titre
  var testimony='';
  var href=$(el).attr('href');
  testimony=getPopupContent(href, 'popup_main_container');

  if(testimony && testimony.length>0) {
    $('#popup_main_container').html(testimony);
  }
  
  return false;
}

function getPopupContent(href, search_tag_id) {
  var ret_val='';
  $.ajax({
    url: href,
    global: true,
    type: "POST",
    async: false,
    dataType: "html",
    error:function (xhr){
      alert('Error : '+xhr.status+' '+xhr.statusText);
    },
    success: function(data) {
      var sub_html=$(data).find('#'+search_tag_id);
      //si le contenu n'est pas vide, on remplace le contenu actuelle
      if(sub_html.html() && sub_html.html().length>0) {
        ret_val=sub_html.html();
      }
    }
  });
  return ret_val;
}

function printPage() {
  $('.print_page').click(function() {
    window.print();
    return false;
  });
}

function fontResizing() {
  var multiplicator=1.1;
  var min_font_size=8;
  var max_font_size=14;

  $("a.change_font_size").click(function(){
    var $mainText = $('body');
    var currentSize = $mainText.css('font-size');
    // parse the number value out of the font size value, set as a var called 'num'
    var num = parseInt(currentSize, 10);
    //var unit = currentSize.slice(-2);

    if (this.id == 'link_increase'){num = num +1 ;}
    else if (this.id == 'link_decrease'){num = num -1 ;}
    if(num<=max_font_size && num>=min_font_size) {
      $mainText.css('font-size', num);
    }
    return false;
  });
}

function selectMenuItemActive(active_el) {
  var menu_link_elements=$('.menu').find('a.pages_parent');
  var active_el_parent_class=active_el.parents('span:first').attr('class');

  //remove active class
  menu_link_elements.each(function () {
    if ($(this).parents('span:first').attr('class')!=active_el_parent_class) {
      $(this).removeClass('active');
    }
    else {
      $(this).addClass('active');
    }
  });
}

function redirectAndReplace(el) {
  if(is_root_page) {
    getAndReplacePageContent($(el).attr('href'));
    selectMenuItemActive($(el));
  }
  else {
    var val=$(el).parents('span:first').attr('class');

    var form_el=$('<form>');
    form_el.attr('method', 'post');
    form_el.attr('action', root_url);

    var input=$('<input/>');
    input.attr('type', 'hidden');
    input.attr('name', 'dpid');
    input.attr('value', val);
    input.appendTo(form_el);

    form_el.appendTo($(el).parents(':first'));
    form_el.submit();
  }
  return false;
}// menuvar timers = new Array();function menu_over(id_menu,langue){	$("#menu_cache"+id_menu).css('color', '#336e97');	$("#menu_cache"+id_menu).css('background', '#e5e5e5');	$("#menu_cache"+id_menu).css('font-weight', '700');	clearTimeout(timers[id_menu+langue]);	this.timers[id_menu+langue] = setTimeout("afficher(" + id_menu +", '"+ langue +"')",1);}function menu_out(id_menu,langue){		clearTimeout(timers[id_menu+langue]);	this.timers[id_menu+langue] = setTimeout("cacher(" + id_menu +", '"+ langue +"')",30);}function afficher(id_menu,langue){	$("#menu"+id_menu+langue).show();	$("#menu_cache"+id_menu).show();}function cacher(id_menu,langue){	$("#menu_cache"+id_menu).css('color', '#ffffff');	$("#menu_cache"+id_menu).css('background', 'none');	$("#menu_cache"+id_menu).css('font-weight', '100');	$("#menu"+id_menu+langue).hide();	//$("#menu_cache"+id_menu).hide();}// fin menu

