Event.observe(window, 'load', init, false);

function init(){
	//makeEditable('desc');
	//makeEditable('pizza');
}

function showPreview(id,id_cmt,id_final,id_n,id_item){
	
	//Event.observe(id, 'click', function(){preview($(id),$(id_cmt),$(id_final),$(id_n),id)}, false);
	preview($(id),$(id_cmt),$(id_final),$(id_n),$(id_item),id);
	//Event.observe(id, 'click', function(){edit($(id))}, false);
	//Event.observe(id, 'mouseover', function(){showAsEditable($(id))}, false); // style change only
	//Event.observe(id, 'mouseout', function(){showAsEditable($(id), true)}, false); // style change only
}

function preview(obj,obj_cmt,obj_final,obj_n,obj_item,id){
	Element.hide(obj);
	Element.hide(obj_cmt);
	var textarea =  '<div id="'+obj.id+'_editor"><div id="'+obj.id+'_edit"><pre class="edit_pre" WRAP="virtual">'+obj_cmt.value+'</pre></div><BR>'; //'<div id="'+obj.id+'_editor"><div id="'+obj.id+'_edit" name="'+obj.id+'">'+obj.innerHTML+'</div>';
	var button	 = '<div><input id="'+obj.id+'_cancel" type="button" value="EDIT" /> <input id="'+obj.id+'_save" type="button" value="SAVE" /></div></div>';
	new Insertion.After(obj, textarea+button);	
		
	Event.observe(obj.id+'_save', 'click', function(){saveChanges(obj,obj_cmt,obj_final,obj_n,obj_item)}, false);
	Event.observe(obj.id+'_cancel', 'click', function(){cleanUp(obj,obj_cmt)}, false);
}

function showEdit(id){
	Event.observe(id, 'click', function(){edit($(id))}, false);
	Event.observe(id, 'mouseover', function(){showAsEditable($(id))}, false); // style change only
	Event.observe(id, 'mouseout', function(){showAsEditable($(id), true)}, false); // style change only
}


function edit(obj){

	Element.hide(obj);
	if(document.getElementById(obj.id+'_editor') != null) {
		Element.show(obj.id+'_editor');
	}
	else {
	var textarea = '<div id="'+obj.id+'_editor"><textarea id="'+obj.id+'_edit" name="'+obj.id+'" rows="4" cols="60">'+obj.innerHTML+'</textarea>';
	var button	 = '<div><input id="'+obj.id+'_save" type="button" value="SAVE" /> or <input id="'+obj.id+'_cancel" type="button" value="CANCEL" /></div></div>';
	new Insertion.After(obj, textarea+button);	
	}
	
		
	Event.observe(obj.id+'_save', 'click', function(){saveChangesEdit(obj)}, false);
	Event.observe(obj.id+'_cancel', 'click', function(){cleanUpEdit(obj)}, false);
	
}

function showAsEditable(obj, clear){
	if (!clear){
		Element.addClassName(obj, 'editable');
	}else{
		Element.removeClassName(obj, 'editable');
	}
}

function saveChanges(obj,obj_cmt,obj_final,obj_n,obj_item){
//global ajax_result_url;	
	var new_content	=  escape($F(obj_cmt));
	var new_user	=  escape($F(obj_n));
	if (trim(new_user)=='') {alert("Please enter your name");return false;}
	//cleanUp(obj,obj_cmt, true);
	removePreview(obj,obj_n);
	obj_final.innerHTML	= '<img src="indicator.gif"> Saving...';

	var success	= function(t){editComplete(t, obj_final);}
	var failure	= function(t){editFailed(t, obj_final);}

  	var url = ajax_result_url; // 'ajax_edit.php';
	var pars = 'id='+obj.id+'&content='+new_content+'&user='+new_user+"&item="+$F(obj_item);
	var myAjax = new Ajax.Request(url, {method:'post', postBody:pars, onSuccess:success, onFailure:failure});

}

function saveChangesEdit(obj){
//global ajax_result_url;	
	var new_content	=  escape($F(obj.id+"_edit"));
	cleanUpEdit(obj, true);
	obj.innerHTML	= '<img src="indicator.gif"> Saving...';

	var success	= function(t){editComplete(t, obj);}
	var failure	= function(t){editFailed(t, obj);}

  	var url = ajax_update_url; // 'ajax_cmt_update.php';
	var pars = 'id='+obj.id+'&content='+new_content;
	var myAjax = new Ajax.Request(url, {method:'post', postBody:pars, onSuccess:success, onFailure:failure});

}

function editComplete(t, obj){
	obj.innerHTML	= t.responseText;
	showAsEditable(obj, true);
}

function removePreview(obj,obj_n) {
	Element.remove(obj.id+'_editor');
	Element.remove(obj_n);
       // document.getElementById('extras').style.display="none";
        document.getElementById('extras').innerHTML = '<b>All comments for this video</b>';
}


function cleanUp(obj, obj_cmt, keepEditable){
	Element.remove(obj.id+'_editor');
	Element.show(obj);
	Element.show(obj_cmt);
	if (!keepEditable) showAsEditable(obj, true);
}

function cleanUpEdit(obj, keepEditable){
	Element.remove(obj.id+'_editor');
	Element.show(obj);
	//if (!keepEditable) showAsEditable(obj, true);
}


function editFailed(t, obj){
	obj.innerHTML	= 'Sorry, the update failed.';
	cleanUp(obj);
}


function trim(inputString)
{
 if (typeof inputString != "string") { return inputString; }
 var retValue = inputString;
 var ch = retValue.substring(0, 1);
 while (ch == " ")
  { // Check for spaces at the beginning of the string
  retValue = retValue.substring(1, retValue.length);
  ch = retValue.substring(0, 1);
 }
 ch = retValue.substring(retValue.length-1, retValue.length);
 while (ch == " ")
 { // Check for spaces at the end of the string
  retValue = retValue.substring(0, retValue.length-1);
  ch = retValue.substring(retValue.length-1, retValue.length);
 }
 while (retValue.indexOf("  ") != -1)
 { 
 // Note that there are two spaces in the string
 // - look for multiple spaces within the string
  retValue = retValue.substring(0, 
    retValue.indexOf("  ")) + 
    retValue.substring(retValue.indexOf("  ")+1, 

 retValue.length);
 // Again, there are two spaces in each of the strings
 }
 return retValue; // Return the trimmed string back to the user
}


function valid_cmt() {

	if (trim(document.cmt_form.yourname.value)!='') {
		document.cmt_form.yourname.value=trim(document.cmt_form.yourname.value);
	}
	else {
		alert('Enter your name');
		return false;
	}
	
	if (trim(document.cmt_form.comment.value)!='') {
		document.cmt_form.comment.value=trim(document.cmt_form.comment.value);
	}
	else {
		alert('Enter a comment');
		return false;
	}
	return true;

}
