//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 showTray(navElement) {
	$('#footer-extra').slideDown(600, function() { });
	$('#tray-nav li a').removeClass('current');
	$('#' + navElement).addClass('current');

	$('#footer-extra #footer-content > div').hide();
	var trayElement = "tray" + navElement.substring(8);
	$('#' + trayElement).fadeIn('fast');
	
	if ($('#backgroundText #caption').is(":hidden")) {
		// possible action
	} else {
		$('#backgroundText #caption').slideToggle(200);
		$('#backgroundText a#moretext').html("Next >");		
	}
}
function closeTray() {
	$('#footer-extra').slideUp(400);
	$('#tray-nav li a').removeClass('current');
}

function showCaption() {
	$('#backgroundText #caption').slideToggle(200, function() { 
		if ($('#backgroundText #caption').is(":hidden")) $('#backgroundText a#moretext').html("More +");
		else $('#backgroundText a#moretext').html("Close");
	});
	
	if ($('#footer-extra').is(":hidden")) {
		// possible action
	} else {
		closeTray();
	}
}


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


$(document).ready(function() {

	/* Open external links in a new window */
	/*$("a[href^='http']").attr('target','_blank');*/
	/*$("form[action^='http']").attr('target','_blank');*/
	
	/* email onclick changes*/
	defaultValue1 = jQuery('#newsletter-signup .formText').val();  
	jQuery('#newsletter-signup .formText').click(function() {
		if( this.value == defaultValue1 ) jQuery(this).val("");
	}); 
	
	// SHOW BG IMG
	$('a#showBg').toggle(function(event) {
		event.preventDefault;
		$("#content,#sidebar,#media").fadeOut(300, function() {
			$('a#showBg').html("Show Website");
		});
		return false;
	}, function(event) {
		event.preventDefault;
		$("#content,#sidebar,#media").fadeIn(300, function() {
			$('a#showBg').html("Show Background Image");
		});
		return false;
	});
	
	$("#media-videos a").fancybox({
		'width'				: 660,
		'height'			: 568,
	    'autoScale'     	: false,
	    'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'titleShow'			: false,
		'showCloseButton'	: true,
		'href'				: '/gallery_window.php?g=v'
	});
	$("#media-webcams a").fancybox({
		'width'				: 660,
		'height'			: 568,
	    'autoScale'     	: false,
	    'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'titleShow'			: false,
		'showCloseButton'	: true,
		'href'				: '/gallery_window.php?g=w'
	});
	$("#media-photos a").fancybox({
		'width'				: 660,
		'height'			: 568,
	    'autoScale'     	: false,
	    'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'titleShow'			: false,
		'showCloseButton'	: true,
		'href'				: '/gallery_window.php'
	});
	
	$('#fancybox-close').append('Close');
	

	// 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 
	});
	
	$('#relatedLinks').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 
	});

	// footer navs pipe separator added 
	$('#footer #footer-details ul li').not('#footer #footer-details ul > li:last-child').append('|');
    
    
    // footer tray actions
	$('#tray-nav li a').click(function(event) {
		event.preventDefault;
		showTray($(this).attr('id'));
		return false;
	});
	$('#closetray').click(function(event) {
		event.preventDefault;
		closeTray();
		return false;
	});
	
	// tray carousels
	jQuery('.jcarousel-skin-prop').jcarousel({
	    scroll: 1,
	    easing: 'easeInSine'
	});
	
	// TWITTER
	var username='schweitzerid'; // 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
	});
		
	if (navigator.platform != "iPad") {
		/* DATE PICKER */
		Date.firstDayOfWeek = 0;
		Date.format = 'mm/dd/yyyy';
		$('.date-pick').datePicker({clickInput:true});
		$('#checkin').bind(
			'dpClosed',
			function(e, selectedDates)
			{
				var d = selectedDates[0];
				if (d) {
					d = new Date(d);
					$('#checkout').dpSetStartDate(d.addDays(1).asString());
				}
			}
		);
		$('#checkout').bind(
			'dpClosed',
			function(e, selectedDates)
			{
				var d = selectedDates[0];
				if (d) {
					d = new Date(d);
					$('#checkin').dpSetEndDate(d.addDays(-1).asString());
				}
			}
		);
	}
	
	// EMAIL NEWSLETTER FORM ACTIONS
	$("#icpsignup1774").submit(function() {
		$(this).attr('target','emailForm');
		document.location.href = '/things-to-do/email/thank-you/'
    });
	
	
	// 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").html("-");
		});
		return false;
	}, function(event) {
		event.preventDefault;
		$(this).parent().next("div").slideUp('fast', function() {
			$(this).prev("h3").children("a").html("+");
		});
		return false;
	});
	

    // $('img.caption').jcaption();
    

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

    /***************
    set last classes
    ****************/
    //$('#news .row:last').addClass('last');
    

    /**************
    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'));
        });
    });
    /* Override style to print device
    $("#srprint").click(function(event) {
		event.preventDefault;
	    $("link[media=print]").attr({media : "all"});
	    return false;
    });    
    */
    $("#srprint").click(function(event) {
    	event.preventDefault;
        window.open($(this).attr("href"));
        return false;
    });
    
    
    $("#srmetric").toggle(function(event) {
    	event.preventDefault;
        $('#sr-stats').hide();
    	$('#sr-stats-metric').show()
        $("#sr-options .metric > a > span").html("Fahrenheit/Inches");
    	return false;
    }, function(event) {
    	event.preventDefault;
		$('#sr-stats-metric').hide();
		$('#sr-stats').show()
		$("#sr-options .metric > a > span").html("Celsius/Centimeters");
    	return false;
    });
    
    $("#srdifficulty").toggle(function(event) {
        event.preventDefault;
        $('#sr-trails_by_lift').hide();
        $('#sr-trails_by_difficulty').show()
        $("#srdifficulty").html("Sort by Lift");
        return false;
    }, function(event) {
        event.preventDefault;
                $('#sr-trails_by_difficulty').hide();
                $('#sr-trails_by_lift').show()
                $("#srdifficulty").html("Sort by Difficulty");
        return false;
    });
});

