/* LiveOak.js */
var gGetMeetings = false;
var gGetLibrary = false;
var gGetPolicies = false;
var gGetPacketSearchText = false;
$(document).ready(function() {
// refreshToolbar
$.refreshToolbar = function() {	
	// Show or Hide toolbar buttons
	( $.trim( $("#btn-home").attr('href') ) == '' ) ?  $("#btn-home").hide() : $("#btn-home").show(); 
	( $.trim( $("#btn-policies").attr('href') ) == '' ) ?  $("#btn-policies").hide() : $("#btn-policies").show(); 
}
// windowResize
$.windowResize = function() {	
	var winheight = $(window).height();
	var winwidth = $(window).width();
	$('#agenda, #drafts, #templates, #library, #meetings, #my-agenda-items').css('max-height', winheight - 270 + 'px').css('overflow', 'auto');
	$('#wrap-packet').css('max-height', winheight - 230 + 'px').css('overflow', 'auto');
	$('#policies').css('max-height', $(window).height() - 290 + 'px').css('overflow', 'auto');
	// IE7 fixes
	if ( $.browser.msie && $.browser.version == 7 ) { 
		$(".ui-accordion .ui-accordion-header").css("position", "static");
		$(".ui-accordion .ui-accordion-content").css("position", "static");
		$(".ui-accordion .ui-accordion-header .ui-icon").css("display", "none");
		// fixes print dialog from showing off screen.  started with jqueryui version 1.8.
		$(".ui-dialog").css("position", "absolute");  
		$(".ui-dialog").css("top", "10px");
	}
	$('#intro-page').css('max-height', winheight - 215 + 'px').css('overflow', 'auto');
	$('#view-meeting, #edit-meeting').css('max-height', winheight - 285 + 'px').css('overflow', 'auto');
	$('#view-agenda-item, #edit-agenda-item').css('height', winheight - 270 + 'px').css('overflow', 'auto');
	$('#view-policy-item, #edit-policy-item').css('height', winheight - 270 + 'px').css('overflow', 'auto');
	$('#view-library-item, #edit-library-item').css('height', winheight - 270 + 'px').css('overflow', 'auto');
	$('.search-results').css('max-height', winheight - 280 + 'px').css('overflow', 'auto');
	// BD Notes
	$('.bd-notes-panel').css('width', winwidth/3 + 'px');
	$('.bd-notes').css('max-height', winheight - 285 + 'px');
	// $('#wrap-agenda-item').css('width', winwidth - 540 + 'px');
	// Tab Content height
	$(".tab-content").css('height', winheight - 200 + 'px').css('overflow', 'hidden');
	$("#bd-options").css('height', winheight - 235 + 'px').css('overflow', 'auto');
	// Left Navigation
	$(".pane-navigation").each( function() { $(this).css('width', ($(this).closest("td").width() -13) + 'px'); });
	// Dialog sizing
	if ( ! $.browser.msie ) { 
		// Print Dialog
		$('#print-dialog').dialog('option', 'height', winheight - 50);
		$('#print-dialog').dialog('option', 'width', winwidth - 50);
	
		// Minutes Dialog
		$('#minutes-dialog').dialog('option', 'height', winheight - 50);
		$('#minutes-dialog').dialog('option', 'width', winwidth - 50);
	
		// Generic Dialog
		$('#generic-dialog').dialog('option', 'height', winheight - 50);
		$('#generic-dialog').dialog('option', 'width', winwidth - 50);
	 }
	// Timer demo
	$("#bd-timer, #bd-timer-background").height( winheight );
	$("#bd-timer, #bd-timer-background").width( winwidth );
	// Remove icons from tabs
	(winwidth < 1000) ? $("#tab-left-nav ul li a span").addClass('small-screen') : $("#tab-left-nav ul li a span").removeClass('small-screen')
	// generic dialog content
	$("#wrap-dialog .content").css('height', winheight - 215 + 'px').css('overflow', 'auto');
	
	// hide non print
	$.hideNonPrint();
}
// Hide Non Print
$.hideNonPrint = function () {
	$("body > *").not("#for-print").addClass("do-not-print");
}
// Ajax Loading
$("body").prepend('<div id="ajax-status" style="position: absolute; top: 72px; left: 30px; font-size: 11px; color: #eee; z-index: 1000; opacity: 1; display: none;"><img src="ajax-loader.gif">&nbsp;&nbsp;Please wait...</div>');
// updateToolTips
$.updateToolTips = function(el) {	
	$(el).tooltip({ opacity: 1, track: true, fade: 400, delay: 1000, showURL: false, showBody: " - " });
	$("#tooltip").addClass("ui-corner-bottom").addClass("ui-corner-tr");
}
	
// getPublicFiles
$.getPublicFiles = function( id, el ) {	
	$.post("/vsba/fairfax/Board.nsf/LT-GetPublicFiles?open&" + Math.random(), 
		{ id : id }, 
		function(data){ 
			var target;
			$(el).html( data );
			if ( isPublisher ) { 
				target = $(el).find('.public-file');
			} else {
				if ( isAdmin ) { 
					target = $("#meeting-content").find('.public-file');
				} else {
					return false;
				}
			}
			$(target).contextMenu('myMenu1', {
				onShowMenu: function(e, menu) {
					$('li', menu).not("#delete").remove();
					return menu;
				}, 
				bindings: {
						'delete': function(t) {
							if ( confirm( 'Delete this file?\n\n' + $(t).children('a').text() + '\n ') ) { 
								$.deleteByID( $(t).attr('unique') );
								$(t).addClass('being-deleted').hide("pulsate", { times: 5 }, 500);
							}
						}
				}
			});
			
		}
	);
}
// getAdminFiles
$.getAdminFiles = function( id, el ) {	
	$.post("/vsba/fairfax/Board.nsf/LT-GetAdminFiles?open&" + Math.random(), 
		{ id : id }, 
		function(data){ 
			var target;
			$(el).html( data );
			if ( isPublisher ) { 
				target = $(el).find('.admin-file');
			} else {
				if ( isAdmin ) { 
					target = $("#meeting-content").find('.admin-file');		
				} else {
					return false;
				}
			}
			$(target).contextMenu('myMenu1', {
				onShowMenu: function(e, menu) {
					$('li', menu).not("#delete").remove();
					return menu;
				}, 
				bindings: {
					'delete': function(t) {
						if ( confirm( 'Delete this file?\n\n' + $(t).children('a').text() + '\n ') ) { 
							$.deleteByID( $(t).attr('unique') );
							$(t).addClass('being-deleted').hide("pulsate", { times: 5 }, 500);
						}
					}
				}
			});
		}
	);
}
// getExecFiles
$.getExecFiles = function( id, el ) {	
	$.post("/vsba/fairfax/Board.nsf/LT-GetExecFiles?open&" + Math.random(), 
		{ id : id }, 
		function(data){ 
			var target;
			$(el).html( data );
			if ( isPublisher ) { 
				target = $(el).find('.executive-file');
			} else {
				if ( isAdmin ) { 
					target = $("#meeting-content").find('.executive-file');
				} else {
					return false;
				}
			}
			$(target).contextMenu('myMenu1', {
				onShowMenu: function(e, menu) {
					$('li', menu).not("#delete").remove();
					return menu;
				}, 
				bindings: {
						'delete': function(t) {
							if ( confirm( 'Delete this file?\n\n' + $(t).children('a').text() + '\n ') ) { 
								$.deleteByID( $(t).attr('unique') );
								$(t).addClass('being-deleted').hide("pulsate", { times: 5 }, 500);
							}
						}
				}
			});
			
		}
	);
}
// clearAgendaSearchBox
$.clearAgendaSearchBox = function() {	
	$("#agenda-search-box").removeClass('searchvaluechanged').val('').trigger("focus").siblings('.number-found').html('');
}
// updateAgendaType
$.updateAgendaType = function() {	
	return;
	var combine1 = '';
	$('#agendatype option:selected').each( function(){ combine1 = combine1 + $(this).text() + "<span class='comma'>, </span>"; } )
	$('#display-agendatype').html( combine1 );
	$('#display-agendatype .comma:last').remove();
}	
// deleteByID
$.deleteByID = function( uniqueid ) {	
	$.get("/vsba/fairfax/Board.nsf/LT-DeleteByID?open&" + Math.random(), { id: uniqueid }, 
		function(data){ 
			$.getPacket();
		}
	);					
}	
// meetingMakeActive
$.meetingMakeActive = function( uniqueid ) {	
	$.get("/vsba/fairfax/Board.nsf/LT-MeetingMakeActive?open&" + Math.random(), { id: uniqueid }, 
		function(data){ 
			if ($.trim(data) == "2") {
				$.quickAlert("Cannot Make Active", "There are still items in workflow.  Wait for approval or force approve these items first.", 7000);
			} else {
				$.quickAlert("Meeting Now Active", "The meeting has been made active.", 2000);
				$.getMeetings();
				$.getDrafts();
				$.getPacket();
			}
		}
	);
}
// meetingMakeDraft
$.meetingMakeDraft = function( uniqueid ) {	
	$.get("/vsba/fairfax/Board.nsf/LT-MeetingMakeDraft?open&" + Math.random(), { id: uniqueid }, 
		function(data){ 
			$.quickAlert("Meeting Now Draft", "The meeting has been made a draft.", 2000);
			$.getMeetings();
			$.getDrafts();
			$.getPacket();
		}
	);					
}	
// addToPacket
$.addToPacket = function( uniqueid ) {	
	$.get("/vsba/fairfax/Board.nsf/LT-AddToPacket?open&" + Math.random(), { id: uniqueid }, 
		function(data){ 
			$.quickAlert("Packet Add", "The Item has been added to the packet.", 2000);
			$.getPacket();
		}
	);					
}	
// removeFromPacket
$.removeFromPacket = function( uniqueid ) {	
	$.get("/vsba/fairfax/Board.nsf/LT-RemoveFromPacket?open&" + Math.random(), { id: uniqueid }, 
		function(data){ 
			$.quickAlert("Packet Remove", "The Item has been removed from the packet.", 2000);
			$.getPacket();
		}
	);					
}	
// checkChangesMade
$.checkChangesMade = function() {
	if ( $('.valuechanged').is(":visible") ) { 
		if ( confirm('Abandon the changes you have made?') ) { 
			return false;
		} else {
			return true;
		}
	} else {
		return false; 
	}
}	
// attachSearchFunctions
$.attachSearchFunctions = function() {						
	$("#search .searchitem").click( function() { 
		if ( $.checkChangesMade() ) { return; }   // if data needs to be saved, then don't advance
		// 
		$('#agenda-content').html('');
		var activeItemID = $(this).attr('id')
		$.get("/vsba/fairfax/Board.nsf/LT-GetAgendaItem?open&" + Math.random(), { id : $(this).attr('id') }, 
			function(data){ 
				$("#current-agenda-item").val( activeItemID );
				// alert( activeItemID );
				$('#agenda-content').html(data);
				$.attachAgendaItemFunctions();
			}
		);  
	});	
}	
// getPrevItemDOM
$.getPrevItemDOM = function() {						
	var activehit = 0;
	var previtem;
	var allitems = $.makeArray( $("#agenda .item:visible") ).reverse();  // reverse the order
	$(allitems).each( function(el) {
		if (activehit == 1) { previtem = this; return false; }
		if ( $(this).hasClass("active") ) { activehit = 1; }			
	});	
	return previtem;
}	
// getNextItemDOM
$.getNextItemDOM = function() {						
	var activehit = 0;
	var nextitem;
	var allitems = $("#agenda .item:visible");
	$(allitems).each( function(i) {
		if (activehit == 1) { nextitem = this;  return false; }
		if ( $(this).hasClass("active") ) { activehit = 1; }
	});	
	return nextitem;
}	
// keepAlive
$.keepAlive = function() {	
	$.ajax({ 
		type: "POST", 
		url: "/vsba/fairfax/Board.nsf/LT-KeepAlive?open&" + Math.random(), 
		cache: false, 
		dataType: "html",
		success: function(data){ 
			$("#alert-box").html("");
			$("#alert-box:visible").hide();
		},
		error: function(data){ 
			$("#alert-box").html("Warning: Connection to the server may have been lost.  Please check your network.");
			$("#alert-box:hidden").show();
		}
	});
}	
// agendaSorting
$.agendaSorting = function() {	
	if (! isPublisher) { return false; }
	/* Remove previous sortable */
	$("#agenda").sortable("destroy");
	$(".wrap-items", "#agenda").sortable("destroy");
					
	/* Add sorting ability */
	$("#agenda").sortable({ 
		start: function(){ 
			$(".wrap-items", "#agenda").css("opacity","0.25"); 
		}, 
		stop: function(){ 
			$.numberTheAgenda( $("#agenda") ); 
			$.saveAgendaOrder(); 
			$(".wrap-items", "#agenda").css("opacity","1"); 
		}, 
		handle: '.category',
		cursor: 'crosshair',
		distance: 5, 
		axis: 'y' 
	});
	$(".wrap-items", "#agenda").sortable({ 
		connectWith: [".wrap-items"], 
		start: function(){ 
			$(".wrap-category .category", "#agenda").css("opacity","0.25"); 
		}, 
		stop: function(){ 
			$.numberTheAgenda( $("#agenda") ); 
			$.saveAgendaOrder(); 
			$(".wrap-category .category", "#agenda").css("opacity","1"); 
		}, 
		distance: 5, 
		axis: 'y' 
	});
}	
$("#agenda .category").live("click", function() {
	$("#btn-expand-agenda").trigger("click");
});
$("#agenda .category").live("mouseup", function() {
//	$(".wrap-items", "#agenda").show(); 
});
// numberTheAgenda
$.numberTheAgenda = function( container ) {	
	if (! isPublisher) { return false; }	
	
	var ucLetters = [ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" ];
	var lcLetters = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" ];
	$.noNumbering = function() {
		// No Numbering
		$(".category", $(container)).each( function(i, n){  
			$(".order", n).html("");
	
			$(n).siblings('.wrap-items').children('.item').each( function(j, o){  
				j++;
				$(".order", o).html("");
			});
		});
	}
	$.doubleDecimalNumbering = function() {
		// 1.01, 1.02, 1.03
		$(".category", $(container)).each( function(i, n){  
			i++
			$(".order", n).html( i + ". " );
	
			$(n).siblings('.wrap-items').children('.item').each( function(j, o){  
				j++;
				if ( j < 10 ) {   
					$(".order", o).html( i + ".0" + j + " " );
				} else {
					$(".order", o).html( i + "." + j + " " );
				}
			});
		});
	}
	$.Numbering_A1 = function() {
		// A.1, B.2, C.3
		$(".category", $(container)).each( function(i, n){  
			$(".order", n).html( ucLetters[i] + ". " );
	
			$(n).siblings('.wrap-items').children('.item').each( function(j, o){  
				j++;
				// $(".order", o).html( ucLetters[i] + "." + j );
				$(".order", o).html( j + ". " );
			});
		});
	}
	$.Numbering_1A = function() {
		// 1.A, 2.B, 3.C
		$(".category", $(container)).each( function(i, n){  
			i++
			$(".order", n).html( i + ". " );
	
			$(n).siblings('.wrap-items').children('.item').each( function(j, o){  
				// $(".order", o).html( i + "." + ucLetters[j] );
				$(".order", o).html( ucLetters[j] + ". " );
			});
		});
	}
	$.singleDecimalNumbering = function() {
		// 1.1, 1.2, 1.3
		$(".category", $(container)).each( function(i, n){  
			i++;
			$(".order", n).html( i + ". " );
	
			$(n).siblings('.wrap-items').children('.item').each( function(j, o){
				j++;
				$(".order", o).html( i + "." + j + " " );
			});	
		});
	}
	switch( $.trim( $("#agenda-numbering").text() ) )
	{
		case "0": { $.noNumbering(); break; }
		case "1": { $.doubleDecimalNumbering(); break; }
		case "2": { $.singleDecimalNumbering(); break; }
		case "3": { $.Numbering_A1(); break; }
		case "4": { $.Numbering_1A(); break; }
		default: { $.noNumbering(); break; }
	}
	
}	
// saveAgendaOrder
$.saveAgendaOrder = function(loadagenda) {	
	var unid;
	var itemsinorder;
	itemsinorder = "";
	$("#agenda .agendaorder").each( function(){
		unid = $(this).attr('unid');
		if ( unid != "" && unid.length == 32 ) { itemsinorder = itemsinorder + $(this).attr('unid') + "~"; }
	})
	$.post("/vsba/fairfax/Board.nsf/LT-SaveAgendaOrder?open&" + Math.random(), 
		{ agendaitems : itemsinorder, meetingid : $("#current-meeting").val() }, 
		function(data){ 
			// message if change was not successful
			if ( $.trim(data) != "1" ) { 
				$.quickAlert("Please Note", "The agenda order could not be saved.  It's possible another publisher has control of sorting.  Try again in a few minutes.  In the meantime new items will show at the bottom of the agenda.", 8000); 
				loadagenda = true;
			}
			// reload agenda if true
			if ( loadagenda ) { $.getAgenda( $("#current-meeting").val() ); }
		}
	);
}	
// start
$(document).ajaxStart(function(){ 
	$("#ajax-status").show();
});
$(document).ajaxStop(function(){ 
	$("#ajax-status").hide();
	$.windowResize();
});
// Attach resize event
$(window).resize(function(){ $.windowResize(); });   
$("#btn-new-category").click( function() {
	var newitem;
	$.post("/vsba/fairfax/Board.nsf/LT-NewCategory?open&" + Math.random(), 
		{ meetingid : $("#current-meeting").val() }, 
		function(data){ 
			$.quickAlert("Category Added", "The category was added to the bottom of the agenda.", 4000);
			$.getAgenda( $("#current-meeting").val() );		
		}
	);
});
$("#btn-new-agenda-item").click( function() {
	var newitem;
	$.post("/vsba/fairfax/Board.nsf/LT-NewAgendaItem?open&" + Math.random(), 
		{ meetingid : $("#current-meeting").val() }, 
		function(data){ 
			var agendaitemunid = $.trim( data );
			var newitem = '<div class="item agendaorder" unid="' + agendaitemunid + '"><div class="actiontype">Action </div><span class="order"></span>&nbsp;New Agenda Item</div>';			
			if ( $("#agenda .item.active").length >= 1 ) { 
				$("#agenda .item.active").after( newitem );
				$.quickAlert("Agenda Item Added", "The agenda item was added right after the currently highlighted item.", 4000);
			} else {
				$("#agenda .category:first").after( newitem );
				$.quickAlert("Agenda Item Added", "The agenda item was added to the 1st category.", 4000);
			}
			$.saveAgendaOrder(true);
		}
	);
});
$("#btn-expand-agenda").click( function() {
	$(this).hide();
	$("#btn-collapse-agenda").show();
	$("#agenda").find(".wrap-items").slideDown(500);
});
$("#btn-collapse-agenda").click( function() {
	$(this).hide();
	$("#btn-expand-agenda").show();
	$("#agenda").find(".wrap-items").slideUp(500);
});
/* People Manager button */
$("#btn-people-manager").click( function() {
	window.open("https://www.boarddocs.com/pm.nsf/admin", "PeopleManager","directories=0,location=no,menubar=0,resizable=1,toolbar=0,width=1000,height=750");
});
/* Update ToolTips */
$.updateToolTips( $("#agenda-nav a") );
$.updateToolTips( $("#meetings-nav a") );
/* Right Click Menu defaults */
if (isNotAnonymous) { 
	$.contextMenu.defaults({
		menuStyle : { width : '170px' },
		itemStyle : { fontSize : '11px' },
		shadow: true
	});
}
/* Refresh Window Resizing */
$.windowResize();
/* Keep-alive polling */
var keepalive_interval = setInterval ( "$.keepAlive()", 180000 );  // Keep Alive.  Poll every 3 mins.
// Page Tab Navigation
$("#tab-left-nav").tabs({
	select: function(event, ui) {
		$(".tab-menuitems").hide();  // hide any tab menus
		// Get Meetings
		if (ui.panel.id == "tab-meetings") { 
			if ( $.getCurrentMeetingStatus() == "" ) {
				$("#li-meetings .tab-menuitem[key=active]").trigger("click");
			}
		} 
		// Get Agenda
		if (ui.panel.id == "tab-agenda") { 
			// nothing
		} 
		// Get Options
		if (ui.panel.id == "tab-options") { 
			if ( $.trim($("#tab-options").text() ) == '' ) { 
				$.getScript("/vsba/fairfax/Board.nsf/javascript/options.js?open"); 
			} 
		} 
		// Get Policies
		if (ui.panel.id == "tab-policies") { 
			if ( 1==0  ) {
				window.open("","policywindow","menubar=1,resizable=1,location=1,status=1,scrollbars=1,width=800,height=600");
				return false;
			} else {
				if ( $(".tab-menuitem.checked", "#li-policies").length == 0 ) { 
					$(".tab-menuitem:first", "#li-policies").trigger("click");
				}
			}
		} 
		// Get Library
		if (ui.panel.id == "tab-library") { 
			if ( $(".tab-menuitem.checked", "#li-library").length == 0 ) { 
				$(".tab-menuitem:first", "#li-library").trigger("click");
			}
		} 
	},
	show: function(event, ui) {
		$.windowResize();
	}
});
// Remove rounded corners from tab interface
$("#tab-left-nav, .tab-content").removeClass("ui-corner-all ui-corner-bottom").addClass("ui-corner-tl").addClass("ui-corner-tr");
// For Tab Menus with one item, hide the menu
$(".tab-menuitems").each( function() {
	if( $(".tab-menuitem", $(this)).length < 2) {
		$(this).closest(".tab-menu").hide();
		$(this).closest(".tab-menu").closest("li").find("a span").css("padding-right","0");
	}
});
// No right padding for tabs without tab-menus
$("#tab-left-nav > ul > li").each( function() {
	if( $(".tab-menuitem", $(this)).length < 1) {
		$(this).find("a span").css("padding-right","0");
	}	
});
// Search Box
// Agenda Item search results box
$(".search-results > .item").live("click", function() { $(this).addClass("active").siblings().removeClass("active"); });
$(".search-results > .item").live("mouseover", function() { $(this).addClass("highlight"); });
$(".search-results> .item").live("mouseout", function() { $(this).removeClass("highlight"); });
$(".search-box img.search").click( function(){
	var searchicon = $(this);
	var searchbox = $(searchicon).closest(".search-box");
	var isagendasearch = false;
	var othersearchboxes = $(".search-box").not( searchbox );
	var searchresults = $(searchbox).find(".search-results");
	var searchstring = $.trim($("input", searchbox).val());
	var siblings = $(searchbox).siblings();
	var policybooks = $(searchbox).find("#policy-books");
	var panenav = $(searchicon).closest(".pane-navigation");
	var itemstosearch = $(panenav).find("a.icon[unique!=][searchresult!=1], .item[unique!=][searchresult!=1]");  // items with unique that are not search results
	var theIDs = "";
	var agendaitem;
	// is agenda search
	if ( $(searchbox).closest(".tab-content").is("#tab-agenda") ) {
		isagendasearch = true;
	}
	// clear out any previous results
	$(".search-results", searchbox).empty();
	// get the Unique IDs of all visible items.  add to string
	$(itemstosearch).each( function() { 	
		theIDs = theIDs + $(this).attr("unique") + "~"; 
	});
	$(searchicon).hide();  // hide search icon
	$(policybooks).hide();  // hide policy books selector
	// hide everything nearby then show search results box
	$(siblings).slideUp(250, function(){
		$(".search-results", searchbox).hide().slideDown(500);
	});
	// show the "x" icon
	$("img.clear", searchbox).show();
	
	// clear all other search boxes
	if ( ! isagendasearch ) { 
		$(othersearchboxes).each( function() {
			var box = $(this);
			$(box).find("input").val('');
		});
	}
	// $(".search-results", searchbox).html("<div style='margin: 10px;'><img src='ajax-loader.gif'></div>");
	$.ajax({ 
		type: "POST", 
		url: "/vsba/fairfax/Board.nsf/LT-SearchInContext?open&" + Math.random(), 
		dataType: "xml",
		data: { ids: theIDs, searchstring: searchstring },
		success: function(xml){ 
			var numFound = $(xml).find("unid").length;
			if ( numFound == 0 ) {
				$(".search-results", searchbox).html("<div style='margin: 10px; font-weight: bold;'>No matches found.</div>");
			} else {
				$(xml).find("unid").each( function() {
					// clone items that match and put them in the search results
					$(searchresults).append( $("a[unique=" + $(this).text() + "]:first", panenav).clone(true).attr("searchresult", "1") );
					$(searchresults).append( $("div.item[unique=" + $(this).text() + "]:first", panenav).clone(false).attr("searchresult", "1") );
				});
				// Trim action type from Agenda Items
				$(searchresults).find(".actiontype").hide();
			}
		}
	});
});
$(".search-box img.clear").click( function(){
	var searchbox = $(this).closest(".search-box");
	var siblings = $(searchbox).siblings();
	var policybooks = $(searchbox).find("#policy-books");
	$(".search-results", searchbox).empty();
	$(this).hide();
	$("img.search", searchbox).show();
	$("input.input", searchbox).val("").removeClass('highlight').val("Search");
	$(".search-results", searchbox).slideUp(250, function(){ 
		$(siblings).slideDown(500);
		$(policybooks).show();
	});
	// clear out any search results
	$(".search-results", searchbox).empty();
});
$(".search-box input.input").focus( function(){
	var searchbox = $(this).closest(".search-box");
	var thisval = $(this).val();
	if (thisval == "Search") { $(this).val(''); }
	$(this).addClass('highlight');
});
$(".search-box input.input").keydown( function(el){
	var thisval = $(this).val();
	if (thisval == "Search") { $(this).val(''); }
});
$(".search-box input.input").keyup( function(el){
	var searchbox = $(this).closest(".search-box");
	var thisval = $(this).val();
	$("img.clear", searchbox).hide();
	$("img.search", searchbox).show();
	if (thisval == "Search") { $(this).val(''); }
	$(this).addClass('highlight');
	if (el.keyCode == 13) { $("img.search", searchbox).trigger("click"); }
	if (el.keyCode == 27) { $("img.clear", searchbox).trigger("click"); }
});
// Drop Down Tab Menus
$(".tab-content").live( "click", function() {
	$(".tab-menuitems").hide();
});
$(".tab-menu img").live( "click", function() {
	$(".tab-menuitems").not($(this).siblings(".tab-menuitems")).hide();
	$(this).siblings(".tab-menuitems").toggle();
});
$(".tab-menuitem").live( "click", function() {
	$(this).addClass("checked");  // check the item
	$(this).siblings(".tab-menuitem").removeClass("checked");  
	$(".tab-menuitems").hide();  // hide other
	$(this).closest(".tab-menu").siblings("a").trigger("click");  // make parent tab active	
});
// Previous Item
$(".content-navigation img.previous").live( "click", function() {
	var id = $(this).attr("base");
	var parent = $(this).closest(".tab-content");
	var navigation = $(".pane-navigation", $(parent));
	var allitems = $(".prevnext", $(navigation));
	var matcheditem = $(".prevnext.active", $(navigation));
	var nextitem = $(matcheditem).prevAll(".prevnext:first");
	$(nextitem).trigger("click");
	return false;
});
// Next Item
$(".content-navigation img.next").live( "click", function() {
	var id = $(this).attr("base");
	var parent = $(this).closest(".tab-content");
	var navigation = $(".pane-navigation", $(parent));
	var allitems = $(".prevnext", $(navigation));
	var matcheditem = $(".prevnext.active", $(navigation));
	var nextitem = $(matcheditem).nextAll(".prevnext:first");
	$(nextitem).trigger("click");
	return false;
});
// Content Navigation buttons
$("img", ".content-navigation").live("mouseover", function() {
	$(this).addClass("ui-corner-all").addClass("ui-widget-content");
});
$("img", ".content-navigation").live("mouseout", function() {
	$(this).removeClass("ui-corner-all").removeClass("ui-widget-content");
});
// Left Navigation buttons
$("a.icon", ".pane-navigation").live("click", function() {
	return;
	$(this).addClass("ui-corner-all").addClass("ui-widget-content");
	$("a.icon", $(this).closest(".pane-navigation")).not($(this)).removeClass("highlight").removeClass("ui-corner-all").removeClass("ui-widget-content");
	$(this).addClass("highlight");
});
// Hide Debug
$("#debugclick").click( function() { 
	$("#debug-window").slideToggle(); 
	if ( $("#debug-window:visible").length != 0 ) {
		$("body").addClass("indebugX");
	} else {
		$("body").removeClass("indebugX");
	}
	$.windowResize();
});
// Quick Alert box
$.quickAlert = function( title, msg, time ) {	
	$("#quick-alert").remove();
	$("body").prepend('<div id="quick-alert" class="ui-corner-tr ui-corner-br ui-widget-content" style="display: none;"><h1>' + title + '</h1><p>' + msg + '</p></div>');
	
	$("#quick-alert").fadeIn(250);
//	$("#quick-alert").effect("slide", { direction: "left" }, 500, function () {  });
	var dothis = '$("#quick-alert").effect("drop", { direction: "left" }, 1000, function () { $("#quick-alert").remove(); });';
	setTimeout(dothis, time);
}
// Update Tool Tips
$.updateToolTips( $(".search-box img, .content-navigation img") );
// File Upload
$.fileUpload = function(el) {
	theForm = $(el).closest("form");
	theFormName = $(theForm).attr("name");
	theBrowseButton = $(theForm).find("input[type=file]");
	theUploadButton = $(theForm).find("input[type=submit]");
	theFileList = $(theForm).parent().next();
	$(theForm).ajaxForm(); 
	$(theForm).ajaxSubmit({
		beforeSend: function(){
			$(theFileList).append("<div class='uploading-file'><img src='/global.nsf/images/ajax/ajax-spin.gif'> <span>Uploading your file...</span></div>");
			$(".uploading-file span", theFileList).effect("pulsate", { times: 5 }, 1000);
		},
		success: function(){
			$.updateUploadAreas();
			switch (theFormName)
			{
				case "_FilePublic":
					$.getPublicFiles( $(theForm).find("input[name=AgendaItemID]").val(), $(theFileList) );
					break;
				case "_FileAdmin":
					$.getAdminFiles( $(theForm).find("input[name=AgendaItemID]").val(), $(theFileList) );
					break;
				case "_FileExecutive":
					$.getExecFiles( $(theForm).find("input[name=AgendaItemID]").val(), $(theFileList) );
					break;
			}
		}
	});
}
// Ajax the File Uploads
$.updateUploadAreas = function() {	
	$("form.public-file-upload:visible").each( function() { $(".upload-control", this).load("/vsba/fairfax/Board.nsf/FilePublic input:file"); });
	$("form.admin-file-upload:visible").each( function() { $(".upload-control", this).load("/vsba/fairfax/Board.nsf/FileAdmin input:file"); });
	$("form.exec-file-upload:visible").each( function() { $(".upload-control", this).load("/vsba/fairfax/Board.nsf/FileExecutive input:file"); });
}
// Previous and Next in Left Nav
$(".pane-navigation .icon, #agenda .item").live("mouseover", function() {
	$(this).addClass("highlight").addClass("ui-corner-all");
});
$(".pane-navigation .icon, #agenda .item").live("mouseout", function() {
	$(this).removeClass("highlight");
});
$(".pane-navigation .icon, #agenda .item").live("click", function() {
	$(this).closest(".pane-navigation").find(".prevnext").removeClass("active").removeClass("highlight");
	$(this).addClass("active");
	$("#wrap-packet .prevnext").removeClass("active").removeClass("highlight");
});
// Launch Agenda Item links in New Browser window
$("#view-agenda-item a, #view-policy-item a, #view-library-item a").live("click", function() { 
	$(this).attr("target", "_blank"); 
});
// Trap old policy links
$("a[href*=cca18a640d445b5585256e7400431d4e]").live("click", function() { 
//	$.quickAlert("Please note", "This link temporarily disabled.", 2000);
//	return false; 
});
// Window Close
window.onbeforeunload = function () {
	if (isAnonymous) { return }
	if ( $("img.save:visible").not("#bd-options-nav img.save").length > 0 ) {
		return 'You have items in Edit mode.  If you leave the page, you will lose any changes.';
	}
	
}
// Wrap Content Areas in a Table
$(".pane-navigation").closest(".tab-content").wrapInner('<table width="100%"><tr></tr></table>');  // wrap tab-content that has pane-navigation in a table
$(".tab-title").each( function() { $(this).prependTo( $(this).closest(".tab-content") ); });  // move title out of table tag
$(".pane-navigation").wrap('<td class="pane-navigation-td" width="275" valign="top" align="left"></td>'); 
$(".pane-navigation-td").append('<img src="/global.nsf/spacer.gif" height=1 width=275 />');
$(".pane-content").wrap('<td valign="top" align="left"></td>');
// Date clickers
$("input.date").live("focusin", function() {
	$(this).datepicker({
		showOn: 'button',
		buttonImage: '/global.nsf/calendar.png',
		buttonImageOnly: true
	});
});
// Printing
// Print buttons
$(".content-navigation img.print, #btn-print-agenda, #btn-print-agenda1, #btn-print").live("click", function() {
	key = $(this).attr("key");
	$.printDialog(key);
});
// Print Dialog
$.printDialog = function(key) {	
	$.getAgendaDetailedForPrint( $.trim($("#current-meeting").val()), key );
	// Erase previous content
	$("#print-dialog .tab-content-print").empty();
	// Copy on screen content to print dialog
	$(".category, .item", "#agenda").clone(false).appendTo("#tab-1.tab-content-print");
	// copy in existing on-screen content
	$("#tab-3.tab-content-print").html( $("#view-agenda-item").html() );
	$("#tab-4.tab-content-print").html( $("#view-policy-item").html() );
	$("#tab-5.tab-content-print").html( $("#view-library-item").html() );
	// Add logo, header, footers
	$("#tab-1.tab-content-print, #tab-2.tab-content-print").append( $("#print-agenda-footer").html() );
	$("#tab-3.tab-content-print").prepend( $("#print-agenda-item-header").html() );
	$("#tab-3.tab-content-print").append( $("#print-agenda-item-footer").html() );
//	$("#print-dialog .tab-content-print").prepend( $("#print-logo").html() );
	// add class to keep other stuff from printing
	$.hideNonPrint();
}
// make sure print dialog links launch in new window 
$("#print-dialog .tab-content-print a").live("click", function() {
	$(this).attr("target", "_blank");
});
// Init Print Dialog
$("#print-dialog").tabs({
    	show: function(event, ui) {
		// copy visible tab's content to print div
		$("#for-print").html( $(ui.panel).html() );
	}
});
// Print dialog box setup
$("#print-dialog").dialog({
	autoOpen: false,
	bgiframe: true,
	width: $(window).width()-50,
	height: $(window).height()-50,
	modal: true,
	resizable: false,
	draggable: false,
	buttons: {
		'Close': function() {
			$(this).dialog('close');
			$("#for-print").html("Please print by using the print button within the BoardDocs application.");
		},
		'Print': function() {
			window.print();
			$.windowResize();
		}
	},
	close: function() {
		$.windowResize();
	}
});
// Generic Dialog
$("#generic-dialog").dialog({
	autoOpen: false,
	bgiframe: true,
	width: $(window).width()-50,
	height: $(window).height()-50,
	modal: true,
	resizable: false,
	draggable: false,
	title: 'Meeting Minutes',
	buttons: {
		'Close': function() {
			$(this).dialog('close');
			$.windowResize();
		},
		'Print': function() {
			// Copy to print div
			$("#for-print").html( $("#generic-dialog").html() );
			window.print();
		}
	},
	close: function() {
		$.windowResize();
	}
});
// Site Images Dialog
$("#site-images-dialog").dialog({
	autoOpen: false,
	bgiframe: true,
	width: 700,
	height: 350,
	modal: true,
	resizable: false,
	draggable: false,
	title: 'Site Images',
	buttons: {
		'Close': function() {
			$(this).dialog('close');
			$.windowResize();
		}
	},
	open: function() {
		// upload controls
		$.updateUploadAreas();
		$.getPublicFiles( "SITE_LOGO", $("[key=publicfiles]", "#site-logo-upload") ); 
		$.getPublicFiles( "PRINT_LOGO", $("[key=publicfiles]", "#print-logo-upload") ); 
		$.getPublicFiles( "WELCOME_IMAGE", $("[key=publicfiles]", "#welcome-image-upload") ); 
	},
	close: function() {
		var href;
		href = $("#site-images-dialog .public-file[unique=SITE_LOGO]").find("a").attr("href");
		if ( href != "" ) { $("#header img").attr("src", href ) };
		href = $("#site-images-dialog .public-file[unique=PRINT_LOGO]").find("a").attr("href");
		if ( href != "" ) { $(".print-logo img").attr("src", href ) };
		href = $("#site-images-dialog .public-file[unique=WELCOME_IMAGE]").find("a").attr("href");
		if ( href != "" ) { $("#intro-page img.intro-image").attr("src", href ) };
	}
});
// getAgendaDetailedForPrint
$.getAgendaDetailedForPrint = function( meetingid, key ) {	
	$.ajax({
		type: "POST",
		cache: false,
		url: "/vsba/fairfax/Board.nsf/PRINT-AgendaDetailed?open&" + Math.random(), 
		data: ({ id : meetingid }),
		success: function(data){ 
			$("#print-dialog #tab-2").prepend( data );
			// action type hidden by default, show for detailed agenda
			$("#tab-2 .actiontype").show();
			// move agenda items into the wrap-items area 
			$("#print-dialog #tab-2 .item").each( function(){ $(this).prev(".wrap-category").children(".wrap-items").append( $(this) ); });
			// Number the two agendas
			// $.numberTheAgenda( $("#print-dialog #tab-2") );
			// Category 
			$("#tab-2 .category").hide();
			// Page Break		
			if ( 1==0  ) {
				$("#tab-2 .item").css("page-break-after", "always");
			}
			// Copy meeting info to simple agenda
			$("#print-top-meeting-info").clone(false).prependTo("#print-dialog #tab-1");
			// Prepend the logo
			$("#print-dialog .tab-content-print").prepend( $("#print-logo").html() );
			// remove blank file areas
			$("#print-dialog #tab-2 .print-files").each( function() {
				if ( $(this).find(".print-file").length == 0 ) {
					$(this).remove();
				}
			});
			// Copy content of active tab to print div
			$("#for-print").html( $(".tab-content-print").not(".ui-tabs-hide").eq(0).html() );
			// initially show all tabs & panels
			$("#print-dialog ul li").show();  
			$(".tab-content-print", "#print-dialog").show(); 
			// make content copy to print area by selecting a few random tabs first.  
			$("#print-dialog").tabs('select', '#tab-1');  $("#print-dialog").tabs('select', '#tab-2');  
			// trim out tabs with no content
			$("#print-dialog .tab-content-print").each( function() {
				if ( $.trim( $(this).html() ) == "" ) {
					thekey = $(this).attr("key");
					$("#print-dialog ul li[key=" + thekey + "]").hide();
				}
			});
			// additional tab hiding	
			if ( key == "4" ) {  
				$(".print-agenda, .print-library", "#print-dialog").hide(); 
			} 
			if ( key == "5" ) { 
				$(".print-agenda, .print-policy", "#print-dialog").hide(); 
			}
			
			if ( key == "1" || key == "2" || key == "3") { 
				$(".print-policy, .print-library", "#print-dialog").hide(); 
			}
			// select featured tab, copies contents to print area
			$("#print-dialog").tabs('select', '#tab-' + key);
			// resize window
			$.windowResize();
			// open the print dialog box
			$("#print-dialog").dialog("open");
		}
	});
}
// getSearchBoxText
$.getSearchBoxText = function(el) {
	var searchbox = $(el);
	var searchstring = $("input", searchbox).val();
	if (typeof(searchstring) != "string") { return false }
	if ( searchstring == "Search" ) { return false }
	return searchstring;
}
// setSearchBoxText
$.setSearchBoxText = function(el, value) {
	var searchbox = $(el);
	$("input", searchbox).val(value);
}
// Navigate To
$.navigateTo = function(unique) {
	$.ajax({
		type: "POST",
		dataType: "script",
		cache: false,
		url: "/vsba/fairfax/Board.nsf/LT-NavigateTo.js?open&" + Math.random(), 
		data: ({ id : unique })
	});
	
}
// browserCheck
$.browserCheck = function () {
	// set browser
	if ( $.browser.msie ) { $("#display-browser").html( "Internet Explorer" ); }
	if ( $.browser.mozilla ) { $("#display-browser").html( "Mozilla/Firefox" ); }
	if ( $.browser.webkit ) { $("#display-browser").html( "Webkit/Safari/Chrome" ); }
	// set browser version
	$("#display-browser-version").html( $.browser.version );
	// redirect old browsers
	var check = true;
	if ( $.browser.msie && $.browser.version < 7 ) { check = false; }
	if ( $.browser.mozilla && parseFloat($.browser.version) < 1.9 ) { check = false; }
	if ( $.browser.webkit && parseFloat($.browser.version) < 520 ) { check = false; } 
	
	if ( check == false ) {
		alert("Please upgrade your browser to get the most out of BoardDocs.");
		window.location = "http://www.boarddocs.com/Home.nsf/(WebContent)/752B674671E5EFAD872576D300437E3F"
	}	
}
// Allow New Item for Select Box
$.allowNew = function(el) {
	if ( $.trim($(el).val()) == "allownew" ) {
		var select = $(el);
		var selectname = $(select).attr("name");
		$(select).after("<input name='" + selectname + "' type='text' class='ui-widget-content ui-corner-all' />");
		$(select).closest(".tab-content").find("[name=" + selectname + "]").trigger("focus");
		$(select).remove();
	}
}
// site image edit
$("#click-for-site-images").live("click", function() {
	if (! isPublisher) { return false; }
	
	$("#site-images-dialog").dialog("open");
	return false;
});
// update upload controls
// if ( isPublisher ) { $.updateUploadAreas(); }
// Init
$("#for-print").html("Please print by using the print button within the BoardDocs application.");
$.browserCheck();
// check for goto id
if (queryGotoID != "" && queryGotoID != "none") { 
	// navigate to a goto ID
	$.navigateTo(queryGotoID);  
} else {
	// no goto ID
	if (isBoardDocsLT) { 
		// BoardDocs LT shows meetings tab by default, so click active meetings
		$("#li-meetings .tab-menuitem[key=active]").trigger("click");
	}
}
});
