/*
 * Codestars reply handler 1.0
 * By Floris Weijenburg (http://www.florisweijenburg.nl)
 * Copyright (c) 2008 Floris Weijenburg
 * All programming rights reserved.
*/

CS.kernel.replies = function(targetField, type) 
{
	this.init(targetField, type);
}

CS.kernel.replies.prototype.init = function(targetField, type) 
{
	this.type = type;
	this.DOM = new CS.DOM.utils();
	this.itemId = this.GetUrlVars()[(this.GetUrlVars()).length-2];
	this.targetField = document.getElementById(targetField);
	this.sLoc = config.rootFolder + 'ajax/ajaxDataHandler.php';
	this.refreshTime = 60 * 3; // Auto-refresh after 3 minutes 
	
	setTimeout(function(){ this.autoRefreshReplies(); }.bind(this), this.refreshTime * 1000);
} 

CS.kernel.replies.prototype.InitReplyMethod = function(container) 
{
	if(config.userSessId != null && config.userSessId != "")
	{
		var submitContainer = document.getElementById(container);
		var btn = document.createElement('input');
		
		btn.type = 'button'; btn.className = 'submitButton'; 
		btn.value = 'Opslaan'; btn.id = 'replyBtn';
		
		YAHOO.util.Event.addListener(btn, "click", this.InsertReply, this, true); 
		submitContainer.appendChild(btn); 
	}
}

CS.kernel.replies.prototype.GetUrlVars = function() 
{
    var url=window.location.href; 
    var startLocation=url.indexOf('/');
    var hashes;

    if (startLocation > 0) {
        startLocation += 4;
        hashes = window.location.href.slice(startLocation + 1).split('/'); 
    } 
    
    return hashes;
}

CS.kernel.replies.prototype.InsertReply = function(e, obj) 
{
	/* Profile message quickfix */
	var postType = this.GetUrlVars()[1];
	
	if(this.itemId == 'profiles') 
	{ this.itemId = this.GetUrlVars()[(this.GetUrlVars()).length-1]; }
	
	var message = (this.targetField).value;
	var form = document.getElementById('replyForm');
	var replyData = { itemId: this.itemId, content: message, userSessId: config.userSessId, itemType: postType };
	var jsonString = Object.toJSON(replyData);
	 
	if(message != "") 
	{
		new Ajax.Request( 
		this.sLoc, 
		{
		  	method: 'get',
		  	parameters: 
		  	{
			  	cmd: 'InsertReply', 
			  	value: jsonString
			}
		});
		
		this.FetchReplies('replyForm', 'repliesContainer');
		this.targetField.value = '';
	}
}

CS.kernel.replies.prototype.autoRefreshReplies = function()
{
	this.FetchReplies('replyForm', 'repliesContainer'),
	setTimeout(function(){ this.autoRefreshReplies(); }.bind(this), this.refreshTime * 1000);	
}

CS.kernel.replies.prototype.FetchReplies = function(form, container, navPage) 
{
	var container = document.getElementById(container);
	var ajaxContainer = document.getElementById('ajaxMessageContainer');
	var postType = this.GetUrlVars()[1];
	
	if(this.itemId == 'profiles') 
	{ this.itemId = this.GetUrlVars()[(this.GetUrlVars()).length-1]; }
	
	var fetchData = { itemId: this.itemId, itemType: postType , navPage : navPage };
	var jsonString = Object.toJSON(fetchData);
	
	if(container != null) 
	{
		ajaxContainer.style.display = 'block';
		
		setTimeout( function()
		{
			new Ajax.Updater('repliesContainer', this.sLoc + '?cmd=FetchReplies&value=' + jsonString, 
			{
				method: 'get',
				onSuccess: function(transport)
				{
					/* Let's hide the ajax notice again */
					ajaxContainer.style.display = 'none';	
				}
			});

		}.bind(this), 0);
	}
}