$(document).ready(function(){

  // 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"});
  }

  // colorbox (http://colorpowered.com/colorbox/)
  $("a.gallery_photo-link").colorbox({rel:'photo_gallery', photo:true});
  $("a[target='colorbox-image']").colorbox();
  $("a.colorbox-iframe").colorbox({iframe:true, width:939, height:700});


    // handles overflow for nav menu without having it wrap... which is bad for popups.
    var max_nav_width = 637; 
    var total_nav_width = 42; // 2*21 for padding
    var navmenu_padding = 10; // width of navmenu-item1+padding
    $("li.navmenu-item1").each(function() {
        total_nav_width += ($(this).width() + navmenu_padding);
    });
    if (total_nav_width > max_nav_width) {
        var neg_margin = (total_nav_width - max_nav_width);
        var neg_margin = (neg_margin - neg_margin) - neg_margin; // better way?
        $("#main-nav-inner").css("margin-right", neg_margin);

        // catch the menu overflow and replace all spans with background images.
        if (total_nav_width > max_nav_width+52) {
            // add .backup to all spans and empty them
            $("li.navmenu-item1 a.navmenu-toplink span").each(function() {
                $(this).addClass("backup");
                $(this).empty();
                $(this).append("&nbsp;");
            });
        }
    }

    var target_redirect_map = {
        'index.html' : 'parks_blog',
        'freeride_team.html' : 'freeride',
        'oregon_cup.html' : 'oregon_cup',
        'mtn_photographers.html' : 'Mtn%20Photographers',
        'park_crew.html' : 'Park%20Crew'};
    

    $("a[href$='winter/parks-pipes/parks-blog/index.html'], a[href$='winter/parks-pipes/parks-blog/freeride_team.html'], a[href$='winter/parks-pipes/parks-blog/oregon_cup.html'], a[href$='winter/activities/mtn_photographers.html'], a[href$='winter/parks-pipes/parks-blog/park_crew.html']").each(function() {
        // adds target="_blank" to anchor
        anchor = $(this);
        anchor.attr('target', '_blank');
        var url = anchor.attr('href');
        var file = url.match(/(.*\/)([^\/]*)$/);
        anchor.attr('href', file[1]+target_redirect_map[file[2]]);
     });


    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();


  // set the height for the weblog sidebar
  var weblog_listing_h = $("div#weblog_listing-wrapper").height();
  if (weblog_listing_h < 912) { // top:446 + bottom:466 = 912
    weblog_listing_h = 912;
  }
  $("div#weblog_sidebar-top").height(weblog_listing_h+"px");
  $("div#weblog_listing-wrapper").height(weblog_listing_h+"px");

});
    // homepage alert box or newbar
    var show_alert_box = function() {
      alert_box = jQuery('div#alert-box');
      // For some reason, Safari doesn't like the fade-in.
      if(jQuery.browser.safari == true)
        alert_box.show('slow');
      else
        alert_box.fadeIn(1500);
    };

    jQuery(window).load(function() { 
      setTimeout(show_alert_box, 1000);
      // v_justify the gallery thumbnails
      $("div#photo_gallery-page ul.float-grid").v_justify("li");
    });


