$(document).ready(function() {
	var ie7 = navigator.appVersion.indexOf("MSIE 7.") != -1;
	var tabs = $('ul.tabs li a');
	if (tabs) {
		$('div.tabSection ul.tabs li:first-child').addClass('active');
		$('div.tabSection div.tabContainer div.tabContent').hide();
		$('div.tabSection div.tabContainer').each(function() {
			$(this).children('div.tabContent').first().show();
		});
		tabs.click(function(event) {
			event.preventDefault();
			$(this).parent().parent().children('li.active').removeClass('active').parent().next().children('div.tabContent').hide();
			$(this).parent().addClass('active');
			$('div#tab_' + $(this).attr('href').substring(1)).show();
		});
	}
	var subTabs = $('h4.accTrigger a');
	if (subTabs) {
		$('div.accSection h4.accTrigger:first-child').addClass('active');
		$('div.accSection div.accContainer').hide();
		$('div.accSection').each(function() {
			$(this).children('div.accContainer').first().show();
		});
		subTabs.click(function(event) {
			event.preventDefault();
			$(this).parent().parent().children('h4.active').removeClass('active').next().slideUp('slow');
			$(this).parent().addClass('active').next().slideDown('slow');
		});
	}
	if ($('ul#topNav')) {
		var topWidth, subWidth, subDim;
		$('ul#topNav li.topNavItem ul.subNav').each(function() {
			$(this).css('display', 'block');
			subWidth = 0;
			$(this).children('li.subNavItem').each(function() {
				subWidth += $(this).width();
				if (ie7) {
					subWidth += 62;
				}
			});
			subWidth += 1;
			$(this).css('width', subWidth + 'px');
			topWidth = $(this).parent().width();
			$(this).parent().nextAll().each(function() {
				topWidth += $(this).width();
			});
			$(this).css('display', '');
			if (subWidth >= topWidth) {
				$(this).css('right', 0);
			}
		});
		$('ul#topNav li.topNavItem ul.subNav li.subNavItem ul.subNav2').each(function() {
			subWidth = 0;
			$(this).parent().parent().css('display', 'block');
			$(this).css('display', 'block');
			$(this).children('li').each(function() {
				subWidth += $(this).width();
			});
			subWidth += 1;
			$(this).css('width', subWidth + 'px');
			topWidth = $(this).parent().width();
			$(this).parent().nextAll().each(function() {
				topWidth += $(this).width();
			});
			$(this).css('display', '');
			$(this).parent().parent().css('display', '');
			if (subWidth >= topWidth) {
				$(this).css('right', 0);
			}
		});
		$('ul#topNav li.topNavItem').mouseover(function() {
			$(this).addClass('active');
		}).mouseout(function() {
			$(this).removeClass('active');
		});
		$('ul#topNav li.topNavItem ul.subNav li.subNavItem').mouseover(function() {
			$(this).addClass('active');
		}).mouseout(function() {
			$(this).removeClass('active');
		});
		$('ul#topNav li.topNavItem ul.subNav li.subNavItem ul.subNav2 li').mouseover(function() {
			$(this).addClass('active');
		}).mouseout(function() {
			$(this).removeClass('active');
		});
	}
	$('a[href^="/restricted"]').click(function(e) {
		e.preventDefault();
		var url = $(this).attr('href');
		$.get('/_authorizeAccess.php?url=' + url, function(data) {
			if (data == 1) {
				if (url.substr(-3) == 'pdf') {
					var name = url.substring(url.lastIndexOf('/') + 1);
					_gaq.push(['_trackPageview', '/downloads/' + name.substr(0, name.length - 4) + '2.pdf']);
				}
				if (url.indexOf('/videos/') != -1) {
					$.colorbox({iframe:true, innerWidth:630, innerHeight:430, href: url});
				} else {
					window.location.href = url;
				}
			} else {
				$('<div title="Please Log In"><p>We\'d love to share this file with you, but first you need to log in or register for an account.  Please <a href="/members">click here</a> to access this file.</p></div>').dialog();
			}
		});
	});
	$('a[href^="/files"]').click(function(e) {
		var url = $(this).attr('href');
		if (url.substr(-3) == 'pdf') {
			var name = url.substring(url.lastIndexOf('/') + 1);
			_gaq.push(['_trackPageview', '/downloads/' + name.substr(0, name.length - 4) + '1.pdf']);
		}
	});
	$('a.file').click(function(e) {
		var url = $(this).attr('href');
		if (url.indexOf('/videos/') != -1) {
			e.preventDefault();
			$.colorbox({iframe:true, innerWidth:630, innerHeight:430, href: url});
		} else {
			window.location.href = url;
		}
	});
	$('a.imageBox').click(function(e) {
		var url = $(this).attr('href');
		e.preventDefault();
		$.colorbox({href: url});
	});
	$('a.lightBox').click(function(e) {
		var url = $(this).attr('href');
		e.preventDefault();
		$.colorbox({iframe:true, innerWidth:650, innerHeight:600, href: url});
	});
	var urlVars = getUrlVars();
	if (urlVars['access']) {
		var access = $('a[href="' + urlVars['access'] + '"]');
		if (access) {
			access.click();
		}
	}
	$('div.inquiryTab a#inquiryClose').click(function() {
		$('div.inquiryTab').hide();
	});
	var billboard = $('div.billboard');
	if (billboard) {
		$('div.billboard a#billboardClose').click(function() {
			$('div.billboard').hide();
		});
		setTimeout("$('div.billboard').slideDown()", 3000);
	}
	$('#siteSearch').click(function() {
		if ($(this).val() == 'Site Search') {
			$(this).val('');
		}
	});
});

function getUrlVars() {
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for(var i = 0; i < hashes.length; i++) {
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}
