Program = Class.create();
Program.prototype = {
	url : this.url,
	initialize : function(url){
	  this.url = url;
	},

	module : prototype = {

	},
	component : prototype = { 
 		/*
 		 * Mark the component complete
 		 */
		complete : function(event,componentID,form){
			new Ajax.Request(Program.url + event, {
				method: 'get',
				parameters: "&component_id=" + componentID,
				onSuccess: function(e){
					if (form != '') {
						$(form).submit();
					}
				}
			} );
			return false;
		},
		
    	/*
    	 * Open a new window that is 800x600 with scrollbars for Component of type Resource
    	 */
		open : function(url,event,componentID,link){
		  window.open(url, '_blank', 'scrollbars=yes,status=true,toolbar=true,resizeable=true,menubar=yes');
		  Program.component.complete(event,componentID,link);
		  return false;
		},
		
    	/*
    	 * Open component in current window and mark it as completed (used for DATSTAT)
    	 */
		openInCurrentWindow : function(url,event,componentID,link){
		  new Ajax.Request(Program.url + event, {method: 'get', parameters: "&component_id=" + componentID, onSuccess : function(e){ document.location = url } });
		  return false;
		},
	
	
		/* Journal section    */
		journal : prototype = {
			
			url  : '',
			form : '',
		
	  	/* Mark the component complete */
			save : function(submitFlag){
				// Manually trigger the save since we are calling form.serialize
				tinyMCE.triggerSave();
				new Ajax.Request(Program.url + Program.component.journal.url, {method: 'post', parameters: Form.serialize(Program.component.journal.form), onSuccess : function(e){ if(submitFlag){$('commit').submit();}  } });
				return false;
			}, 
			/* Update the word count on the textarea onchange */
			change : function(inst){
				var content = inst.getBody().innerHTML;
				var contentCount = content.split(" ");
				$('wordCount').innerHTML = contentCount.length;
			},
			/* Count the word count on keypress */
			setup : function(e){
				switch (e.type) {
					case "keydown":
						Program.component.journal.change(tinyMCE.getInstanceById('answer'));
						return true;
				}
			return false;
			},
			/* Autosave the form every 5 minutes */
			autosave : function(inst){
				window.setInterval("Program.component.journal.save(false)",300000);
				//Program.component.journal.autosave(inst);
			}
		},
		/* Testing section   */ 
		test : prototype = {
			update : function(form){
				new Ajax.Request(Program.url + $F('xe.commit'), {method: 'post', parameters: Form.serialize(form)});
			},
			save : function(form){
				// Ensure that the form variables are set to save and not complete the test
				$('save').value = '1';
				$('complete').value = '0';
				
				// Set the form action to return to the program listing
				$(form).action = $F('xe.save');
				$(form).submit();
			},
			complete : function(form){
				$('save').value = '0';
				$('complete').value = '1';
				$(form).submit();
			}
		}
	},
	
	Media : prototype = {
			remove : function(src, name, type){
			  if(confirm("Are you sure you want to delete this " + type + "?")){
					Element.remove(src);
					
					// Hide the detailDiv if present 
					if($('detailDiv'))
						Element.hide('detailDiv');
						
					// Update the image source if present
					if(name != undefined){ 
						$(name).value = 0;
					}
						
				}
				return false;
			}
	}
};

/* Global functions for Flash Module to mark completed and return to dashboard actions */
componentComplete = function(){ 
	Program.component.complete($F('event'), $F('component_id'), ''); 
} 
returnToDashboard = function(){ 
	$('flashForm').submit(); 
}