undefined;

var		menus;
var		dissolver;

function initialize()
{
	menus = new Popup_Menus( 330 );
	menus.add( 'nav_main_link_1', 'nav_join_the_club' );
	menus.add( 'nav_main_link_2', 'nav_sports_and_spa' );
	menus.add( 'nav_main_link_3', 'nav_youth_programs' );
	menus.add( 'nav_main_link_4', 'nav_banquet_facilities' );
	menus.add( 'nav_main_link_5', 'nav_corporate_programs', 'right' );
	
	if( window.location.href == "http://richmondhillcountryclub.com/" ||
		window.location.href == "http://www.richmondhillcountryclub.com/" ||
		window.location.href == "http://richmondhillcountryclub.com/sports_and_spa/martial_arts" ||
		window.location.href == "http://www.richmondhillcountryclub.com/sports_and_spa/martial_arts" ||
		window.location.href == "http://richmondhillcountryclub.com/youth_programs/martial_arts" ||
		window.location.href == "http://www.richmondhillcountryclub.com/youth_programs/martial_arts"
		)
		dissolver_init();
}


function dissolver_init()
{
	dissolver = new Dissolver();
	dissolver.initialize();
	setTimeout( "dissolver.start()", 3000 );
	// setTimeout( "dissolver.stop()", 30000 );
}

// -------------------------------------------------------------------------- //
/*	Dissolver Class
	Version: 1.0.0
	Date: 2008.06.30
	
	Copyright 2008 Cognizant Consulting Inc.
	http://cogni.ca/
*/
// -------------------------------------------------------------------------- //

function Dissolver()
{
	if( !Dissolver.Instances )				// Record instances as part of the
		Dissolver.Instances = [];			// object so setTimeout can access
	this.id = Dissolver.Instances.length;	// the correct instance directly,
	Dissolver.Instances[this.id] = this;	// without needing to know external
											// variable names.
	this.disolving = false;
	this.current_id = 1;
	this.next_id = 2;
	this.max_id = 4;
	this.step = 100;
	this.step_size = 2;
	this.delay = 5 * 1000;	// delay is in milliseconds
	this.targets = [""];
	this.interval = 0;
	
	this.initialize = function()
	{
		for( var i = 1; i <= this.max_id; i++ )
		{
			this.targets[i] = document.getElementById( 'dissolve' + i );
		}
	}
	
	this.stop = function()
	{
		this.disolving = false;
	}

	this.pause = function()
	{
		if( this.disolving )
			setTimeout( 'Dissolver.Instances['+this.id+'].resume()', this.delay );
	}

	this.start = function()
	{
		if( !this.disolving )
		{
			this.disolving = true;
			this.resume();
		}
	}
	
	this.resume = function()
	{
		this.interval = setInterval( 'Dissolver.Instances['+this.id+'].dissolve()', 20 );
	}

	this.dissolve = function()
	{
		try
		{
			if( this.step >= 100 )
			{
				this.step = 100;	// Safety
				this.targets[this.current_id].className = 'front';
				this.targets[this.next_id].className = 'back';
				this.targets[this.current_id].style.opacity = (this.step / 100);
				this.targets[this.current_id].style.filter = 'alpha(opacity=' + this.step + ')';
				this.targets[this.next_id].style.opacity = (this.step / 100);
				this.targets[this.next_id].style.filter = 'alpha(opacity=' + this.step + ')';
			}
			
			this.step -= this.step_size;
			this.targets[this.current_id].style.opacity = (this.step / 100);
			this.targets[this.current_id].style.filter = 'alpha(opacity=' + this.step + ')';
			
			if( this.step <= 0 )
			{
				clearInterval( this.interval );
				this.interval = 0;
				this.current_id = this.next_id;
				this.next_id = (this.current_id == this.max_id) ? 1 : this.current_id + 1;
				this.step = 100;
				this.pause();
			}
		}
		catch( e )
		{
			clearInterval( this.interval );
			this.interval = 0;
			alert( "Error:\n" + e.toString() );
		}
	}
}


// ---------------------------------------------------------------------------- //
/*	validate_form
	
*/
// ---------------------------------------------------------------------------- //

function validate_form( form )
{
	var			inputs = form.getElementsByTagName("input");
	
	return validate_inputs( inputs );
}


