function mini_player(){
  this.videoCounter = new Array();
  this.playlist_videos = new Array();      // All video in the playlists
  this.playlist_current_video = -1;        // Current video in the playlist
  this.playlist_params = new Array();      // All params of the playlist
  this.playlist_params_backup = new Array();  // To get back to the playlist

  this.queue_videos = new Array();        // La liste des vidéos à loader...
  
  this.others_videos_tab = 'recent';
  this.others_videos_params = new Array();
  
  this.video_last_id = -1;
  this.autorun = 0;
  this.locked = 0;                        // Locké par une pub ?
	this.first_time = 1;										//Première fois = spécial
  
  this.url_param = '';                    //location.hash;
  this.menu_open = '';
  
  this.video_index=0;
  //this.pub_sequence=[0,4,8,12,16,20,24,28]; //Normal 2010-03-03
  //this.pub_sequence=[0,3,7,11,15,18,20,24]; //Aggressive 2010-03-31
  this.pub_sequence=[0,3,6,9,12,15,18,21,24]; //Very Aggressive 2010-04-07
  
  this.url_script = 'http://www.rds.ca/app/zonevideo';
  //this.url_script = 'http://www.rds.ca/app/zonevideo';

  this.have_paused = 0;

  this.init = function(params){
  
    if( params == undefined ){
      params = 'accueil=on&order_by=ordo';
    }
    params += '&player=mini_player';
    /*if( params == '' || params == NULL || params == undefined ){
      params = 'accueil=on&order_by=ordo';
    }
    params += '&player=mini_player';*/
    
    this.refresh_playlist(params);
//    this.refresh_playlist('accueil=on&order_by=ordo&player=mini_player');
  }

  //-----------------------------------------------------------------------------------------
  //- PLAYLIST
  //-----------------------------------------------------------------------------------------

  this.refresh_playlist = function(params, reset_param){
		//trace('sway');
		
    if( reset_param == null && params != null ){
      array_add_str(this.playlist_params, params);
    }else if( reset_param != null && params != null ){
      this.playlist_params = new Array();
      this.playlist_params[0] = params;
    }

    $('playlist').innerHTML = get_loading_animation();
    //queue.add('http://app.rds.vdl2.ca/zonevideo/get_playlist.php', array_to_str(this.playlist_params), 'refresh_playlist_done', this);
    queue.add(this.url_script+'/get_playlist.php', array_to_str(this.playlist_params), 'refresh_playlist_done', this);
    
    return false;
  }
  
  this.refresh_playlist_done = function(result){
    result = unescape(result);
    
    if( result == '' ){
      $('playlist').innerHTML = get_mini_warning_content();
      this.playlist_videos = new Array();
    }else if( result == 'error_0') {
      $('playlist').innerHTML = get_playlist_empty()
      this.playlist_videos = new Array();
    }else{  
      
	  var parts = result.split('#PIPO#');
      if( parts.length == 1 ){
        $('playlist').innerHTML = get_mini_warning_content();
      }else{    
	  	
        $('playlist').innerHTML = parts[0];
        if( this.playlist_videos.length == 0 ){
          this.playlist_videos = parts[1].split(',');
          this.playlist_current_video = 0;
          
          // Original Script called the get_video.php
          //this.get_video(this.playlist_videos[this.playlist_current_video]);
		  
		  // Modified Script sets a default object in order to avoid calling get_video and get_pub onLoad.
		  defaultObject=new Object();
		  defaultObject.coAd = '';
		  defaultObject.coAdbox = '';
		  defaultObject.id = '';
		  defaultObject.publicity = '';
		  defaultObject.rtmp = '';
		  defaultObject.sport_id = '';
		  defaultObject.thumbnail = '';
		  defaultObject.title = '';
		  defaultObject.video = '';
		  document.getElementById("myId").LoadPlayer(create_xml_from_object(defaultObject,this.autorun, true)); 
		  this.autorun = 1;
        }else{
          this.playlist_videos = parts[1].split(',');
          //this.playlist_current_video = 0;
          this.playlist_current_video = -1;
        }
      }
    }
  }
  
  //-----------------------------------------------------------------------------------------
  //- GET_VIDEO
  //-----------------------------------------------------------------------------------------
  this.get_video = function(video_id){
    if( this.locked == 0 || this.first_time == 1){
			this.video_last_id = video_id;
	
			//trace('get_video...: ' + video_id + ' ... ' + this.playlist_current_video);
			
			// Sélectionne le bon vidéo dans la playlist et reset les autres...
			//trace2('Total playlist: ' + this.playlist_videos.length);
			for(var i=0; i<this.playlist_videos.length; i++){
				//trace(this.playlist_videos[i] + ' == ' + video_id);
				if( this.playlist_videos[i] == video_id ){
					this.playlist_current_video = i;
					//trace('Changing the cv: ' + this.playlist_current_video);
				}
				$('playlist_elem_'+i).className = 'playlist_elem';
			}
			if( this.playlist_current_video >= 0 ){
				$('playlist_elem_'+this.playlist_current_video).className = 'playlist_elem_encours';
			}
			
			//trace('get a video...'+video_id);
					
			// Essaie de loader les informations sur le video demandé...    
			queue.add(this.url_script+'/get_video.php', 'video_id='+video_id, 'get_video_done', this);
		}
    
    return false;
  }
  
  this.get_video_done = function(result){
    result = unescape(result);
		//trace('');
		//trace('-----------');
		//trace('get_video_done');
		//trace('-----------');
		
		//trace2('get_video_done: queue_videos.length = '+this.queue_videos.length);
		//trace2('get_video_done: '+result);
		this.queue_videos = new Array();
		
    if( result == 'error' || result == '' ){
      $('video_warning').style.display = '';
      $('mini_main').style.display = 'none';
      
      $('video_warning').innerHTML = get_mini_visionneuse_content();
    }else{
      $('video_warning').style.display = 'none';
      
      $('mini_main').style.display = '';
      
      var videos = result.split('#PIPO#');
			//trace('videos.length:'+videos.length);
      for(var i=0; i<videos.length; i++){
				//trace('for: i='+i);
        this.queue_videos[this.queue_videos.length] = create_object_from(videos[i]);
				//trace('this.queue_videos.length:'+this.queue_videos.length);
      }
      if(in_array(this.pub_sequence,this.video_index)==true){
        //Va chercher une pub
        this.get_pub();
      }else{
        this.request_video();
      }
    }
  }
  
  //-----------------------------------------------------------------------------------------
  //- GET_PUB
  //-----------------------------------------------------------------------------------------
  
  this.get_pub = function(){
    if(this.queue_videos.length>0){
      var video_id='';
      if(this.queue_videos[0]['id']){
        video_id=this.queue_videos[0]['id'];
      }
      
      var sport_id='';
      if(this.queue_videos[0]['sport_id']){
        sport_id=this.queue_videos[0]['sport_id'];
      }
      
      var sous_sport_id='';
      if(this.queue_videos[0]['sous_sport_id']){
        sous_sport_id=this.queue_videos[0]['sous_sport_id'];
      }
      
      //queue.add(this.url_script+'/get_pub.php', 'video_id='+video_id+'&sport_id='+sport_id+'&sous_sport_id'+sous_sport_id, 'get_pub_done', this);
	  sport_id = sport_id.toLowerCase()
	  
      pub_url = 'http://ad.ca.doubleclick.net/pfadx/ctv.rdswatch.ca/video/REPERTOIRE/'+sport_id+';dcmt=text/xml;gateway='+sport_id+';section_1='+sous_sport_id+';section_2=EPISODE_DETAIL;mode=;video_id='+video_id+';arena=video;sz=416x312;tile=1;ord='+Math.floor(Math.random()*10000000);
      document.getElementById("myId").LoadPub(pub_url,'get_pub_done');

    }
  }
  
  this.get_pub_done = function(result){
    result = unescape(result);
    if(result!='error' && result!='bad_pub_info'){
      	var pubObj = create_object_from(result);
		$('startUrl').src = pubObj.startUrl;
		$('endUrl').src = pubObj.endUrl;
		this.queue_videos.splice(0,0,pubObj);
    }
    this.request_video();
  }
  
  //-----------------------------------------------------------------------------------------
  //- REQUEST_VIDEO
  //-----------------------------------------------------------------------------------------
  
  this.request_video = function(){
    
    //if( 0 == 0 ){
		//trace('first_time = '+this.first_time);
		//trace2('locked = '+this.locked);
    if( this.locked == 0 || this.first_time == 1){
			//this.first_time=0;
			//trace2('queue_videos.length = '+this.queue_videos.length);
      if( this.queue_videos.length == 0 ){
        if( this.playlist_current_video < this.playlist_videos.length ){
          this.playlist_current_video++;
          
          if( this.playlist_current_video >= this.playlist_videos.length ){
            this.playlist_current_video = 0;
          }
          this.get_video(this.playlist_videos[this.playlist_current_video]);
        }
      }else{    
        var video = this.queue_videos[0];
        this.queue_videos.splice(0, 1);
        
        if( video.publicity == 1 ){
          this.locked = 1;
        }
		
		 if( video.coAd != '' ){
            try{
              remove_all_child_nodes('leaderboard');
              document.getElementById('leaderboard').innerHTML = '<iframe scrolling="no" width="726" height="120" frameborder="0" src="' + video.coAd + '"></iframe>';
            }catch(err){
            }
          }
          //document.getElementById('leaderboard').innerHTML = '<div style="background-color: #ff00ff;">SWAY</div>';
          if( video.coAdbox != '' ){
            try{
              remove_all_child_nodes('bigBox');
              document.getElementById('bigBox').innerHTML = '<iframe scrolling="no" width="300" height="250" frameborder="0" src="' + video.coAdbox + '"></iframe>';
            }catch(err){
            }
          }
		
        this.player_load(video);
      }
      
    }
    
   // trace('After: ' + this.playlist_current_video);
  }
  
  //-----------------------------------------------------------------------------------------
  //-- PLAYER_LOAD
  //-----------------------------------------------------------------------------------------
  
  this.player_load = function(video){
    
    <!-- DEBUT modif omniture //-->
    track_pub(video.id, video.title, 'mini');
    <!-- FIN modif omniture //-->
    
    document.getElementById("mini_title").innerHTML = video.title;
   // trace('XML ...... calling LoadPlayer...');
    document.getElementById("myId").LoadPlayer(create_xml_from_object(video,this.autorun, true));
    
    if( this.autorun == 0 ){
      this.autorun = 1;
    }
  }

  //-----------------------------------------------------------------------------------------
  //-- ONGLET PLAYLIST/MA PLAYLIST
  //-----------------------------------------------------------------------------------------
  this.show_my_playlist = function(){
    $('playlist').innerHTML = get_loading_animation();
    this.playlist_params_backup = this.playlist_params;
    this.refresh_playlist('playlist=custom', true);
  }
  
  this.show_my_playlist_done = function(result){
    result = unescape(result);
    
    $('playlist').innerHTML = result;
  }
  
  this.show_playlist = function(){
    $('playlist').innerHTML = get_loading_animation();
    this.playlist_params = this.playlist_params_backup;
    this.refresh_playlist();
  }
  
  //-----------------------------------------------------------------------------------------
  //- Player CALLBACK 
  //-----------------------------------------------------------------------------------------
  
  this.bof_callback = function(){
		//trace2('BOF...');
		//trace2('this.locked = '+this.locked);
		this.first_time=0;
		//queue.add(this.url_script+'/pub_counter.php', '', 'bof_callback_done', this);
    this.video_index++;
	this.videoCounter.push(this.video_last_id);
		//queue.add(this.url_script+'/video_view.php', 'video='+this.video_last_id, 'eof_video_view_done', this);
    this.bof_callback_done('');
  }

  this.bof_callback_pub = function(){
		//trace2('BOF PUB...');
		//trace2('this.locked = '+this.locked);
		this.first_time=0;
  }

  this.bof_callback_done = function(result){
//    console.log( unescape(result) );
   // trace('BOF_CALLBACK_DONE...');
  }
  
  this.eof_callback = function(){
    this.eof_callback_done('skip');
  }
  
  this.eof_callback_done = function(result){
    this.locked = 0;
    
    this.request_video();
  }
  
  this.eof_video_view_done = function(result){
  
  }
  
  this.eof_callback_pub = function(){
    //queue.add(this.url_script+'/pub_counter.php', '', 'eof_callback_done', this);
    this.video_index++;
    this.eof_callback_done('');
  }
  
  this.eof_callback_pub_done = function(result){
    this.locked = 0;
    this.request_video();
  }
  
  	//-----------------------------------------------------------------------------------------
	//- Increment Video Counters
	//-----------------------------------------------------------------------------------------
	this.incrementVideoCounters = function(){
		if(this.videoCounter.length > 0){
			var params = '';
			while(this.videoCounter.length > 0){
				var vid = this.videoCounter.pop();
				if(!isNaN(vid)) {
					params = params + 'views[]=' + vid +'&';
				}
			}
			if(params != ''){
				new Ajax.Request(this.url_script+'/update_views.php', {method: 'post', parameters: params});
			}
			vid = '';
		}
	}
}

mini_player = new mini_player();
// On Before Unload - Send View Counter to server for Incrementation
window.onbeforeunload = function () {
	mini_player.incrementVideoCounters();
}
