function Actualidad() 
{
   var self=this
   var map_join=function(pre,post) {
     return $.map(['entrevistas','estrenos','noticias'],
      function(i,x) { return pre+i+post }).join(',')
   }

   this.lists=map_join('#div-','-list li')
   this.links=map_join('#div-','-list li a')
   this.nexts=map_join('#div-',' .next img')
   this.prevs=map_join('#div-',' .prev img')
   this.video_playing=false
   this.scroll_q=8

   // this programs the click and ajax of news, interviews and premieres
   $(this.links).click(function() {
      self.load_content(this)
      return false
   })

   // this programs the tabs
   $("#pestanas-actualidad .pestanas li").live('click',function(){
      var id=$(this).attr('id')
      log('seleccionando el tab:'+id)
      $("#pestanas-actualidad .pestanas li").removeClass('on')
      $(this).addClass('on')
	  $('ul.list').hide();
      $(".tab-pestana").addClass('offleft')
      $(".tab-pestana#div-"+id).removeClass('offleft')
	  
      self.load_content($('#div-'+id+'-list li:first a'))
      var pid=$('#div-'+id)
      scroll_to(pid,pid.data('length'),0)
      program_scrollers(pid,0,pid.data('length'))
      $(pid).data('position',0)
	  $('ul.list').show();
      program_height(pid,0,pid.data('length'))
   })

   // this programs the trailer/not trailer viewing
   $('.vertrailer').live('click',function(){ self.show_trailer() })
   $('.close-trailer').live('click',function(){ self.hide_trailer() })

   var program_scrollers = function(pid,pos,len) {

      $('.scrollers .prev',pid).css('visibility', 
         (pos > 0) ? 'visible' : 'hidden')
      $('.scrollers .next',pid).css('visibility', 
         (pos + self.scroll_q < len) ? 'visible' : 'hidden')
   }

   var program_height = function(pid,pos,len) {
      var list_e=$('ul.list li',pid)
      var top1=list_e.eq(pos).offset().top
      var top2=null
      if(pos + self.scroll_q < len) {
         top2=list_e.eq(pos+self.scroll_q).offset().top
      } else {
         var h=list_e.eq(len-1).height()
         top2=list_e.eq(len-1).offset().top+h+14
      }
      var listp=$('ul.list',pid).parent()
      listp.css('height',top2-top1-1)
   }

   var scroll_to = function(pid,pos,new_pos) {
      log('about to scroll '+pid.attr('id')+' from '+pos+' to '+new_pos)
      var list_e=$('ul.list li',pid)
      var list=$('ul.list',pid)
      list.css('top',-(list_e.eq(new_pos).offset().top-list.offset().top)-1)
   }

   $.each(['entrevistas','estrenos','noticias'],function(i,x) { 
      var pid=$('#div-'+x)
      var len=$('#div-'+x+'-list li').size()
      pid.data('position',0)
      pid.data('length',len)
      program_scrollers(pid,0,len)
      program_height(pid,0,len)
   })
   
   $(this.nexts).click(function() { 
      var pid=$(this).parents('[id]').eq(0)
      var elm=$(this).parent()
      var pos=$(pid).data('position')
      var len=$(pid).data('length')
      log('next from position: ['+pos+','+len+']')
      var new_pos=pos+self.scroll_q
      if(new_pos >= len) new_pos=len-1
      log('     into position: ['+new_pos+','+len+']')
      $(pid).data('position',new_pos)
      program_scrollers(pid,new_pos,len)
      program_height(pid,new_pos,len)
      scroll_to(pid,pos,new_pos)
   })

   $(this.prevs).click(function() { 
      var pid=$(this).parents('[id]').eq(0)
      var elm=$(this).parent()
      var pos=$(pid).data('position')
      var len=$(pid).data('length')
      log('prev from position: ['+pos+','+len+']')
      var new_pos=pos-self.scroll_q
      if(new_pos < 0) new_pos=0
      log('     into position: ['+new_pos+','+len+']')
      $(pid).data('position',new_pos)
      program_scrollers(pid,new_pos,len)
      program_height(pid,new_pos,len)
      scroll_to(pid,pos,new_pos)
   })
}

Actualidad.prototype.load_content = function(e) {
   var self=this
   this.hide_trailer()
   var url='ajax-'+$(e).attr('href')
   log('about to access this:'+url) 
   $.ajax({
      type: "GET",
      url: url,
      data: {},
      success: function(a) {
         $(self.lists).removeClass('on')
         $(e).parent().addClass('on')
         var cd=$('.content-detail')
         cd.children().remove()
         var vt=$('#tab-videotrailer')
         vt.children().remove()
         $(a).each(function() {
            if(this.nodeType == 3 ) return // TEXT
            if(this.tagName=='SCRIPT') {
               log('add script')
               $('body').append($(this))
            } else {
               if($(this).hasClass('content-detail')) {
                  $(this).children().appendTo(cd)
               } else if($(this).attr('id') == 'tab-videotrailer') {
                  log('add videotrailer')
                  $(this).children().appendTo(vt)
               }
            }
         })
      },
      //timeout: 5000,
      error: function(request,error) {
         mensaje.by_id('#problemservidor')
      }
   })
} 

Actualidad.prototype.hide_trailer = function() {
   $('.content-detail').css('display', 'block')
   $('#tab-videotrailer').css('display', 'none')
   log('auto closing trailer')
   var v=$('#flashvideo')
   if(v.size()==1 && this.video_playing) {
      v.get(0).stopVideo()
      this.video_playing=false
   }
}

Actualidad.prototype.show_trailer = function() {
   var self=this
   $('#tab-videotrailer').css('display', 'block')
   $('#tab-videotrailer').removeClass('offleft')
   $('.content-detail').css('display', 'none')
   setTimeout(function() { 
      log('auto starting trailer')
      $('#flashvideo').get(0).playVideo()
      self.video_playing=true
   }, 500)
}

$(function() { 
	if($('#div-entrevistas').size()>0) new Actualidad() 
});
