// variable pour affichage des categories de programmes
var chainechargee   = 18;
var chaineaffichee  = 0;
var rechargement  = 0;
var affichecourant  = 0;
var max       = 10;
var posabs      = 150;
var posx      = 0;

// Après chargement complet de la page
$().ready( function()
{
  // initialisation diverses
  uploadchaine(-1);

  // Valeurs css dynamique
  jQuery( '#progtvgrille #chaineaffichage, #progtvgrille #grilleaffichage' ).css( 'width', '7084px');
  jQuery( '#progtvgrille #grille' ).css('height','450px');
  jQuery( '#progtvgrille #grilleaffichage .colonnep').css( 'width', '158px');
  jQuery( '#progtvgrille #chaineaffichage .colonnec').css( 'width', '157px');
  jQuery( '#progtvgrille #grilleaffichage .colonnep .attente' ).css('marginTop','794px' );

  // Div draggable
  jQuery( '#progtvdetail' ).css('cursor', 'move').draggable();

  scrollGrilleLoad();
  // Scroll auto de la grille
  jQuery( '#progtvgrille #grille' ).scroll( function() { scrollGrille(); });
});

// fonction gérant les upload appel Ajax
function uploadchaine(posx)
{
  exchaineaffichee = chaineaffichee;

  if( posx >= 0 )
  {
    test = ( posx - posx%160 ) / 160;
    chaineaffichee = test;
  }
  else
  {
    test = 44 - chainechargee;
    chaineaffichee = 0;
  }
  
  if( chainechargee < test + chainechargee ) 
  {
    var stop = 44 -1;

    for ( k = chainechargee; k <= stop ; k++ )
    {
      var url = '/index.php/wpProgtvModule/uploadprogrammechaine/num/'+k+'/ts1/1280721600/ts2/1280808000';
      eval( "jQuery.ajax({ type: 'post', url: '"+url+"', success: function(msg) { jQuery('#chaine_"+k+"').html(msg).show(); } });" );
    }

    chainechargee = test + chainechargee;
  }

  if(exchaineaffichee != chaineaffichee) 
  {
    if(exchaineaffichee > chaineaffichee)
    {
      pas = -1;
      debut = exchaineaffichee - 1;
      fin = chaineaffichee - 7
    }
    else
    {
      pas = +1;
      debut = exchaineaffichee + 1;
      fin = chaineaffichee + 7
    }  
  }
}

function hideDetail()
{
  jQuery('#progtvdetail').html( '<img src="http://intranet.teleprogrammes.fr/themes/defaut/images/chrono.gif" alt="Chrono" />' );
  jQuery('#progtvdetail').css ( 'display', 'none');
}

function majPositionDiv ( numchaine , nbprog )
{
  margin = (28 - nbprog) * 28;
  jQuery( '#chaine_'+numchaine+' a').click ( function( e ) { jQuery( '#progtvdetail' ).css( 'top', 75 ); jQuery( '#progtvdetail' ).show( 'slow' );jQuery.ajax({ type: 'post', url: jQuery(this).attr('href'), success: function( msg ) { jQuery( '#progtvdetail' ).html(msg).show( 'slow' ); } }); return false; });
  jQuery( '#chaine_'+numchaine).css( 'marginTop', margin+'px' ).corner('round');
}

