function openCalendar(FormElement){
	var calendarwindow;
	url = "/_includes/calendar.html?formname=resform&formelement=" + FormElement;
	calendarwindow = window.open(url,"calendar","toolbar=no,width=200,height=144,top=150,left=150,status=no,scrollbars=no,resize=no,menubar=no");
	calendarwindow.focus();
}

//equal heights function - matches heights of a group of elements to the largest 
function equalHeight(group) {
    tallest = 0;
    group.each(function() {
        thisHeight = $(this).height();
        if(thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
}

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

function loadBackground() {
	$('#background').hide();
	jQuery.preLoadImages(backgroundImg);
	$('<img src="'+backgroundImg+'" width="100%" />').appendTo("#background");
	$('#background').fadeIn('slow');
}

// Define: Linkify plugin
(function($){

  var url1 = /(^|&lt;|\s)(www\..+?\..+?)(\s|&gt;|$)/g,
      url2 = /(^|&lt;|\s)(((https?|ftp):\/\/|mailto:).+?)(\s|&gt;|$)/g,

      linkifyThis = function () {
        var childNodes = this.childNodes,
            i = childNodes.length;
        while(i--)
        {
          var n = childNodes[i];
          if (n.nodeType == 3) {
            var html = $.trim(n.nodeValue);
            if (html)
            {
              html = html.replace(/&/g, '&amp;')
                         .replace(/</g, '&lt;')
                         .replace(/>/g, '&gt;')
                         .replace(url1, '$1<a href="http://$2" target=\"_blank\">$2</a>$3')
                         .replace(url2, '$1<a href="$2" target=\"_blank\">$2</a>$5');
              $(n).after(html).remove();
            }
          }
          else if (n.nodeType == 1  &&  !/^(a|button|textarea)$/i.test(n.tagName)) {
            linkifyThis.call(n);
          }
        }
      };

  $.fn.linkify = function () {
    return this.each(linkifyThis);
  };

})(jQuery);


$(document).ready(function() {

	/* Open external links in a new window */
	$("a[href^='http']").attr('target','_blank');
	$("form[action^='http']").attr('target','_blank');
	$("li.printable a").attr('target','_blank');
	
	//footer carousel
	$('#footer_logos > ul.jcarousel-skin-footer_logos').jcarousel({
        wrap: 'circular',
        animation: 5000,
        easing: 'easeOutSine',
        auto: 1        
    });
	
	/* temp removal of dropdowns 
	// dropdowns
	$('#nav').superfish({ 
		delay:       200,                      	// one second delay on mouseout 
		animation:   {opacity:'show'},  		// fade-in and slide-down animation 
		speed:       200,                       // faster animation speed 
		autoArrows:  false,                    	// disable generation of arrow mark-up 
		dropShadows: false                   	// disable drop shadows 
	});
	*/
	
	// SEARCH
	$('#header-links li.first a').toggle(function(event) {
		event.preventDefault;
		$('#header-links li.first').animate({
			marginTop: '22px'
		}, 500, function() {
			$("#quick-search").slideDown('fast');
		});
		
		return false;
	}, function(event) {
		event.preventDefault;
		$("#quick-search").slideUp('fast', function() {
			$('#header-links li.first').animate({
				marginTop: '0px'
			}, 500);
		});
		return false;
	});
	
	

	
	$("#resform #DateIn,#resform a.dp-choose-date").click(function(event) { 
		event.preventDefault;
		openCalendar('DateIn'); 
		return false;
	});

	loadBackground();
	
	// LIGHTBOXES //
	$(".video-gallery-item a,.gallery-vimg a").click(function() {
		$.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'width'			: 680,
				'height'		: 495,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
					'wmode'			: 'transparent',
					'allowfullscreen'	: 'true'
				}
			});
	
		return false;
	});
	$(".photo-gallery-item a,.gallery-img a").fancybox();
	$('#fancybox-close').append('Close');
	
	// TWITTER
	var username='mountaincreek'; // set user name
	var format='json'; // set format, you really don't have an option on this one
	var url='http://api.twitter.com/1/statuses/user_timeline/'+username+'.'+format+'?callback=?'; // make the url
	$.getJSON(url,function(tweet){ // get the tweets
		$("#tweet").html(tweet[0].text); // get the first tweet in the response and place it inside the div
		$("#tweet").linkify();
		$("#tweet").fadeIn(500);
		
		$("#tweet1").html(tweet[0].text); // get the first tweet in the response and place it inside the div
		$("#tweet1").linkify();
		$("#tweet2").html(tweet[1].text); // get the first tweet in the response and place it inside the div
		$("#tweet2").linkify();
		$("#tweet3").html(tweet[2].text); // get the first tweet in the response and place it inside the div
		$("#tweet3").linkify();
		$("#tweet4").html(tweet[3].text); // get the first tweet in the response and place it inside the div
		$("#tweet4").linkify();
		$("#tweet5").html(tweet[4].text); // get the first tweet in the response and place it inside the div
		$("#tweet5").linkify();
		$(".tweet").fadeIn(800);
	});
	
	// SNOW REPORT PAGE
	$('.sr-block .cms_html h3 a').toggle(function(event) {
		event.preventDefault;
		$(this).parent().next("div").slideDown('fast', function() {
			$(this).prev("h3").children("a").children("em").html("-");
			$(this).prev("h3").children("a").children("span").html("Collapse");
		});
		return false;
	}, function(event) {
		event.preventDefault;
		$(this).parent().next("div").slideUp('fast', function() {
			$(this).prev("h3").children("a").children("em").html("+");
			$(this).prev("h3").children("a").children("span").html("Expand");
		});
		return false;
	});
	

    /**************************************
    set equalHeights on content and sidebar
    ***************************************/
    // set equal heights on sidebar and content
    //equalHeight($('#sidebar, #content'));

    /***************
    set last classes
    ****************/
    //$('#news .row:last').addClass('last');
    
    /************
    lavalamp nav
    *************/
	//$("#primary-nav ul").addClass('lavaLampWithImage');
    //$("#primary-nav ul li a.current").parent('li').addClass('current');
    //$("#primary-nav ul").lavaLamp({
    //    speed: 350,
    //    click: function(event, menuItem) {
    //        //return false;
    //    }
    //});

    /**************
    input help text
    ***************/
    // set value to title text
    $('form .hashelptext').each(function(){
        // set original value to title
        $(this).val($(this).attr('title'));

        // set value to null on focus
        $(this).focus(function(){
            $(this).val('');
        });

        // reset help text to title if no user input
         $(this).blur(function(){
            if ( $(this).val() == '' )
                $(this).val($(this).attr('title'));
        });
    });




	/* DATE PICKER */
	Date.firstDayOfWeek = 0;
	Date.format = 'mm/dd/yyyy';
	$('.date-pick').datePicker({clickInput:true});
	$('#DateIn').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#checkout').dpSetStartDate(d.addDays(1).asString());
			}
		}
	);



});


