function comm_tab(n)
{
	var selected_tab_id = 'tab' + n;
	var list_items = document.getElementsByTagName('li');
	var tab;
	var tab_page;
	
	for(i = 0; i < list_items.length; i++)
	{
		tab = list_items.item(i);
		
		if(tab.id.substr(0, 3) == 'tab')
		{
			if(tab.id == selected_tab_id)
			{
				tab.className = "comm_activeTab";
				tab_page = document.getElementById('tabPage' + tab.id.substr(3));
				if(tab_page) tab_page.style.display = 'block';
			}
			else
			{
				tab.className = "comm_tab";
				tab_page = document.getElementById('tabPage' + tab.id.substr(3));
				if(tab_page) tab_page.style.display = 'none';
			}
		}
	}
}

/**
 * function show_menu()
 * Show menu panels.
 */
function show_menu(menu)
{
	$("#"+menu+"_menu").slideDown("slow");
}

function customise_dialog()
{
	showPopWin('/customise/dialog.php',700,500,'Customise',null);
}


function photo_callback(data, status)
{
	var id = jQuery('id', data).text();
	var photo = jQuery('photo', data).text();
	var caption = jQuery('caption', data).text();
	var prev = jQuery('prev', data).text();
	var next = jQuery('next', data).text();
	var preload = jQuery('preload', data).text();
	
	var prev_link = (prev != "") ? "javascript:get_photo('" + prev + "')" : '#';
	var next_link = (next != "") ? "javascript:get_photo('" + next + "')" : '#';
	
	//photo id
	$("input#photo_id").val(id);
	
	//preload next image
	var preload_img = new Image();
	preload_img.src = '/images/content/' + preload;
	
	//image
	$("div#image").html('<a href="' + next_link + '"><img src="/images/content/' + photo + '" ' + ' title="' + caption + '" alt="' + caption + '" border="0" /></a>');
	
	//previous link
	if(prev != "") $("div#prev_arrow").html("<a class=\"carousel_previous\" href=\"" + prev_link + "\" title=\"previous\">previous</a>");
	else $("div#prev_arrow").html("<a class=\"carousel_previous_disabled\" title=\"previous\">previous</a>");
	
	//next link
	if(next != "") $("div#next_arrow").html("<a class=\"carousel_next\" href=\"" + next_link + "\" title=\"next\">next</a>");
	else $("div#next_arrow").html("<a class=\"carousel_next_disabled\" title=\"next\">next</a>");
}

function remove_headline(id, section_id)
{
	if(confirm('Are you sure you want to remove this headline?\nAll information will be permanently deleted.'))
	{
		document.location = '/headlines_update.php?action=remove&id=' + id + '&section_id=' + section_id;
	}
}

function remove_content(id, query_string)
{
	if(confirm('Are you sure you want to remove this content?\nAll information and photos will be permanently deleted.'))
	{
		//alert('before replace: ' + query_string);
		
		query_string = query_string.replace(/amp;/g, "");
		
		//alert('after replace: ' + query_string);
		
		document.location = '/content_update.php?action=remove&id=' + id + '&' + query_string;
	}
}

function remove_photo()
{
	var id = $("input#photo_id").val();
	
	if(confirm('Are you sure you want to remove this photo?'))
	{
		document.location = '/photo_delete.php?id=' + id;
	}
}

function remove_video()
{
	var id = $("input#video_id").val();
	
	if(confirm('Are you sure you want to remove this video?'))
	{
		document.location = '/video_delete.php?id=' + id;
	}
}

function carousel1_buttonNextCallback(carousel, control, flag)
{
	if(flag) $("#carousel1-next").removeClass("disabled");
	else $("#carousel1-next").addClass("disabled");
}

function carousel1_buttonPrevCallback(carousel, control, flag)
{
	if(flag) $("#carousel1-prev").removeClass("disabled");
	else $("#carousel1-prev").addClass("disabled");
}

