$(document).ready(function(){
  /*** Curtain animations ***/
  $openCurtain = 0;
  $curtainAnim = 0;
  
  /* Small Animation on rollover */
  $('#promocurtain').hover(function(){
    if(!$curtainAnim && !$openCurtain){
      $('#promocurtain').stop().animate({'top':'-316px'});
    }
  }, function(){
    if(!$curtainAnim && !$openCurtain){
      $('#promocurtain').stop().animate({'top':'-336px'});
    }
  });
  
  /* Open/close the curtain on click */
  $('#curtainbtn').click(function(){
    $curtainAnim = 1;
    if(!$openCurtain){
      $('#rentbtn').fadeOut();
      $('#promocurtain').stop().animate({top:0}, function(){
        $openCurtain = 1;
        $curtainAnim = 0;
      });
      $(this).find('a').blur();
      return false;
    }
  });
  
  /* Adding the arrow for the sub navs */
  $('#mainnav li.special').append('<span class="arrow"></span>');
  
  /* Tweek for hover state bug after pressing back button */
  $('#mainnav > li').click(function(){
    $(this).find('span.arrow').hide();
    $(this).find('ul').hide();
    $theimage = $(this).find('img');
    $theimage.attr('src',$theimage.attr('src').replace('_on.png','_off.png'));
  });
  
  $('#mainnav li ul li').click(function(){
    $(this).parent().parent().find('span.arrow').hide();
    $(this).parent().hide();
    $theimage = $(this).parent().parent().find('img');
    $theimage.attr('src',$theimage.attr('src').replace('_on.png','_off.png'));
  });  
  
  /* Main nav image switch */
  $('#mainnav > li').hover(function(){
    $theimage = $(this).find('img');
    $theimage.attr('src',$theimage.attr('src').replace('_off.png','_on.png'));
    
    $thisElem = $(this);
      
    subnavOpen = setTimeout(function(){
      $thisElem.find('span.arrow').show();
      $thisElem.find('ul').slideDown();
    }, 250);    
  }, function(){
    $theimage.attr('src',$theimage.attr('src').replace('_on.png','_off.png'));
    clearTimeout(subnavOpen);
    $(this).find('span.arrow').hide();
    $(this).find('ul').slideUp();
  });
  
  // Window image switch
  if($('body.homepage').length > 0){
    setInterval("slideSwitch()", 7000);
  }
});

/* Resize the promo image */
function resizePromos(size,image,imgId){
  maxWidth = size;
  promoImage = new Image();
  promoImage.onload = function(){
    if (promoImage.width > maxWidth){
      document.getElementById(imgId).style.width = maxWidth; 
    }else{
      document.getElementById(imgId).style.width = promoImage.width;
    }
    document.getElementById(imgId).src = image;
  }
  promoImage.src = image;
}

/* Window image animation */
function slideSwitch(){
  $active = $('#windowview img.active');
  if($active.length == 0) $active = $('#windowview img:last');
  
  $next = $active.next().length ? $active.next() : $('#windowview img:first');
  $active.addClass('last-active');
  
  $next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 3000, function(){
    $active.removeClass('active last-active');
  });
}

/* Functions to show the promo overflow */
function showPromos(obj){
  obj.style.overflowY = 'auto';
}
function hidePromos(obj){
  obj.style.overflowY = 'hidden';
}
