$(document).ready(function(){

	// Get tabs
	var AllTabs = $("#tourNavButtons a");
	var TourTab = $("#tourNavButtons #tourTour a");
	var AboutTab = $("#tourNavButtons #tourAbout a");
	var PeopleTab = $("#tourNavButtons #tourPeople a");
	var selected_video_section = "";
	var selected_content_id = 0;
	
	
	if (jQuery.url.segment(2) != null) {
		selected_video_section = jQuery.url.segment(2);
		selected_content_id = $("#tourThumbWrapper.attr('rel')");
	}
	else {
		selected_video_section = "tour";
		selected_content_id = $("#tourThumbWrapper.attr('rel')");
	}
	
	
	// Set default tab to Tour
	if (selected_video_section == "tour") {
		TourTab.attr("class","active");
	}
	else if (selected_video_section == "about"){
		AboutTab.attr("class","active");
	}
	
	
	// Set up thumb/link binding
	BindThumbsAndTitles();
	BindClickActions();
	
	setInitialActiveItem($("#tourThumbWrapper").attr("default"));
	
	// Loop through tabs and add click behavior
	AllTabs.each(function(a){
		// Apply click actions
		$(this).click(function(){
			
			// Loop through the tabs and remove class (making them appear inactive)
			AllTabs.each(function(b){ $(this).removeClass(); });
			
			// Activate the tab that was clicked
			$(this).attr("class","active");
			
			var ClickedSectionName = $(this).attr("rel");
			
			// Send AJAX Request to load section video list
			$.get(
				"index.cfm",
				{
					fuseaction: "tour.displaySubSection",
					section: $(this).attr("rel")
				},
				function(data){
					$("#tourThumbWrapper").fadeOut("normal",
						function(){
							$(this).html(data);
							BindThumbsAndTitles();
							BindClickActions();
							ActivateSelectedItem(ClickedSectionName);
						}).fadeIn("normal");
				},
				"html"
			)// end get
			
			// Note section change with Google Analytics
			var google_url = "/online/take-the-tour/" + ClickedSectionName
			pageTracker._trackPageview(google_url);
			
			// Do not follow the link
			return false;
			
		}); // end click()
	}); // end AllTabs.each()
	
	
	// Apply image and title click actions
	function BindClickActions() {
		// Get all thumb titles
		var ThumbTitles = $("p.thumbTitle a");
		ThumbTitles.each(function(c){
			$(this).click(function(){
				// Get and load data
				$.getJSON(
					"index.cfm",
					{
						fuseaction: "tour.getVideoDataJSON",
						contentid: $(this).attr("rel")
					},
					function(data){
						$("#rightColTitle h3").html(data.CONTENT.TITLE);
						$("#rightColContent p:first").html(data.CONTENT.BODY);
						$("#video_summary_runtime").html(data.CONTENT.SUBTITLE);
						sendToFlash(data.VIDEOPATH,"yes");
					}
				)// end getJSON
				
				// Change classes
				clearAllHighlighting();
				var RelatedThumbImg = $(this).parent().parent().find("a img");
				$(this).attr("class","selected");
				RelatedThumbImg.attr("class","selected");
				
				// Make note of section and index in global variables
				// so we can re-highlight this if the user switches out tab sections
				selected_content_id = $(this).attr("rel");
				AllTabs.each(function(a){
					if ($(this).attr("class") == "active"){
						selected_video_section = $(this).attr("rel");
					}
				});
				
				// Set Google tracking
				var google_url = "/online/take-the-tour/" + selected_video_section + "/" + $(this).parent().attr("rel") + "-" + createFooText($(this).find("span.movieTitle").html()) + ".html";
				pageTracker._trackPageview(google_url);
				
				return false;
			}); // end click
		}); // end ThumbTitles.each()
		
		// Get all Thumbnails
		var ThumbImgs = $(".tourThumb a img");
		ThumbImgs.each(function(d){
			$(this).click(function(){
				$.getJSON(
					"index.cfm",
					{
						fuseaction: "tour.getVideoDataJSON",
						contentid: $(this).parent().attr("rel")
					},
					function(data){
						$("#rightColTitle h3").html(data.CONTENT.TITLE);
						$("#rightColContent p:first").html(data.CONTENT.BODY);
						$("#video_summary_runtime").html(data.CONTENT.SUBTITLE);
						sendToFlash(data.VIDEOPATH,"yes");
					}
				)// end getJSON
				
				// Change classes
				clearAllHighlighting();
				var RelatedThumbTitle = $(this).parent().parent().find(".thumbTitle a");
				$(this).attr("class","selected");
				RelatedThumbTitle.attr("class","selected");
				
				// Make note of section and index in global variables
				// so we can re-highlight this if the user switches out tab sections
				selected_content_id = $(this).parent().attr("rel");
				AllTabs.each(function(a){
					if ($(this).attr("class") == "active"){
						selected_video_section = $(this).attr("rel");
					}
				});
				
				// Set Google tracking
				var google_url = "/online/take-the-tour/" + selected_video_section + "/" + $(this).parent().attr("rel") + "-" + createFooText($(this).parent().parent().find("span.movieTitle").html()) + ".html";
				//alert(google_url);
				pageTracker._trackPageview(google_url);
				
				return false;
			}); // end click
		}); // end ThumbImgs.each()
	}//end BindClickActions
	

	// Loop through thumb titles
	function BindThumbsAndTitles(){
		// Get all thumb titles
		var ThumbTitles = $("p.thumbTitle a");
		ThumbTitles.each(function(e){
			$(this).hover(
				function () {
					if ($(this).attr("class") != "selected") {
						var RelatedThumbImg = $(this).parent().parent().find("a img");
						$(this).attr("class","active");
						RelatedThumbImg.attr("class","active");
					}
				},
				function () {
					if ($(this).attr("class") != "selected") {
						var RelatedThumbImg = $(this).parent().parent().find("a img");
						$(this).attr("class","inactive");
						RelatedThumbImg.attr("class","inactive");
					}
				}
			)
		}); // end ThumbTitles.each()
		
		// Get all Thumbnails
		var ThumbImgs = $(".tourThumb a img");
		ThumbImgs.each(function(f){
			$(this).hover(
				function () {
					if ($(this).attr("class") != "selected") {
						var RelatedThumbTitle = $(this).parent().parent().find(".thumbTitle a");
						$(this).attr("class","active");
						RelatedThumbTitle.attr("class","active");
					}
				},
				function () {
					if ($(this).attr("class") != "selected") {
						var RelatedThumbTitle = $(this).parent().parent().find(".thumbTitle a");
						$(this).attr("class","inactive");
						RelatedThumbTitle.attr("class","inactive");
					}	
				}
			)
		}); // end ThumbImgs.each()
	}//end BindThumbsAndTitles
	
	function setInitialActiveItem(contentid){
		var ActiveThumbTitle = $("p.thumbTitle a[rel='"+ contentid +"']");
		var ActiveThumbImg = $(".tourThumb a[rel='"+ contentid +"'] img");
		ActiveThumbTitle.attr("class","selected");
		ActiveThumbImg.attr("class","selected");
	}
	
	function ActivateSelectedItem(ClickedSection){
		if (ClickedSection == selected_video_section){
			setInitialActiveItem(selected_content_id);
		}
	}
	
	function clearAllHighlighting(){
		var ThumbImgs = $(".tourThumb a img");
		var ThumbTitles = $("p.thumbTitle a");
		ThumbImgs.each(function(g){$(this).attr("class","inactive");});
		ThumbTitles.each(function(h){$(this).attr("class","inactive");});
	}
	
	function createFooText(sOriginalText){
		var sWorkingText = sOriginalText;
		sWorkingText = sWorkingText.toLowerCase();
		sWorkingText = sWorkingText.replace(/<[^>]*>/g,"");
		sWorkingText = sWorkingText.replace(/'/g,"");
		sWorkingText = sWorkingText.replace(/’/g,"");
		sWorkingText = sWorkingText.replace(/\W/g,"-");
		sWorkingText = sWorkingText.replace(/--/g,"-");
		if (sWorkingText.charAt((sWorkingText.length-1)) == "-") {
			sWorkingText = sWorkingText.substr(0,(sWorkingText.length-1));
		}
		return sWorkingText;
	}
		
});