$(window).load(function() {
  // need to wait until the page is done loading before justify
  $("div#photo_gallery-page ul.float-grid").v_justify("li");
  //$("div#page").v_justify("div.layout-block");
  $("div#home-page div#small_promos").v_justify("div.small_promo-inner");
  // lodging-page toggle descriptions
  $("div#lodging-page div.lodging_room-block").each(function() {
    var my_block = $(this);
    my_block.find("div.long_description").hide();
    my_block.find("a.toggle_description").click(function(e) {
      if ($(this).html() == "More Details") {
        my_block.find("div.long_description").show();
        my_block.find("p.short_description").hide();
        $(this).html("Less Details");
      } else {
        my_block.find("p.short_description").show();
        my_block.find("div.long_description").hide();
        $(this).html("More Details");
      }
      e.preventDefault();
    });
  });

});
jQuery(document).ready(function($) {
  // snowflake overflow
  var bg_h = Math.max($(window).height(), $("#page").outerHeight());
  $('.bg_sprite').each(function(){
    var e = $(this);
    if (bg_h > (parseInt(e.css('top')) + e.height())) {
      e.show();
    }
  });
  if ($(window).width() < $("#page").outerWidth()) {
    $("body").css('overflow-x', 'scroll');
  }
    


  // superfish menus
  $('ul#nav').superfish(
    { 
      delay:       1000,                           
      animation:   {opacity:'show'}, 
      speed:       'fast',                         
      autoArrows:  false,                          
      hoverClass	: 'sfHover',
      dropShadows: true                           
    }
  );

  // equalize blocks.
  $.fn.v_justify = function(block_sel) {
    var min_height = 100;
    $(this).find(block_sel).each(function() {
      var element = $(this);
      if (element.height() > min_height) {
        min_height = element.height();
      }
    });
    $(this).find(block_sel).css({height: min_height+"px"});
  }

  // 'expand' layout to fit window
  function expand_to_window_width() {
    var page_margin = 0;
    if (window.innerWidth) {
      page_margin = window.innerWidth;
    } else if (document.documentElement && document.documentElement.clientWidth) { /* ie6 and up */
      page_margin = document.documentElement.clientWidth;
    }
    if (page_margin != 0) {
      page_margin = (page_margin - 960) / 2;
      $("#top_menu").css({'padding-right': page_margin+'px', 'margin-right': '-'+page_margin+'px'});
      $("#bottom_menu").css({'padding-right': page_margin+'px', 'margin-right': '-'+page_margin+'px'});
      $("#logo").css({'padding-left': page_margin+'px', 'margin-left': '-'+page_margin+'px'});
      $("#sidebar_footer").css({'padding-left': page_margin+'px', 'margin-left': '-'+page_margin+'px'});
    }
  }
  //expand_to_window_width();
  //$(window).bind('resize', expand_to_window_width);
 


  function title_to_value() {
    input_field = $(this);
    if (input_field.attr("value") == input_field.attr("title")) {
      input_field.attr("value", '');
    }
  }
  function reset_to_value() {
    input_field = $(this);
    if (!input_field.attr("value")) {
      input_field.attr("value", input_field.attr("title"));
    }
  }
  // grab the text from the label with the same name
  $.fn.sink_label = function() {
    return this.each(function() {
      var input_field = $(this);

      // empty the field on click if it has default value
      input_field.bind("focus", title_to_value);
      // reset the field to default value on blur if it is empty
      input_field.bind("blur", reset_to_value);

      // empty the field if the same value as title before submitting
      $("form:has(input[name='"+input_field.attr("name")+"'])").bind('submit', title_to_value);

      // reset the field to the title
      $("form:has(input[name='"+input_field.attr("name")+"'])").bind('reset', function() {
        input_field.attr("value", input_field.attr("title"));
        return false;
      });

      var sink_label_name = input_field.attr("name");
      sink_label_name = "label[for='"+sink_label_name+"']";
      input_field.attr("value", $(sink_label_name).html());
      input_field.attr("title", $(sink_label_name).html());

      $(sink_label_name).remove();
    });
  };
  $("div#search input.header-search_field").sink_label();

  // colorbox (http://colorpowered.com/colorbox/)
  $("a.colorbox").colorbox();
  $("a.gallery_photo-link").colorbox({rel:'photo_gallery', photo:true});
    // group inline photo gallery
  $("a.inline_photo_gallery-link").each(function(i) {
    $(this).siblings("a.photo-link").attr("rel", 'group'+i);
    $(this).siblings("a.photo-link").html("");
    $(this).siblings("a.photo-link:first").html($(this).html());
    $(this).hide();
    $("a.photo-link[rel='group"+i+"']").colorbox({photo:true, speed:200});
  });
    // single colorbox views
  $("a.colorbox-mov").each(function(i) {
    $(this).colorbox({iframe:true, speed:200, preloading:false, width:800, height:800});
  });

    // book_lodging submit to coris (simplified version)
		$("#form-widgets-arrival").datepicker({minDate:'+2', defaultDate:'+2'});
    $("a#plan_your_vacation_check_link").bind('click', function(e){
        var arrival_date = $("input#form-widgets-arrival").val();
        var nights = $("select#planform-nights option:selected").val();
        var adults = $("select#planform-party option:selected").val();
        var width = 800, height = 570;
        var leftposition = (screen.width) ? (screen.width-width)/2 : 100;
        var topposition = (screen.height) ? (screen.height-height)/2 : 100;
        var destination = (
            "http://shop.killington.com/coris/vacationplanner/vacation.aspx?action=startres"
            +"&adt="+arrival_date
            +"&ddt=&days="+nights
            +"&ad="+adults
            /*
            +"&ch="+children
            +"&tn="+teens
            +"&sr="+seniors
            */
        );
        window.open(destination, 'CORIS', "height="+height+",width="+width+",toolbar=1,resizable=1,scrollbars=1,location=1,status=1,menubar=1,top="+topposition+",left="+leftposition);
        e.preventDefault();
    });
});


