function Contenidos()
{
   var self=this
   this.id='#carousel-content'
   this.prev='.carousel-prev'
   this.next='.carousel-next'
   this.wrapper='#carousel-wrapper'
   this.legend='#carousel-legend'
   this.hover_out='#carousel-content'
   this.hover_in='.box' //'.transparent-box'
   this.growable='.has_big'
   this.closeable='.big .close'

   $.fn.extend({
      me_or_parent: function() {
         if(this.size()!=1) alert('me_or_parent only wants 1')
         if(this.hasClass('highlighted') && ie8()) 
            return this
         else
            return $('.transparent-box',this)
      },
      high: function() {
	  	 this.each(function() {
		  			$(this).find(".transparent-box").removeClass('off');
	  	}) 
      },
      low: function() {
    	  $('.trans-promo').remove();
	  	 this.each(function() {
	  			$(this).find(".transparent-box").addClass('off');
	  	 }) 
      },
      id: function() { 
         var i=this.attr('id')
         if(i) return i
         var p=this.parents('[id]').eq(0)
         if(p)
            return p.attr('id')
         return null
      }
   });

   this.cerveza_legend_button='#carousel-legend li.cerveza'
   this.cine_legend_button='#carousel-legend li.cine'
   this.futbol_legend_button='#carousel-legend li.futbol'
   this.musica_legend_button='#carousel-legend li.musica'
   this.promo_legend_button='#carousel-legend li.promocion'

   // is the respective button selected
   this.cerveza_selected=false
   this.cine_selected=false
   this.futbol_selected=false
   this.musica_selected=false
   this.hovering_active=true
   this.promo_selected=false

   this.resizeTimer=null
   this.min_space_left=this.compute_minimum_space_left()
   this.grown_element=null
   this.grown_base_element=null

   // this is for the carousel
   $(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: 265,
      previous: self.prev,
      next: self.next
   });

   // we capture all resize events (but filter some)
   $(window).bind('resize', function() {
      // this trick solves IE6 problem with
      // calling many times the resize event
      // we put a timer, if we receive another,
      // we remove the timer and put one again
      if (self.resizeTimer) clearTimeout(self.resizeTimer);
      self.resizeTimer = setTimeout(function() { self.resize_handler() }, 50);
   });

   // we call a resize just in case nobody does
   this.resize_handler();

   var none=function() { }
   var out_big=function() { 
      if(self.none_selected() && self.hovering_active)
         $(self.hover_in).high()
   } 
   var in_small=function() { 
      if(self.none_selected() && self.hovering_active) {
         $(self.hover_in).low()
         $(this).high()
      }
   } 
   $(this.hover_out).hover(none,out_big)
   $(this.hover_in).hover(in_small,none)

   $(this.futbol_legend_button).click(function()  { self.futbol_click() })
   $(this.cerveza_legend_button).click(function() { self.cerveza_click() })
   $(this.musica_legend_button).click(function()  { self.musica_click() })
   $(this.cine_legend_button).click(function()    { self.cine_click() })
   $(this.promo_legend_button).click(function()    { self.promo_click() })

/*
Chapuza para evitar que aparezca el layer "big" cuando haces click en un destacado grande. Al no haber video, el layer "big" aparece vacío, así que evitamos de esta manera que aparezca

   $(this.growable).click(function(e) { 
      e.stopPropagation()
      self.grow_from_to($(this),$('#'+$(this).id()+'-big'))
	  $(this).find('h3 a').attr('href');
      return false
   })
*/
   
   $(this.closeable).click(function() { 
      self.close($(this).parents('[id]').eq(0))
   })
}

Contenidos.prototype.compute_minimum_space_left = function() {
   // we compute the initial minimum space to leave on the side
   // (offset + image + offset ) on each side
   var left_img=$(this.prev)
   var img_width=left_img.width()
   var img_offset=left_img.offset().left
   log('left_img width: '+img_width)
   log('left_img offset: '+img_offset)

   return img_offset*4+img_width*2;
}

