
var WebtaObject = new Class.create();

WebtaObject.prototype = {
	options:		{},
	timoutid:		0,
	stopshowing:	false,
	
	initialize: function() {
		var options = Object.extend({
			fullsearch: null
		}, arguments[0] || {});
		
		this.options = options;
		
		// setup fullsearch input
		this.setupSearch();
	},
	
	setupSearch: function() {
		var element = $(this.options.fullsearch);
		if (!element) return false;
		
		var callerObj = this;
		
		element.onblur = function() {
			callerObj.stopshowing = false;
			callerObj.showValue(this, 'search', 0.5);
		};
		
		element.onclick = element.onfocus = function() {
			callerObj.stopshowing = true;
			callerObj.hideValue(this, 'search', 0.1);
		};
		
		element.parentNode.onclick = function(event) {
			var event = event || window.event;
			var element = event.target || event.srcElement;
			if (element.tagName != "DIV") return false;

			var frm = Event.findElement(event, "form");
			if (frm && typeof(frm) == 'object')
			{
				if (typeof ($('searcharea')) == 'object')
					WO.ajaxSearch($('fullsearch').value)
				else
					frm.submit();
			}
		};
	},
	
	hideValue: function (element, text, period) {
		this.timoutid = this.setTimeout(
			function () {
				try {
					if (element.value == text) element.value = '';
				} catch (err) {}
			}, period);
	},
	
	showValue: function (element, text, period) {
		if (this.stopshowing) return false;
		
		this.timoutid = this.setTimeout(
			function () {
				try {
					if (element.value == '') element.value = text;
				} catch (err) {}
			}, period);
	},
	
	setTimeout: function(callback, period) {
		if (typeof(callback) != 'function') callback = function() {};
		if (!period) period = 0;

		return window.setTimeout(callback, period * 1000);
	},
	
	clearTimeout: function(id) {
		var timoutid = id || this.timoutid;
		
		if (timoutid)
			clearTimeout(timoutid);
	},
	
	showForumLoading: function() {
		$('forumLoading').style.visibility = 'visible';
	},
	
	hideForumLoading: function() {
		$('forumLoading').style.visibility = 'hidden';
	},
	
	forumReply: function(post, isauthor) {
		var element = $('forumreply');
		if (!element) {
			 /* alert('Please log in before posting!'); */
			 var Href = location.href;
			location.href += (Href.charAt(Href.length-1) == '/') ? 'login.html' : '/login.html';
			return false;
		}
		
		if (typeof (post) != 'object')
			return false;
		
		this.hideError();
		
		element.style.display = 'block';
		post.parentNode.appendChild(element);
		var postidObj = $('postid');
		
		postidObj.value = post.parentNode.id;
		
		postidObj.setAttribute("isauthor", (isauthor) ? "1" : "0");
		
		try {
			$('yourPost').focus();
		} catch (err) {}
		return false;
	},
	
	forumCancel: function() {
		var element = $('forumreply');
		if (element) {
			element.style.display = 'none';
			this.hideError();
			
			try {
				var preview = $('postpreviewwrap');
					preview.style.display = 'none';
					$('yourPost').value = '';
			} catch (err) {}			
		}
	},
	
	forumAjaxPreview: function() {
		var post = $('yourPost').value;
		this.showForumLoading();
		
		var obj = {
			method: 	'post',
			onSuccess: 	this.onPreviewSuccess,
			asynchronous: true, 
			postBody: "action=preview&post=" + post,
			onFailure: 	this.onPreviewFailure,
			callerObj: 	this
		};
		
		var req = new Ajax.Request("/server/forum.php", obj);
	},
	
	onPreviewSuccess: function(response, json, callerObj, params) {
		callerObj.hideForumLoading();
		var ajaxres = AJAXResponse.unserialize(response);
		callerObj.hideError();

		if (!ajaxres.data.error)
		{
			var preview = $('postpreviewwrap');
			var username = $('replyusername');
			var reply 	= $('replypreview');
			
			try {
				preview.style.display = 'block';
				username.innerHTML = ajaxres.data.result.username + ' ';
				reply.innerHTML = ajaxres.data.result.preview;
			} catch (err) {}
		}
		else
		{
			callerObj.insertError(ajaxres.data.error);
		}
	},
	
	onPreviewFailure: function(response, json, callerObj, params) {
		callerObj.hideForumLoading();
		// alert(response.responseText);
	},
	
	forumAjaxReply: function() {
		this.showForumLoading();
		var post = $('yourPost').value;
		var postid = $('postid').value;
		var isauthor = $('postid').getAttribute("isauthor");
		
		var obj = {
			method: 	'post',
			onSuccess: 	this.onReplySuccess,
			asynchronous: true, 
			postBody: "action=reply&post=" + escape(post) + "&postid=" + postid + "&isauthor=" + isauthor,
			onFailure: 	this.onPreviewFailure,
			callerObj: 	this
		};
		
		var req = new Ajax.Request("/server/forum.php", obj);
	},
	
	onReplySuccess: function (response, json, callerObj, params) {
		callerObj.hideForumLoading();
		var ajaxres = AJAXResponse.unserialize(response);
		callerObj.hideError();
		
		try {
			var preview = $('postpreviewwrap');
				preview.style.display = 'none';
				$('yourPost').value = '';
		} catch (err) {}
		
		if (!ajaxres.data.error)
		{
			var element = document.createElement("DIV");
			var post = $(ajaxres.data.result.postid);
			try {
				post.appendChild(element);
				element.innerHTML = ajaxres.data.result.post;
				$('forumreply').style.display = 'none';
			} catch (err) {}
		}
		else
		{
			callerObj.insertError(ajaxres.data.error);
		}
	},
	
	insertError: function(errorText) {
		var errorObj = $('errors');
		
		try {
			errorObj.style.display = 'block';
			$('errorText').innerHTML = '&bull; ' + errorText;
		} catch (err) {}
	},
	
	hideError: function() {
		var errorObj = $('errors');
		
		try {
			errorObj.style.display = 'none';
		} catch (err) {}
	},
	
	ajaxSearch: function(query, page) {
		var page = (page) ? page : 1;
		var query = (query) ? query : "";
		this.showLoader();
		var searchContentDiv = $('searcharea');
		
		try {
			searchContentDiv.style.display = 'none';
		} catch (err) {}
		
		var obj = {
			method: 	'get',
			onSuccess: 	this.onSearchSuccess,
			asynchronous: true, 
			parameters: "q="+ escape(query) +"&pn=" + page,
			onFailure: 	this.onSearchFailure,
			callerObj: 	this
		};
		var req = new Ajax.Request("/server/search.php", obj);
		return false;
	},
	
	onSearchSuccess: function(response, json, callerObj, params) {
		var ajaxres = AJAXResponse.unserialize(response);
		callerObj.hideLoader();
		var searchContentDiv = $('searcharea');

		try {
			searchContentDiv.style.display = 'block';				
		} catch (err) {}				

		if (!ajaxres.data.error)
		{
			if (ajaxres.data.count)
				searchContentDiv.innerHTML = ajaxres.data.content;
			else
				searchContentDiv.innerHTML = "<center><div class='title'>No matches found for '" + ajaxres.data.query + "'</div></center>";
		}
		else
		{
			searchContentDiv.innerHTML = "<center><div class='title'>No matches found for '" + ajaxres.data.query + "'</div></center>";
		}
	},
	
	onSearchFailure: function(response, json, callerObj, params) {
		// nothing to do
		callerObj.hideLoader();
	},
	
	showLoader: function()
	{
		var loader = $('loader');
		var loader2 = $('toploader');
		try {
			loader.style.display = 'block';
			loader2.style.visibility = 'visible';
		} catch (err) {}
	},
	
	hideLoader: function()
	{
		var loader = $('loader');
		var loader2 = $('toploader');
		try {
			loader.style.display = 'none';
			loader2.style.visibility = 'hidden';
		} catch (err) {}
	}
	
};