/* library.js */
$(document).ready(function() {
var librarypubliceditor;
var libraryadmineditor;
var libraryexeceditor;
// Click a Library Item - live
$("#tab-library a.icon, #tab-library img.add").live("click", function() {
	// Check for Library in Edit Mode
	if ( $("#edit-library-item:visible").length > 0 ) {
		if (! confirm("Close this item without saving?")) { return false };
	}
	// left nav highlighting
	$("a.icon", $(this).closest(".pane-navigation")).not($(this)).removeClass("active").removeClass("ui-corner-all").removeClass("ui-widget-content");
	$(this).filter(".library").addClass("ui-corner-all").addClass("ui-widget-content").addClass("active");
	$("#wrap-library-item div", "#tab-library").not(".content-navigation").addClass("visibility-hidden");
		
	// remove editors
	// $.destroyLibraryItemEditors();  
	var unique = $(this).attr("unique");
	$("#current-library-item").val( unique );
								
	// Show & Hide Buttons
	if ( ! unique ) {
		// New Item
		$(".content-navigation img.edit", "#tab-library").addClass("display-none");
		$(".content-navigation img.save", "#tab-library").removeClass("display-none");
	} else {
		// Existing Item
		$(".content-navigation img.edit", "#tab-library").removeClass("display-none");
		$(".content-navigation img.save", "#tab-library").addClass("display-none");
	}
	$.ajax({
		type: "GET",
		url: "/co/acsd50/Board.nsf/LT-GetLibraryItem?open&" + Math.random(),
		data: { id: unique, type: $("#current-library-type").val() },
		dataType: "html",
		success: function(data){
			$("#library-content").html(data);
			
			// show document
			$(".pane-content", "#tab-library").show();
			
			// put new doc in edit mode
			var isnewdoc = $("[name=isnewdoc]", "#library-form").val();
			if ( isnewdoc == "1") { $("img.edit", "#wrap-library-item").trigger("click"); }
			// Attach Button hover info
			$.updateToolTips( $("#tab-library .content-navigation img") );
			$.getPublicFiles( unique, $("[key=publicfiles]", "#wrap-library-item") );
			if (isAdmin || isExecutive) { $.getAdminFiles( unique, $("[key=adminfiles]", "#wrap-library-item") ); }
			if (isExecutive) { $.getExecFiles( unique, $("[key=execfiles]", "#wrap-library-item") ); }
			// annotations
			if ( isExecAnnotations ) { $.addBdAnnotationsPanel( $("#tab-library"), unique ); }
		}
	});	
	return false;
});
// attachLibraryListFunctions()
$.attachLibraryListFunctions = function() {	
	// If a library item is current, then open it and expand it's section
	var currentlibraryid = $.trim($("#current-library-item").val());
	$("#tab-library .pane-navigation a[unique=" + currentlibraryid + "]").eq(0).trigger("click").parent().prev("h3").find("a").trigger("click");
	// expand 1st section if none are open
	if ( $(".ui-state-active", "#library-accordion").length == 0 ) { $("h3:first", "#library-accordion").trigger("click"); }
	if ( ! isPublisher ) { return }
	// Library right-click
	$("a.icon", "#library").not(".boardmember").contextMenu('myMenu1', {
		onShowMenu: function(e, menu) {
			$('li', menu).not("#addpacket, #delete, #copyasnew").remove();
			return menu;
		}, 
		bindings: {
			'addpacket': function(t) {
				$.addToPacket( $(t).attr('unique') );
			},
			'delete': function(t) {
				if ( confirm( 'Delete this Library Item?\n\n' + $(t).children('a').text() + '\n ') ) { 
					$.deleteByID( $(t).attr('unique') );
					$(t).addClass('being-deleted').hide("pulsate", { times: 5 }, 500);
				}
			},
			'copyasnew': function(t) {
				// not yet
			}
		}
	});
}
// getLibrary()
$.getLibrary = function(type) {	
	$.ajax({
		type: "POST",
		url: "/co/acsd50/Board.nsf/LT-GetLibrary?open&" + Math.random(),
		data: { type: type },
		dataType: "html",
		success: function(data){
			$("#library").html(data);
			// Remove empty categories
			$("h3", "#library-accordion").each( function() {
				theNext = $(this).next("div");
				if ( ! $.trim( $(theNext).text() ) ) {
					$(this).remove();
					$(theNext).remove();
				}
			});
			$("#library-accordion").accordion({ collapsible: true, autoHeight: false, active: false });
			$.attachLibraryListFunctions();
		}
	});
}
// Tab Menu
$(".tab-menuitem", "#li-library").live( "click", function() {
	var key = $(this).attr("key");
	$("#tab-library .pane-navigation > div").hide();
	$("#tab-library .pane-navigation > div").show();
	switch (key)
	{
		case "general":
			$("#tab-library .tab-title").html("General");
			break;
		case "board":
			$("#tab-library .tab-title").html("Board Members");
			break;
		case "event":
			$("#tab-library .tab-title").html("Events");
			break;
		case "goal":
			$("#tab-library .tab-title").html("Goals");
			break;
	}
	
	$("#current-library-type").val( key );
	$.getLibrary(key);
});
// Edit Library
$(".content-navigation img.edit", "#tab-library").live( "click", function() {
	var libraryid = $("[name=unique]", "#library-form").val();
	// refresh page to show/hide event specific fields
	$.updateLibraryPage();
	// show & hide buttons
	$(".content-navigation img.previous", "#tab-library").addClass("display-none");
	$(".content-navigation img.next", "#tab-library").addClass("display-none");
	$(".content-navigation img.edit", "#tab-library").addClass("display-none");
	$(".content-navigation img.print", "#tab-library").addClass("display-none");
	$(".content-navigation img.close", "#tab-library").removeClass("display-none");
//	$(".content-navigation img.save", "#tab-library").removeClass("display-none");
	// show & hide content
	$("#view-library-item").hide();
	$("#edit-library-item").show();
	// HTML Editors
	$.addLibraryItemEditors();
	// Grab upload controls
	$.updateUploadAreas();
	$.windowResize();
	return false;
		
});
// Save Library Item
$(".content-navigation img.save", "#tab-library").live( "click", function() {
	var lengthpublic; 	try { lengthpublic = $("[name=library_public_body]", "#edit-library-item").val().length; } catch(err) { lengthpublic = 0; }
	var lengthadmin; 	try { lengthadmin = $("[name=library_admin_body]", "#edit-library-item").val().length; } catch(err) { lengthadmin = 0; }
	var lengthexec; 	try { lengthexec = $("[name=library_exec_body]", "#edit-library-item").val().length; } catch(err) { lengthexec = 0; }
	if ( lengthpublic > 64000 || lengthadmin > 64000 || lengthexec > 64000 ) {
		$.quickAlert("Cannot Save", "You have too much information in one field.  Try to trim the amount of information you are saving.  Please contact Support if you need assitance.", 8000);
		return false;
	}
	// get form values
	var theType = $("[name=type]", "#library-form").val().toLowerCase();
	// set current Library item id
	var currentlibraryid = $("[name=unique]", "#library-form").val();
	$("#current-library-item").val( currentlibraryid );
	// check required fields
	if ($("[name=name]", "#library-form").val() == "" || $("[name=type]", "#library-form").val() == "" || $("[name=ReleaseDate]", "#library-form").val() == "") {
		$.quickAlert("Fields Required", "Type, Name and Release Date are required to save.", 4000);
		$("[name=name]", "#library-form").css("background","#ffe");
		$("[name=type]", "#library-form").css("background","#ffe");
		$("[name=ReleaseDate]", "#library-form").css("background","#ffe");
		return;
	}
	// date required for event
	if ($("[name=type]", "#library-form").val() == "event" && $("[name=StartDate]", "#library-form").val() == "") {
		$.quickAlert("Field Required", "Date required to save an event.", 4000);
		$("[name=StartDate]", "#library-form").css("background","#ffe");
		return;
	}
	// hide content area
	$(".pane-content", "#tab-library").hide();
	// Destroy Item Editors
	// $.destroyLibraryItemEditors();
	// Serialize form with HTML areas
	var query = $("#library-form, [name=library_public_body], [name=library_admin_body], [name=library_exec_body]", "#edit-library-item").serialize();
	// Send the data	
	$.ajax({
		type: "POST",
		url: "/co/acsd50/Board.nsf/LT-SaveLibraryItem?open&" + Math.random(),
		dataType: "html",
		data: query,
		success: function(){
			$("#li-library .tab-menuitems [key=" + theType + "]").trigger("click");
		}
	});
});
// Close Library Item
$(".content-navigation img.close", "#tab-library").live( "click", function() {
	if (! confirm("Close this item without saving?")) { return false };	
	$( "#library-content").empty();
 	$(".pane-navigation a.icon.active", "#tab-library").trigger("click");
});
// Add Library Item Editors
$.addLibraryItemEditors = function() {
	// remove any instances
	$("#edit-library-item .ckeditor").each( function() {
		var id = $(this).attr("id");
		var instance = CKEDITOR.instances[ id ];
		if ( instance ) { CKEDITOR.remove( instance ); }
	})
	// create public and admin
	$("#edit-library-item ").find(".ckeditor.public, .ckeditor.admin").ckeditor( 
		function( ) { $(".content-navigation img.save", "#wrap-library-item").removeClass("display-none"); }, 
		{ customConfig : '/co/acsd50/Board.nsf/javascript/ckconfig.js?open&' } 
	);
	// create exec
	$("#edit-library-item ").find(".ckeditor.exec").ckeditor( 
		function( ) {  }, { customConfig : '/co/acsd50/Board.nsf/javascript/ckconfig-exec.js?open&' } 
	);
}
// Change event for Library Type
// $("#library-form [name=type]").live( "change", function() {
//	$.updateLibraryPage();
// });
// Update Library Page
$.updateLibraryPage = function() {	
	if ( $("[name=type]", "#library-form").val() == "event" ) {
		$(".show-not-event").hide();
		$(".show-event").show();
		$(".library-category").show();
	} else if ( $("[name=type]", "#library-form").val() == "board" ) {
		$(".show-not-event").show();
		$(".show-event").hide();
		$(".library-category").hide();
	} else {
		$(".show-not-event").show();
		$(".show-event").hide();
		$(".library-category").show();
	}
};
// initialize
});