// ---------------------------------------------------------------------------- //
/*	validate_ids
	
*/
// ---------------------------------------------------------------------------- //

function validate_ids( ids )
{
	var			inputs = [];
	var			id_count = ids.length;
	
	for( var i = 0; i < id_count; i++ )
	{
		var		elem = document.getElementById( ids[i] );
		
		if( elem != undefined )
			inputs[inputs.length] = elem;
		else
			alert( ids[i] );
	}
	
	return validate_inputs( inputs );
}


// ---------------------------------------------------------------------------- //
/*	validate_inputs
	
	This function accepts a series of HTML objects and checks their value.
	bother with <textarea> yet) and that these fields will have labels with ids
	that are the same as the field + "_req" (e.g., f_name & f_name_req) if that
	field is required.
	
	It also assumes any email address in the form will begin 'f_email'.
	
	Any field that does not contain data will have its label's class changed
	to "required". The class name is cleared every time data is found in a 
	required fields, so class names shouldn't carry class names of their own.
*/
// ---------------------------------------------------------------------------- //

function validate_inputs( inputs )
{
	var			n = inputs.length;
	var			form_ok = true;
	
	for( i = 0; i < n; i++ )
	{
		var		id = inputs[i].id;
		var		req = $( id + "_req" );
		
		if( req != undefined )
		{
			var		value = inputs[i].value;
			
			if( inputs[i].type == 'radio' )	// Note: Radio Group name must be same as id.
				value = get_radio_value( inputs[i].name );
			
			if( inputs[i].type == 'checkbox' )
				value = inputs[i].checked ? inputs[i].value : '';
			
			if( value.trim() == '' )
			{
				req.className = 'required';
				form_ok = false;
			}
			else
			{
				if( id.substr(0, 7) == 'f_email' )
				{
					if( value.search( "^[A-Za-z0-9._%-]+@[A-Za-z0-9.-_]+\\.[A-Za-z]{2,4}$" ) == -1 )
					{
						req.className = 'required';
						form_ok = false;
					}
					else
						req.className = '';
				}
				else
					req.className = '';
			}
		}
	}
	
	if( !form_ok )
		alert( "Please complete the required fields, highlighted in red." );
	
	return form_ok;
}
	

// -------------------------------------------------------------------------- //
/*	Popup_Menus Class

*/
// -------------------------------------------------------------------------- //

function Popup_Menus( offset )
{
	this.menus = [];
	this.menus_offset = offset;
	this.extra_menu_width = 30;
	
	this.add = function( link_id, menu_id, align_edge )
	{
		var			menu_elem = document.getElementById( menu_id );
		var			link_elem = document.getElementById( link_id );
		
		if( menu_elem != undefined && link_elem != undefined )
		{
			var		menu_index = this.menus.length;
			
			menu_elem.parent_link = link_elem;
			this.menus[ menu_index ] = menu_elem;
			
			link_elem.menu_index = menu_index;
			menu_elem.menu_index = menu_index;
			
			if( link_elem.offsetParent )
			{
				
				menu_elem.style.width = (link_elem.parentNode.clientWidth + this.extra_menu_width) + "px";
				
				if( align_edge == 'right' )
					menu_elem.style.left = (this.menus_offset + link_elem.parentNode.offsetLeft - this.extra_menu_width - 2) + "px"; // I'm uncertain why an extra 2px must be subtracted.
				else
					menu_elem.style.left = (this.menus_offset + link_elem.parentNode.offsetLeft) + "px";
				
			}
		}
	}
	
	this.show = function( i )
	{
		if( i < this.menus.length )
		{
			// this.hide_all_others( i );
			this.menus[i].style.display = "block"
			this.menus[i].parent_link.className = "hover";
		}
	}
	
	this.hide = function( i )
	{
		if( i < this.menus.length )
		{
			this.menus[i].style.display = "none";
			this.menus[i].parent_link.className = "";
		}
	}
	
	this.hide_all_others = function( but_this )
	{
		var		len = this.menus.length;
		
		for( var i = 0; i < len; i++ )
		{
			if( i != but_this )
				this.menus[i].style.display = "none";
		}
	}
}



// ---------------------------------------------------------------------------- //
/*	$
*/
// ---------------------------------------------------------------------------- //

function $()
{
	if( arguments.length == 1 && typeof( arguments[0] ) == 'string' )
		return document.getElementById( arguments[0] );
}


