$(document).ready(function(){
	var GB = {
		text_labels: function(){
			//to automatically erase text in a search field
			$('input[type=text], textarea').each(function(){
				var $input = $(this);
				if(!$input.attr('placeholder'))
					$input.attr('placeholder', this.value);
				$input.focus(function(){
					if(this.value == $input.attr('placeholder'))
					this.value = '';
					$input.addClass('focus');
				});
				$input.blur(function(){
					if(this.value == '')
					this.value = $input.attr('placeholder');
				$input.removeClass('focus'); 
				});
			});
		},
		close_overlay: function(){
			$('#contactOverlay, #overlay_container, #overlay').remove();
			return false
		}
	};
	
	GB.text_labels()	

//SET LI HOVER BACKGROUND FOR LANDING NAVIGATION
	$('#landingNavigation li a').mouseover(function() {
		if ($(this).parent().hasClass('active')) {
			return false;
		}
		else {					
			$(this).parent().css('background' , '#fff');
		}
	});
	$('#landingNavigation li a').mouseout(function() {
		if ($(this).parent().hasClass('active')) {
			return false;
		} 
		$(this).parent().css('background' , 'none');
	});

//SIDEBAR DROPDOWN P LAST CLASS
	$('#sidebars .sidebarDropDown p:last-child, #sidebars .sidebarDropDown ul li ul li:last-child').css('padding-bottom' , '0px');

//SIDEBAR DROPDOWN OPEN/CLOSE FUNCTION
	$('.sidebarDropDown h4.open').find('span').html('-').next().css('display' , 'block');
	$('.sidebarDropDown h4.open').next().css('display' , 'block');
	
	$('.sidebarDropDown h4').click( function() {
		$(this).toggleClass('open').next().slideToggle(400);
		if ($(this).hasClass('open')) {
			$(this).find('span').html('-')
		}
		else {
			$(this).find('span').html('+')
		}
	});

//SIDEBAR HOVER - CHANGE LIST STYLE COLOR
//	$('.sidebarDropDown ul li a').hover( 
//		function() {
//			$(this).parent().css('color' , '#fff');
//		},
//		function () {
//			$(this).parent().css('color', '#AC001B');
//	});
	
//SET OPACITY FOR LINKS
	$('ul#alphabet li').css('opacity' , '.6');
	$('ul#alphabet li a.active').parent().parent().css('background', '#fff');
	
	if( $('ul#alphabet li').find('a').length > 0 ) {
		$('ul#alphabet li').find('a').parent().parent().css('opacity' , '1');
	}
	
	$('ul#alphabet li span').mouseover( function() {
		$(this).parent().css('background', '#fff');
		$(this).children().css('color', '#EB7F14')
	});
	$('ul#alphabet li span').mouseout( function() {
		$(this).parent().css('background', '#EB7F14');
		$(this).children().css('color', '#fff')
	});

//FIREFOX 3 HACK (ISSUES WITH VERTICALLY-ALIGNING THE ALPHABET LIST)
	if(navigator.userAgent.match("rv:1\.9")) {
		$('ul#alphabet li span').css('line-height' , '15px');
		$('div.pagination div a').css('margin-top', '0px');
		$('#secondaryNavigation').css('left', '560px');
							   
	}

//PAGINATION STOP COLORS 
	if( $('ul#pagination li:first').find('a').hasClass('active') ) {
		$('.previous').css('background-position' , '0px 0px');
	}
	
	if( $('ul#pagination li:last').find('a').hasClass('active') ) {
		$('.next').css('background-position' , '-11px 0px');
	}

//SITEMAP TOGGLE BUTTON
	$('ul#siteMap li h2').click(function () {
		var children = $(this).next().length;

		if ( children == 1 ) {
			$(this).next().slideToggle(900);
			return false;
		}
		else {
			return true;
		}

	});

//LOAD SIDEBAR CONTENT 
	//hide related content 
	$('#Related-News, #Related-Resources-Events, #Related-Success-Stories, #Related-Professionals').css('display' , 'none');
	
	//checks the url to make sure the hash and the content match (for Back and Forward Browser Events)
	function urlcheck() {
		var urlhash = window.location.hash;
		var id = '#' + $('#mainContentContainer').find('div').attr('id');
		if (urlhash != id) {
			$('#sidebarNavigation a[href='+ urlhash+']').click();
		}
	}
//	setInterval( function() { urlcheck();}, 100 );
	var title = $('#breadcrumbEventTitle').html();
	//brings in new content based on the relevant sidebar
	$('#sidebarNavigation a, a.register, #breadcrumbEventTitle a').live('click', function(e) {
		var href = $(this).attr('href');
		
		if($(this).parent().attr('id') == 'breadcrumbEventTitle') {
			var href ='#Overview';
		}
		
		var myclass = $(this).attr('class');
		
		function loadSidebar() {		
			$('#mainContentContainer').children('div').hide();
			$(href).prependTo('#mainContentContainer').show();
			$('#sidebarNavigation a.active, a.register, #breadcrumbEventTitle a').removeClass('active');
		}
		
		if($(this).hasClass('active')) {
			return false;
		}
		
		else {
			if (myclass !== 'register') {
				$('#breadcrumbEventReg').css('display' , 'none');
				$('#breadcrumbEventTitle').css('color' , '#EB7F14')
			};
			loadSidebar();
			$(this).addClass('active');
			if($(this).hasClass('overview')) {
				$('a.overview').addClass('active');
			}
			return false;
		}
	});
//EVENT REGISTER BUTTON 
	$('a.register').click(function() {
		if(!$(this).hasClass('active')) {
			var title = $('#breadcrumbEventTitle').html();
			$('#breadcrumbEventReg').css('display' , 'inline-block');
			$('#breadcrumbEventTitle').html('<a href="#Overview" class="overview">' + title +' </a>').css('color' , '#5A595A');
		}
	});
	
//CREATE PRINT PAGE CONTENT 
	var headline = $('#headline');
	var subhead = headline.find('.subHead').html();
	var h1 = headline.find('h1').html();
	var narrative = headline.find('.narrative').html();
	var title = $('.title').html();
	var breadcrumb = $('#breadCrumb span:last').html();
	var breadcrumbspans = $('#breadCrumb span').length;
	if(breadcrumbspans >= 3) {
		var breadcrumb = $('#breadCrumb').find('a:last').text();
		
	}
	var copyright = $('#copyright').html();
	var h1HTML = '';	
	if(h1 !== null) {
		var h1HTML = '<p class="pageTitle">'+h1+'</p>';
	}
	var breadcrumbHTML = '';
	if (title !== null) {
		var breadcrumbHTML = '<p class="breadcrumbPrint">'+ breadcrumb +'</p>';
	}
	var titleHTML = '';
	if(title !== null) {
		var titleHTML = '<p class="titlePrint">'+ title +'</p>';
	}
	var subheadHTML = '';
	if(subhead !== null) {
		var subheadHTML = '<p class="subHead">'+ subhead +'</p>';
	}
	var narrativeHTML = '';
	if (narrative !== null) {
		var narrativeHTML = '<p class="narrative">'+ narrative +'</p>';
	}
	$('#professionalsResults li').each(function() {
		var content = $(this).html();
		if(content === '') {
			$(this).html('&nbsp;');
		}
	});
	
	setTimeout(function() {
		$('#headlineContainer').before('<div id="printHeadline" class="printOnly">'+ subheadHTML + h1HTML + narrativeHTML + breadcrumbHTML + titleHTML +'</div>');
		$('#mainContentContainer').after('<div class="printClear printOnly"></div><div id="printFooter" class="printOnly"><p>' + copyright + '</p></div>');
	}, 1000);
	
	setTimeout(function() {
		$('#flashContainer').css({'background':'none'});
		$('#flashContainer #headlineContainer').show();
	}, 1000);
});
