/**
 *     This file is part of Ibsn.

    Ibsn is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ibsn is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */  
function toggle_sidebar(title)
{
	areaname = $(title).innerHTML + "_text";
	areaname = areaname.replace(' ','_');
	Effect.toggle(areaname, 'blind', { duration:0.5 });
	
	if(title.hasClassName('box_cap'))
		title.removeClassName('box_cap').addClassName('box_cap_hidden');
	else
		title.addClassName('box_cap').removeClassName('box_cap_hidden');
}
function onFail()
{
	alert("Ajax Request failed.");
}
function get_comments(type,id,owner)
{
	var options = {
		method		:	'post',
		parameters	:	'parent_id='+id,
		onSuccess	:	function handleSuccess(transport)
		{
			$('commentspace').innerHTML = transport.responseText;
		},
		onFailure	:	onFail
	};
	transport = new Ajax.Request('/comments/list_from_location/'+type+'/'+id+'/'+owner,options);
}
function new_comment(type,id)
{
	$('comment_input').slideup;
	var options = {
		method		:	'post',
		parameters	:	'parent_id='+id+'&body='+$('body').value+'&name='+$('name').value,
		onSuccess	:	function handleSuccess(transport)
		{
			$('alertarea').innerHTML = transport.responseText;
		},
		onFailure	:	onFail,
		onComplete	:	function handleCompletion()
		{
			$('body').value = '';
			Effect.toggle('comment_input','slide',{ duration: 0.5 });
			get_comments(type,id);
		}
	};
	transport = new Ajax.Request('/comments/create/'+type+'/'+id,options);
	return false;
}
/**ADMIN*/
function deleteTag(tag)
{
	var options = {
		method		:	'post',
		parameters	:	'id='+$('blog_id').value+'&tag='+tag,
		onSuccess	:	get_tags,
		onFailure	:	onFail,
		onComplete	:	function handleCompletion()
		{
			$('tagname').value = tag;
		}
	};
	transport = new Ajax.Request('/blog/deleteTag',options);
}
function addTag()
{
	tag = $('tagname').value;
	var options = {
		method		:	'post',
		parameters	:	'id='+$('blog_id').value+'&tag='+tag,
		onSuccess	:	get_tags,
		onFailure	:	onFail,
		onComplete	:	function handleCompletion()
		{
			$('tagname').value = '';
		}
	};
	transport = new Ajax.Request('/blog/addTag',options);
}
function get_tags()
{
	var options = {
		method		:	'post',
		parameters	:	'id='+$('blog_id').value,
		onSuccess	:	function handleSuccess(transport)
		{
			$('tagspace').innerHTML = transport.responseText;
		},
		onFailure	:	onFail
	};
	transport = new Ajax.Request('/blog/ajax/tags',options);
}
function deleteComment(id)
{
	var options = {
		method		:	'post',
		parameters	:	'comment_id='+id,
		onSuccess	:	get_comments,
		onFailure	:	onFail
	};
	transport = new Ajax.Request('/comments/ajax/delete',options);
}
function aclswitch(id,acl_id)
{
	var options = {
		method		:	'post',
		parameters	:	'change='+id+'&acl_id='+acl_id,
		asynchronous: 	false,
		onSuccess	:	function handleSuccess(transport)
		{
			if(transport.responseText == "OK"){
				if($(id).hasClassName('yes'))
					$(id).removeClassName('yes').addClassName('no');
				else
					$(id).removeClassName('no').addClassName('yes');
			}
			else
				alert("Error: "+transport.responseText);
		},
		onFailure	:	onFail
	};
	transport = new Ajax.Request('/sys/acledit',options);
}
function include_script(file, event)
{
	var script = new Element("script", { type: "text/javascript", src: file });
	$$("head")[0].insert(script);
}
var dropLocation = null;
function dropIframe(location)
{
	dropOpen();
	$('dropbox').innerHTML = '<iframe width=100% height="300px" src="'+location+'"></iframe>';
	dropLocation = location;
}
function dropAjax(location)
{
	var options = {
		method		:	'get',
		parameters	:	'',
		onSuccess	:	function handleSuccess(transport)
		{
			dropOpen();
			$('dropbox').innerHTML = transport.responseText;
		},
		onFailure	:	onFail
	};
	transport = new Ajax.Request(location+'/ajax',options);
	dropLocation = location;
}
function dropLogin()
{
	include_script('/webroot/javascript/auth.js');
	include_script('/webroot/javascript/frameworks/sha2.js');
	dropAjax('/auth/login/ajax');
	dropLocation = '/auth/login';
}
function dropOpen()
{
	Effect.ScrollTo('dropbox');
	Effect.Appear('dropbox');
	Effect.Appear('dropboxcontrol');
}
function dropClose()
{
	Effect.Fade('dropboxcontrol',{duration:.2});
	Effect.Fade('dropbox',{duration:.2});
	dropLocation = null;
}
function dropFree()
{
	window.open(dropLocation,dropLocation);
	dropClose();
}
