var Weblog_Title_Editor = null;
/*
	tinyMCE.init({
		theme : "advanced",
		mode : "textareas",
		//elements : "read,readmore",
		//insertlink_callback : "customInsertLink",
		//insertimage_callback : "customInsertImage",
		save_callback : "",
		//content_css : "html/css/css{/literal}{$_clientid}{literal}.css",
		extended_valid_elements : "a[name|href|target|title|onclick|class],img[class|src|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style],select[class|name|id|onchange],option[value|class]",
		plugins : "directionality,table,contextmenu,preview,searchreplace,paste,save,fullscreen,flash,advlink",
		theme_advanced_buttons1 : "bold,italic,underline,separator,justifyleft,justifycenter,justifyright,justifyfull,separator,fontselect,fontsizeselect,styleselect,separator,strikethrough,left,center,right,full,bullist,numlist,separator,indent,outdent,separator,search,replace",
		theme_advanced_buttons2 : "newdocument,separator,undo,redo,separator,cut,copy,pastetext,separator,link,unlink,separator,image,flash,separator,cleanup,separator,preview,forecolor",
		//theme_advanced_buttons3 : "",
		theme_advanced_buttons3_add_before : "tablecontrols,separator",
		theme_advanced_buttons3_add : "separator,fullscreen,code",
		//invalid_elements : "a",
		//theme_advanced_styles : tinyMCEStylesList, // Theme specific setting CSS classes
		custom_undo_redo_levels : 10,
		relative_urls : false,
		remove_script_host : true,
		document_base_url : siteurl,
		debug : false
	});
*/

function Weblog_AJAX_TitleEdit (obj) {
	this.tinyMCE = false;
	this.idarr = obj.id.split ('-');
	this.inputwidth = 0;
	this.htmlelement = 'input';
	if ( this.idarr[1] == 'post' ) { // page title
        this.linkobj = document.getElementById ('posttitle-'+this.idarr[2]);
        this.inputwidth = 650;
        this.htmlelement = 'input';
	} else if ( this.idarr[1] == 'postcontent' ) { // page simple content
        this.linkobj = document.getElementById ('postcontent-'+this.idarr[2]);
        this.inputwidth = 650;
        this.htmlelement = 'textarea';
	} else if ( this.idarr[1] == 'content' && this.idarr[2] == 'text' ) { // content titles
		this.linkobj = document.getElementById ('content-'+this.idarr[3]+'-'+this.idarr[4]);
        this.inputwidth = 650;
        this.htmlelement = 'input';
	} else if ( this.idarr[1] == 'content' && this.idarr[2] == 'textarea' ) { // content contents
		this.linkobj = document.getElementById ('content-'+this.idarr[3]+'-'+this.idarr[4]);
        this.inputwidth = 650;
        this.htmlelement = 'textarea';
	} else if ( this.idarr[1] == 'gallery' ) { // gallery title
		this.linkobj = document.getElementById ('gallerytitle-'+this.idarr[2]);
        this.inputwidth = 650;
        this.htmlelement = 'input';
	} else if ( this.idarr[1] == 'galleryimage' ) { // gallery image
		this.linkobj = document.getElementById ('galleryimage-title-'+this.idarr[2]);
        this.inputwidth = 650;
        this.htmlelement = 'input';
	} else if ( this.idarr[1] == 'category' ) { // category title
		this.linkobj = document.getElementById ('category-title-'+this.idarr[2]);
        this.inputwidth = 180;
        this.htmlelement = 'input';
	} else if ( this.idarr[1] == 'commentname' ) { // comment name
		this.linkobj = document.getElementById ('commentname-'+this.idarr[2]);
        this.inputwidth = 180;
        this.htmlelement = 'input';
	} else if ( this.idarr[1] == 'commenttext' ) { // comment text
		this.linkobj = document.getElementById ('commenttext-'+this.idarr[2]);
        this.inputwidth = 450;
        this.htmlelement = 'textarea';
	}

	this.container = this.linkobj.parentNode;
	
	this.editobj = null;

	this.origtitle = '';
	this.editstate = false;
	
	this.edit ();
}


