var homePagefadeSpeed = 0.5;
var visibleSubnavActivity = null;
var subNavFadeSpeed = 0.5;

var regionDestinationTimeout = null;
var regionDestinationId = null;
var deactivateRegionDestinationId = null;

function activateRegionDestination(id)
{
	logToFirebug('log','called activateRegionDestination(' + id + ')');
	if(null != regionDestinationTimeout && id != regionDestinationId && null != regionDestinationId && deactivateRegionDestinationId != regionDestinationId)
	{
		_deactivateRegionDestination(regionDestinationId);
	}
	
	if(id == regionDestinationId)
	{
		stopDeactivateRegionDestination(id);
	}
	
	regionDestinationId = id;
	
	$('destination_name_' + id).addClassName('active');
	//$('destination_link_' + id).addClassName('active');
	$('destination_' + id).show();
	if(!$('destination_row_' + id).visible())
	{
		Effect.Appear('destination_row_' + id, { duration: homePagefadeSpeed });
	}
	
}

function deactivateRegionDestination(d_id)
{
	logToFirebug('log','called deactivateRegionDestination(' + d_id + ')');
	deactivateRegionDestinationId = d_id;
	regionDestinationTimeout = setTimeout("_deactivateRegionDestination('" + d_id + "')",500);
}

function stopDeactivateRegionDestination(s_id)
{
	logToFirebug('log','called stopDeactivateRegionDestination(' + s_id + ')');
	if(regionDestinationTimeout != undefined)
	{
		clearTimeout(regionDestinationTimeout);
	}
}

function _deactivateRegionDestination(_d_id)
{
	logToFirebug('log','called _deactivateRegionDestination(' + _d_id + ')');
	$('destination_name_' + _d_id).removeClassName('active');
	//$('destination_link_' + _d_id).removeClassName('active');
	$('destination_' + _d_id).hide();
	if($('destination_row_' + _d_id).visible())
	{
		Effect.Fade('destination_row_' + _d_id, { duration: homePagefadeSpeed });
	}
	if($('destination_' + _d_id).visible())
	{
		Effect.Fade('destination_' + _d_id, { duration: homePagefadeSpeed });
	}
	deactivateRegionDestinationId = null;
}

function _hideRegionDestination(id)
{
	$(regionDestinationElm).hide();
	regionDestinationTimeout = null;
	regionDestinationElm = null;
	regionDestinationNameElm = null;
}

var SubNavTimeout = setTimeout('',1);
var subnavActiveElm = null;

function activateActivitySubNav(slug,elmnt,num)
{
	if(null != visibleSubnavActivity)
	{
		_deactivateActivitySubNav(visibleSubnavActivity);
	}
	
	subnavActiveElm = $(elmnt.id);
	var page_subnav_margin_width = subnavActiveElm.getWidth();
	var leftOffset = (page_subnav_margin_width * num) + num;
	
	visibleSubnavActivity = slug;
	var background_height = $('page_subnav_' + visibleSubnavActivity).getHeight() + 10;
	
	subnavActiveElm.addClassName('active');
	if(undefined != $('destinationJump'))
	{
		$('destinationJump').hide();
	}
	if(undefined != $('athleteJump'))
	{
		$('athleteJump').hide();
	}
	$('page_subnav_margin').setStyle({left: leftOffset + 'px',width: page_subnav_margin_width + 'px'});
	$('page_subnav_margin').show();
	$('page_subnav_top').show();
	$('page_subnav_background').setStyle({height: background_height + 'px'});
	$('page_subnav_background').show();
	$('page_subnav_' + visibleSubnavActivity).show();
	
}

function deactivateActivitySubNav(slug)
{
	SubNavTimeout = setTimeout("_deactivateActivitySubNav('" + slug + "')",500);
}

function stopDeactivateActivitySubNav(slug)
{
	if(SubNavTimeout != undefined)
	{
		clearTimeout(SubNavTimeout);
	}
}