Contenidos.prototype.resize_handler = function() {
   var window_width=$(window).width()
   log('resize_handler:: window_width is '+window_width)
   var new_width=window_width-this.min_space_left;
   log('resize_handler:: new_width is '+new_width)
   $(this.wrapper).width(new_width);
   $(this.id).resize_to(new_width);
   log('resize_handler:: width set to '+$(this.id).width())
   var left1=$(this.id).offset().left
   var left2=$(this.wrapper).offset().left
   $(this.legend).css('left',(left1-left2))
   log('resize_handler:: legend aligned to '+(left1-left2))

   if(this.grown_element) {
      log('need to reposition grown element')
      var o=$('#'+this.grown_base_element).offset()
      var p=$('#carousel-wrapper').offset()
      $('#'+this.grown_element).css({top: o.top-p.top,left: o.left})
   }
}

/*
Contenidos.prototype.first_visible     = function() { $(this.prev).hide() }
Contenidos.prototype.last_visible      = function() { $(this.next).hide() }
Contenidos.prototype.first_not_visible = function() { $(this.prev).show() }
Contenidos.prototype.last_not_visible  = function() { $(this.next).show() }
*/
//2009-08-26 - Nahuel Scotti (nahuel.scotti@doubleyou.com) - fixed
//FIX for IE6
Contenidos.prototype.first_visible     = function() { $(this.prev).css({visibility:'hidden'}) }
Contenidos.prototype.last_visible      = function() { $(this.next).css({visibility:'hidden'}) }
Contenidos.prototype.first_not_visible = function() { $(this.prev).css({visibility:'visible'}) }
Contenidos.prototype.last_not_visible  = function() { $(this.next).css({visibility:'visible'}) }

Contenidos.prototype.start_animation = function() { this.close_all() }
Contenidos.prototype.finish_animation = function() { }

Contenidos.prototype.any_selected = function() {
   return (this.cine_selected || 
      this.musica_selected || 
      this.cerveza_selected || 
      this.futbol_selected ||
	  this.promo_selected) 
}

Contenidos.prototype.none_selected = function() {
   return !this.any_selected()
}

Contenidos.prototype.legend_selection = function() {
   	this.any_selected() ? $(this.hover_in).low() : $(this.hover_in).high()
   	if(this.cine_selected)
      	$('.cine'+this.hover_in).high();
   	if(this.musica_selected)
      	$('.musica'+this.hover_in).high();
   	if(this.cerveza_selected)
    	$('.cerveza'+this.hover_in).high();
   	if(this.futbol_selected)
      	$('.futbol'+this.hover_in).high();
	if (this.promo_selected) {
		$('.promotion' + this.hover_in).high();
	}
}

Contenidos.prototype.cine_click = function() {
   	if(!this.hovering_active) return
	
	this.cerveza_selected=false
   	this.futbol_selected=false
   	this.musica_selected=false
   	this.hovering_active=true
   	this.promo_selected=false
   
   	this.cine_selected=!this.cine_selected
   	this.legend_selection()
		
	$(this.futbol_legend_button+' a').removeClass('on');
	$(this.musica_legend_button+' a').removeClass('on');
	$(this.cerveza_legend_button+' a').removeClass('on');
	$(this.promo_legend_button+' a').removeClass('on');
	
   	$(this.cine_legend_button+' a').toggleClass('on')
}

Contenidos.prototype.musica_click = function() {
   	if(!this.hovering_active) return
	
	this.cerveza_selected=false
   	this.cine_selected=false
   	this.futbol_selected=false
   	this.hovering_active=true
   	this.promo_selected=false
   
   	this.musica_selected=!this.musica_selected
   	this.legend_selection()
		
	$(this.futbol_legend_button+' a').removeClass('on');
	$(this.cine_legend_button+' a').removeClass('on');
	$(this.cerveza_legend_button+' a').removeClass('on');
	$(this.promo_legend_button+' a').removeClass('on');
	
   	$(this.musica_legend_button+' a').toggleClass('on')
}

Contenidos.prototype.cerveza_click = function() {
   	if(!this.hovering_active) return
	
   	this.cine_selected=false
   	this.futbol_selected=false
   	this.musica_selected=false
   	this.hovering_active=true
   	this.promo_selected=false
   
   	this.cerveza_selected=!this.cerveza_selected
   	this.legend_selection()
		
	$(this.futbol_legend_button+' a').removeClass('on');
	$(this.musica_legend_button+' a').removeClass('on');
	$(this.cine_legend_button+' a').removeClass('on');
	$(this.promo_legend_button+' a').removeClass('on');
	
   	$(this.cerveza_legend_button+' a').toggleClass('on')
}