function carousel2_buttonNextCallback(carousel, control, flag)
{
	if(flag) $("#carousel2-next").removeClass("disabled");
	else $("#carousel2-next").addClass("disabled");
}

function carousel2_buttonPrevCallback(carousel, control, flag)
{
	if(flag) $("#carousel2-prev").removeClass("disabled");
	else $("#carousel2-prev").addClass("disabled");
}

function carousel1_initCallback(carousel)
{
	$('#carousel1-next').click(function() {
        carousel.next();
        return false;
    });
    
    $('#carousel1-prev').click(function() {
        carousel.prev();
        return false;
    });
}

function carousel2_initCallback(carousel)
{
	$('#carousel2-next').click(function() {
        carousel.next();
        return false;
    });
    
    $('#carousel2-prev').click(function() {
        carousel.prev();
        return false;
    });
}

function load_content(id)
{
	if(content_id == id) return;
	
	//get content
	$.get("/ajax/load_content.php", { id: id }, load_content_callback, "xml");
}

function load_content_callback(data, status)
{
	var id = jQuery('id', data).text();
	var title = jQuery('title', data).text();
	var dt = jQuery('date', data).text();
	var thumbnail = jQuery('thumbnail', data).text();
	var img = jQuery('image', data).text();
	var content = jQuery('content', data).text();
	var ftypes = jQuery('filetypes', data).text();
	var fname = jQuery('filename', data).text();
	var link = jQuery('url', data).text();
	var link_title = jQuery('url_title', data).text();
	var prev_blog_title = jQuery('prev_blog_title', data).text();
	var prev_blog_url = jQuery('prev_blog_url', data).text();
	var next_blog_title = jQuery('next_blog_title', data).text();
	var next_blog_url = jQuery('next_blog_url', data).text();
	
	document.title = title + " : uExplore.com";
	$("#team_members").hide();
	
	$("#content_title").html(title);
	$("#content_content").html(content);
	
	if(dt == '00/00/00') $("#content_date").hide();
	else
	{
		$("#content_date").html(dt);
		$("#content_date").show();
	}
	
	if(!thumbnail) $("#content_thumbnail").hide();
	else
	{
		$("#content_thumbnail img").attr('src', '/site_data/images/thumbs/'+thumbnail);
		$("#content_thumbnail").show();
	}
	
	if(!img) $("#content_image").hide();
	else
	{
		$("#content_image").attr('src', '/site_data/images/'+img);
		$("#content_image").show();
	}
	
	if(!ftypes) $("#content_filetypes").hide();
	else
	{
		$("#content_filetypes").html('<li>'+ftypes+'</li>');
		$("#content_filetypes").show();
	}
	
	if(!fname) $("#content_download").hide();
	else
	{
		$("#content_download_link").attr('href', '/site_data/downloads/'+fname);
		$("#content_download").show();
	}
	
	if(!link) $("#story_links").hide();
	else
	{
		$("#story_link").attr('href', link);
		$("#story_link").html(link_title);
		$("#story_links").show();
	}
	
	if(!prev_blog_title && !next_blog_title) $("#blog_links").hide();
	else
	{
		var html = '';
		if(prev_blog_title) html += '&lt; <a href="'+prev_blog_url+'" title="'+prev_blog_title+'">'+prev_blog_title.substr(0,50)+'</a>';
		if(prev_blog_title && next_blog_title) html += ' | ';
		if(next_blog_title) html += '<a href="'+next_blog_url+'" title="'+next_blog_title+'">'+next_blog_title.substr(0,50)+'</a> &gt;';
		$("#blog_links").html(html);
		$("#blog_links").show();
	}
	
	content_id = id;
}

