var homepageCategoryIndex = 0;

$(document).ready( function() {
    if ( $(".home-thema").html() ) {
      initThemes();
    } else {
      if ( $(".thema-middle") ) {
	initDiseaseThemes();
      }
    }
    
    if ( $(".question_list") ) {
      initQuestions();
    }
    
    if ( $(".popcroix") ) {
      $(".popcroix").click(function() {
	  parent.tb_remove();
	});
    }
	
    if ( $(".goodies") ) {
      initGoodies();
    }
});

function initThemes() {
  var _height = $(".thema-middle ul").height();
  var animate = false;

  $(".thema-bottom .up").click( function() {
      if ( animate ) retrn;
      animate  = true;
      $(".thema-middle ul").css("position", "relative");
      var top = parseInt($(".thema-middle ul").css("top"));      
      if ( (_height + top) >= 192 )
        $(".thema-middle ul").animate({top: (top - 192) + "px"}, 1000, "easeOutQuad", function() { animate = false });
      else
	animate = false;
  });

  $(".thema-bottom .down").click( function() {
      if ( animate ) retrn;
      animate  = true;      
      $(".thema-middle ul").css("position", "relative");
      var top = parseInt($(".thema-middle ul").css("top"));
      if ( top < 0 )
	$(".thema-middle ul").animate({top: (top + 192) + "px"}, 1000, "easeOutQuad", function() { animate = false });
      else
	animate = false;
  });

  $(".thema-middle li a").click( function() {
      var nextCategoryIndex = $(this).attr("name")	
      if ( nextCategoryIndex == homepageCategoryIndex )
	return false;
      var currentLi = $(".thema-middle li:eq(" + homepageCategoryIndex + ")");
      var currentText = $(".category_text_" + homepageCategoryIndex);
      var currentArrow = $(".category_arrow_" + homepageCategoryIndex);
      var nextLi = $(".thema-middle li:eq(" + nextCategoryIndex + ")");
      var nextText = $(".category_text_" + nextCategoryIndex);
      var nextArrow = $(".category_arrow_" + nextCategoryIndex);      
      var onend = function() {
	nextText.show();
	nextArrow.fadeIn();
      }     
      var classname =  currentLi.attr("class");
      if ( classname.indexOf('first') == 0) 
	currentLi.attr("class", "first");
      else
	currentLi.attr("class", "");
      currentText.hide();
      currentArrow.fadeOut("", onend);
      classname =  nextLi.attr("class");
      if ( classname.indexOf('first') == 0 ) 
	nextLi.attr("class", "first selected");
      else
	nextLi.attr("class", "selected");
      homepageCategoryIndex = nextCategoryIndex;
      return true;
  });
}

function initDiseaseThemes() {
  var _height = $(".content-maladie .scroller").height();

  $(".content-thema-bottom .up").click( function() {
      $(".content-maladie .scroller").css("position", "relative");
      var top = $(".content-maladie .scroller").css("top");      
      if ( top == "auto" )
	top = 0;
      else
	top = parseInt(top);
      if ( (_height + top) >= 458 )
	$(".content-maladie .scroller").css("top", (top - 458) + "px");
  });

  $(".content-thema-bottom .down").click( function() {
      $(".content-maladie .scroller").css("position", "relative");
      var top = parseInt($(".content-maladie .scroller").css("top"));
      if ( top < 0 )
	$(".content-maladie .scroller").css("top", (top + 458) + "px");
  });

  $(".thema-maladie li a").click( function() {
      $(".content-maladie .scroller").css("top", "0px");
      var nextCategoryIndex = $(this).attr("name")	
      if ( nextCategoryIndex == homepageCategoryIndex )
	return false;
      var currentLi = $(".thema-maladie li:eq(" + homepageCategoryIndex + ")");
      var currentText = $(".category_text_" + homepageCategoryIndex);
      var currentArrow = $(".category_arrow_" + homepageCategoryIndex);
      var nextLi = $(".thema-maladie li:eq(" + nextCategoryIndex + ")");
      var nextText = $(".category_text_" + nextCategoryIndex);
      var nextArrow = $(".category_arrow_" + nextCategoryIndex);      
      var onend2 = function() {
	nextText.show();
	nextArrow.fadeIn();
	_height = $(".content-maladie .scroller").height();
      }     
      var classname =  currentLi.attr("class");
      if ( classname.indexOf('first') == 0) 
	currentLi.attr("class", "first");
      else
	currentLi.attr("class", "");
      currentText.hide();
      currentArrow.fadeOut("", onend2);
      classname =  nextLi.attr("class");
      if ( classname.indexOf('first') == 0 ) 
	nextLi.attr("class", "first selected");
      else
	nextLi.attr("class", "selected");
      homepageCategoryIndex = nextCategoryIndex;
      return true;
  });
}

