//=======================================================================================================
//-------------------------------------------------------------------------------------------------------
//	Custom Javascript functions
//-------------------------------------------------------------------------------------------------------
//=======================================================================================================

var currentTallest;
var stop_rotating = false;
var current_cycle = 1;
var curr_timeout;


//*********************************************************************************************************
//		Function:	show_item()
//---------------------------------------------------------------------------------------------------------
//	What it does:	hide the old item and show the new item
//*********************************************************************************************************
function show_item( widget_num,old_num,new_num)
{
	var current= '#current_'+widget_num;
	var old_id =	'#item_'+widget_num +'_' + old_num;
	var new_id =	'#item_'+widget_num +'_' + new_num;

	//$(old_id).css({"visibility":"hidden"});
	//$(new_id).css({"visibility":"visible"});
	$(old_id).css({"display":"none"});
	$(new_id).css({"display":"block"});

	$(current).html(new_num);
}

//*********************************************************************************************************
//		Function:	next_item()
//---------------------------------------------------------------------------------------------------------
//	What it does:	gets the next rotating widget item 
//*********************************************************************************************************
function next_item( widget_num )
{
	var total_id		= '#total_'+widget_num;
	var current_id		= '#current_'+widget_num;
	
	var total_items		= $(total_id).html();
	var old_num		= $(current_id).html();
	// make sure this is an int.
	old_num = old_num*1;
	

	if(old_num >= total_items )
	{
		new_num	= 	1;	
	}
	else
	{
		new_num = old_num + 1;
	}

	show_item(widget_num,old_num,new_num);
}

//*********************************************************************************************************
//		Function:	prev_item()
//---------------------------------------------------------------------------------------------------------
//	What it does:	scrolls to a current item on the timeline
//*********************************************************************************************************
function prev_item(widget_num)
{
	var total_id		= '#total_'+widget_num;
	var current_id		= '#current_'+widget_num;

	var total_items		= $(total_id).html();
	var old_num		= $(current_id).html();

	// make sure this is an int.
	old_num = old_num*1;

	if(old_num == 1)
	{
		new_num	= 	total_items ;	
	}
	else
	{
		new_num = old_num - 1;
	}

	show_item(widget_num,old_num,new_num);
}

/*
//*********************************************************************************************************
//		Function:	toggle_contact_form()
//---------------------------------------------------------------------------------------------------------
//	What it does:	show/hide the contact form
//*********************************************************************************************************
function toggle_contact_form()
{
	url = '/contact/contact-form-inc.php';
	$("#site_dim").toggle();
	$("#contact_us_form_container").toggle();
}*/

//*********************************************************************************************************
//		Function:	toggle_video()
//---------------------------------------------------------------------------------------------------------
//	What it does:	show/hide the video
//*********************************************************************************************************
function toggle_video( id )
{
	if( !id)
	{
		return;
	}

	var div_id = '#video_'+id;
	$("#site_dim").toggle();
	$("#site_dim").css({'width':screen.width+'px'});
	
	$(div_id).toggleClass('show_mov');
	//$(div_id).toggle();
}

//*********************************************************************************************************
//		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:	toggle_search_text()
//---------------------------------------------------------------------------------------------------------
//	What it does:	 set textbox to null when someone clicks on the textbox 
//*********************************************************************************************************
var field_values = new Array();
function toggle_search_text( findtext , status_field_id)
{
	search_text_status	= document.getElementById(status_field_id);
	
	if( search_text_status.value == '1'  )
	{
		field_values[status_field_id] = findtext.value;
		findtext.value = '';
		search_text_status.value = 0;
	}
	else if( !findtext.value )
	{
		if( !field_values[status_field_id] )
		{
			field_values[status_field_id] = 'Search';
		}
		findtext.value = field_values[status_field_id];
		search_text_status.value = 1;
	}
}

