/* FireBug console.log wrapper */
function log() {
  if (typeof(console) != 'undefined' && typeof(console.log) == 'function') {
    Array.prototype.unshift.call(arguments, '[transactions]');
    console.log(Array.prototype.join.call(arguments, ' '));
  }
}


/*
 * Superfish v1.4.8 - jQuery menu widget
 * Copyright (c) 2008 Joel Birch
 *
 * Dual licensed under the MIT and GPL licenses:
 * 	http://www.opensource.org/licenses/mit-license.php
 * 	http://www.gnu.org/licenses/gpl.html
 *
 * CHANGELOG: http://users.tpg.com.au/j_birch/plugins/superfish/changelog.txt
 */

(function($){
	$.fn.superfish = function(op){

		var sf = $.fn.superfish,
			c = sf.c,
			$arrow = $(['<span class="',c.arrowClass,'"> &#187;</span>'].join('')),
			over = function(){
				var $$ = $(this), menu = getMenu($$);
				clearTimeout(menu.sfTimer);
				$$.showSuperfishUl().siblings().hideSuperfishUl();
			},
			out = function(){
				var $$ = $(this), menu = getMenu($$), o = sf.op;
				clearTimeout(menu.sfTimer);
				menu.sfTimer=setTimeout(function(){
					o.retainPath=($.inArray($$[0],o.$path)>-1);
					$$.hideSuperfishUl();
					if (o.$path.length && $$.parents(['li.',o.hoverClass].join('')).length<1){over.call(o.$path);}
				},o.delay);	
			},
			getMenu = function($menu){
				var menu = $menu.parents(['ul.',c.menuClass,':first'].join(''))[0];
				sf.op = sf.o[menu.serial];
				return menu;
			},
			addArrow = function($a){ $a.addClass(c.anchorClass).append($arrow.clone()); };

		return this.each(function() {
			var s = this.serial = sf.o.length;
			var o = $.extend({},sf.defaults,op);
			o.$path = $('li.'+o.pathClass,this).slice(0,o.pathLevels).each(function(){
				$(this).addClass([o.hoverClass,c.bcClass].join(' '))
					.filter('li:has(ul)').removeClass(o.pathClass);
			});
			sf.o[s] = sf.op = o;

			$('li:has(ul)',this)[($.fn.hoverIntent && !o.disableHI) ? 'hoverIntent' : 'hover'](over,out).each(function() {
				if (o.autoArrows) addArrow( $('>a:first-child',this) );
			})
			.not('.'+c.bcClass)
				.hideSuperfishUl();

			var $a = $('a',this);
			$a.each(function(i){
				var $li = $a.eq(i).parents('li');
				$a.eq(i).focus(function(){over.call($li);}).blur(function(){out.call($li);});
			});
			o.onInit.call(this);

		}).each(function() {
			var menuClasses = [c.menuClass];
			if (sf.op.dropShadows  && !($.browser.msie && $.browser.version < 7)) menuClasses.push(c.shadowClass);
			$(this).addClass(menuClasses.join(' '));
		});
	};

	var sf = $.fn.superfish;
	sf.o = [];
	sf.op = {};
	sf.IE7fix = function(){
		var o = sf.op;
		if ($.browser.msie && $.browser.version > 6 && o.dropShadows && o.animation.opacity!=undefined)
			this.toggleClass(sf.c.shadowClass+'-off');
		};
	sf.c = {
		bcClass     : 'sf-breadcrumb',
		menuClass   : 'sf-js-enabled',
		anchorClass : 'sf-with-ul',
		arrowClass  : 'sf-sub-indicator',
		shadowClass : 'sf-shadow'
	};
	sf.defaults = {
		hoverClass	: 'sfHover',
		pathClass	: 'overideThisToUse',
		pathLevels	: 1,
		delay		: 800,
		animation	: {opacity:'show'},
		speed		: 'normal',
		autoArrows	: true,
		dropShadows : true,
		disableHI	: false,		// true disables hoverIntent detection
		onInit		: function(){}, // callback functions
		onBeforeShow: function(){},
		onShow		: function(){},
		onHide		: function(){}
	};
	$.fn.extend({
		hideSuperfishUl : function(){
			var o = sf.op,
				not = (o.retainPath===true) ? o.$path : '';
			o.retainPath = false;
			var $ul = $(['li.',o.hoverClass].join(''),this).add(this).not(not).removeClass(o.hoverClass)
					.find('>ul').hide().css('visibility','hidden');
			o.onHide.call($ul);
			return this;
		},
		showSuperfishUl : function(){
			var o = sf.op,
				sh = sf.c.shadowClass+'-off',
				$ul = this.addClass(o.hoverClass)
					.find('>ul:hidden').css('visibility','visible');
			sf.IE7fix.call($ul);
			o.onBeforeShow.call($ul);
			$ul.animate(o.animation,o.speed,function(){ sf.IE7fix.call($ul); o.onShow.call($ul); });
			return this;
		}
	});

})(jQuery);