function initQuestions() {
  $(".question_list li div").css("display", "none");
  $(".question_list li h3 a").click( function() {
      var li = $(this).parent().parent();
      var index = $(".question_list li").index(li);
      selectQuestion(index);
    });
  var li = $(".question_list .select");
  li.find("div").slideDown();
}

function selectQuestion(index) {
  var onend = function() {
    $(".question_list li").attr("class", "");
    var li = $(".question_list li:eq(" + index + ")");
    li.attr("class", "select");
    li.find("div").slideDown();
  }
  
  var nb = $(".question_list li div").length;
  for ( i = 0; i < nb; i++) {
    var div = $(".question_list li div:eq("+i+")");
    if ( div.css("display") != "none" ){
      div.slideUp("");
    }
  }
  onend();
}

function plus(){
  var ous = document.getElementById('plus');
  ous.value++;
}

function warning_open(url) {
  window.open(url);
  parent.tb_remove();
}

function trapmail(url) {
  var email = document.getElementById('email').value;
  url = url + "?email=" + email+ "&width=500&height=300&keepThis=true&TB_iframe=true&modal=true";
  tb_show("", url);
}

function asso(url) {
  var region = document.getElementById('region').value;
  url = url + "?region=" + region + "&width=800&height=500&keepThis=true&TB_iframe=true&modal=true";
  tb_show("", url);
}

function video(url) {
  $(document).ready( function() {
      url = url + "?width=800&height=500&keepThis=true&TB_iframe=true&modal=true";
      tb_show("", url);
    });
}

function simplevideo(url) {
  $(document).ready( function() {
      url = url + "?width=550&height=450&keepThis=true&TB_iframe=true&modal=true";
      tb_show("", url);
    });
}

var currentGoodiesIndex = 4;
var currentGoodiesPosition = 1;

function initGoodies() {
  var elem1 = $('.goodies .elem1');
  var elem2 = $('.goodies .elem2');
  var elem3 = $('.goodies .elem3');
  var elem4 = $('.goodies .elem4');
  $('.goodies .element').hide();
  $('.goodies .pos1').html(elem1.html());
  $('.goodies .pos2').html(elem2.html());
  $('.goodies .pos3').html(elem3.html());
  $('.goodies .pos4').html(elem4.html());
  setInterval("nextGoodies()", 8000);
}


function nextGoodies() {
  var nb = $('.goodies .element').length;

  var cell1 = ( ( currentGoodiesPosition == 1 ) ? 'pos1' : 'pos3' );
  var cell2 = ( ( currentGoodiesPosition == 1 ) ? 'pos2' : 'pos4' );
    
  $('.goodies .' + cell1).hide();
  $('.goodies .' + cell2).hide();

  var content1 =  $('.goodies .elem' + ((currentGoodiesIndex % nb) + 1));
  var content2 =  $('.goodies .elem' + ((currentGoodiesIndex % nb) + 2));
  $('.goodies .' + cell1).html(content1.html());
  $('.goodies .' + cell2).html(content2.html());
  $('.goodies .' + cell1).fadeIn("slow");
  $('.goodies .' + cell2).fadeIn("slow");
  
  currentGoodiesPosition = (currentGoodiesPosition == 1 ? 2 : 1);
  currentGoodiesIndex = currentGoodiesIndex + 2;
}

function showGoodies(url) {
  tb_show("", url);
}

function imprime() {
if (typeof(window.print) != 'undefined')
 { window.print(); }
}

var currentZoom = [];
function zoomIn(){  
  var index = 0;
  $(".resize").each(function(){
      var size = parseInt($(this).css('font-size'));
      if (!currentZoom[index])
	currentZoom[index] = size;
      if (size < (currentZoom[index] + 3)){
	$(this).css('font-size', (size + 1) + 'px')
      }
      index++;
    });
}

function zoomOut(){
  var index = 0;
  $(".resize").each(function(){
      var size = parseInt($(this).css('font-size'));
      if ( currentZoom[index] && (size > currentZoom[index]) ){
	$(this).css('font-size', (size - 1) + 'px')
      }
    });
}

function popupGame(url)
{
	window.open(url,'_blank','toolbar=0,location=0,directories=0,status=0,scrollbars=1,resizable=0,copyhistory=0,menuBar=0,width=600,height=600 left=100 top=100') 
}

function warning(url) {
  var url2 = "/crohn/web/warning?url=" + url + "&width=500&height=200&keepThis=true&TB_iframe=true&modal=true";
  tb_show("", url2);
}