function _deactivateActivitySubNav(slug)
{
	if(SubNavTimeout != undefined)
	{
		clearTimeout(SubNavTimeout);
	}
	if(undefined != $('page_subnav_' + visibleSubnavActivity))
	{
		$('page_subnav_' + visibleSubnavActivity).hide();
	}
	
	$('page_subnav_margin').hide();
	$('page_subnav_top').hide();
	$('page_subnav_background').hide();
	subnavActiveElm.removeClassName('active');
	if(undefined != $('destinationJump'))
	{
		$('destinationJump').show();
	}
	if(undefined != $('athleteJump'))
	{
		$('athleteJump').show();
	}
	
	
	visibleSubnavActivity = null;
}

function headerSearch(type)
{
	
}

var HomePageVideoTimeout = setTimeout('',1);

function showHomeVideoBar()
{
	logToFirebug('log','called showHomeVideoBar()');
	_hideHomeAthleteBar();
	$('home_is').addClassName('active');
	$('home_watch_video').addClassName('active');
	if(HomePageVideoTimeout != undefined)
	{
		clearTimeout(HomePageVideoTimeout);
	}

	if(!$('home_video_bar').visible())
	{
		Effect.Appear('home_video_bar', { duration: homePagefadeSpeed });
	}
}

function hideHomeVideoBar()
{
	logToFirebug('log','called hideHomeVideoBar()');
	HomePageVideoTimeout = setTimeout("_hideHomeVideoBar()",500);
}

function stopHideHomeVideoBar()
{
	logToFirebug('log','called stopHideHomeVideoBar()');
	if(HomePageVideoTimeout != undefined)
	{
		clearTimeout(HomePageVideoTimeout);
	}
}

function _hideHomeVideoBar()
{
	if(HomePageVideoTimeout != undefined)
	{
		clearTimeout(HomePageVideoTimeout);
	}
	if($('home_video_bar').visible())
	{
		Effect.Fade('home_video_bar', { duration: homePagefadeSpeed });
	}
	$('home_is').removeClassName('active');
	$('home_watch_video').removeClassName('active');
}

var HomePageAthleteTimeout = setTimeout('',1);

function showHomeAthleteBar()
{
	logToFirebug('log','called showHomeAthleteBar()');
	_hideHomeVideoBar();
	$('home_athlete').addClassName('active');
	$('home_athlete_name').addClassName('active');
	if(HomePageAthleteTimeout != undefined)
	{
		clearTimeout(HomePageAthleteTimeout);
	}

	if(!$('home_athlete_bar').visible())
	{
		Effect.Appear('home_athlete_bar', { duration: homePagefadeSpeed });
	}
}

function hideHomeAthleteBar()
{
	logToFirebug('log','called hideHomeAthleteBar()');
	HomePageAthleteTimeout = setTimeout("_hideHomeAthleteBar()",500);
}

function stopHideHomeAthleteBar()
{
	logToFirebug('log','called stopHideHomeAthleteBar()');
	if(HomePageAthleteTimeout != undefined)
	{
		clearTimeout(HomePageAthleteTimeout);
	}
}

function _hideHomeAthleteBar()
{
	if(HomePageAthleteTimeout != undefined)
	{
		clearTimeout(HomePageAthleteTimeout);
	}
	if($('home_athlete_bar').visible())
	{
		Effect.Fade('home_athlete_bar', { duration: homePagefadeSpeed });
	}
	$('home_athlete').removeClassName('active');
	$('home_athlete_name').removeClassName('active');
}

function hideAllHomeFeatures()
{
	_hideHomeAthleteBar();
	_hideHomeVideoBar();
}

var destinationTimeout = setTimeout('',1);
var selectedDestination = null;

function displayDestinationRow(slug)
{
	//logToFirebug('log','called showHomeAthleteBar()');
	if(destinationTimeout != undefined)
	{
		clearTimeout(destinationTimeout);
	}
	if(null != selectedDestination)
	{
		_hideDestinationRow(selectedDestination);
	}
	selectedDestination = slug;

	if(!$('destinations_' + slug + '_row').visible())
	{
		Effect.Appear('destinations_' + slug + '_row', { duration: homePagefadeSpeed });
	}
}

