function Cartelera()
{
   var self=this
   this.id='#carousel-content-cartelera'
   this.prev='.carousel-prev'
   this.next='.carousel-next'
   this.items='#carousel-content-cartelera li a'
   this.video_playing=false

   $.fn.extend({
      high: function() { this.removeClass('off') },
      low: function() { this.addClass('off') },
      inverse: function() { this.toggleClass('off') }
   })

   $(this.id).carousel({
      first_visible: function() { self.first_visible() },
      last_visible: function() { self.last_visible() },
      first_not_visible: function() { self.first_not_visible() },
      last_not_visible: function() { self.last_not_visible() },
      start_animation: function() { self.start_animation() },
      finish_animation: function() { self.finish_animation() },
      fixed_column_width: 78,
      previous: self.prev,
      next: self.next
   });

   $("#pestanas-cartelera .pestanas li").live('click',function(){
      var what=$(this).attr('id')
      log('seleccionando el tab:'+what)
      $("#pestanas-cartelera .pestanas li").removeClass('on')
      $(this).addClass('on')

      $(".texto-pestana").addClass('offleft')
      $(".texto-pestana#div-"+$(this).attr('id')).removeClass('offleft')
      if(what=='trailer')
         self.start_trailer()
      else
         self.stop_trailer()
   });

   $(this.items).click(function() { 
      
	  self.stop_trailer()
      var url=$(this).attr('href')
      log('about to access this:'+url) 
      $.ajax({
         type: "GET",
         url: url,
         data: {},
         success: function(a) {
            $('#pelicula-ajax').children().remove()
            $('#pelicula-ajax').html(a);
			
			$("#pestanas-cartelera .pestanas li").one('click',function(){
			  	var what=$(this).attr('id')
			  	var name=$("#container").data('moviename');
			  	if(name == undefined) return;
			    ut('/CARTELERA/CLICK/'+name+'/'+what.toUpperCase());
			});
         },
         //timeout: 5000,
         error: function(request,error) {
            mensaje.by_id('#problemservidor')
         }
      })
      return false
   })

   var items=$(this.items).size()
   if(items<10) {
      log('resizing and positionioning to the right for items:'+items)
      $(this.id).resize_to(items*78)
      $(this.id).css({ left: 78*(10-items) })
   }
}

Cartelera.prototype.first_visible     = function() { $(this.prev).hide() }
Cartelera.prototype.last_visible      = function() { $(this.next).hide() }
Cartelera.prototype.first_not_visible = function() { $(this.prev).show() }
Cartelera.prototype.last_not_visible  = function() { $(this.next).show() }

Cartelera.prototype.start_animation  = function() { }
Cartelera.prototype.finish_animation = function() { }

Cartelera.prototype.start_trailer    = function() { 
   var self=this
   setTimeout(function() { 
      log('auto starting trailer')
      $('#flashvideo').get(0).playVideo()
      self.video_playing=true
   }, 500);
}

Cartelera.prototype.stop_trailer    = function() { 
   if(this.video_playing) {
      log('auto closing trailer')
      $('#flashvideo').get(0).stopVideo()
      this.video_playing=false
   }
}

$(function() { 
   if($('#carousel-content-cartelera').size()>0)  new Cartelera() 
});

