$(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();

		//handle title edit
		var left_text_width = 370;
		$('#title_field').editable('ajax/update_title.php?page='+page,{
			indicator	: "Saving...",
			select 		: true,
			submit 		: 'OK',
			cancel 		: 'cancel',
			width		: left_text_width
		});
		
		$('#news_title_field').editable('ajax/update_news_header.php?type=1',{
			indicator	: "Saving...",
			select 		: true,
			submit 		: 'OK',
			cancel 		: 'cancel',
			width		: left_text_width
		});
		
		//handle main article click		
		$('#main_article').bind("click",onShowEditorClicked);
		$('#txtnewsarticle').bind("click",onShowEditorClickedArticle);
		
		var right_text_field = 373;
		
		$('#asu_events_field').editable('ajax/update_events.php?type=1',{
			indicator	: "Saving...",
			type      	: 'textarea',
			select 		: true,
			submit 		: 'OK',
			cancel 		: 'cancel',
			width		: right_text_field
		});
		
		$('#alumni_events_field').editable('ajax/update_events.php?type=2',{
			indicator	: "Saving...",
			type      	: 'textarea',
			select 		: true,
			submit 		: 'OK',
			cancel 		: 'cancel',
			width		: right_text_field
		});
		
		$('#chairs_events_field').editable('ajax/update_events.php?type=3',{
			indicator	: "Saving...",
			select 		: true,
			type      	: 'textarea',
			submit 		: 'OK',
			cancel 		: 'cancel',
			width		: right_text_field
		});
		
		$('#cfrac_events_field').editable('ajax/update_events.php?type=4',{
			indicator	: "Saving...",
			select 		: true,
			type      	: 'textarea',
			submit 		: 'OK',
			cancel 		: 'cancel',
			width		: right_text_field
		});
		
		$('#ccs_events_field').editable('ajax/update_events.php?type=5',{
			indicator	: "Saving...",
			select 		: true,
			type      	: 'textarea',
			submit 		: 'OK',
			cancel 		: 'cancel',
			width		: right_text_field
		});
		
		$('#sjs_events_field').editable('ajax/update_events.php?type=6',{
			indicator	: "Saving...",
			select 		: true,
			type      	: 'textarea',
			submit 		: 'OK',
			cancel 		: 'cancel',
			width		: right_text_field
		});
		
		$('#dss_events_field').editable('ajax/update_events.php?type=7',{
			indicator	: "Saving...",
			select 		: true,
			type      	: 'textarea',
			submit 		: 'OK',
			cancel 		: 'cancel',
			width		: right_text_field
		});
		
	}
	
	Galleria.loadTheme('galleria/themes/classic/galleria.classic.min.js');
	$("#gallery").galleria({
		width: 520,
		height: 350
	});
	
	function onShowEditorClickedArticle(){
		$('#txtnewsarticle').hide();
		//$("#txtarea1_form").bind('submit',onEditorFormSubmitArticle);
		
		editor2 = CKEDITOR.replace('txtContent2',{
			height:380,
			on:{'instanceReady':function(evt){CKEDITOR.instances.txtContent2.focus();}}
		});
			
		CKFinder.setupCKEditor( editor2, '/ckfinder/' ); 
		CKEDITOR.plugins.registered['save']={
		   init : function( editor2 ){
			  var command = editor2.addCommand( 'save', 
				 {
					modes : { wysiwyg:1, source:1 },
					exec : function( editor ) {
						//$("form#txtarea1_form").submit();
						$("form#txtarea1_form").submit(function() {
							var articleText = editor.getData();
							$.post("ajax/update_news.php",{
								value:articleText,
								id:1
							},function(data){
								data = cleanUpForEditor(data);
								$('#txtnewsarticle').html(data).show();
								editor.destroy();
							});
							return false;
						});						
					}
				 }
			  );
			  editor2.ui.addButton( 'Save',{label : 'Save and Close',command : 'save'});
		   }
		}		
	}
	
	function onShowEditorClicked(){
		$('#txtbody').hide();
		//$("#txtarea1_form").bind('submit',onEditorFormSubmit);
		
		editor = CKEDITOR.replace('txtContent',{
			height:380,
			on:{'instanceReady':function(evt){CKEDITOR.instances.txtContent.focus();}}
		});
			
		CKFinder.setupCKEditor( editor, '/ckfinder/' ); 
		CKEDITOR.plugins.registered['save']={
		   init : function( editor ){
			  var command = editor.addCommand( 'save', 
				 {
					modes : { wysiwyg:1, source:1 },
					exec : function( editor ) {
						//$("form#txtarea1_form").submit();
						$("form#txtarea1_form").submit(function() {
							
							var userText = editor.getData();
							$.post("ajax/update_content.php",{
								txtarea:userText,
								page:page
							},function(data){
								data = cleanUpForEditor(data);
								$('#txtbody').html(data).show();
								editor.destroy();
							});
							return false;
						});
					}
				 }
			  );
			  editor.ui.addButton( 'Save',{label : 'Save and Close',command : 'save'});
		   }
		}		
	}
	
//	function onEditorFormSubmit(){
//		var userText = editor.getData();
//		$.post("ajax/update_content.php",{
//			txtarea:userText,
//			page:page
//		},function(data){
//			data = cleanUpForEditor(data);
//			$('#txtbody').html(data).show();
//			editor.destroy();
//		});
//		return false;
//	}
	
//	function onEditorFormSubmitArticle(){
//		var articleText = editor2.getData();
//		$.post("ajax/update_news.php",{
//			value:articleText,
//			id:1
//		},function(data){
//			data = cleanUpForEditor(data);
//			$('#txtnewsarticle').html(data).show();
//			editor2.destroy();
//		});
//		return false;
//	}
});