function hideDestinationRow(slug)
{
	//logToFirebug('log','called hideHomeAthleteBar()');
	destinationTimeout = setTimeout("_hideDestinationRow('" + slug + "')",500);
}

function stopHideDestinationRow(slug)
{
	logToFirebug('log','called stopHideHomeAthleteBar()');
	if(destinationTimeout != undefined)
	{
		clearTimeout(destinationTimeout);
	}
}

function _hideDestinationRow(slug)
{
	if(destinationTimeout != undefined)
	{
		clearTimeout(destinationTimeout);
	}
	if($('destinations_' + slug + '_row').visible())
	{
		Effect.Fade('destinations_' + slug + '_row', { duration: homePagefadeSpeed });
	}
}

var viewport = null;

function openModal(id)
{
	var viewport = document.viewport.getDimensions();
	var bodyElm = $('container');
	if(viewport.height > bodyElm.getHeight())
	{
		$('blackOut').setStyle({width: viewport.width + 'px',height: viewport.height + 'px'});
	} else {
		$('blackOut').setStyle({width: viewport.width + 'px',height: bodyElm.getHeight() + 'px'});
	}
	
	
	var offset = $('container').cumulativeScrollOffset();
	var top = offset.top + 137;
	$('OuterContainer').setStyle({top: top + 'px'});
	
	$('modal_' + id).show();
	$('blackOut').show();
	$('OuterContainer').show();
}

function closeModal(id)
{
	$('modal_' + id).hide();
	$('blackOut').hide();
	$('OuterContainer').hide();
	if(id == 'gallery')
	{
		$('gallery_container').innerHTML = '';
	}
}

function loadDestinationCopy(id)
{
	$(activeDestinationCopy + '_content').hide();
	$('a_' + activeDestinationCopy).removeClassName('active')
	$(activeDestinationCopy + '_expander').innerHTML = '+';
	
	activeDestinationCopy = id;
	
	$(activeDestinationCopy + '_content').show();
	$('a_' + activeDestinationCopy).addClassName('active')
	$(activeDestinationCopy + '_expander').innerHTML = '-';
}

function openPhotoGallery(gallery_id,image_num)
{
	openModal('gallery');
	scrollGallery(gallery_id,image_num);
}

function scrollGallery(gallery_id,image_num)
{
	new Ajax.Updater('gallery_container', '/gallery/load', {
		  parameters: { id: gallery_id,image: image_num }
		});
}

function launchVideo(video_id)
{
	openModal(video_id);
	eval('UFO.create(	FO_' + video_id + ', "videoSpot_' + video_id + '")');
}

function selectUrlJump(id)
{
	var url = $F(id);
	if(0 != url)
	{
		window.location = url;
	}
}





function logToFirebug(type,message)
{
	if(false == (typeof dev == 'undefined'))
	{
		if(false == (typeof console == 'undefined') && true === dev)
		{
			eval('console.' + type + "('" + message + "');");
		}
	}
}

function submitSearchForm()
{
	var query = '/' + form_action + '/';
	var activity = $F('activity');
	if(0 == activity)
	{
		alert('You must select an Activity.');
		return false;
	} else {
		query += activity + '/';
	}
	var region = $F('region');
	if(0 == region)
	{
		alert('You must select a Region.');
		return false;
	} else {
		query += region + '/';
	}
	var destination = $F('destination');
	if(0 != destination)
	{
		query += destination + '/';
	}
	//alert(query);
	$('package_search_form_submit').href = query;
}

