$(document).ready(function(){ 
  //initScroller('focus', 300);
  //animScroller('focus', 0) ;
  initVisual();
  initTables();
  initPager('market', 4, "&nbsp;");
  initPager('news_all', 10, "&nbsp;");
  initPager('press_all', 10, "&nbsp;");
  initPopupPages();  
  //initFocusAgenda();
  initSkycrapers();
});

var activeScroll = true;

function initScroller(obj, width) {	
  var nb = $('.'+ obj +' .liItem').length;
  $('.'+ obj +' .liItem').css({visibility:'visible'});
  $('.'+ obj +' .liItem').css({position:'absolute', overflow:'hidden', width:width+'px' });
  if ( nb == 1 )
      return;
  for (i = 0 ; i < nb; i++) {
    $('.' + obj + ' .nav').append("<a  style='cursor:pointer;'>"+(i+1)+"</a><span>&nbsp;</span>");
    var link = $('.' + obj + ' .nav a').eq(i);
    link.click(function() {	
    activeScroll = false;	
      var num = parseInt(this.innerHTML);
      showScroller(obj, num - 1);
    });
  }
  $('.'+ obj +' .liItem').hide();	 
}

function showScroller(obj, element) {
  $('.' + obj + ' .nav a').removeClass('on');    
  $('.' + obj + ' .nav a:eq(' + element + ')').addClass('on');    
  $('.' + obj + ' .liItem').fadeOut(100);
  setTimeout(function() { 
      $('.' + obj + ' .liItem:eq(' + element + ')').fadeIn(100);    
    }, 1500);
}

function animScroller(obj, element) {		
  if ( !activeScroll )
    return;
  // on recupere le nombre d'item
  var nb = 0;
  if ( $('.' + obj + ' .liItem') )
    nb = $('.' + obj + ' .liItem').length;
  var navElement = $('.' + obj + ' .nav');
  var nextElement = (element + 1 );   
  if (nextElement <= nb) {
    $('.' + obj + ' .nav a').removeClass('on');    
    $('.' + obj + ' .nav a:eq(' + element + ')').addClass('on');    
    $('.' + obj + ' .liItem').fadeOut(200);
    setTimeout(function() { 
      $('.' + obj + ' .liItem:eq(' + element  + ')').fadeIn(200);    
      if ( nb == 1 )
	return;
      setTimeout(function() { animScroller(obj, nextElement, navElement) }, 6000);
    }, 150);
  } else {    
    if ( nb > 0 )
      animScroller(obj, 0, navElement);
  };
}

var currentPageNumber = {};

function initPager(element, nbItemsPerPage, separator, selectedIndex, anchor) {
  var nbItems = $('.' + element + ' .liItem').length;
  var nbPages = Math.ceil(nbItems/nbItemsPerPage);

  if ( nbPages <= 1 )
    return;

  // Init navigation
  for (i = 0 ; i < nbPages; i++) {
    var sep = ( i < nbPages - 1 ? separator : '' );
    $('.' + element + ' .nav').append("<a style='cursor:pointer;' page='" + (i + 1) + "'>" + (i + 1) + "</a>" + sep);
    var link = $('.' + element + ' .nav a').eq(i);
    link.click(function() {
      if ( anchor ) {
	location.hash = "#" + anchor;
      }
      showPage(element, nbItemsPerPage, this.innerHTML);
    });
  }

  // Hide all items
  for (i = 0 ; i < nbItems; i++) {
    $('.' + element + ' .liItem').eq(i).hide();
  }

  // Calc current page number
  var currentPage = 1;
  if( selectedIndex ) 
    Math.ceil(selectedIndex/nbItemsPerPage);

  // Show first page
  showPage(element, nbItemsPerPage, currentPage);
}

function showPage(element, nbItemsPerPage, pageNumber) {
  // Hide page
  if ( currentPageNumber && currentPageNumber[element] ) {
    $('.' + element + ' .nav a').eq(currentPageNumber[element] - 1).removeClass('on');
    var start = (nbItemsPerPage * (currentPageNumber[element] - 1)) + 1;
    for (i = start; i < start + nbItemsPerPage; i++) {	
      $('.' + element + ' .liItem').eq(i - 1).hide();
    }  
  }

  // Show page
  currentPageNumber[element] = pageNumber;
  $('.' + element + ' .nav a').eq(pageNumber - 1).addClass('on');
  var start = (nbItemsPerPage * (pageNumber - 1)) + 1;
  for (i = start; i < start + nbItemsPerPage; i++) {	
    $('.' + element + ' .liItem').eq(i - 1).fadeIn("slow");
  }  

}