/*
$(document).ready(function()
{
	$('#carousel1').jcarousel({
		vertical: true,
		scroll: 5,
		animation: "slow",
        buttonNextCallback: carousel1_buttonNextCallback,
        buttonPrevCallback: carousel1_buttonPrevCallback,
        initCallback: carousel1_initCallback
	});
	
	$('#carousel2').jcarousel({
		vertical: true,
		scroll: 3,
		animation: "slow",
		buttonNextCallback: carousel2_buttonNextCallback,
        buttonPrevCallback: carousel2_buttonPrevCallback,
        initCallback: carousel2_initCallback
	});
	
	$(".carousel-prev").show();
	$(".carousel-next").show();
	
	if(ajax_links)
	{
		$("#carousel1 li a").each(function() {
			var link = $(this).attr('href');
			
			if(link.indexOf('media-player') == -1 &&
			   link.indexOf('meet-the-team') == -1)
			{
				var id = link.substr(link.lastIndexOf('-i')+2);
				$(this).click(function() {
					load_content(id);
					return false;
				});
			}
		});
		
		$("#carousel2 li a").each(function() {
			var link = $(this).attr('href');
			
			if(link.indexOf('media-player') == -1)
			{
				var id = link.substr(link.lastIndexOf('-i')+2);
				$(this).click(function() {
					load_content(id);
					return false;
				});
			}
		});
	}
});
*/

/**
* function validate_fields()
* Validates that all required fields have been completed.
*/
function validate_fields(frm, Fields, Messages) 
{
	// This function validates the content of fields depending on their types
	for (var i = 0; i < Fields.length ; i++)
	{
		var fieldPresent = eval('frm.' + Fields[i]);
		if(fieldPresent)
		{
			var FType = eval ('frm.' + Fields[i] + '.type') ;
			switch(FType)
			{
				case "text" :
					if(eval('frm.' + Fields[i] + '.value') == '')
					{
						alert(Messages[i]);
						eval('frm.' + Fields[i] + '.focus()');
						return false;
					}
					break;
				
				case "password" :
					if(eval('frm.' + Fields[i] + '.value') == '')
					{
						alert(Messages[i]);
						eval('frm.' + Fields[i] + '.focus()');
						return false;
					}
					break;
					
				case "textarea" :
					if(eval('frm.' + Fields[i] + '.value' ) == '')
					{
						alert(Messages[i]);
						eval('frm.' + Fields[i] + '.focus()');
						return false;
					}
					break;

				case "select-one" :
					chk = 'false';
					if(eval('frm.' + Fields[i] + '.options[frm.' + Fields[i] + '.selectedIndex].text') != '-- Please Select --' )
					{
						chk = 'true'; 
					}
					if(chk=='false')
					{
						alert(Messages[i]);
						return false;
					}
					break;

				case "select-multiple":
					if(eval('frm.' + Fields[i] + '.selectedIndex') == -1)
					{
						alert(Messages[i]);
						return false;
					}
					break;
				
				case "checkbox":
					if(eval('frm.' + Fields[i] + '.checked') == false)
					{
						alert(Messages[i]);
						return false;
					}
					break;

				default:
					switch(eval('frm.' + Fields[i] + '[0].type'))
					{
						case "radio" :
							chk = 'false';
							for(var m = 0;  m < eval('frm.' + Fields[i] + '.length'); m++)
							{
								//alert('frm.' + Fields[i] + '[' + m + '].checked');
								if(eval('frm.' + Fields[i] + '[' + m + '].checked'))
								{
									chk = 'true';
								}
							}
							if(chk == 'false')
							{
								alert(Messages[i]);
								return false;
							}
							break;

						case "checkbox":
							chk = 'false';
							for(var m = 0;  m < eval('frm.' + Fields[i] + '.length'); m++)
							{
								//alert('frm.' + Fields[i] + '[' + m + '].checked');
								if(eval('frm.' + Fields[i] + '[' + m + '].checked'))
								{
									chk = 'true'; 
								}
							}
							if(chk == 'false')
							{
								alert(Messages[i]);
								return false;
							}
							break;
					}
			}
		}
	}
	return true;
} /* validate_fields */