Weblog_AJAX_TitleEdit.prototype.edit = function () {
		if (this.editstate == false) {
			if ( this.htmlelement == 'input' ) {
				this.inputobj = document.createElement('input');
				this.inputobj.type = 'text';
				this.inputobj.size = 50;
				this.inputobj.maxLength = 250;
				this.inputobj.style.width = this.inputwidth + 'px';
				this.inputobj.value = this.linkobj.innerHTML;
				this.inputobj.title = this.inputobj.value;
				this.inputobj.id = 'text-'+this.idarr[1]+'-'+this.idarr[2];
			} else if ( this.htmlelement == 'textarea' ) {
				this.inputobj = document.createElement('textarea');
				this.inputobj.cols = 80;
				this.inputobj.rows = 10;
				this.inputobj.style.width = this.inputwidth + 'px';
				this.inputobj.value = this.linkobj.innerHTML;
				this.inputobj.title = this.inputobj.value;
				this.inputobj.id = 'text-'+this.idarr[2]+'-'+this.idarr[3];
			}

			this.inputobj.onblur = Weblog_AJAX_Events.prototype.titleinput_onblur;
			this.inputobj.onkeypress = Weblog_AJAX_Events.prototype.titleinput_onkeypress;

			// insert the editor box and select it
			this.editobj = this.container.insertBefore(this.inputobj, this.linkobj);
			this.editobj.select ();

			// store the original text
			this.origtitle = this.linkobj.innerHTML;

			// hide the original object
			this.linkobj.style.display = 'none';
			
			// start tinyMCE
			if ( this.tinyMCE == true && this.htmlelement == 'textarea' ) {
				tinyMCE.execCommand ("mceAddControl", false, 'text-'+this.idarr[2]+'-'+this.idarr[3]);
				tinyMCE.execCommand ("mceFocus", false, 'text-'+this.idarr[2]+'-'+this.idarr[3]);
			}

			// declare that we are in an editing state
			this.editstate = true;
		}
};


Weblog_AJAX_TitleEdit.prototype.restore = function () {
		if (this.editstate == true) {
			// do we actually need to save?
			if (this.editobj.value != this.origtitle) {
				this.linkobj.innerHTML = this.editobj.value;
				this.save (this.editobj.value);
			} else {
				// set the new contents for the link
				this.linkobj.innerHTML = this.editobj.value;
			}
			
			// remove tinyMCE
			if ( this.tinyMCE == true && this.htmlelement == 'textarea' ) {
				tinyMCE.execCommand ("mceRemoveControl", false, 'text-'+this.idarr[2]+'-'+this.idarr[3]);
			}

			// remove the editor box
			this.container.removeChild(this.editobj);

			// un-hide the text
			this.linkobj.style.display = '';

			// declare that we are in a normal state
			this.editstate = false;
			this.obj = null;
		}
	};
	
Weblog_AJAX_TitleEdit.prototype.save = function (txt) {
		rnd = Math.round(Math.random()*100000);
		if ( this.idarr[1] == 'post' ) { // page title
			AJAX.action = AJAX.pageTitle;
			data = '&title='+txt;
			AJAX.sendData (siteurl+'admin.php?m=ajax&o=pagetitle&id='+this.idarr[2]+'&rnd='+rnd, 'POST', data);
		} else if ( this.idarr[1] == 'postcontent' ) { // page simple content
			AJAX.action = AJAX.pageTitle;
			data = '&pagecontent='+txt;
			AJAX.sendData (siteurl+'admin.php?m=ajax&o=pagesimplecontent&id='+this.idarr[2]+'&rnd='+rnd, 'POST', data);
		} else if ( this.idarr[1] == 'content' ) { // content
			AJAX.action = AJAX.pageContent;
			data = '&text='+txt;
			AJAX.sendData (siteurl+'admin.php?m=ajax&o=pagecontent&element='+this.idarr[3]+'&id='+this.idarr[4]+'&rnd='+rnd, 'POST', data);
		} else if ( this.idarr[1] == 'gallery' ) { // gallery category title
			AJAX.action = AJAX.gallery;
			data = '&title='+txt;
			AJAX.sendData (siteurl+'admin.php?m=ajax&o=gallery&id='+this.idarr[2]+'&rnd='+rnd, 'POST', data);
		} else if ( this.idarr[1] == 'galleryimage' ) { // gallery image title
			AJAX.action = AJAX.galleryImage;
			data = '&title='+txt;
			AJAX.sendData (siteurl+'admin.php?m=ajax&o=galleryimage&id='+this.idarr[2]+'&rnd='+rnd, 'POST', data);
		} else if ( this.idarr[1] == 'category' ) { // category title
			AJAX.action = AJAX.category;
			data = '&title='+txt;
			AJAX.sendData (siteurl+'admin.php?m=ajax&o=categorytitle&id='+this.idarr[3]+'&rnd='+rnd, 'POST', data);
		} else if ( this.idarr[1] == 'commentname' ) { // comment name
			AJAX.action = AJAX.comment;
			data = '&name='+txt;
			AJAX.sendData (siteurl+'admin.php?m=ajax&o=commentname&id='+this.idarr[2]+'&rnd='+rnd, 'POST', data);
		} else if ( this.idarr[1] == 'commenttext' ) { // comment text
			AJAX.action = AJAX.comment;
			data = '&text='+txt;
			AJAX.sendData (siteurl+'admin.php?m=ajax&o=commenttext&id='+this.idarr[2]+'&rnd='+rnd, 'POST', data);
		}

		//this.save_Check ();
		setInterval ('Weblog_Title_Editor.save_Check()', 3000);
	};
	
