function add_tinymce()
{			
	tinyMCE_GZ.init({
		plugins : 'style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,'+ 
	        'searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras',
		themes : 'advanced',
		languages : 'en',
		disk_cache : true,
		debug : false											
	},function()
	{
		$.getScript("/js/tiny_mce/jquery.tinymce.js", function()
		{	
			$('textarea.tinymce').tinymce(
			{
				// Location of TinyMCE script
				script_url : '/js/tiny_mce/tiny_mce_gzip.js',
				
				// General options
				theme : "advanced",
				skin : "thebigreason",
				disk_cache : true,
				debug : false,
				plugins : "safari,advimage,advlink,iespell,inlinepopups,media,contextmenu,paste,fullscreen,visualchars,nonbreaking,xhtmlxtras",
				
				// Theme options
				theme_advanced_buttons1 : "formatselect,bold,italic,underline,strikethrough,|,bullist,numlist,blockquote,|,link,media,charmap,pasteword,code,fullscreen",
				theme_advanced_buttons2 : "",
				theme_advanced_buttons3 : "",
				theme_advanced_toolbar_location : "top",
				theme_advanced_toolbar_align : "left",
				theme_advanced_statusbar_location : "bottom",
				theme_advanced_resizing : true,
								
				// Drop lists for link/image/media/template dialogs
				template_external_list_url : "lists/template_list.js",
				external_link_list_url : "lists/link_list.js",
				external_image_list_url : "lists/image_list.js",
				media_external_list_url : "lists/media_list.js"				
			});									
		});	
	});
}		