// ---------------------------------------------------------------------------- //
/*	String.trim()
*/
// ---------------------------------------------------------------------------- //

String.prototype.trim = function()
{
	var	str = this.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}


// ---------------------------------------------------------------------------- //
/*	go_to
	
	Takes the window base into consideration for browsers that 
	don't think to (i.e., IE).
*/
// ---------------------------------------------------------------------------- //

function go_to( relative_path )
{
	var		base_tags = document.getElementsByTagName( 'base' );
	var		base = '';
	
	if( base_tags != undefined )
		base = base_tags[0].href;
	
	window.location.href = base + relative_path
}


// ---------------------------------------------------------------------------- //
/*	lz_swap
*/
// ---------------------------------------------------------------------------- //

function lz_swap( hide, show )
{
	lz_hide( hide );
	lz_show( show );
}


// ---------------------------------------------------------------------------- //
/*	lz_hide
*/
// ---------------------------------------------------------------------------- //

function lz_hide( id )
{
	if( $(id) != undefined )
	{
		$(id).old_display = $(id).style.display;
		$(id).style.display = 'none';
	}
}


// ---------------------------------------------------------------------------- //
/*	lz_show
*/
// ---------------------------------------------------------------------------- //

function lz_show( id )
{
	if( $(id) != undefined )
	{
		var old_display = $(id).style.display;
		
		if( arguments.length > 1 )		
			$(id).style.display = arguments[1];

		else if( $(id).old_display == undefined || $(id).old_display == '' )
			$(id).style.display = 'block';

		else
			$(id).style.display = $(id).old_display;
			
		$(id).old_display = old_display;
	}
}


// -------------------------------------------------------------------------- //
/*	get_form_float

*/
// -------------------------------------------------------------------------- //

function get_form_float( id, reset_value )
{
	var			id_elem = document.getElementById( id );
	var			id_value = 0;
	
	if( id_elem !== undefined )
	{
		id_value = parseFloat( id_elem.value );

		if( isNaN( id_value ) || (id_value < 0) )
		{
			id_value = 0;
			id_elem.value = reset_value;
			try{ id_elem.focus(); } catch (e) {} 
		}
		else
			id_elem.value = id_value; // Send back the parsed data to clean the field.
	}
	
	return id_value;
}

// -------------------------------------------------------------------------- //
/*	get_form_int

*/
// -------------------------------------------------------------------------- //

function get_form_int( id, reset_value )
{
	var			id_elem = document.getElementById( id );
	var			id_value = 0;
	
	if( id_elem !== undefined )
	{
		id_value = parseInt( id_elem.value );

		if( isNaN( id_value ) || (id_value < 0) )
		{
			id_value = reset_value;
			id_elem.value = reset_value;
			try{ id_elem.focus(); } catch (e) {} 
		}
		else
			id_elem.value = id_value; // Send back the parsed data to clean the field.
	}
	
	return id_value;
}


//	-----------------------------------------------------------------------	//
//	get_radio_value
//	
//	Get the value of a radio button by element name.
//	-----------------------------------------------------------------------	//

function get_radio_value( element_name )
{
	var		elem = document.getElementsByName( element_name );
	
	for( var i=0; i < elem.length; i++)
	{
		
		if( elem.item(i).checked )
			return elem.item(i).value;
	}
	
	return '';
}

//	-----------------------------------------------------------------------	//
//	set_radio_value
//	
//	Set the value of a radio button by element name.
//	-----------------------------------------------------------------------	//

function set_radio_value( element_name, element_value )
{
	var		elem = document.getElementsByName( element_name );
	
	for( var i=0; i < elem.length; i++)
	{
		alert( elem.item(i).value + ' - ' + element_value );
		if( elem.item(i).value == element_value )
			elem.item(i).checked = true;
		else
			elem.item(i).checked = false;
	}
}

//	-----------------------------------------------------------------------	//
//	radio_input_disabled
//	
//	Set the value of a radio button by element name.
//	-----------------------------------------------------------------------	//

function radio_input_disabled( element_name, state )
{
	var		elem = document.getElementsByName( element_name );
	var		len = elem.length;
	
	for( var i=0; i < len; i++)
	{
		if( state == true )
			elem.item(i).checked = false;
		elem.item(i).disabled = state;
	}
}

