var _g_quote_interval = null;

jQuery(document).ready(
	function()
	{
		//homepage quotes
		jQuery('#image-home ul').cycle({timeout:8000});
		
		//contact name input event handling
		jQuery('#contact-name')
			.focus( 
				function() 
				{ 
					if (jQuery(this).val().toLowerCase() == 'your name') 
						jQuery(this).val(''); 
				} 
			)
			.blur( 
				function() 
				{ 
					if (jQuery(this).val().replace(/\s/g,'').length == 0) 
						jQuery(this).val('Your Name'); 
				} 
			);
		
		//contact phone number input event handling
		jQuery('#contact-phone-number')
			.focus( 
				function() 
				{ 
					if (jQuery(this).val().toLowerCase() == 'phone number') 
						jQuery(this).val(''); 
				} 
			)
			.blur( 
				function() 
				{ 
					if (jQuery(this).val().replace(/\s/g,'').length == 0) 
						jQuery(this).val('Phone Number'); 
				} 
			);
			
		//contact email input event handling
		jQuery('#contact-email')
			.focus( 
				function() 
				{ 
					if (jQuery(this).val().toLowerCase() == 'email address') 
						jQuery(this).val(''); 
				} 
			)
			.blur( 
				function() 
				{ 
					if (jQuery(this).val().replace(/\s/g,'').length == 0) 
						jQuery(this).val('Email Address'); 
				} 
			);
			
		//contact additional info input event handling
		jQuery('#contact-additional-info')
			.focus( 
				function() 
				{ 
					if (jQuery(this).val().toLowerCase() == 'message...') 
						jQuery(this).val(''); 
				} 
			)
			.blur( 
				function() 
				{ 
					if (jQuery(this).val().replace(/\s/g,'').length == 0) 
						jQuery(this).val('Message...'); 
				} 
			);
			
		jQuery('#mailinglist_form').submit(
			function()
			{
				return __submit_newsletter_form();
			}
		);
		
		jQuery('#input-button-register').attr('disabled','disabled').fadeTo(100,0.3);
		
		jQuery('#register-input-agree').click(
			function()
			{
				if (jQuery(this).is(':checked'))
					jQuery('#input-button-register').removeAttr('disabled').fadeTo(100,1);
				else
					jQuery('#input-button-register').attr('disabled','disabled').fadeTo(100,0.3);
			});
		
		//show 2nd lever navigation
		if (location.pathname)
		{
			var root = 'http://therulerapproach.org/index.php/';
			var path = location.pathname.split('/');
			var section = path[2] || '';
			
			if (section.toLowerCase() == 'about' || section.toLowerCase() == 'programs-and-services' || section.toLowerCase() == 'marvin-maurer-award')
				jQuery('#nav a[href="' + root + section + '/"]').next().show();
		}
		
		//video play overlay
		if (!jQuery.browser.msie)
		{
			jQuery('#text div.content-right a.flash').each(
				function(index)
				{
					var offset = jQuery(this).find('img').offset();
					var height = jQuery(this).find('img').height();
					var width = jQuery(this).find('img').width();
					
					jQuery('#text div.content-right').append('<a href="" class="video-play"></a>');
					jQuery('#text div.content-right a.video-play')
						.eq(index)
						.css({
							'left': offset.left + 'px',
							'top': offset.top + 'px'
						})
						.mouseover(
							function()
							{
								jQuery(this)
									.fadeTo(150,0.5);	
							}
						)
						.mouseout(
							function()
							{
								jQuery(this).fadeTo(150,1.0);
							}
						)
						.click(
							function()
							{
								var invoker = jQuery('#text div.content-right a.flash').eq(index);
								jQuery(this).attr('href',jQuery(invoker).attr('href'));
								
								Lightview.show({
									href: jQuery(invoker).attr('href'),
									rel: 'flash',
									options: {
										topclose: true,
										width: 800,
										height: 450
									}
								});
									
								return false;
							}
						);
					
				}
			);
		}
	}
);

function __download_images()
{
	var _l_form = document.getElementById('images-form');
	var _l_images = [];
	
	if (_l_form)
	{
		for (i=0 ; i<_l_form.elements.length ; i++)
		{
			if (_l_form.elements[i].type == 'checkbox' && _l_form.elements[i].name == 'images[]' && _l_form.elements[i].checked)
				_l_images.push(_l_form.elements[i].value);
		}
		
		if (_l_images.length == 0)
		{
			alert('Please select at least one image ! ');
			return false;
		}
		else
		{
			_l_form['mode'].value = 'images';
			_l_form.action = '/members/download.php';
			_l_form.method = 'post';
			_l_form.submit();
		}
	}
}

function __handle_role(role)
{
	var _l_form = document.getElementById('register-form');
	
	if (_l_form)
	{
		if (role == 'sl')
		{
			jQuery('#register-row-input-school-name').hide();
			jQuery('#register-row-input-grades-taught').hide();
			jQuery('#register-row-input-subject-id').hide();
			jQuery('#register-row-input-years-at-district').show();
		}
		else if (role == 't')
		{
			jQuery('#register-row-input-school-name').show();
			jQuery('#register-row-input-grades-taught').show();
			jQuery('#register-row-input-subject-id').show();
			jQuery('#register-row-input-years-at-district').hide();
		}
		else
		{
			jQuery('#register-row-input-school-name').hide();
			jQuery('#register-row-input-grades-taught').hide();
			jQuery('#register-row-input-subject-id').hide();
			jQuery('#register-row-input-years-at-district').hide();
		}
	}
}

function __handle_subject_other(is_checked)
{
	var _l_form = document.getElementById('register-form');
	
	if (_l_form)
	{
		if (is_checked)
			_l_form['subject_other'].disabled = false;
		else
			_l_form['subject_other'].disabled = true;
	}
	
}

function __submit_contact_form()
{
	var _l_form = document.getElementById('contact-form');
	var _l_valid = true;
	
	if (_l_form)
	{
		if (jQuery(_l_form['name']).val().replace(/\s+/g,'').length == 0 || jQuery(_l_form['name']).val().toLowerCase() == 'your name')
		{
			_l_valid = false;
			alert('Please enter your name ! ');
			_l_form['name'].focus();
		}
		else if (jQuery(_l_form['email']).val().replace(/\s+/g,'').length == 0 || jQuery(_l_form['email']).val().toLowerCase() == 'email address')
		{
			_l_valid = false;
			alert('Please enter your email address ! ');
			_l_form['email'].focus();
		}
		else if (jQuery(_l_form['contact-additional-info']).val().replace(/\s+/g,'').length == 0 || jQuery(_l_form['contact-additional-info']).val().toLowerCase() == 'message...')
		{
			_l_valid = false;
			alert('Please enter your message ! ');
			_l_form['additional_info'].focus();
		}
	}
	
	return _l_valid;
}

function __submit_newsletter_form()
{
	var _l_form = document.getElementById('mailinglist_form');
	var _l_valid = true;
	
	if (_l_form)
	{
		if (jQuery(_l_form['email']).val().replace(/\s+/g,'').length == 0 || jQuery(_l_form['email']).val().toLowerCase() == 'email address')
		{
			_l_valid = false;
			alert('Please enter your email address ! ');
			_l_form['email'].focus();
		}
	}
	
	return _l_valid;
}

function __submit_trainer_upgrade_form(decision)
{
	var _l_form = document.getElementById('trainer-upgrade-form');
	
	if (_l_form)
	{
		_l_form['decision'].value = decision;
		_l_form.submit();
	}
}

jQuery.noConflict();