Contenidos.prototype.futbol_click = function() {
   	if(!this.hovering_active) return
	
	this.cerveza_selected=false
   	this.cine_selected=false
   	this.musica_selected=false
   	this.hovering_active=true
   	this.promo_selected=false
   	
	this.futbol_selected=!this.futbol_selected
   	this.legend_selection()
		
	$(this.cine_legend_button+' a').removeClass('on');
	$(this.musica_legend_button+' a').removeClass('on');
	$(this.cerveza_legend_button+' a').removeClass('on');
	$(this.promo_legend_button+' a').removeClass('on');
	
   	$(this.futbol_legend_button+' a').toggleClass('on')
}

Contenidos.prototype.promo_click = function() {
   	if(!this.hovering_active) return
	
	this.cerveza_selected=false
   	this.cine_selected=false
   	this.futbol_selected=false
   	this.musica_selected=false
   	this.hovering_active=true
   
   	this.promo_selected=!this.promo_selected
   	this.legend_selection()
		
	$(this.futbol_legend_button+' a').removeClass('on');
	$(this.musica_legend_button+' a').removeClass('on');
	$(this.cerveza_legend_button+' a').removeClass('on');
	$(this.cine_legend_button+' a').removeClass('on');
	
   	$(this.promo_legend_button+' a').toggleClass('on')
}

Contenidos.prototype.grow_from_to = function(src,dst) {
   this.close_all()
   var self=this
   dst.data('open',1)
   log("about to grow id: '"+src.id()+"' into '"+dst.id()+"'")
   if(src.hasClass('highlighted'))
      dst.addClass('highlighted')

   var i_o=src.offset()
   var i_p=$('#carousel-wrapper').offset()

   var start={ 
      position: 'fixed',
      zIndex: 5,
      background: src.hasClass('highlighted') ? '#262626' : 'black',
      width: src.width(),
      height: src.height(),
      top: i_o.top-$(window).scrollTop(),
      left: i_o.left,
      opacity: 0,      // completely fade out
      filter: 'alpha(opacity = 0)'
   }
   var fading_in={ 
      opacity: 1,
      filter: 'alpha(opacity = 100)'
   } 
   var growing={ 
      width: dst.width(),
      height: dst.height()
   } 
   var fading_out={ 
      opacity: 0,
      filter: 'alpha(opacity = 0)'
   } 
   var dst_final= {
      position: 'absolute',
      top: i_o.top-i_p.top,
      left: i_o.left
   }
   if(ie6()) {
      dst.removeClass('offleft').css(dst_final)
      self.grown(src,dst)
      return
   }
   
   var e=$("<div></div>").
      addClass('cajaInicial').
      css(start).
      appendTo($('body')).
      animate(fading_in,250,'linear').
      animate(growing,500,'swing', function() {
         dst.removeClass('offleft').css(dst_final)
         e.animate(fading_out,250,'linear',function() { e.remove() })
         self.grown(src,dst)
      })
}

Contenidos.prototype.close = function(dst) {
   log("about to close id: '"+dst.id()+"'")
   var fading_out={ 
      opacity: 0,
      filter: 'alpha(opacity = 0)'
   } 
   dst.animate(fading_out,'fast','swing',function() {
      dst.addClass('offleft').removeAttr('style')
   })
   this.closed(dst)
}

Contenidos.prototype.grown = function(src,dst) {
   log("element has grown: '"+src.id()+"' into '"+dst.id()+"'")
   this.grown_element=dst.id()
   this.grown_base_element=src.id()
   $(this.hover_in).low()
   this.hovering_active=false
   setTimeout(function() { 
      var v=$('#flashvideo-'+dst.id())
      if(v.size()>=1 && dst.data('open')==1) {
         log('auto starting video')
         v.data('play',1)
         v.get(0).playVideo()
      }
   }, 500)
}

Contenidos.prototype.closed = function(dst) {
   log('element has closed: '+dst.id())
   dst.data('open',0)
   this.grown_element=null
   this.grown_base_element=null
   this.hovering_active=true
   this.legend_selection()
   var v=$('#flashvideo-'+dst.id())
   if(v.size()>=1 && v.data('play')==1) {
      log('auto closing video')
      v.get(0).stopVideo()
      v.data('play',0)
   }
}

Contenidos.prototype.close_all = function() {
   if(this.grown_element!=null) {
      this.close($('#'+this.grown_element))
   }
}

$(function() { if($('#carousel-content').size()>0)  new Contenidos() })

$(function(){
	if (isMac()) {
		$('h3').css('opacity',0.9);
	}
});