function editor_tools()
{
	$('.editor_edit').append('<img src="/images/icons/pencil.png" class="hide edit_this" alt="Edit This" title="Edit This" height="16" width="16" />');
	
	// edit post title
	$('h2.editor_edit img.edit_this').click(function()
	{
		//hide other edits in process
		$('.editor_edit:hidden').toggle();
		$('.editor_edit:visible').prev('.edit_container').remove();						
		//title edit
		var parent_id = $(this).parent().parent().attr('id');
		parent_id = parent_id.split("id_");
		var title = $(this).parent().html();
		title = title.split("<img");						
		$(this).parent().before('<div class="edit_container"><form class="form_edit_title" method="post" action="index.php?do=14"><fieldset><input type="hidden" name="parent_id" value="'+parent_id[1]+'" /><input type="text" maxlength="60" name="edit_title" value="'+title[0]+'" /><input type="image" class="icon_21px" id="edit_publish" src="/images/icons/tick.png" alt="Publish" title="Publish" height="16" width="16" /><img class="icon_21px pointer" id="edit_cancel" src="/images/icons/cross.png" alt="Cancel" title="Cancel" height="16" width="16" /></fieldset></form></div>');
		$(this).parent().prev().find('input[name=edit_title]').focus();
		$(this).parent().hide();
		bind_form_edit_title();
	});
	
	// edit post content
	$('div.editor_edit img.edit_this').click(function()
	{
		//hide other edits in process
		$('.editor_edit:hidden').toggle();
		$('.editor_edit:visible').prev('.edit_container').remove();		
		//content edit
		var parent_id = $(this).parent().parent().attr('id');
		parent_id = parent_id.split("id_");
		var content = $(this).parent().html();						
		content = content.split("<img src=\"/images/icons/pencil.png\"");
		$(this).parent().hide();
		$(this).parent().before('<div class="edit_container edit_content_container"><form class="form_edit_content" method="post" action="index.php?do=15"><fieldset><input type="hidden" name="parent_id" value="'+parent_id[1]+'" /><textarea class="hide tinymce" rows="15" cols="70" name="edit_content">'+content[0]+'</textarea><input type="image" class="icon_21px" id="edit_publish" src="/images/icons/tick.png" alt="Publish" title="Publish" height="16" width="16" /><img class="icon_21px pointer" id="edit_cancel" src="/images/icons/cross.png" alt="Cancel" title="Cancel" height="16" width="16" /></fieldset></form></div>');
		$(this).parent().prev().find('textarea').focus();
		add_tinymce();
		bind_form_edit_content();
	});
	
	// edit post date/tags
	$('span.editor_edit img.edit_this').click(function()
	{
		//hide other edits in process
		$('.editor_edit:hidden').toggle();
		$('.editor_edit:visible').prev('.edit_container').remove();		
		//author edit
		var parent_id = $(this).parent().parent().attr('id');
		parent_id = parent_id.split("id_");			
		var author = $(this).parent().html();						
		author = author.split("<img src=\"/images/icons/pencil.png\"");
		var str = author[0].match(/([a-zA-Z]{3}) (\d{1,2}), (\d{4})/);
		var date = str.slice(1);
		var tags = "";
		$(this).parent().find("a:not(:first)").each(function()
		{
			tags += $(this).text() + ", ";
		});
		
		var months = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
		for (var i=0; i < months.length;++i)
		{
			month_value = i+1;
			if(months[i] == date[0])
				months[i] = '<option value="'+month_value+'" selected>'+months[i]+'</option>';
			else
				months[i] = '<option value="'+month_value+'">'+months[i]+'</option>';
		}
		
		$(this).parent().before('<div class="edit_container edit_author_container"><form class="form_edit_author" method="post" action="index.php?do=16"><fieldset><input type="hidden" name="parent_id" value="'+parent_id[1]+'" /><label for="edit_month">date:</label><select id="edit_month" name="month">'+months+'</select><input type="text" id="edit_day" name="day" value="'+date[1]+'" maxlength="2" /><input type="text" id="edit_year" name="year" value="'+date[2]+'" maxlength="4" /><label for="edit_tags">tags:</label><input type="text" id="edit_tags" name="tags" value="'+tags+'" /><input type="image" class="icon" id="edit_publish" src="/images/icons/tick.png" alt="Publish" title="Publish" height="16" width="16" /><img class="icon pointer" id="edit_cancel" src="/images/icons/cross.png" alt="Cancel" title="Cancel" height="16" width="16" /></fieldset></form></div>');						
		$(this).parent().hide();
		$(this).parent().prev().find('select').focus();
		bind_form_edit_author();
	});					
	$('img#edit_cancel').live("click",function()
	{
		$(this).parent().parent().parent().siblings(':hidden').toggle();
		$(this).parent().parent().parent().remove();					
	});
	$('img#add_cancel').live("click",function()
	{
		$(this).parent().parent().parent().parent().remove();					
	});					
	$('#edit_publish').live("click",function()
	{
		$(this).data('prev_img',$(this).attr("src"));
		$(this).attr("src", "/images/icons/ajax_loader.gif");
	});
	$('#add_publish').live("click",function()
	{
		$(this).data('prev_img',$(this).attr("src"));
		$(this).attr("src", "/images/icons/ajax_loader.gif");
	});
	
	// delete process
	$('img.delete_this').live("click",function()
	{
		var parent_id = $(this).parent().attr('id');
		parent_id = parent_id.split("id_");
		
		$(this).parent().children(":not(img)").fadeTo('slow',0.1);						
		$(this).before('<img src="/images/icons/arrow_rotate_clockwise.png" class="hide recover_this" alt="Recover This Post" title="Recover This Post" height="16" width="16" />');						
		$(this).remove();						
		$.get("/index.php",{'do':'17','id':parent_id},function(responseText)
		{
			//return		
		});	
	});
	// recover the deleted item
	$('img.recover_this').live("click",function()
	{					
		var parent_id = $(this).parent().attr('id');
		parent_id = parent_id.split("id_");
		
		$(this).siblings().fadeTo('slow',1.0);	
		$(this).before('<img src="/images/icons/delete.png" class="hide delete_this" alt="Delete This Post" title="Delete This Post" height="16" width="16" />');					
		$(this).remove();
		$.get("/index.php",{'do':'18','id':parent_id},function(responseText)
		{
			//return		
		});	
	});
	// create new post
	$('img.add_new').live("click",function()
	{
		
		var date = new Date();
		var year = date.getYear()+1900;
		var day = date.getDate();
		var months = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
		for (var i=0; i < months.length;++i)
		{
			month_value = i+1;
			if(i == date.getMonth())
				months[i] = '<option value="'+month_value+'" selected>'+months[i]+'</option>';
			else
				months[i] = '<option value="'+month_value+'">'+months[i]+'</option>';
		}
		
		var add_form = '<div id="article_id_new" class="blog_article"><div class="add_container"><form class="form_add_post" method="post" action="index.php?do=19"><fieldset><input type="text" maxlength="60" name="add_title" class="add_title" value="Article Title" /><div id="add_author_container"><label for="add_month">date:</label><select id="add_month" name="month"><option value="1">'+months+'</option></select><input type="text" id="add_day" name="day" value="'+day+'" maxlength="2" /><input type="text" id="add_year" name="year" value="'+year+'" maxlength="4" /><label for="add_tags">tags:</label><input type="text" id="add_tags" name="tags" value="uncategorized, " /></div><textarea class="tinymce hide" rows="15" cols="70" name="add_content">Article Content</textarea><input type="image" class="icon" id="add_publish" src="/images/icons/tick.png" alt="Publish" title="Publish" height="16" width="16" /><img class="icon pointer" id="add_cancel" src="/images/icons/cross.png" alt="Cancel" title="Cancel" height="16" width="16" /></fieldset></form></div></div>';
		
		$(this).parent().before(add_form);
		$('form.form_add_post').find('input.add_title').focus();
		add_tinymce();
		bind_form_add_post();							
	});
}
editor_tools();
