document.write('<style>');
document.write('	#header ul.navigation li:hover ul { display:none; }');
document.write('	#change-location ul.navigation li:hover ul { display:none; }');
document.write('	#change-day ul.navigation li:hover ul { display:none; }');
document.write('</style>');

$(document).ready(function(){
									
	// attach ga tracking to no webpage links
	//GALinker.attach();		
	
	// validation for any forms
  $("#form-login").validate();  				
  $("#form-advanced-search").validate();  		
	
	$('#header ul.navigation > li, #change-location ul.navigation > li, #change-day ul.navigation > li').mouseenter(function(){
																		 
		var position = $(this).position();
		var top = position.top;
		var left = position.left;
		var width = $(this).width();
		var height = $(this).height();
		
		// Firefox 2 bug fix
		if ($.browser.mozilla && (parseFloat($.browser.version) < 1.9)) {
			top -= 1;
		}		
		
		if ($(this).find('a').hasClass('hover')) {
			return false;
		}
		
		$(this).find('ul').css('top',top+height);
		$(this).find('ul').css('left',left);
		$(this).find('ul').css('width',width);
		
		var self = $(this);
		self.find('a').addClass('delay');
		
		setTimeout(function() {
								  
			if (!self.find('a').hasClass('delay')) {
				self.find('a').removeClass('delay');				
				return false;
			}						
						
			self.find('a').addClass('hover');
			self.find('ul').slideDown('fast');
			self.find('a').removeClass('delay');
								  
		}, 500);
		
	}).mouseleave(function() {
		var self = $(this);
		
		if ($(this).find('a').hasClass('delay')) {
			self.find('a').removeClass('delay');				
			return false;
		}
		
		$(this).find('ul').slideUp('fast', function() {
			self.find('a').removeClass('hover');
		});
	});
	
	$('#trigger-login').click(function(event) {
		event.preventDefault();
		$(this).find('span').replaceWith('▲');
		$.get("form-login.asp", function(data){
			var htmlData = $(data).find("#form-login").html();
			var action = $(data).find("#form-login").attr('action');
      	htmlData = '<form id="overlay-form-login" action="../home_files/%27%20+%20action%20+%20%27">' + htmlData + '</form>';
			Overlay.width = 250;
			Overlay.opacity = 80;
			Overlay.setStyle('overlay');
			Overlay.show(htmlData, function() {      
         $("#overlay-form-login input:first").focus();
         $("#overlay-form-login").validate();
      });
      
		});
	});
	
	$('#home ul#locations li:first').click(function(event) {
		event.preventDefault();
		$(this).find('span').replaceWith('▲');
		$.get("/files/index.html", function(data){
			var htmlData = $(data).find("ul#locations").html();
			htmlData = '<a href="#"><img class="close png" src="../structural_images/bn-overlay-close.png" alt="Close" title="" /></a><div id="overlay-locations"><h2>CPA Congress locations and dates</h2><ul>' + htmlData + '</ul></div>';
			Overlay.width = 450;
			Overlay.opacity = 50;
			Overlay.setStyle('overlay');
			Overlay.show(htmlData, function() {
				$('.close').click( Overlay.hide );
			});
		});
	});
	
	var pause = 0;
	
	function scroll_text() {
		
		if ( pause == 0 ) {
			pause = 1;
			
			if ( $.browser.msie && (parseFloat($.browser.version) < 8 )) {
				$('#home ul#locations li:not(:first)').filter(':first').fadeOut(1500, function() {
					$('#home ul#locations li:not(:first)').filter(':first').appendTo('#home ul#locations').slideDown(1500, function() {
						pause = 0;
						scroll_text();
					});
				});
			} else {
				$('#home ul#locations li:not(:first)').filter(':first').slideUp(1500, function() {
					$('#home ul#locations li:not(:first)').filter(':first').appendTo('#home ul#locations').slideDown(1500, function() {
						pause = 0;
						scroll_text();
					});
				});
			}
			
			
			
		}
	}
	
	scroll_text();
	
	
/* Session block - view sessions */
	$('a.view-all, a.hide-all').click(function(event) {
		event.preventDefault();
		var btn = $(this);
		var btnSrc = btn.attr('class');
		var btnText = btn.text();
		var action = (btnSrc == 'view-all')?'view-more':'view-less';
		
		$('dl#program dd a.'+action).prev('.details').slideToggle('medium', function() {
			var img = $(this).next('a.'+action);
			var src = img.attr('class');
			var tooltip = img.text();
			if (src.search(/-less/) >= 0) {
				src = src.replace(/-less/, '-more');
				tooltip = tooltip.replace("Less","More");
			} else {
				if (src.search(/-more/) >= 0) {
					src = src.replace(/-more/, '-less');
					tooltip = tooltip.replace("More","Less");
				}
			}
			img.attr('class', src);
			img.text(tooltip);
		});
		
		if (btnSrc.search(/view-/) >= 0) {
			btnSrc = btnSrc.replace(/view-/, 'hide-');
			btnText = btn.text().replace("Expand","Collapse");
		} else {
			if (btnSrc.search(/hide-/) >= 0) {
				btnSrc = btnSrc.replace(/hide-/, 'view-');
				btnText = btn.text().replace("Collapse","Expand");
			}
		}
		btn.attr('class', btnSrc);
//		alert(btnText);
		btn.text(btnText);
//		btn.text(btnSrc.replace("-"," "));
		
	});	
	
/* Individual sessions - view details */
	$('dl#program dd a.view-more, dl#program dd a.view-less').click(function(event) {																							 
		event.preventDefault();
		var img = $(this);
		$(this).prev('.details').slideToggle('medium', function() {
			var src = img.attr('class');
			var tooltip = img.text();
			if (src.search(/-less/) >= 0) {
				src = src.replace(/-less/, '-more');
				tooltip = tooltip.replace("Less","More");
			} else {
				if (src.search(/-more/) >= 0) {
					src = src.replace(/-more/, '-less');
					tooltip = tooltip.replace("More","Less");
				}
			}
			img.attr('class', src);
			img.text(tooltip);
		});
	});
	
});

