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

//=======================================================================================================
//-------------------------------------------------------------------------------------------------------
//	Custom Javascript functions
//-------------------------------------------------------------------------------------------------------
//=======================================================================================================
//
//
/*
//var onload_functions = Array();
var slideshows = [];

function slideshow( container_div_name, div_array, rot_speed )
{
	this.container_div = document.getElementById(container_div_name);
	this.container_div_name = container_div_name;
	this.div_array = div_array;
	this.rot_speed = rot_speed * 1000;
	this.current_index = 0;
	this.slide_total = div_array.length;

	// Global register
	slideshows.push(this);

	// Get all heights
	var max_height = 0;
	var content_divs = [];
	for(var i = 0; i < div_array.length; i++)
	{
		var curr_elem_children = document.getElementById(div_array[i]).getElementsByTagName('div');

		for(var j = 0; j < curr_elem_children.length; j++)
		{
			var curr_elem = curr_elem_children[j];
			if(curr_elem.className == 'content')
			{
				content_divs.push(curr_elem);

				curr_height = curr_elem.offsetHeight;

				if(curr_height > max_height)
				{
					max_height = curr_height;
				}
			}
		}
	}

	// This takes care of a little extra padding
	max_height -= 10;
	
	// Set maxHeight for currently displaying item
	this.container_div.childNodes[1].style.height = max_height + 'px';

	for(var i = 0; i < content_divs.length; i++)
	{
		content_divs[i].style.height = max_height + 'px';
		content_divs[i].style.maxHeight = max_height + 'px';
	}

}

slideshow.prototype.rotateSlide  = function() {

	this.current_index++;
	adjusted_slide_index = this.current_index % this.slide_total;
	this.container_div.innerHTML = document.getElementById(this.div_array[adjusted_slide_index]).innerHTML;

};

slideshow.prototype.startSlideshow = function() {

	if(this.slide_total == 0) return;

	var myself = this;

	// This may possibly leak memory, but not sure how to get around.
	function rotatefunc(){
		//myself.rotateSlide();
	};

	//setInterval(rotatefunc, this.rot_speed);
};




// thanks to simon willison /
function addToOnload(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
*/
//*********************************************************************************************************
//		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++;
		$('#thumb_roller').animate({'left':0});
	}
	else
	{
		q++;

		if((((q-1) % 3) == 0) && (!stop_rotating))
		{
			RollerPrevNext('navs_next');
		}
	}


	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:	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:	navs_prev()
//---------------------------------------------------------------------------------------------------------
//	What it does:	get prev thumbs in banner
//*********************************************************************************************************
function navs_prev()
{

}

//*********************************************************************************************************
//		Function:	navs_next()
//---------------------------------------------------------------------------------------------------------
//	What it does:	get next thumbs in banner
//*********************************************************************************************************
function navs_next()
{

}

//-------------------------------------------------------------------------------------------------------
//	Toggle the "email this page" form
//-------------------------------------------------------------------------------------------------------
function ToggleEmailThisPage()
{
	FormObject	= document.getElementById( 'emailthispage' );
	DimObject	= document.getElementById( 'emailthispage-dim' );

	if( FormObject.style.display == "block" )
	{
		document.body.style.overflow	= "auto";
		FormObject.style.display		= "none";
		DimObject.style.display			= "none";
	}
	else
	{
		scroll(0,0);
		document.body.style.overflow	= "hidden";
		FormObject.style.display		= "block";
		DimObject.style.display			= "block";
	}
}

//-------------------------------------------------------------------------------------------------------
//	Toggle the "email this page" form
//-------------------------------------------------------------------------------------------------------
function RollerPrevNext( id )
{
	var animate = 0;
	var left			= ($('#thumb_roller').css('left').slice(0,-2)) *1;
	var thumbs_width	= $('#thumb_navs').width();

	if( id == 'navs_prev'){
		left += thumbs_width;
		if( left <= 0 )
		{
			animate = 1;
		}
	}
	else if( id == 'navs_next') {
		left -= thumbs_width;
		if( (Math.abs(left) <  nav_roller_width ))
		{
			animate = 1;
		}
	}
	if( animate)
	{
		$('#thumb_roller').animate({'left':left});
	}
}
//*********************************************************************************************************
//		document.ready:::::::
//---------------------------------------------------------------------------------------------------------
//*********************************************************************************************************
$(document).ready(function(){
	$('#social-media-links').css('display','none');

	//---------------------------------------------------------------------------------------------------------
	//		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);
			}
		});
    });

	//---------------------------------------------------------------------------------------------------------
	//		share
	//---------------------------------------------------------------------------------------------------------
		$('#share-button').click(function(){ 
			$('#share-button').toggleClass('share-button-active');
			$('#social-media-links').toggle('slow');
			return false;
		});


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

		//$(this).css({"height":currentTallest +'px !important;'});
		$(this).find('div.slide-widget-content').height(currentTallest);
		//alert($(this).attr('id')+' ' + currentTallest);
	});

	//---------------------------------------------------------------------------------------------------------
	//	homepage banner prev and next 
	//---------------------------------------------------------------------------------------------------------
	$('.navs_prev_next').click(function(){
		stop_rotating = 1;
		var prev_next_id	= $(this).attr('id');

		RollerPrevNext(prev_next_id);


	});

	
	//---------------------------------------------------------------------------------------------------------
	// IE 6
	//---------------------------------------------------------------------------------------------------------
	if( is_ie6 )
	{
		DD_belatedPNG.fix('.png_fix,.banner');
	}


});