Weblog_AJAX_TitleEdit.prototype.save_Check = function () {
	if ( AJAX.TXT != 'yes' ) {
		this.linkobj.innerHTML = this.origtitle;
		alert ('There was a problem saving the new data. Make sure you are logged in');
	}
};


function Weblog_AJAX_Events () {
};


Weblog_AJAX_Events.prototype.title_doubleclick = function(e) {
	if (Weblog_Title_Editor && Weblog_Title_Editor.obj == this) {
		return false;
	} else {
		try {
			Weblog_Title_Editor.restore ();
		}
		catch (ex) {}

		Weblog_Title_Editor = new Weblog_AJAX_TitleEdit (this);
	}
};


Weblog_AJAX_Events.prototype.titleinput_onblur = function(e) {
	Weblog_Title_Editor.restore();
};


Weblog_AJAX_Events.prototype.titleinput_onkeypress = function (e) {
	e = e ? e : window.event;
	switch (e.keyCode) {
		case 13: { // enter
			Weblog_Title_Editor.inputobj.blur ();
			return false;
		}
		case 27: { // escape
			Weblog_Title_Editor.inputobj.value = Weblog_Title_Editor.origtitle;
			Weblog_Title_Editor.inputobj.blur ();
			return true;
		}
	}
};


start_AJAX = function (tag) {
	startCheck = getCookie ('user');
	if ( startCheck == "" ) {
	    return false;
	}
	try {
		divs = fetchTags (document, tag);
		for ( i=0;i<divs.length;i++ ) {
			div = divs[i].id.split ('-');
			if ( div[0] != 'ajax' ) {
				continue;
			}
			if ( div[1] == 'post' ) {
				divs[i].ondblclick = Weblog_AJAX_Events.prototype.title_doubleclick;
			} else if ( div[1] == 'postcontent' ) {
				divs[i].ondblclick = Weblog_AJAX_Events.prototype.title_doubleclick;
			} else if ( div[1] == 'content' && (div[2] == 'text' || div[2] == 'textarea') ) {
				divs[i].ondblclick = Weblog_AJAX_Events.prototype.title_doubleclick;
			} else if ( div[1] == 'gallery') {
				divs[i].ondblclick = Weblog_AJAX_Events.prototype.title_doubleclick;
			} else if ( div[1] == 'galleryimage') {
				divs[i].ondblclick = Weblog_AJAX_Events.prototype.title_doubleclick;
			} else if ( div[1] == 'category') {
				divs[i].ondblclick = Weblog_AJAX_Events.prototype.title_doubleclick;
			} else if ( div[1] == 'commentname') {
				divs[i].ondblclick = Weblog_AJAX_Events.prototype.title_doubleclick;
			} else if ( div[1] == 'commenttext') {
				divs[i].ondblclick = Weblog_AJAX_Events.prototype.title_doubleclick;
			}
		}
	}
	catch (ex) {}
};