function decrease() {
	oHTML = document.getElementsByTagName('BODY')[0];
	oHTML.style.fontSize = parseInt(oHTML.style.fontSize)-10+"%";
//	window.status += oHTML.style.fontSize;

	// Update the cookie that holds the font size setting. Do this by calling the cookieMonster function, which
	// looks at the oHTML.style.fontSize; and updates the cookie value accordingly.  If a cookie doesn't exist it creates one.
	updateCookie();
}

function increase() {
	oHTML = document.getElementsByTagName('BODY')[0];
	oHTML.style.fontSize = parseInt(oHTML.style.fontSize)+10+"%";
//	window.status += oHTML.style.fontSize;

	// Update the cookie that holds the font size setting. Do this by calling the cookieMonster function, which
	// looks at the oHTML.style.fontSize; and updates the cookie value accordingly.  If a cookie doesn't exist it creates one.
	updateCookie();
}

function cookieMonster() {
	// This function looks at the oHTML.style.fontSize; and updates the cookie value accordingly.  If a cookie doesn't exist it creates one.
	var oHTML = document.getElementsByTagName('BODY')[0];

	// Look for the cookie that holds font size.  If found, set document to that font size, else set cookie to current font size.
	if(readCookie('fontSize')==null) {
		// No cookie found. Create a new cookie that is set to the font size as defined in the HTML tag on the page (70%).
		createCookie('fontSize',parseInt(oHTML.style.fontSize),365);	
	} else {
		// Cookie found.  Set the HTML font size to the value of the cookie.
		oHTML.style.fontSize = readCookie('fontSize')+"%";
//		window.status += readCookie('fontSize');
	}

	//alert('cookieMonster: ' + readCookie('fontSize'));
}

function updateCookie() {
	var oHTML = document.getElementsByTagName('BODY')[0];

	// Check if the cookie exists.  If not, create one.
	if(readCookie('fontSize')==null) {
		// Create a new cookie that is set to the font size as defined in the HTML tag on the page (70%).
		cookieMonster();	
	} else {
		// Cookie exists, so update it's value.  Look at the recently set font size as defined in the HTML tag on the page, which will have been increased/decreased.
		createCookie('fontSize',parseInt(oHTML.style.fontSize),365);
	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0){
			return c.substring(nameEQ.length,c.length);
		}
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

// JavaScript Document
// jQuery main
$(function() {
			  
	$('#nav a').click(function() {
										
		$('#player li.active').stop();										
										
		$('#nav li.active').removeClass('active');
		$(this).parent('li').addClass('active');
										
		var load_clip = $(this).attr('href');
		
		$('#player li.active').fadeOut('fast', function() {
			$(this).removeClass('active');
			$(load_clip).fadeIn('slow');
			$(load_clip).addClass('active');
		});
										
	});
	
	// toolbar print button handler
	$('.launch-print').click(function(e) {
		e.preventDefault();						
		window.print();							
	});
	
	// toolbar decrease text size
	$('.text-decrease').click(function(e) {
		e.preventDefault();
		decrease();
	});
	
	// toolbar increase text size
	$('.text-increase').click(function(e) {
		e.preventDefault();
		increase();
	});	
	
	$('dl.search-results dd.keywords ul li:first-child').css('backgroundImage','none');
	
});
