<?php
include('config.php');
header("content-type: application/x-javascript");
?>
var my_base_url='<?php echo my_base_url ?>';
var my_pligg_base='<?php echo my_pligg_base ?>';

Event.observe(window, 'load', init, false);

function init(){
}

function makeEditable(id){
	Event.observe(id, 'click', function(){edit($(id))}, false);
	Event.observe(id, 'mouseover', function(){showAsEditable($(id))}, false);
	Event.observe(id, 'mouseout', function(){showAsEditable($(id), true)}, false);
}

function edit(obj){
	Element.hide(obj);
	var catID = obj.id;
	var catIndex = catID.substr(catID.lastIndexOf('-')+1);
	var tag_id = document.getElementById("link_id-"+catIndex).value;
	if(obj.id == ('cat-'+catIndex)){
		var textarea1 = '<div id="'+obj.id+'_editor"><select id="'+obj.id+'_edit" name="'+obj.id+'" style="background-color: #ffffd3;width: 10%;font-size:16px;">';
		<?php
			global $db;
			$cats = $db->get_results(" Select * from " . table_categories . " where `category_id` != 0 ");
			if ($cats) {
				foreach($cats as $catname) {
				$cat_option .= "<option value=$catname->category_id>".$catname->category_name."</option>";
			}
		?>
			var textarea2 = '<?php echo $cat_option;?>';
		<?php
		}
		?>
		var textarea = textarea1 + textarea2 + '</select>';
	}
	else{
		var textarea = '<div id="'+obj.id+'_editor"><textarea id="'+obj.id+'_edit" name="'+obj.id+'" rows="4" cols="60" style="background-color: #ffffd3;width: 70%;font-size:16px;">'+trim(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(){saveChanges(obj)}, false);
	Event.observe(obj.id+'_cancel', 'click', function(){cleanUp(obj)}, false);
	
}

function showAsEditable(obj, clear){
	if (!clear){
		Element.addClassName(obj, 'editable');
	}else{
		Element.removeClassName(obj, 'editable');
	}
}

function saveChanges(obj){
	
	var new_content	=  escape($F(obj.id+'_edit'));
	obj.innerHTML	= "Saving...";
	cleanUp(obj, true);

	var success	= function(t){editComplete(t, obj);}
	var failure	= function(t){editFailed(t, obj);}
  	var url = my_base_url+my_pligg_base+'/edit_content.php';
	// get the linksummary number from obj.id
	var objID = obj.id;
	var linksummaryIndex = objID.substr(objID.lastIndexOf('-')+1);
	var link_id = document.getElementById("link_id-"+linksummaryIndex).value;
	var pars = 'id='+obj.id+'&content='+new_content+'&link_id='+link_id;
	/*
	if(obj.id=='cont'){var pars = 'id='+obj.id+'&content='+new_content+'&link_id='+link_id;} 
	if(obj.id=='cat'){var pars = 'id='+obj.id+'&content='+new_content+'&link_id='+link_id;} 
	*/
	var myAjax = new Ajax.Request(url, {method:'post', postBody:pars, onSuccess:success, onFailure:failure});

}

function cleanUp(obj, keepEditable){
	Element.remove(obj.id+'_editor');
	Element.show(obj);
	if (!keepEditable) showAsEditable(obj, true);
}

function editComplete(t, obj){
	obj.innerHTML	= t.responseText;
	showAsEditable(obj, true);
}

function editFailed(t, obj){
	obj.innerHTML	= 'Sorry, the update failed.';
	cleanUp(obj);
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
