$(document).ready(function(){

    $('.navlinks li').biggerlink();

    $('.roundcorners, #trail-search-form, .staticcolumn blockquote')
        .css("position","relative")
        .prepend('<div class="tlcnr"></div>' + '<div class="trcnr"></div>')
        .append('<div class="blcnr"></div>' + '<div class="brcnr"></div>');   // this function adds a small 4px radius corner to a block on a white background

	$('.browsecolumns').columnize({ width: 260, lastNeverTallest: true });

    $('#hero ul')
        .cycle({ 
			cleartypeNoBg: true,
            timeout: 4000,
            pause: 1
    });

	$(".adpanel").equalHeights();
	
    $('a.button').wrapInner("<span></span>");

    $('#hpfeaturesmore').prepend('<div id="featurenav">');

    $('#homefeatures ul')
        .cycle({ 
			cleartypeNoBg: true,
            fx:    'scrollLeft', 
            timeout: 10000,
            pause: 1,
            pager:  '#featurenav'
    });

  $.cookie('ui-tabs-16', null); // delete cookie

	$("#trailtabs").tabs({
		// cookie: { expires: 7 },
		spinner: false,
    select: function(event, ui) {
      var url = $.data(ui.tab, 'load.tabs');
      if( url ) {
          location.href = url;
          return false;
      }
      return true;
    }
	});
	
	
	
//	$("#trailtabs").bind('tabsshow', function(event, ui) {
//		$('.browsecolumns').columnize({ width: 260, lastNeverTallest: true });
//	    $("a[rel^='prettyPhoto']").prettyPhoto({
//	        animationSpeed: 'normal', /* fast/slow/normal */
//	        padding: 40, /* padding for each side of the picture */
//	        opacity: 0.35, /* Value betwee 0 and 1 */
//	        showTitle: false, /* true/false */
//	        allowresize: true, /* true/false */
//	        counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
//	        theme: 'light_rounded', /* light_rounded / dark_rounded / light_square / dark_square */
//	        callback: function(){}
//	    });
//	});

	$("#trailtabs ul.ui-tabs-nav").append("<li id='phantomtab'></li>");

    $('#trail-search-form').validate({
		rules: {
			keywords: {
				required: true,
				minlength: 3
			}
		},
		messages: {
			keywords: "Search terms must be at least 3 characters in length"
		},
		submitHandler: function(form) {
	       // do other stuff for a valid form
			$('#trail-search-form').ajaxSubmit({ 
		        // target identifies the element(s) to update with the server response 
		        target: '#trail-search-results',

		        beforeSubmit: function() {
					$("#trail-search-results").html('<p id="trail-searching-progress">Searching for results&hellip;</p>');
				},

		        success: function() { 
				    $('.roundcorners')
				        .css("position","relative")
				        .prepend('<div class="tlcnr"></div>' + '<div class="trcnr"></div>')
				        .append('<div class="blcnr"></div>' + '<div class="brcnr"></div>');   // this function adds a small 4px radius corner to a block on a white background
		        } 
			});
		}
	});

    $('#contact-form').validate();

    $('#disclaimer-accept-form').validate({
		rules: {
			terms: "required"
		},
		messages: {
			terms: "You must accept terms to download this file."
		},
		submitHandler: function() { 
            alert("accepted!"); 
        }
	});
	
    $('#contentbody :first-child').css("margin-top", "0");
    $('#contentsidebar :first-child').css("margin-top", "0");
    $('#contentsidebar .sidecontenttop :first-child').css("margin-top", "0");

	$(".traildetail-toggle p").click(function() {
		$(this).next().slideToggle('fast');
		$(this).toggleClass('item-open');
		return false;
	}).next().hide();

	$(".traildetail-toggle p,.defaultimage,.thumbimage,.staticimage").hover(
		function() {
			$(this).addClass('hot');
		},
		function() {
			$(this).removeClass('hot');
		}
	);

    $("a[rel^='prettyPhoto']").prettyPhoto({
        animationSpeed: 'normal', /* fast/slow/normal */
        padding: 40, /* padding for each side of the picture */
        opacity: 0.35, /* Value betwee 0 and 1 */
        showTitle: true, /* true/false */
        allowresize: true, /* true/false */
        counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
        theme: 'light_rounded', /* light_rounded / dark_rounded / light_square / dark_square */
        callback: function(){}
    });

    $(".browsemap-panel ul li a, #gmcontrols a").click(function() {
		$(this).toggleClass('item-active');
		return false;
	});

	$('a[rel*=facebox]').facebox();
	
	$('.adpanel a').filter(function() {
    var theHref = this;
    if (theHref.hostname && theHref.hostname !== location.hostname) {
      $(theHref).not(".noAutoIcon").addClass("offSite");
      $(theHref).not(".noAutoLink").attr('target','_blank').bind('click keypress', function(event) {
        var code=event.charCode || event.keyCode;
        if (!code || (code && code == 13)) {
          if(pageTracker){
            var fixedLink = this.href;
            fixedLink = fixedLink.replace(/https?:\/\/(.*)/,"$1");
            fixedLink = '/outgoing/' + fixedLink;
            pageTracker._trackPageview(fixedLink);
          };
        };
      });
    };
  });


});