//Global value to store the current font size in for resetting if they'd like.
var originalFontSize = 0;
var calendarAnimating = false;
var todaysDate = new Date();
var todaysDateString = (todaysDate.getMonth() + 1) + '/' + todaysDate.getDate() + '/' + todaysDate.getFullYear();

var browser = null;
var version = null;

$(document).ready(function(){						
	if(navigator.appVersion.indexOf("MSIE 7.0") != -1)
		isIE7 = true;
	else
		isIE7 = false;
	
	//Enable the header drop down menus.
	$('ul#headerMenu > li').hover(function(){
		if(!isIE7) {
			$(this).css('z-index', $(this).css('z-index') + 1); //Force the active LI to be higher than the one under it
			$('> a', this).addClass('menuHover');
			$('> ul', this).css('z-index', 10000); //The one we're currently hovering over should be in front
			$('> ul', this).stop(true, true).slideDown(  $('li', this).size() * 35 );
		}
		else
			$('> ul', this).css('display', 'block');
	},function(){
		if(!isIE7) {
			$(this).css('z-index', 30); //Force it back under
			$('> ul', this).css('z-index', 1); //Now send it to the back
			if($('> ul.subMenu', this).size() > 0) {
				$('> ul', this).slideUp( $('li', this).size() * 20, function() {
					$(this).parent().find('> a').removeClass('menuHover');
				});
			}
			else {
				$('> a', this).removeClass('menuHover');
			}
		}
		else
			$('> ul', this).css('display', 'none');
	});
	
	//Enable the footer drop down menus.
	$('table#footerMenu td li').hover(function(){
		$(this).css('z-index', $(this).css('z-index') + 1); //Force the active LI to be higher than the one under it
		$('> ul', this).css('z-index', 10000); //The one we're currently hovering over should be in front
		$('> ul', this).stop(true, true).show(  $('li', this).size() * 75 );
	},function(){
		$(this).css('z-index', 1); //Force it back under
		$('> ul', this).css('z-index', 1); //Now send it to the back
		$('> ul', this).hide( $('li', this).size() * 45 );
	});
	
	//Adjust the positioning on the subMenu2 in the footer so it appears to the right of the a tag
	$('table#footerMenu td li ul.subMenu2').each(function() {
		$(this).css('left', $(this).parent().find('> span').width() + 5);
		$(this).css('top', 0);
	});
	
	//For every element that should have tooltips activated, turn them on!	
	$('.toolTip').livequery(function() {
		$(this).tooltip({
			track: true,
			delay: 0,
			showBody: " - ",
			fade: 250,
			extraClass: "blackText",
			showURL: false,
			positionLeft: true
		});
	});
	
	//Assign text resizing for the smallA and largeA spans in the header
	originalFontSize = $('html').css('font-size');
	$('span.smallA').click(function(){
		$('html').css('font-size', parseFloat($('html').css('font-size')) * .9);
	});
	
	$('span.middleA').click(function(){
		$('html').css('font-size', originalFontSize);
	});
	
	$('span.largeA').click(function(){
		$('html').css('font-size', parseFloat($('html').css('font-size')) * 1.1);
	});
	
	//Bind the coda-slider to the div we want.
	$("div#mainBodySlider").livequery(function() {
		$(this).codaSlider();
	});
	
	//The previous and next buttons for the image slider should be display on hover
	$('div#imageSliderWrap').livequery(function() {
		$(this).hover(function(){
			//Show them
			$('div#imageSliderWrap div.prev').stop(true, true).fadeIn('slow');
			$('div#imageSliderWrap div.next').stop(true, true).fadeIn('slow');
		},function(){
			//Hide them
			$('div#imageSliderWrap div.prev').fadeOut('slow');
			$('div#imageSliderWrap div.next').fadeOut('slow');
		});												
	});
	
	//Set the p tags within the welcome right column to have a line-height of the image within them
	$('div#welcome div.right div.image p').each(function() {
		$(this).css('line-height', $(this).parent().find('img').height() + 'px');
	});
	
	//Use liveQuery to bind the click functionality to these images regardless of whether they are loaded via ajax or not
	$('a#previousDates').livequery('click', function(){
		if(!calendarAnimating) {
			calendarAnimating = true;
			//We pass our date information via the a tag's class attribute
			paramsString = $(this).attr('class');
			startDate = paramsString.split(',')[0];
			numWeeks = paramsString.split(',')[1];
			$('div#weeksWrapper').prepend("<div id='calendarWrapPrevious'></div>").css('width', $('div#weeksWrapper').width() * 2);
			$('div#calendarWrapPrevious').load('_shared/topCalendar/topCalendarWeeks.cfm?startDate=' + startDate + '&numWeeks=' + numWeeks, '', function() {
				//Set the wrapper's left offscreen so the previous div does not cover the current.
				$('div#weeksWrapper').css('left', -1 * $('div#calendarWrapPrevious').width());
				//Reload the months div to reflect the current settings
				$('div#months').load('/_shared/topCalendar/topCalendarMonths.cfm?startDate=' + startDate + '&numWeeks=' + numWeeks, '', function() {
					//Done loading, do some animation stuff.
					$('div#weeksWrapper').animate({ left: 0}, 1000, "expoinout", function() {
						//Done animating, now we want to remove the current div and change the id on this one.
						$('div#calendarWrapCurrent').remove();
						$('div#calendarWrapPrevious').attr('id', 'calendarWrapCurrent');
						$('div#weeksWrapper').css('width', $('div#calendarWrapCurrent').width());
						calendarAnimating = false;
					});
				});
			});
		}
		return false;
	});
	
	$('a#nextDates').livequery('click', function(){
		if(!calendarAnimating) {
			calendarAnimating = true;
			//We pass our date information via the a tag's class attribute
			paramsString = $(this).attr('class');
			startDate = paramsString.split(',')[0];
			numWeeks = paramsString.split(',')[1];
			$('div#weeksWrapper').append("<div id='calendarWrapNext'></div>").css('width', $('div#weeksWrapper').width() * 2);
			$('div#calendarWrapNext').load('/_shared/topCalendar/topCalendarWeeks.cfm?startDate=' + startDate + '&numWeeks=' + numWeeks, '', function() {
				//Reload the months div to reflect the current settings
				$('div#months').load('_shared/topCalendar/topCalendarMonths.cfm?startDate=' + startDate + '&numWeeks=' + numWeeks, '', function() {
					//Done loading, do some animation stuff.
					$('div#weeksWrapper').animate({ left: -1 * $('div#calendarWrapNext').width()}, 1000, "expoinout", function() {
						//Done animating, now we want to remove the current div and change the id on this one.
						$('div#weeksWrapper').css('left', 0); //We need to set the wrapper back to 0 so once it's done sliding it doesn't dissapear.
						$('div#calendarWrapCurrent').remove();
						$('div#calendarWrapNext').attr('id', 'calendarWrapCurrent');
						$('div#weeksWrapper').css('width', $('div#calendarWrapCurrent').width());
						calendarAnimating = false;
					});
				});
			});
		}
		return false;
	});

	//If the user focuses it and the text is the default, empty it.
	$('input.searchField').focus(function() {
		if(this.value == 'Find Event...')
			this.value = '';
	});

	//If the user leaves the field and it's empty, put the default text back in.
	$('input.searchField').blur(function() {
		if(this.value == '')
			this.value = 'Find Event...';
	});

	//Load the starting calendar information
	$('div#weeksWrapper').append("<div id='calendarWrapCurrent'></div>");
	$('div#calendarWrapCurrent').load('/_shared/topCalendar/topCalendarWeeks.cfm?startDate=' + todaysDateString + '&numWeeks=5');

	//If the browser is IE, don't cache the data.
	if(navigator.appVersion.indexOf('MSIE') != -1) {
		$.ajaxSetup({
			cache: false
		});
	} else {
		$.ajaxSetup({
			cache: true
		});		
	}

	//Overwrite a tags to load ajax
	$("a[href*='index.cfm']").livequery(function() {
		//If the a tag has index.cfm in it, it's a functional link. Ajax it up!
		$(this).click(function() {
			return ajaxPageLoader(this.href);
		});
	});
	
	//Take the events search form post and ajax it up
	$('form#eventSearchForm').submit(function() {
		return ajaxPageLoader($(this).attr('action') + '&searchVal=' + this.searchVal.value);
	});
	
	//On load, if we have a # in our url, we want to load that page.
	if(window.location.href.indexOf('#') != -1) {
		ajaxPageLoader('index.cfm?' + window.location.href.split('#')[1].replace(/_is_/g, '=').replace(/_and_/g, '&').replace(/#/, '?'));
	}
	
	//Hovering over an image, underline the a tag in the right column
	$('div#welcome table#usefulLinks tr.image td.left a').livequery(function() {
		$(this).hover(function() {
			//In
			$('a', $(this).parent().next()).css('text-decoration', 'underline');
		}, function() {
			//Out
			$('a', $(this).parent().next()).css('text-decoration', 'none');
		});
	});
	
	//Hovering over the right column a tag, underline it. Have to use JS instead of CSS because JS above this kills the CSS after running
	$('a.iconLink').livequery(function() {
		$(this).hover(function() { $(this).css('text-decoration', 'underline'); }, function() { $(this).css('text-decoration', 'none'); });									   
	});
	
	//Ajax page load function
	function ajaxPageLoader(href) {
		$('div#splashContents').load(href.replace(/index\.cfm/gi, 'ajaxIndex.cfm'), '', function() {
			//Grab the page ID out of the href so we can wrap the site in a named div for forced viewing
			if(href.indexOf('?') != -1)
				pageID = href.match(/pageID=([^\&]+)/i)[1];
			else
				pageID = 'welcome';
			
			if(pageID == 'welcome') {
				//We're on the home page, remove the cookie trail.
				$('div#cookieTrail').removeClass().addClass('zoomOff').html('');
				$('div#imageSliderWrap').removeClass().addClass('stripViewer').addClass('zoomOn');
				$('div#imageSliderWrap').load('/_shared/imageSlider.cfm');
				divID = 'pageID--welcome';
			}
			else {
				//We're on a contents page, clean up the coda slider stuff.
				$('div#imageSliderWrap').html('').removeClass('stripViewer').addClass('zoomOff');
				$('div#cookieTrail').load('/_shared/cookieTrail.cfm', '', function() {
					$('div#cookieTrail').removeClass().addClass('zoomOn');
				});
				divID = href.split('?')[1].replace(/&/g, '_and_').replace(/=/g, '_is_');
			}
			
			//Reset the coda variables every time the page refreshes, doesn't hurt anything but guarantees the slider resets.
			clearInterval(codaTimerObj);
			cPanel = 1;
			
			//Set the focus information for adjusting the screen position when they change a page
			
			$('body > div.focusDiv').attr('id', divID);
			window.location = '#' + divID;
		});
		return false;
	}
});