//=======================================================================================================
//-------------------------------------------------------------------------------------------------------
//	Custom Javascript functions
//-------------------------------------------------------------------------------------------------------
//=======================================================================================================

var currentTallest;
var stop_rotating = false;
var current_cycle = 1;
var curr_timeout;

//=======================================================================================================
//-------------------------------------------------------------------------------------------------------
//	Custom Javascript functions
//-------------------------------------------------------------------------------------------------------
//=======================================================================================================
function expand(param)
{
		param.style.display=(param.style.display=="none")?"block":"none";
}

function expandById(id)
{
	expand(document.getElementById(id));
}

//*********************************************************************************************************
//		Function:	startRotation()
//---------------------------------------------------------------------------------------------------------
//	What it does:	start the homepage rotation
//*********************************************************************************************************
function startRotation_Homepage()
{
	$('.home_flash').css({'display':'none'});
	$('.home_banners').css({'display':'block'});
	//$('.home_banners').css({'visibility':'visible'});
	startRotation()
}

//*********************************************************************************************************
//		Function:	startRotation()
//---------------------------------------------------------------------------------------------------------
//	What it does:	start the homepage rotation
//*********************************************************************************************************
function startRotation()
{
	total_banners = $('.home_banner').size();
	curr_timeout = setTimeout( "NextHomeBanner(1)", speed );
}

//*********************************************************************************************************
//		Function:	NextHomeBanner()
//---------------------------------------------------------------------------------------------------------
//	What it does:	show the next homepage banner
//*********************************************************************************************************
function NextHomeBanner(q)
{
	if(q >= total_banners )
	{
		q	= 	1;
		if( banner_cycles && (current_cycle  >=banner_cycles) && !stop_rotating)
		{
			return;
		}
		current_cycle++;
	}
	else
	{
		q++;
	}

	var next		= '#home_banner_'+q;
	var next_thumb	= '#thumb_nav_div_'+q;
	
	var link = $(next+' a').attr('href');
	$('#home_banner_link').attr('href',link);

	// unset current thumb and banner image
	$('.thumb_nav_div_active').removeClass('thumb_nav_div_active'); 
	$(".home_banner:visible").toggle(); 

	// set current thumb and banner image
	$(next).toggle();
	$(next_thumb).addClass('thumb_nav_div_active');
	
	if( stop_rotating )
	{
		return;
	}
	curr_timeout = setTimeout( "NextHomeBanner("+q+")", speed );
}

//*********************************************************************************************************
//		Function:	GoToHomeBanner()
//---------------------------------------------------------------------------------------------------------
//	What it does:	show a certain homepage banner and stop the animation
//*********************************************************************************************************
function GoToHomeBanner(q)
{
	stop_rotating = true;
	clearTimeout(curr_timeout);
	NextHomeBanner(q);
}


function resetlink(selector_id)
{
	$('#'+selector_id).click();

	$('#'+selector_id).removeClass('sm2_playing');
}


//*********************************************************************************************************
//		Function:	ToggleVideo()
//---------------------------------------------------------------------------------------------------------
//	What it does:	show/hide the video
//*********************************************************************************************************
function ToggleVideo( id )
{
	if( !id)
	{
		$("#site_dim").toggle();
		$('#movie_container').toggle();
		$("#movie_container").html('');
		return;
	}

	$("#site_dim").toggle();
	$('#movie_container').toggle();
	

	var d = new Date();
	var video_url = '/site/aja_video.php?a='+id+'&t='+d.getTime();
	$.ajax({ 
		url: video_url
		, success: function(html)
			{
				$("#movie_container").html(html); 
				//alert(html);
			}
	});
}

function TogglePopupDiv( )
{
	$("#site_dim").toggle();
	$('#popup_div_container').toggle();

}

//*********************************************************************************************************
//		document.ready:::::::
//---------------------------------------------------------------------------------------------------------
//*********************************************************************************************************
$(document).ready(function(){
	//---------------------------------------------------------------------------------------------------------
	// Togglable sidebar
	//---------------------------------------------------------------------------------------------------------
		$('a.toggler').toggle(function(event) {
			event.preventDefault();
			$(this).next('ul').slideDown();
		}, function(event) {
			event.preventDefault();
			$(this).next('ul').slideUp();
		});

	$('#share-button').toggle(
		function(){ 
			$('#share-button-image0').toggle();
			$('#share-button-image1').toggle();
			$('#share-button-image').attr('src','/site/images/share1.png');
			$('#social-media-links').toggle();
			if( is_ie6 )
			{
				DD_belatedPNG.fix('#share-button-image1,#social-media-links-bottom');
			}

			return false;
		},
		function(){ 
			$('#share-button-image0').toggle();
			$('#share-button-image1').toggle();
			$('#social-media-links').toggle();
			return false;
		});

	/*$('#newsletter-signup-link').click(function(){ 
		$('#newsletter-signup').toggle();
		return false;
	});*/

 // track external links
	$('a').each(function() {
		var a = $(this);
		var href = a.attr('href');

		// see if the link is external
		if ( href &&  (href.substring(0,4) == 'http' )   && (! href.match(document.domain)))
		{
			// if so, add the GA tracking code
			a.click(function() {
			pageTracker._trackPageview('/external-link/' + href);
			});
		}
	 
	});


	$(".sm2_link").toggle(
		function (event) {
			event.preventDefault();
			
			$(this).removeClass('sm2_paused');
			$(this).addClass('sm2_playing');
			
			var id = $(this).attr('id').substring(7);

			var selector_id = $(this).attr('id');

			soundManager.play(id,{
				onfinish:function(){
					resetlink(selector_id);
				}
			});
		},
		function (event) {
			event.preventDefault();
			$(this).removeClass('sm2_playing');
			$(this).addClass('sm2_paused');

			var id = $(this).attr('id').substring(7);
			soundManager.stop(id);
		}
	);

	//---------------------------------------------------------------------------------------------------------
	//		hover image
	//---------------------------------------------------------------------------------------------------------
		$('.button-image').hover( 
			function()
			{
				if( $(this).attr('hover_image'))
				{
					$(this).attr('src',$(this).attr('hover_image'));
				}
			},
			function()
			{
				if( $(this).attr('normal_image'))
				{
					$(this).attr('src',$(this).attr('normal_image'));
				}
			}
		);

	//---------------------------------------------------------------------------------------------------------
	//		remove text from the textboxes when one focuses on the textbox
	//---------------------------------------------------------------------------------------------------------
    $('.label_in_textbox').each(function(){
		$(this).attr('label',$(this).val());

		$(this).focus(function(){
			var label = $(this).attr('label');
			var value = $(this).val();
			
			//alert('l='+label +' v='+value);
			if( label == value )
			{
				$(this).val('');
			}
			
		});

		$(this).blur(function(){
			var label = $(this).attr('label');
			var value = $(this).val();

			if( !value )
			{
				$(this).val(label);
			}
		});
    });
	
//---------------------------------------------------------------------------------------------------------
// IE 6
//---------------------------------------------------------------------------------------------------------
	if( is_ie6 )
	{
		//DD_belatedPNG.fix('.png_fix');
	}

});
