// common.js
// Jason Merkel
// common javascript functions

var news_main_index = 0;
var max_height = 0;
var rotate_news = true;
var all_news = false;

if (document.images) 
{
	light_gray = new Image();
	button_left_blue = new Image();
	button_right_blue = new Image();
	light_gray.src = 'graphics/light_gray.png';
	button_left_blue.src = 'graphics/button_left_blue.png';
	button_right_blue.src = 'graphics/button_right_blue.png';
}

$(document).ready(function()
{
	$("#photos").EmbedPicasaGallery('fbcgmedia',{
		  matcher:    /./,  // string or regexp matching album title
		  size:      '160',  // thumb size (32,48,64,72,144,160)) 
		  msg_loading_list :  'Loading list from PicasaWeb',
		  msg_back :   ''
		});
		
	if($('.news_feed_main'))
	{
		// set height of news container block to max height of all stories
		$('.news_feed_main_block').find('.news').each(function()
		{
			if($(this).height() > max_height)
				max_height = $(this).height();
		});
		$('.news_feed_main_block').height(max_height + 10);
		
		// hide all news but first story
		$('.news_feed_main_block').find('.news:not(:first)').hide();
		
		// rotate through all news stories
		var num_news = $('.news_feed_main_block').find('.news').length;
		$.timer(5000, function (timer)
		{
			if(rotate_news && num_news > 1)
			{
				if(news_main_index < num_news-1)
					next_index = news_main_index+1;
				else
					next_index = 0;
				
				if(jQuery.browser.msie)
				{
					$('.news_feed_main_block').find('.news:eq(' + news_main_index + ')').hide();
					$('.news_feed_main_block').find('.news:eq(' + next_index + ')').show();
				}
				else
				{
					$('.news_feed_main_block').find('.news:eq(' + news_main_index + ')').fadeOut(250, function(){$('.news_feed_main_block').find('.news:eq(' + next_index + ')').fadeIn(250);});
				}
				
				news_main_index = next_index;
			}
		});
	}
});

function show_all_news()
{
	rotate_news = false;
	all_news = true;
	$('.show_all').hide();
	$('.news_feed_main_block').height('auto');
	$('.news_feed_main').find('.news').slideDown(250);	
}

function show_news_form(cmd, id)
{
	rotate_news = false;
	
	$('.news_form').find('.cmd').val(cmd);
	
	if(cmd == 'add_news')
	{
		$('.news_form').find('.title').val("");
		$('.news_form').find('.body').val("");
	}
	else if(cmd == 'edit_news')
	{
		$('.news_form').find('.id').val(id);
		$('.news_form').find('.title').val($('#news_' + id).find('.title').val());
		$('.news_form').find('.body').val($('#news_' + id).find('.body').val());
	}

	if(jQuery.browser.msie)
	{
		$('.news_modal').show();
	}
	else
	{
		$('.news_modal').fadeIn(250);
	}
}


function hide_news_form()
{
	if(!all_news)
		rotate_news = true;
	
	if(jQuery.browser.msie)
	{
		$('.news_modal').hide();
	}
	else
	{
		$('.news_modal').fadeOut(250);
	}
}

function submit_news_form()
{
	$('.news_form').submit(); 
}

function show_confirm_news(id)
{
	rotate_news = false;
	
	$('.remove_news_form').find('.id').val(id);
	
	if(jQuery.browser.msie)
	{
		$('.confirm_news_modal').show();
	}
	else
	{
		$('.confirm_news_modal').fadeIn(250);
	}
}

function hide_confirm_news(id)
{	
	if(!all_news)
		rotate_news = true;
	
	if(jQuery.browser.msie)
	{
		$('.confirm_news_modal').hide();
	}
	else
	{
		$('.confirm_news_modal').fadeOut(250);
	}
}

function remove_news()
{
	$('.remove_news_form').submit(); 
}



/*
function edit_news(id)
{
	rotate_news = false;
	$('.news_feed_main_block').height("auto");
	$('#news_' + id).hide();
	$('#edit_news_' + id).show();
}

function cancel_edit_news(id)
{
	if(!all_news)
	{
		rotate_news = true;
		$('.news_feed_main_block').height(max_height + 10);
	}
	$('#edit_news_' + id).hide();
	$('#news_' + id).show();
}

function update_news(id)
{
	$('#edit_news_' + id).find('.edit_news_form').submit(); 
}

function remove_news(id)
{
	$('#news_' + id).find('.remove_news_form').submit(); 
}
*/