function initPopupPages() {
  $('.html a').click(function() {	
    var link = this.href;    
    if ( this.target == "_popup" ) {
      if ( link.indexOf("?") == -1 )
	link += "?";
      link += "TB_iframe=true&height=480&width=525&modal=true";
      tb_show(null, link, false);
      this.blur();  
      return false;
    } else {
      return true;
    }
  });
}

function initFocusAgenda() {
  $('.focus').css('visibility', 'visible');
  $('.agenda').css('visibility', 'visible');
  var fh = $('#focus').height();
  var ah = $('#agenda').height();  
  if ( fh > ah ) {
    fh = ( $.browser.msie ? fh - 1 : fh - 4 );
    $('#agenda').css('height', fh);
  } else {    
    $('#focus').css('height', ah);
  }  
}

function initVisual() {
  if ( $('.visual').length ) {
    var image1 = new Image();
    $(image1).attr('src', $('.visual1-over').attr('src'));
    var image2 = new Image();
    $(image2).attr('src', $('.visual2-over').attr('src'));
    if ( !$.browser.msie ) {
      $('.visual1').mouseover(function() { $('.visual1-over').fadeIn(); });
      $('.visual1-over').mouseout(function() { $('.visual1-over').fadeOut(); });
      $('.visual2').mouseover(function() { $('.visual2-over').fadeIn(); });
      $('.visual2-over').mouseout(function() { $('.visual2-over').fadeOut(); });
    } else {
      $('.visual1').mouseover(function() { $('.visual1-over').show(); });
      $('.visual1-over').mouseout(function() { $('.visual1-over').hide(); });
      $('.visual2').mouseover(function() { $('.visual2-over').show(); });
      $('.visual2-over').mouseout(function() { $('.visual2-over').hide(); });
    }
  }
}

function showPopup(url, width, height) {
  var params = "";
  params += "innerwidth  = " + width  + ", ";
  params += "innerheight = " + height + ", ";
  params += "width       = " + width  + ", ";
  params += "height      = " + height + ", ";
  params += "dependent   = yes, ";
  params += "location    = no , ";
  params += "menubar     = no , ";
  params += "status      = no , ";
  params += "toolbar     = no , ";
  params += "scrollbars  = yes, ";
  params += "resizable   = yes, ";    
  params += "top         = " + 10 + ", ";
  params += "left        = " + ((screen.width  - width)  / 2);
  var win = window.open(url, "win", params);
}

function initTables() {
  $(".html .table").each(function(i) {
      $(this).attr("border", 0);
      $(this).find("tr:even").css("background", "#d2d1d1");
      $(this).find("tr:odd").css("background", "#eaeaea");
    });

  $(".html .table-title").each(function(i) {
      $(this).attr("border", 0);
      $(this).find("tr:even").css("background", "#d2d1d1");
      $(this).find("tr:odd").css("background", "#eaeaea");
      $(this).find("tr:eq(0)").css("background", "white");
      //$(this).find("tr:eq(0)").css("color", "white");
      //$(this).find("tr:eq(0) h1").css("color", "white");
  });


  $(".html .table1").each(function(i) {
      $(this).attr("border", 0);
      $(this).find("tr:even").css("background", "#d2d1d1");
      $(this).find("tr:odd").css("background", "#eaeaea");
      $(this).find("tr td").css("font", "11px Arial");
      $(this).find("tr td span").css("font", "11px Arial");
      $(this).find("tr:eq(0)").css("background", "white");
      $(this).find("tr:eq(0) td").css("whiteSpace", "nowrap");
      $(this).find("tr:eq(0)").css("font", "Bold 12px Arial");
      $(this).find("tr:eq(0) span").css("font", "Bold 11px Arial");
    });
}

function initSkycrapers() {
  var index = 0;
  var nb = $('.skycraper').length;
  var randomnumber = Math.floor(Math.random() * nb)
 
  $('.skycraper').hide();
  $('.skycraper:eq(' + randomnumber + ')').show();
  var url = $('.skycraper:eq(' + randomnumber + ')').attr('url');
  $.ajax({url:url});

  var foo = function() {
    var nextIndex = ( index < nb - 1 ? index + 1 : 0 );
    $('.skycraper:eq(' + index + ')').fadeOut(1500, function() {
	$('.skycraper:eq(' + nextIndex + ')').fadeIn(1500);
	//var url = $('.skycraper:eq(' + nextIndex + ')').attr('url');
	//$.ajax({url:url});
    });
    index = nextIndex;
    setTimeout(foo, 13500);    
  }
  
  setTimeout(foo, 12000);
}

