$(document).ready(function() {

	/** ------------------------------------------------------
	 * HomePage
	 */
	/* --- WORK OUT THE HEIGHT - TABBED BOX ------- */
	if($(".homePicks").length > 0) {
		if($("#HomeHappeningNow").length > 0) {
			var totalHeight = $("#HomeHappeningNow").height();
			var height = totalHeight - 335; // nice and abstract here
			$(".homePicks").css('height', height);
		}	
	}

	/* --- TABBED BOXES --------------------------- */
	$("#TabOption li a").click(function() {
		if($("#HomeTabPicks").hasClass('alt')) {
			// showing the orange tab
			// check if they have clicked on the orange tab
			if($(this).attr("rel") != "TodaysNews") {
				$("#HomeTabPicks").removeClass('alt');
				$("#TodaysNews").addClass('hidden');
				$("#TopPicks").removeClass('hidden');
			}
		}
		else {
			if($(this).attr("rel") != "TopPicks") {
				$("#HomeTabPicks").addClass('alt');
				$("#TopPicks").addClass('hidden');
				$("#TodaysNews").removeClass('hidden');
			}
		}
		return false;
	});
	/* --- MAIN SCROLLER PREVIEWS ----------------- */
	$("#FeaturedPreviews").multipleElementsCycle({
		'overrideStart': 0
	});
	$("#FeaturedPreviews li a").click(function() {
		if($(this).attr("rel")) {
			$("body").stopTime("rotate");
			$("#FeaturedContent li").addClass('hidden');
			$(".currentlyViewing").removeClass("currentlyViewing");
			$(this).addClass("currentlyViewing");
			$("#FeaturedContent li.banner"+$(this).attr("rel")).removeClass('hidden');
		}
		return false;
	});
	
	
	/* --- SCROLLER GALLERY ----------------------- */
	$("#HomeGallery").everyTime(3500, function() {
		var item = $("#HomeGallery div.current");
		item.hide().removeClass("current");
		if(!item.next("div.showcaseItem").attr("class")) {
			// at the end. Show the first one
			$("#HomeGallery div.showcaseItem:first").show().addClass("current");
		}
		else {
			// has another one to show
			item.next("div.showcaseItem").show().addClass("current");	
		}
	});

	/** ------------------------------------------------------
	 * Tabbed Content Boxs
	 */

	$("a.tabbed").click(function () {
		
		// fade the active tab out
		$(".active").fadeOut("slow");
		$(".active").removeClass("active");
		$(".viewingThis").removeClass("viewingThis");	
		var content_show = $(this).attr("title");
		$(this).addClass("viewingThis");
		$("#"+content_show).addClass("active");
		$("#"+content_show).fadeIn("slow");
		return false;
	});
	
	/** ------------------------------------------------------
	 * Recommend Minerva Page
	 */
	$("#RecommendMinervaScrollFooter a").click(function () {

		var old = $("a.selected");
		old.removeClass("selected");
		$("."+ old.attr("rel")).hide();
		$("."+$(this).attr("rel")).show();	
		$(".pagination a[rel="+$(this).attr("rel") +"]").addClass("selected");

		if($(this).attr("rel") == "thirdPanel") {
			$("#RecommendMinervaScrollFooter a.next").hide();
			$("#RecommendMinervaScrollFooter a.previous").show().attr("rel", "secondPanel");
		}
		else if($(this).attr("rel") == "secondPanel") {
			$("#RecommendMinervaScrollFooter a.next").show().attr("rel", "thirdPanel");
			$("#RecommendMinervaScrollFooter a.previous").show().attr("rel", "firstPanel");
		}
		else if($(this).attr("rel") == "firstPanel") {
			$("#RecommendMinervaScrollFooter a.next").show().attr("rel", "secondPanel");
			$("#RecommendMinervaScrollFooter a.previous").hide();
		}
		
		return false;
	});
	/** ------------------------------------------------------
	 * Awards Jump / Filter
	 */
		// Hide unselected years and the 'No Speakers' field
		$("p#Form_AwardSelector_NoSpeakers").hide();
		var currentYearAndSession = $("select#Form_AwardSelector_Year").val();

		// Hide the 'Go' button
		$("input#Form_AwardSelector_action_changepage").hide();

		$("select#Form_AwardSelector_Year").children().each(function(i, child) {
			year = child.value;
			// Skip this child if it should be shown
			if(currentYearAndSession != year) {
				// Hide the associated <select> box
				$("select#Form_AwardSelector_Award_" + year).hide();
			}

		});

		// Now setup a handler for clicking on the 'Year' dropdown
		// TODO Refactor this jQuery-noobness into something nicer
		$("select#Form_AwardSelector_Year").change(function() {
			// Make sure this value has an associated dropdown field to show
			newYear = $("select#Form_AwardSelector_Year").val();

			if($("select#Form_AwardSelector_Award_" + newYear).length > 0) {
				// Hide the no speakers box if it's showing
				$("p#Form_AwardSelector_NoSpeakers").hide();
			} else {
				$("p#Form_AwardSelector_NoSpeakers").show();
			}

			// Hide all other dropdowns
			$("select#Form_AwardSelector_Year").children().each(function(i, child) {
				year = child.value;
				if(newYear != year) {
					// Hide the associated <select> box
					$("select#Form_AwardSelector_Award_" + year).hide();
					} else {
						// Show the associated <select> box
						$("select#Form_AwardSelector_Award_" + year).show();
					}
			});
		});

		// Now setup a handler for onchange element of the speaker list dropdown
		$("form#Form_AwardSelector select.minervaAwards").change(function() {
			window.location = this.value + "/";
		});
	/** ------------------------------------------------------
	 * Sponsor Jump. Jump to sponsor
	 */
	
	$("input#Form_SponsorSelector_action_changepage").hide();
	
	// Setup handler for onchange element of the select dropdown
	$("select#Form_SponsorSelector_PageURL").change(function() {
		window.location = this.value + "/";
	});
	// Hide unselected years and the 'No Speakers' field
	$("p#Form_SpeakerSelector_NoSpeakers").hide();
	var currentYearAndSession = $("select#Form_SpeakerSelector_Year").val() + "_" + $("select#Form_SpeakerSelector_Session").val();

	/** ------------------------------------------------------
	 * Speaker Form. Has a year, session and then speaker
	 * allow filtering on all 3
	 */
	
	$("input#Form_SpeakerSelector_action_changepage").hide();
	
	$("select#Form_SpeakerSelector_Year").children().each(function(i, child) {
		year = child.value;
		$("select#Form_SpeakerSelector_Session").children().each(function(i, child) {
			// Skip this child if it should be shown
			if(currentYearAndSession != year + "_" + child.value) {
				// Hide the associated <select> box
				$("select#Form_SpeakerSelector_SpeakerList_" + year + "_" + child.value).hide();
			}
		});
		
	});
	
	// Now show the whole form
	$("form#Form_SpeakerSelector").show();
	
	// Now setup a handler for clicking on the 'Year' dropdown
	// TODO Refactor this jQuery-noobness into something nicer
	$("select#Form_SpeakerSelector_Year, select#Form_SpeakerSelector_Session").change(function() {
		// Make sure this value has an associated dropdown field to show
		newYear = $("select#Form_SpeakerSelector_Year").val() + "_" + $("select#Form_SpeakerSelector_Session").val();

		if($("select#Form_SpeakerSelector_SpeakerList_" + newYear).length > 0) {
			// Hide the no speakers box if it's showing
			$("p#Form_SpeakerSelector_NoSpeakers").hide();
		} else {
			$("p#Form_SpeakerSelector_NoSpeakers").show();
		}
			
		// Hide all other dropdowns
		$("select#Form_SpeakerSelector_Year").children().each(function(i, child) {
			year = child.value;
			$("select#Form_SpeakerSelector_Session").children().each(function(i, child) {
				if(newYear != year + "_" + child.value) {
					// Hide the associated <select> box
					$("select#Form_SpeakerSelector_SpeakerList_" + year + "_" + child.value).hide();
				} else {
					// Show the associated <select> box
					$("select#Form_SpeakerSelector_SpeakerList_" + year + "_" + child.value).show();
				}
			});
		});
	});
	
	// Now setup a handler for onchange element of the speaker list dropdown
	$("form#Form_SpeakerSelector select.speakerlist").change(function() {
		window.location = this.value + "/";
	});
	
	/** ----------------------------------------
	 * PhotoGallery Tabbing
	 */
	//$("ul#TabNav li:first a").addClass("active");
	var current_year = $("ul#TabNav li a.active").attr("rel");
	$("div#PhotoGalleries ul."+current_year)
		.addClass("viewingThis")
		.css("display", "block");
	
	$("#TabNav a").click(function () {
		var getYear = $(this).attr("rel");
		// fade the active tab out
		$(".active").removeClass("active");
		// hide the content
		$(".viewingThis").fadeOut("slow").hide();

		$(".viewingThis").removeClass("viewingThis");
		$(this).addClass("active");
		$("."+getYear).fadeIn("slow");
		$("."+getYear).addClass("viewingThis");

		return false;
	});
	
	/** ----------------------------------------
	 * Vision page Tabbing
	 */
	$(".visionNavigation a").click(function() {
		var getPageID = $(this).attr("rel");
		$(".active").removeClass("active");
		$(".viewingVision").fadeOut("slow").hide();
		$(".viewingVision").removeClass("viewingVision");
		$(".visionContent"+getPageID).fadeIn("slow");
		$(this).addClass("active");
		$(".visionContent"+getPageID).addClass("viewingVision");
		
		return false;
	})
	
	/** ------------------------------------------
	 * Fade In/Out of logos
	 * This uses the jquery.timer.js plugin for jQuery
	 */
	var count = 1;
	var oneTotal = $("li.firstRotate a.rotating").attr("rel");
	var secondTotal = $("li.secondRotate a.rotating").attr("rel");
	
	$("#SponsorOverview li span").hide();
	$("#SponsorOverview li span.current").show();
	
	$("#SponsorOverview #OfficialSponsorRotate").everyTime(2000, function() {
		// get the current vaule
		var current = $("#SponsorOverview li.firstRotate span.current").attr("class");
		
		current = current.split(' ');
		if(parseInt(current[0]) < parseInt(oneTotal)) {
			// move current up
			var theclass = parseInt(current[0])+1;
			$("#SponsorOverview li.firstRotate span.current").hide().removeClass("current");
			$("#SponsorOverview li.firstRotate span."+theclass).show().addClass("current");
		}
		else {
			// go back to the start
			$("#SponsorOverview li.firstRotate span.current").hide().removeClass("current");
			$("#SponsorOverview li.firstRotate span.1").show().addClass("current");	
		}
	});
	$("#SponsorOverview #MediaRotate").everyTime(3000, function() {
		var secondCurrent = $("#SponsorOverview li.secondRotate span.current").attr("class");
		
		secondCurrent = secondCurrent.split(' ');

		if(parseInt(secondCurrent[0]) < parseInt(secondTotal)) {
			// move current up
			var theclass = parseInt(secondCurrent[0])+1;
			$("#SponsorOverview li.secondRotate span.current").hide().removeClass("current");
			$("#SponsorOverview li.secondRotate span."+theclass).show().addClass("current");
		}
		else {
			// go back to the start
			$("#SponsorOverview li.secondRotate span.current").hide().removeClass("current");
			$("#SponsorOverview li.secondRotate span.1").show().addClass("current");	
		}	
	});
	/** ------------------------------------------
	 * Site Map
	 */
	// hide everything
	$("#Sitemap ul").hide();
	
	// show the children under the one you clicked
	$("#Sitemap a.child").click(function() {
		var getRel = $(this).attr("rel");
		if($(this).attr("class") == "child hideChild") {
			$("#Sitemap ul.sub-"+ getRel).show();
			$(this).removeClass("hideChild");
			$(this).addClass("showChild");
		} 
		else if($(this).attr("class") == "child showChild") {
			$("#Sitemap ul.sub-"+ getRel).hide();
			$(this).removeClass("showChild");
			$(this).addClass("hideChild");
		}
		
		return false;
	})
	/** --------------------------------------
	 * Forum threaded view
	 */
	// hide the threads by default
	
	$("a.expandForum").click(function() {
		var Title = $(this).attr("title"); // get the title of the div to open
		// we need to check if this is an opened or closed
		if($(this).hasClass('opened')) {
			$(this).text("[EXPAND]");
			$("div#"+Title).slideUp();
			$(this).removeClass("opened");
			$(this).addClass("closed");
		} else {
			// well its not opened, and we know they clicked it we need to expand it
			$(this).text("[HIDE]");
			$("div#"+Title).slideDown(); // show the div
			$(this).removeClass("closed");
			$(this).addClass("opened"); // add a class to show its open
			
		}
	
		return false;
	})
	/* -----------------------------------
	 * Vertical Align of Quotes
	 */
	if($(".heightContainer")) {
		var paddingTop = ($(".heightContainer").height() - $("#QuoteBannerText").height()) / 2;
		$("#QuoteBannerText").css("padding-top", paddingTop);
	}
	/*** ---------------------------------
	 * Lightbox Gallery
	 * ------------------------------- */
	$("ul#PhotoList li a").click(function(){
		var changeTo = $(this).attr("rel");
		$("#PhotoGalleryRight p").hide();
		$("#PhotoGalleryRight").html("<img src='"+changeTo+"' alt='Image'/>");
		return false;
	});
	
	/*** ----------------------------------
	 * FORCE Text Limit
	 */
	$("#Form_RecommendMinervaForm textarea").keyup(function() {
		if($(this).val().length >= 1900) {
			var contentShort = $(this).val().substr(0,1900);
			$(this).val(contentShort);
		}
	});
	
	/** -----------------------------------
	 * Conference Content Swapper
	 */
	$("#ConfScheduleNav a").click(function() {
		if($(this).parents("li").hasClass("active")) return false;
		
		var currentActive = $(this).parents("ul").find("li.active").removeClass("active");
		$("div"+ currentActive.find("a").attr("rel")).fadeOut().addClass("hidden");
		
		$(this).parents("li").addClass("active");
		$("div"+$(this).attr("rel")).fadeIn().removeClass("hidden");
		
		return false;
	});
	
	function rightVersionOfFlash() {
		var requiredMajorVersion = 9;
		var requiredMinorVersion = 0;
		var requiredRevision = 115;

		return DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
	}
	
	$.fn.loadFlash = function(fileBase, width, height, bgcolor, fileBaseXML) {
		$(this).html(AC_FL_GetContent(
			'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0',
			'width', width,
			'height', height,
			'src', fileBase,
			'quality', 'high',
			'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
			'align', 'middle',
			'play', 'true',
			'loop', 'true',
			'scale', 'showall',
			'wmode', 'transparent',
			'devicefont', 'false',
			'id', $(this).attr('id') + "Obj",
			'bgcolor', bgcolor,
			'name', fileBaseXML,
			'menu', 'true',
			'allowScriptAccess','sameDomain',
			'allowFullScreen','false',
			'movie', fileBaseXML,
			'salign', ''
		));
	}
	
	if(rightVersionOfFlash()) {
		var URL = $('base').attr('href') + '/send-friend/tip/';
		// $('#RecommendMinervaVideoHolder').loadFlash('themes/womenconf/flash/recommend_minerva', 235, 210, '#FFFFFF', 'themes/womenconf/flash/recommend_minerva?thisvid=http://blip.tv/file/get/Thewomensconference-RecommendARemarkableWoman765.flv');
		$('#VideoPlayerFlash').loadFlash('themes/womenconf/flash/video_player', 950, 550, '#FFFFFF', 'themes/womenconf/flash/video_player?video_xml=video-gallery/xml');
		$('#Minerva2009Flash').loadFlash('themes/womenconf/flash/minerva_awards_2009_promote', 620, 260, '#FFFFFF', 'themes/womenconf/flash/minerva_awards_2009_promote');
	}
	
	/**
	 * Helpers to load content via ajax so we can cache the page data
	 */
	
	if($("#AjaxTodaysDay")) $("#AjaxTodaysDay").html(new Date().getDate());
	if($("#AjaxTodaysMonth")) {
		var months = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
		$("#AjaxTodaysMonth").html(months[new Date().getMonth()]);
	}
	
	/**
	 * Load the tips via ajax
	 */
	if($("#TodaysTipContent").length > 0) {
		$.getJSON("home/CurrentTip/", {ajax: "true"}, function(data) {
			if($("#TodaysTipID").html() == data.ID) return false;
			
			$("#TodaysTipContent").html(data.Content);
			$("#TodaysTipAuthor").html(data.Author);
			$("#TodaysTipAuthorTitle").html(data.AuthorTitle);
		});
	}
	 
});
