$(document).ready(function(){
	setMenus();
	var strLoginLinkText = $('#header_nav_link_login').html();
	if( strLoginLinkText == 'SIGN OUT'){
		$('#header_nav_link_login').attr('href','javascript:void(0);');
		$('#header_nav_link_login').bind('click',onLogoutClicked);
		$("#logout_form").bind('submit',onLogoutFormSubmit);
		var page = GetFilename();
		$('#btnShowTextArea').show().bind('click',onShowEditorClicked);
	}
	
	$('.printButton').bind('click',printContent);
	
	function printContent(){
		$("#txtbody").printElement({
			printMode:'popup',
			overrideElementCSS:[
			'ckeditor/contents.css',
			{ href:'ckeditor/contents.css',media:'print'}]
		});
	}

	function onShowEditorClicked(){
		$('#content_container').addClass('loaders');
		$('#txtbody').hide();
		$('#btnShowTextArea').hide();
		$("#txtarea1_form").bind('submit',onEditorFormSubmit);
		
		editor = CKEDITOR.replace('txtContent',{
			height:380,
			on:{'instanceReady':function(evt){CKEDITOR.instances.txtContent.focus();}}
		});
			
		//CKFinder.setupCKEditor(editor);
		CKFinder.setupCKEditor( editor, '/ckfinder/' ); 
		CKEDITOR.plugins.registered['save']={
		   init : function( editor ){
			  var command = editor.addCommand( 'save', 
				 {
					modes : { wysiwyg:1, source:1 },
					exec : function( editor ) {
						$('#content_container').removeClass('loaders');
						$("form#txtarea1_form").submit();
					}
				 }
			  );
			  editor.ui.addButton( 'Save',{label : 'Save and Close',command : 'save'});
		   }
		}		
	}
	
	function onEditorFormSubmit(){
		$('#dbstatus').html('saving...').show();
		var userText = editor.getData();
		$.post("ajax/update_content.php",{
			txtarea:userText,
			page:page
		},function(data){
			$('#dbstatus').html('saved!').show();
			data = cleanUpForEditor(data);
			$('#txtbody').html(data).show();
			$('#btnCloseTextArea').hide(); 
			$('#dbstatus').html('').hide(); 
			editor.destroy();
			$('#btnShowTextArea').show();
		});
		return false;
	}
});

