jQuery(document).ready(function($) {
  // equalize columns.
  $.fn.v_justify = function() {
    var min_height = 100;
    $(this).find("div.column").each(function() {
      var element = $(this);
      if (element.height() > min_height) {
        min_height = element.height();
      }
    });
    $(this).find("div.column").css({height: min_height+"px"});
  }
  $("div#footer").v_justify();

  // insert extra structure for contrast blocks that the user may have added.
  $(".js.contrast-block").wrapInner("<div class='contrast-block-content'></div>"); // NOTE: wrapInner causes scripts that are within it to load twice.
  $(".js.contrast-block").prepend("<div class='contrast-block-tl'></div><div class='contrast-block-tr'></div>");
  $(".js.contrast-block").append("<div class='contrast-block-bl'></div><div class='contrast-block-br'></div>");
  
  // add print button ( unobtrusive way )
  $("#v-page_tools-aside").prepend( "<a class='icon-wrapper' id='print-icon' href='javascript:window.print();'><span class='icon sprites'></span>Print</a>" );


  $("ul.js.float-grid").each(function() {
    var ul_width = $(this).width();
    var li_element = $(this).find("li:first-child");
    var li_width = li_element.width() + parseInt(li_element.css("padding-left")) + parseInt(li_element.css("padding-right"));
    if (ul_width != 0 && li_width != 0) {
      column_count = Math.floor(ul_width / li_width); // drop remainder
      var end = $(this).find("li").length;
      for (i=0; i<=end; i = column_count + i) {
        
        var min_height = 100;
        var j = i+column_count;
        $(this).find("li").slice(i, j).each(function() {

          var element = $(this);
          if (element.height() > min_height) {
            min_height = element.height();
          }
        });
        $(this).find("li").slice(i, j).css({height: min_height+"px"});
      }
    }
  });
        
      
  // handle any photo to show up in a colorbox
  $("a[rel='colorbox']").colorbox();
  $("a[rel='member_service_photos']").colorbox();


  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"));
    }
  }
  // set the value from the title
  $.fn.placeholder = 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;
      });

      input_field.attr("value", input_field.attr("title"));
    });
  };
  $(".js_placeholder").placeholder();

  // enable toggle buttons to show less/more on inline hot deals
  $("div.hot_deal-listing div.contrast-block.toggle_on").each(function() {
    // record the expand/collapse heights
    $(this).next("div.contrast-block").addClass("toggle_on").removeClass("toggle_off");

    var collapse_height = $(this).children("div.contrast-block-content").height();
    var expand_height = $(this).next("div.contrast-block").children("div.contrast-block-content").height();

    $(this).next("div.contrast-block").removeClass("toggle_on").addClass("toggle_off");

    $(this).attr('toggle_height', expand_height);
    $(this).next("div.contrast-block").attr('toggle_height', collapse_height);
  });
    
  $("div.hot_deal-listing a.button-toggle").click(function(e) {
    var toggle_button = $(this).children("span.button-toggle");
    var cb = $(this).parents("div.hot_deal-listing div.contrast-block");
    if (toggle_button.hasClass("toggle_less")) {
      // show the prev block and hide this one ( collapse )
      cb.prev("div.contrast-block").children("div.contrast-block-content").animate({'height':cb.attr('toggle_height')}, 0);
      cb.children("div.contrast-block-content").animate({'height':cb.attr('toggle_height')}, 400, function() {
        cb.prev("div.contrast-block").addClass("toggle_on").removeClass("toggle_off");
        cb.addClass("toggle_off").removeClass("toggle_on");;
      });
    } else {
      // show the next block and hide this one ( expand )
      cb.next("div.contrast-block").children("div.contrast-block-content").animate({'height':cb.attr('toggle_height')}, 0);
      cb.children("div.contrast-block-content").animate({'height':cb.attr('toggle_height')}, 400, function() {
        cb.next("div.contrast-block").addClass("toggle_on").removeClass("toggle_off");
        cb.addClass("toggle_off").removeClass("toggle_on");;
      });
    }
    e.preventDefault();
  });


});
