function addComment(){
	var c = $F('comment');
	if(!c){
		alert('You need to fill in a comment');
		return;
	}
	else{
		new Ajax.Updater('comments', 'functions.php', {parameters:$('formComment').serialize(true),
			onComplete:function(){$('comment').value = '';initDeleteComments()}});
	}
}

function approveEntry(id){
	new Ajax.Updater('approve_entries', 'functions.php', {postBody:'action=approveBlog&id='+id});
}

function blog_init(){
	var loader = new YAHOO.util.YUILoader({
		require: ['editor', 'container'],
		onSuccess:editor_init
	});
	loader.insert();
	YAHOO.util.Get.css('../css/blog.css');
	// argument formId can be the id or name attribute value of the
	// HTML form, or an HTML form object.
	var formObject = document.getElementById('aForm');
	// This example facilitates a POST transaction.
	// An HTTP GET can be used as well.
	YAHOO.util.Connect.setForm(formObject, true, true);
	
	if(uploadComplete){
		var callback =
		{
			upload: uploadComplete,
			failure: function(o) {alert('failed')},
			timeout:0
		}
	}
}

function changePage(page, approved){
	new Ajax.Updater('approve_entries', '/network/blog/functions.php', { postBody:'action=changePage&page='+page+'&approved='+approved });
}

function deleteComment(){
	var id = this.id.split('_');
	if(confirm('Are you sure you want to delte this comment?')){
		new Ajax.Updater('comments', 'functions.php', {postBody:'action=deleteComment&comment_id='+id[1]+
			'&id='+$F('id'), onComplete:initDeleteComments});
	}
}
function deleteEntry(id){
	if(confirm('Are you sure you want to delte this post?')){
		new Ajax.Updater('approve_entries', 'functions.php', {postBody:'action=deleteEntry&approved=0&id='+id});
	}
}

function editComment(){
	var id = this.id.split('_');
	new Ajax.Updater('comments', 'functions.php', {postBody:'action=editComment&comment_id='+id[1]+
		'&comment='+$F('comment_'+id[1])+'&id='+$F('id'), onComplete:initDeleteComments});
}
function editCommentCreate(){
	// Creates the form
	var id = this.id.split('_');
	var frm = document.createElement('form');
	frm.id = 'form_'+id[1];
	// Creates the textarea
	var t = document.createElement('textarea');
	t.className ='fullText';
	t.name = 'comment_'+id[1];
	t.id = 'comment_'+id[1];
	t.rows = '10';
	t.cols = '10';
	// Creates the div
	var d = document.createElement('div');
	// Creates Edit Button
	var bs = document.createElement('input');
	bs.type = 'button';
	bs.value = 'Edit';
	bs.id = 'btn_'+id[1];
	bs.className = 'submit_wall';
	// Creates Cancel Button
	var bc = document.createElement('input');
	bc.type = 'button';
	bc.value = 'Cancel';
	bc.className = 'submit_wall';
	// Appends documents
	frm.appendChild(t);
	d.appendChild(bs);
	d.appendChild(bc);
	frm.appendChild(d);
	var text = $('edit_'+id[1]).innerHTML.stripTags();
	t.value = text;
	var a = $('edit_'+id[1]);
	$('edit_'+id[1]).update(frm);
	bs.observe('click', editComment);
	bc.observe('click', function(){$(a).update(text)});
}
function editor_init(){	
	myEditor = new YAHOO.widget.Editor('msgpost', {
    height: '300px',
    width: '600px',
    markup:'xhtml',
    dompath: false, //Turns on the bar at the bottom
    animate: true //Animates the opening, closing and moving of Editor windows
});
	myEditor._defaultToolbar.buttons[10].buttons.pop();
	myEditor.on('toolbarLoaded', function(){
		//The Toolbar buttons config
   		 var insertDocConfig = {
            type: 'push',
            label: 'Insert Document from "My Files"',
            value: 'insertdoc'
    	}
    	//Add the button to the "insertitem" group
   		 myEditor.toolbar.addButtonToGroup(insertDocConfig, 'insertitem');
   		 this.toolbar.on('insertdocClick', function(ev){
   		 	files.show();
   		 });
	});
	myEditor.render();
	
	files = new YAHOO.widget.Panel("docs",{
		width:"500px",
		visible:false,
		context:['msgpost', 'tl', 'tr'],
		constraintoviewport:true
		});
	files.render();
	
	var divs = $('docs_bd').childElements();
	var l = divs.length;
	for(var i=0; i < l; i++){
		$(divs[i]).observe('click', putdocinblog);
	}
}

function getMod(){
	if($F('uploadedfile') == '')
		return 0;
	$('anim').style.display = 'inline';
	var cObj = YAHOO.util.Connect.asyncRequest('POST', '../includes/upload_blog.php', callback);
	var formObject = document.getElementById('aForm');
	YAHOO.util.Connect.setForm(formObject, true, true); 
}

function initBlogView(){
	$('addComment').observe('click', addComment);
	initDeleteComments();
}

function initDeleteComments(){
	var dc = $$('img.deleteComment');
	var l = dc.length;
	for(var i=0; i<l; i++){
		dc[i].observe('click', deleteComment);
	}
	dc = $$('img.editComment');
	l = dc.length;
	for(var i=0; i<l; i++){
		dc[i].observe('click', editCommentCreate);
	}
}
function putdocinblog(){
	files.hide();
	var html = '<a id="links" class="doc" href="'+this.title+'">'+this.innerHTML+'</a>';
	myEditor.execCommand('insertHTML', html);
}

function removeImage(){
	new Ajax.Updater('test', 'functions.php', {postBody:'action=removeImage&image='+$F('pic_id')});
	$('pic_id').value = '';
	$('profile_image').src = '';
	$('pic_status').innerHTML = 'Image Removed';
	
}

function tagToolTip(){
	if($('help_tags')){
		new Tip('help_tags', 'Tags are keywords that help you find people or groups with similar interests.  These tags also help people find you and the files or resources you want to share.  ',{title:'Tags Help'});
	}
}

function toggle_div(what){
	if($(what).visible())
		$(what+'_image').src = 'css/icons/bullet_arrow_right.png';
	else
		$(what+'_image').src = 'css/icons/bullet_arrow_down.png';
}

function validate(){
	error = '';
	myEditor.saveHTML();
	if($F('title') == ''){
		error += '* - Please add a title for the entry.' + "\n";
	}
	if($F('msgpost') == ''){
		error += '* - Please add text for the entry.' + "\n";
	}
	if($('profile_image').src == '' || $('pic_status').innerHTML == 'Image Removed'){
		error += '* - Please add an image for the entry.' + "\n";
	}
	if($F('tags') == ''){
		error += '* - Please enter at least one tag.';
	}
	if(error){
		alert(error);
		return false;
	}
	return true;
}
