var NGE = new Object(); // namespacing

$(document).ready(function(){
	
		// Activate Superfish Menus
		$("ol.sf-menu").superfish();
	
		// Add utility first/last/alt classes to LIs.
		$("ul li:first-child").addClass("first");
		$("ul li:last-child").addClass("last");
		$("ul li:nth-child(even)").addClass("alt");
		$("table tr:nth-child(even)").addClass("alt");
		
		// Hide certain selects on a page when the nav is hovered
			$("#primary_nav a.sf-with-ul, #primary_nav ul a").hover(function(){
				if($.browser.msie && /6.0/.test(navigator.userAgent)){		
					$("select.hide_me").hide();
				}
			}, function(){
			  if($.browser.msie && /6.0/.test(navigator.userAgent)){		
					$("select.hide_me").show();
				}
			});
		
		// Replace HRs with Divs in IE
	  if (jQuery.browser.msie) {
			$("hr").wrap("<div class='hr'></div>");
		}
		
		// Simple Reservation Form Hider
		$("fieldset.traveler_fieldset:not(.trav_1)").hide();
		$("fieldset.cabin_fieldset:not(.cabin_1)").hide();
		$(".number_chooser").change(function(){
				var to_show = $(this).find("option:selected").attr("value");
				$("fieldset." + to_show + "").fadeIn();
				$("fieldset." + to_show + "").parent().children("fieldset:not(." + to_show + ")").fadeOut();
				return false;
		});

		// The Chooser - Opens, closes, and swaps text
		$(".chooser_button").click(function(){
			if($(this).hasClass("open")) {
				$(this).next("ul").hide();
				$(this).children("span").html("Click to see dates");
				$(this).removeClass("open");				
				return false;				
			} else {
				$(this).next("ul").show();
				$(this).children("span").html("Select a date");
				$(this).addClass("open");
				return false;
			}
		});
		
		
		// Tab_options and Tabs: Two DIVs that interact using Rels and IDs
		$(".tabs a:first-child").each(function(){
			$(this).addClass("selected");
		});
		$(".tab_options").each(function(){
			$(this).children(":not(:first)").hide();	
			$(this).find(".date").hide();	
		});
		$(".tab_nav a").click(function(){
				var thisTarget = $(this).attr("href");
				$(this).parents(".tab_options").prev().find("a.selected").removeClass("selected");
				$(this).parents(".tab_options").prev().find("a[href='" + thisTarget + "']").addClass("selected");
				if ($(this).attr("href") != "#all")	
					{
						$(this).parent().parent().fadeOut("fast", function() {
							$(thisTarget).fadeIn("slow");
							$(thisTarget).find(".date").hide();
							$(thisTarget).find(".tab_nav").show();
						});	
					} else {
						$(this).parents(".tab_options").children(":not(:visible)").fadeIn('slow');					
						$(this).parents(".tab_options").find(".tab_nav").hide();
						$(this).parents(".tab_options").find(".date").show();
					}			
		});
		$(".tabs a").click(function(){

			if ($(this).hasClass("selected") == false)
				{
					var thisTarget = $(this).attr("href");
					$(this).parent().children('a.selected').removeClass('selected');
					$(this).addClass('selected');
					if ($(this).attr("href") != "#all")	
					{
						$(this).parent().next().children(":visible").fadeOut("fast", function() {
							$(thisTarget).fadeIn("slow");
							$(thisTarget).find(".date").hide();
							$(thisTarget).find(".tab_nav").show();
						}).removeClass('selected');
					} else {

						$(this).parents(".tabs").next().children(":not(:visible)").fadeIn('slow');					
						$(this).parents(".tabs").next().find(".tab_nav").hide();
						$(this).parents(".tabs").next().find(".date").show();
					}
				}
				return false; 
				});
		




		// Do-it-yourself Gallery
		
		$(".simple_gallery_list p").hide();
		
		$(".simple_gallery").each(function(increment){
			$(this).attr("id","simple_gallery_" + increment);
			increment++;
		});		
		
		$(".simple_gallery_caption").each(function(){
			var imageCount = $(this).next().find("li").length;
			if (imageCount != 1)
			  {
			
			var controls = $(document.createElement('span')).attr('class','simple_gallery_changer').html('<a class="previous" href="#">&laquo; previous</a><a class="next" href="#">next &raquo;</a>')
			$(this).append(controls)
			
			  }
		});
		
		
		function simple_switch(imageLink) {
			
      var imageSource = imageLink.attr("href");
      var captionSource = imageLink.next().html();
      var galleryId = imageLink.parents(".simple_gallery").attr("id");
			
			$("#" + galleryId + " .selected").removeClass("selected");
			imageLink.parent().addClass("selected");
			
			var largeImage = new Image();
			$(largeImage).load(function()
			
	     		{
	        $(largeImage).hide();
	        $("#" + galleryId + " .simple_gallery_loader").prepend(largeImage).removeClass("loading").css("height","")
	        $(largeImage).fadeIn("slow"); 
	        }
	 		).attr("src", imageSource);
	
			// var imageHeight = $("#" + galleryId + " .simple_gallery_loader img").height();
			// $("#" + galleryId + " .simple_gallery_loader").animate({height: "" + imageHeight + "px"}, 350);
	
			$("#" + galleryId + " .simple_gallery_caption p").html(captionSource);
			
			
		}
		
		
		$(".simple_gallery_list").each(function(){
			
			var objectLink = $(this).find("li:first a");
			simple_switch(objectLink);
			
			var imageCount = $(this).find("li").length;
			if (imageCount == 1) {
				$(this).hide();
			}
			
		});
		
		$(".simple_gallery_list a").click(function(){
			
			if ( $(this).parent().hasClass("selected") ) {

			} else {
			
			var objectLink = $(this);
			var loaderHeight = $(this).parents(".simple_gallery").find(".simple_gallery_loader img").height();
			
      $(this).parents(".simple_gallery").find(".simple_gallery_loader").addClass("loading").css("height",""+ loaderHeight +"px");		
      $(this).parents(".simple_gallery").find(".simple_gallery_loader img").remove()
			simple_switch(objectLink);
			}
			return false;
		});

		$(".simple_gallery_changer a.next").click(function(){
				if ( $(this).parents(".simple_gallery").find("li.selected").hasClass("last") ) {
				} else {
					var objectLink = $(this).parents(".simple_gallery").find(".selected").next().find("a");
					var loaderHeight = $(this).parents(".simple_gallery").find(".simple_gallery_loader img").height();
				
		      $(this).parents(".simple_gallery").find(".simple_gallery_loader").addClass("loading").css("height",""+ loaderHeight +"px");			
		      $(this).parents(".simple_gallery").find(".simple_gallery_loader img").remove()
					simple_switch(objectLink);
				}
				return false;
		});
		
		$(".simple_gallery_changer a.previous").click(function(){
				if ( $(this).parents(".simple_gallery").find("li.selected").hasClass("first") ) {
				} else {
					var objectLink = $(this).parents(".simple_gallery").find(".selected").prev().find("a");
					var loaderHeight = $(this).parents(".simple_gallery").find(".simple_gallery_loader img").height();
				
		      $(this).parents(".simple_gallery").find(".simple_gallery_loader").addClass("loading").css("height",""+ loaderHeight +"px");		
		      $(this).parents(".simple_gallery").find(".simple_gallery_loader img").remove()
					simple_switch(objectLink);

				}
				return false;
		});
		


			

					// Activate tableHover on the Trip Cal
					$('.trip_calendar').tableHover({colClass: 'hover'});


					// Homepage Functions
					// $("#main_finder select").change(function() {
				        //
					//	var url = $(this).find("option:selected").attr("value");
					//	window.location = url;
					//	return false;
				        //
					// });

					//Use links in the Main_Finder to get the other three finders
					$("#main_finder a").click(function() {
						  var get_finder = $(this).attr("rel");
							$("#main_finder").animate({left: "324px"});
							if($.browser.msie && /6.0/.test(navigator.userAgent)){	
								$("#main_finder select").hide();
								$("#" + get_finder + " select").hide();
							}
							$("#" + get_finder + "").css('left','324px').animate({left: "644px", opacity: 1}, function(){
								$("#" + get_finder + " select").show();
							});
							
							$("#home_background").fadeOut("slow");
							return false;
						});

						// Close a finder div
						$(".finder a.close").click(function() {
							  $(this).parent().parent().animate({left: "324px", opacity: 0}).css('left','-9999px');
								if($.browser.msie && /6.0/.test(navigator.userAgent)){	
							  	$(this).parent().parent().find("select").hide();
								}
								$("#main_finder").css('left','324px').animate({left: "644px"}, function(){
									$("#main_finder select").show();
								});
								$("#home_background").fadeIn("slow");
								return false;
							});

						
						//Image carousel, using jquery's Cycle plugin
						$('#home_carousel') 
						.before('<ul id="home_carousel_captions">') 
						.cycle({ 
							 	cleartype:  1,
						    fx:     'fade', 
						    speed:  'fast', 
						    timeout: 7000, 
						    pager:  '#home_carousel_captions',
    						pagerEvent:   'click',
    						pause:         true,
						    pagerAnchorBuilder: function(idx, slide) { 
						        return '<li><a href="#">' + $(slide).find("img").attr("alt") + '</a></li>'; 
						    }
						});

						$('#home_carousel_captions a').click(function() { $('#home_carousel').cycle('pause'); });

    // OmniTI Changes
    $("#destinations_select").change(function() {
        var url = $(this).find("option:selected").attr("value");
        var destination = '/nge/destinations/' + url;

        // Per Tim, we want to effect an internal click here as well.
        // This assumes GA code loaded, as usual.
        pageTracker._trackPageview('/internal-links/homepage/mainfinder/destinations-select/' + url);

        window.location = destination;
        return false;
    });
    $("#trip_types_select").change(function() {
        var url = $(this).find("option:selected").attr("value");
        var destination = '/nge/triptypes/' + url;

        // Per Tim, we want to effect an internal click here as well.
        // This assumes GA code loaded, as usual.
        pageTracker._trackPageview('/internal-links/homepage/mainfinder/triptypes-select/' + url);

        window.location = destination;
        return false;
    });

    // per Tim, the Depart Finder needs to register an internal link as well.
    $("#depart_finder_submit").click(function() {
        pageTracker._trackPageview('/internal-links/homepage/departfinder/submit');
    });

    // For the Dates/Costs widgets, to effect a click on the All tab
    $("#dates_all, #costs_all").trigger('click');
});