// site.js
jQuery(document).ready(function($) {

  // initialize superfish menu
  $('ul.sf-menu').superfish({
    delay: 800,
    autoArrows: false,
    disableHI: true,
    dropShadows: false
    });


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


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

  // cam lightbox
      $("div#live-cam a, div#live_view_cam-label_image a").cam_colorbox({
        width:739,
        height:679,
        iframe:true
      });

  $("a.gallery_photo-link").colorbox({
    photo: true
  });

  $.fn.rel_parse_value = function(key) {
    // get the value from a rel attr (;width=480;height=270;)
    regex = new RegExp(key+"\w*=\w*(.+?);");
    m = regex.exec($(this).attr('rel'));
    return parseInt(m[1])+20; // add 20 for iframe body margins
  };
  $("a.colorbox-video").colorbox({
    iframe: true,
    transition: 'none',
    current: "video {current} of {total}",
    initialWidth: 480,
    initialHeight: 270,
    onComplete: function(e){
      // resize colorbox based on rel attr
      $.colorbox.resize({
        innerWidth: $(this).rel_parse_value('width'),
        innerHeight: $(this).rel_parse_value('height')
      });
    },
    rel: "a.colorbox-video"
    });

  // snow_report
    $("a[rel='mountain_sensors']").cam_colorbox({
      photo: true
    });
    $("div.runs-block").hide();
    $("div.runs-block").each(function(e){ // only add toggle for lift-box that has runs-block sibling
      var lift_box = $(this).siblings("div.lift-box");
      lift_box.find("td.lift-name span.icon").addClass("closed");
      lift_box.css({'cursor':'pointer'});
    });
    $("div.lift-and-runs div.lift-box").toggle(function(){
      var runs_block = $(this).siblings("div.runs-block");
      if (runs_block.get().length != 0) {
        runs_block.show();
        $(this).find("td.lift-name span").removeClass('closed');
        $(this).find("td.lift-name span").addClass('open');
      }
    },
    function() {
      var runs_block = $(this).siblings("div.runs-block");
      if (runs_block.get().length != 0) {
        runs_block.hide();
        $(this).find("td.lift-name span").removeClass('open');
        $(this).find("td.lift-name span").addClass('closed');
      }
    }
    );

  // home-page hero promo cycle
  $("div#hero-box").cycle({
    timeout: 9000
  });
  $("ul#mountain-updates-list").cycle({
    cleartypeNoBg: true,
    timeout: 8000
  });
  // home-page bottom promo fade hover and click
  promo_image_delay = 300;
  $("div#bottom-promos div.promo-image").each(function(){
    var time = new Date();
    $(this).attr('last_shown', time.getTime());
  });
  $("div#bottom-promos div.promo-inner").hover(function(){
    var time = new Date();
    $(this).attr('last_shown', time.getTime());
    if ($(this).find("div.promo-text").is("div")) {
      $(this).find("div.promo-image-border, div.promo-image > img, div.promo-image").fadeOut(100, function(){});
    }
  }, function(){
    var promo_id = $(this).parents("div.promo").attr('id');
    var hide_promo = function() {
      var time = new Date();
      var t = time.getTime() - $("div#"+promo_id).find("div.promo-inner").attr('last_shown');
      if (t > promo_image_delay) {
        $("div#"+promo_id).find("div.promo-image-border, div.promo-image > img, div.promo-image").fadeIn(100);
      }
    };
    setTimeout(hide_promo, promo_image_delay);
  });

  // home-page bottom promo photo-video tabs
  $("a#video-promo-title, a#photo-promo-title").bind("click", function(e){
    $('a.active').addClass('inactive').removeClass('active').css('color','#999');
    $(this).removeClass('inactive').addClass('active').css('color','#fff');
    var promo_id = jQuery(this).attr('href');
    $("div#video-promo, div#photo-promo, div#body-promo").hide();
    $(promo_id).show();
    e.preventDefault();
    Cufon.refresh();
  });

});
    // 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);
      //$("div#photo_gallery-page ul.float-grid").v_justify("li");

    });