function getOptions(type)
{
	var url = '/ajax/load/';
	if(type == 'regions')
	{
		if($F('activity') == 0)
		{
			$('region').innerHTML = '';
			$('destination').innerHTML = '';
			$('region').disable();
			$('destination').disable();
			return false;
		}
		url += 'activity/' + $F('activity') + '/';
		var update = 'region';
		var field = 'Regions.';
	} else if(type == 'destinations') {
		if($F('activity') == 0 || $F('region') == 0)
		{
			$('destination').innerHTML = '';
			$('destination').disable();
			return false;
		}
		url += 'activity/' + $F('activity') + '/region/' + $F('region') + '/';
		var update = 'destination';
		var field = 'Destinations.';
	}
	
	new Ajax.Updater(update,url,{onSuccess: function(){$(update).enable();},onFailure: function(){$(update).disable();}});
	/*
	new Ajax.Request(url, {
		method: 'get',
		onSuccess: function(transport) {
			$(update).innerHTML = transport.responseText;
			$(update).enable();
		},
		onFailure: function(transport) {
			$(update).innerHTML = '';
			$(update).disable();
			alert('Your search did not return any ' + field);
		}
	});
	*/
}

function enableSearchField()
{
	alert(update);
	$(update).enable();
}

function disableSearchField()
{
	$(update).disable();
}

function clearFormField(elm,action,default_val)
{
	if(elm.value == default_val && action == 'focus')
	{
		elm.value = '';
	} else if(elm.value == '' && action == 'blur') {
		elm.value = default_val;
	}
}

function showDownloads(download_id)
{
	plus_id = 'media_downloads_plus_' + download_id
	download_id = 'media_downloads_' + download_id;
	
	var elm = $(download_id);
	if(elm.visible())
	{
		Effect.BlindUp(download_id, { duration: .5 });
		$(plus_id).innerHTML = '+';
	} else {
		Effect.BlindDown(download_id, { duration: .5 });
		$(plus_id).innerHTML = '-';
	}
}

function headerNews(direction)
{
	var x = 0;
	if(direction == 'next' && current_head_news < num_head_news)
	{	
		x = $('header_news_center').getWidth();
		current_head_news++;
	} else if(direction == 'last' && current_head_news != 0) {
		x = $('header_news_center').getWidth() * -1;
		current_head_news--;
	}
	if(current_head_news == 1)
	{
		$('header_news_right_a').hide();
		logToFirebug('log',"hide right :: current_head_news: " + current_head_news);
	} else {
		logToFirebug('log',"show right :: current_head_news: " + current_head_news);
		$('header_news_right_a').show();
	}
	if(current_head_news == num_head_news)
	{
		logToFirebug('log',"hide left :: current_head_news: " + current_head_news);
		$('header_news_left_a').hide();
	} else {
		logToFirebug('log',"show left :: current_head_news: " + current_head_news);
		$('header_news_left_a').show();
	}
	if(x != 0)
	{
		new Effect.Move($('header_news_inner'), { x: x });
	}
}


function popMail(host,user) {
	var acct = user +'@'+ host;
	self.location.href = 'mailto:'+ acct;
}
function linkMail(host,user,cls) {
	document.write('<a href="javascript:popMail(\''+ host +'\',\''+ user +'\');"');
	if (cls) {
		document.write(' class="'+ cls +'"');
	}
	document.write('>');
	showMail(host,user);
	document.write('</a>');
}
function showMail(host,user) {
	var acct = user +'@'+ host;
	document.write(acct);
}

function clearText(thefield){
	if (thefield.defaultValue==thefield.value)
	thefield.value = ""
}

var pt = setTimeout('',1);
var p_start = 1;
var show_length = 5;

function main_cycle()
{
	if(num_features < 2)
	{
		//alert('none');
		return '';
	}
	pt = setTimeout('_main_cycle();',show_length * 1000);
	
}

function _main_cycle()
{
	p_start++;
	if(p_start > num_features)
	{
		p_start = 1;
	}
	swapFeature(p_start);
	pt = setTimeout('_main_cycle();',(show_length * 1000) + (feature_fade_duration * 1000));
}

var currentFeature = 1;
var feature_fade_duration = 1.5;

function swapFeature(id)
{
	if(id != currentFeature)
	{
		Effect.Appear('body_image_' + id, { duration: feature_fade_duration });
		//Effect.Appear('body_image_tag_' + id, { duration: feature_fade_duration });
		Effect.Fade('body_image_' + currentFeature, { duration: feature_fade_duration });
		//Effect.Fade('body_image_tag_' + currentFeature, { duration: feature_fade_duration });
		currentFeature = id;
	}
}

