document.documentElement.className = 'js';
$(document).ready(function() {
	
	//nav SECONDARY
	$('li li li.here ul, li li li.parent_here ul').slideDown('slow');
	var url = window.location.toString();
	if(url.indexOf('news_archive')>=0)
	{
		$(".nav_secondary a:contains('News Archive')").parent('li').addClass('here');
		$(".nav_secondary a:contains('News and Notification')").parent('li').addClass('here');
	}
	$('.nav_secondary li a').each(function()
	{
		var sibs = $(this).siblings('ul');
		if(sibs.length < 1)
		{
			$(this).addClass('no_accordion');
		}
	}).delay(200);
/*	$(".nav_secondary").accordion({
		header: 'li li li.here a:not(li li li li a, a.no_accordion), li li li.parent_here a:not(li li li li a, a.no_accordion)',
		collapsible: true,
		autoHeight: false,
		active: '.parent_here>.ui-accordion-header, .here>.ui-accordion-header'
	});*/

	//NAV Primary
	$('#nav li ul').hide();
	$('#nav li:not(li li)').hover(function()
	{
		$(this).children('ul').fadeIn('fast');
	},
	function()
	{
		$(this).children('ul').fadeOut('fast');
	});
	
	//CONTENT ACCORDION
	$('.accordion').accordion({
		header: 'h3',
		active: 'none', 
		collapsible: true,
		autoHeight: false,
		icons: { 'header': 'ui-icon-triangle-1-e', 'headerSelected': 'ui-icon-triangle-1-s' }
	});
	
	//Swap Share Image
	$('.share a').prepend('<span class="icon">+</span> ');
	
	//Print page
	$('.print a').live('click',function()
	{
		if (window.print)
		{
			window.print();
			return false;
		}
		else
		{
			alert("Sorry. Your browser does not support printing by this method. You need to choose Print from the File menu.")
			return false;
		}
	});
	
	//obfuscate and make a link for the nat'l office e-mail
	var sText = "nationaloffice";
	sText += "@";
	sText += "aephi";
	sText += ".org";
	var sHref = "ma";
	sHref += "il";
	sHref += "to";
	sHref += ":";
	sHref +=sText;
	$('#footer .email_container').html('<a class="email" href="'+sHref+'" >'+sText+'</a>');

	//hacky home page header movement for now
	
//	w = $(window).width();
	w = 1600;
	$('#home h1 div')
		.css({backgroundPosition: '0px 0px'})
		.click(function(event, cycle)
		{
			
			switch($(this).attr('id'))
			{
			case 'top':
			  	dir = cycle % 2 == 0 ? '+' : '-';
				speed = (Math.floor(Math.random()*11)*5000)+150000;
			  break;
			case 'mid':
			  	dir = cycle % 2 == 0 ? '-' : '+';
				speed = (Math.floor(Math.random()*11)*5000)+150000;
			  break;
			default:
			  	dir = cycle % 2 == 0 ? '+' : '-';
				speed = (Math.floor(Math.random()*11)*5000)+150000;
			}
			cycle++;
			$(this).animate({
			backgroundPosition:"("+dir+w+"px 0px)"
			},
			 speed, 'linear', function()
			{
				$(this).trigger('click', cycle);
			});
		})
		.animate({opacity:1},5000)
		.trigger('click', 1);
		
		
	//add new fields to a form...
	$('.add_new').click(function()
	{
		//hard-coded for now!
		var table = "members";
        $('#'+table+' tbody>tr:last').clone(true).insertAfter('#'+table+' tbody>tr:last');
		$('#'+table+' tbody>tr:last input').val('');
		$('#'+table+' tbody>tr:last select').each(function() {this.selectedIndex = -1;});
		return false;
	});

	$('.position_holder').change(function()
	{
		val = $(this).val();
		$this = $(this);
		for(i = 0; i < members.length; i++) 
		{ 
		  if(members[i][0] == val)
			{
				$this.parents('tr').children('td').children('.address').val(members[i][1]);
				$this.parents('tr').children('td').children('.zip').val(members[i][2]);
				$this.parents('tr').children('td').children('.phone').val(members[i][3]);
				$this.parents('tr').children('td').children('.email').val(members[i][4]);
				break;
			} 
		}
	});
	
});