//*********************************************************************************************************
//		Function:	timeline_goto()
//---------------------------------------------------------------------------------------------------------
//	What it does:	scrolls to a current item on the timeline
//*********************************************************************************************************
function timeline_goto(q)
{
	var offset = ((q-1)*795);
	$('.timeline_nav_active').toggleClass('timeline_nav_active');
	
	$('#timeline_roller').animate({'left':'-'+offset+'px'});
	$('#timeline_nav_item_'+q).toggleClass('timeline_nav_active');
}

//*********************************************************************************************************
//		document.ready:::::::
//---------------------------------------------------------------------------------------------------------
//*********************************************************************************************************
$(document).ready(function(){
	if( is_ie6 )
	{
		DD_belatedPNG.fix('.png_fix,.banner');
	}

	//*************************************************************************************************
	//	Sidebar forms
	//*************************************************************************************************
	//	Sidebar Toggle
	$('.toggler').toggle(function(event) {
		event.preventDefault();
		$(this).parent().next('.toggle').slideDown();
	}, function(event) {
		event.preventDefault();
		$(this).parent().next('.toggle').slideUp();
	});

	//	Remove the label on focus
	$('.widget .textfield').focus(function(){
		var title = $(this).attr('title');
		var value = $(this).attr('value');
		if (title && value == title) {
			$(this).val('');
		};
	});

	//	IF nothing is entered add the label back on blur
	$('.widget .textfield').blur(function(){
		var title = $(this).attr('title');
		var value = $(this).attr('value');
		if (title && value == '') {
			$(this).val(title);
		};
	});
	
	//	Clear the labels on submit
	$('.widget form button').click(function(event){
		$(this).parents('form').find('input').each(function(){
			var title = $(this).attr('title');
			var value = $(this).attr('value');
			if (title && value == title) {
				$(this).val('');
			};
		});
		$(this).submit();
	});

//*************************************************************************************************
//*************************************************************************************************
// for all images change the following to css rules
//  Change Vspace to margin left/right 
//  Change hspace to margin top/right 
//  Change border to   
//*************************************************************************************************
//*************************************************************************************************

	 $("table#body img").each(function (i){
		
		var vspace = $(this).attr('vspace');
		var hspace = $(this).attr('hspace');
		var border = $(this).attr('border');
		
		if( hspace >= 1 )
		{
			$(this).css({'margin-left':hspace+'px', 'margin-right':hspace+'px'});
		}
		
		if( vspace >= 1 )
		{
			$(this).css({'margin-top':vspace+'px', 'margin-bottom':vspace+'px'});
		}

		if( border >= 1 )
		{
			$(this).css('border',border+'px solid black');
		}

	 });

	//---------------------------------------------------------------------------------------------------------
	//		make sure rotating items height is uniform so the prev/next links dont move up and down.
	//---------------------------------------------------------------------------------------------------------
	$('.rotating_items').each(function(q){
		currentTallest = 0;
		$(this).find('div.rotating_item').each(function(){
			if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
			//alert($(this).height());
		});
		currentTallest = (currentTallest*1) + 6;

		//$(this).css({"height":currentTallest +'px !important;'});
		$(this).height(currentTallest);
		///alert($(this).attr('id') + currentTallest);
	});

	//---------------------------------------------------------------------------------------------------------
	//		show/hide web 2.0 links
	//---------------------------------------------------------------------------------------------------------
    $('.web20box a.sharelink').each(function(){
        var share_link = $(this);
        share_link.toggle( 
            function(){
                share_link.parents('.web20box').toggleClass('web20box-expanded').find('.buttons').show();
                return false;
            },
            function(){
                share_link.parents('.web20box').toggleClass('web20box-expanded').find('.buttons').hide();
                return false;
            }
        );

    });

	$('#site_dim').css({'visibility':'visible','display':'none'});
	
	var fileTypes = ['doc','xls','pdf','mp3'];
 
	$('a').each(function() {
		var a = $(this);
		var href = a.attr('href');
		if(href)
		{		
			var hrefArray = href.split('.');
			if( hrefArray )
			{
				var extension = hrefArray[hrefArray.length - 1];
				if ( extension && $.inArray(extension,fileTypes) != -1) {
					a.click(function() {
						pageTracker._trackPageview('/documents/' + href);
					});
				}
			}
		}

	});

});

