/*
####################################################################################################
# COMMENTS PANEL OBJECT
# Version: 1.0.2 | Last Update: 09 June 2009 | Created On: 19 May 2009
####################################################################################################
# Copyright (C) by E-Muze (www.E-Muze.net)
# Copying the content of this file or fragments of this file's content with individual 
# functionality is strictly forbidden without the written consent of the author. Using this code
# as an example in order to understand and learn from it, with the purpose of improving your own
# work and/or writing your own code with similar functionality, is allowed and encouraged.
####################################################################################################
# functions 1.0.7+ must be included
# httpBus 1.0.6+ must be available for instancing
# navBar 1.0.2+ must be available for instancing
####################################################################################################
# DOCUMENTATION
# Handles the entire operation of the user comments panel system.
####################################################################################################
# NO KNOWN UNFIXED PROBLEMS
####################################################################################################
*/

var commPanTracker = new Array ();
function commentsPanel (panIdx, rec_total, rec_pp, comments_url_tpl, form_url_tpl)
{
	//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	// OBJECT PROPERTIES
	
	var self = this; // bind reference to self
	
	this.panIdx = (panIdx > 0) ? panIdx : 1; // index of this panel
	this.nav = null; // reference to navBar object
	this.loader = null; // reference to httpBus object
	this.rec_total = rec_total; // total number of records
	this.rec_pp = rec_pp; // requested number of records per page
	this.total_pages = 0; // total number of pages for nav bar
	this.current_page = 1; // currently displayed comments page
	this.comments_url_tpl = comments_url_tpl; // url template to the script that generates the comments list
	this.form_url_tpl = form_url_tpl; // url template to the script that generates the comments form
	this.comments_visible = false; // flag to mark if the comments list is currently visible
	this.form_visible = false; // flag to mark if the comments form is currently visible
	this.nav_visible = true; // flag to mark if the nav bar is currently visible
	
	//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	// OBJECT INIT
	
	this.init = function ()
	{
		// add object to tracker
		if (commPanTracker[self.panIdx])
		{
			alert ('Failed to init comments panel with id #' + self.panIdx + '. Object already exists for this id.');
			return false;
		}
		else commPanTracker[self.panIdx] = self;
		
		// calc total pages
		self.total_pages = (self.rec_total > 0) ? Math.ceil (self.rec_total / self.rec_pp) : 1;
		
		// init nav bar object
		self.nav = new navBar ('comment_nav_bar_' + self.panIdx, self.panIdx, {totalPages : self.total_pages, size: 3, currentPage : self.total_pages, callback : self.navCallback});
		
		// init http bus object
		self.loader = new httpBus ('commPanTracker[' + self.panIdx + '].loader', {callback : self.loaderCallback});
	};
	
	//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	// NAV CALLBACK METHOD
	
	this.navCallback = function (pagIdx)
	{
		if (pagIdx > 0 && pagIdx <= self.total_pages)
		{
			self.load_page (pagIdx);
			return true;
		}
		
		return false;
	};
	
	//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	// LOADER CALLBACK METHOD
	
	this.loaderCallback = function (myBus)
	{
		// if we have data
		if (myBus.returnData.text)
		{
			// handle page load operation
			if (myBus.auxData.op == 'load_page')
			{
				// write comments list within list container
				document.getElementById ('comment_list_' + self.panIdx).innerHTML = myBus.returnData.text;
				
				// if set to show all
				if (myBus.auxData.showAll)
				{
					// hide nav bar and set flag
					document.getElementById ('comment_paging_bottom_' + self.panIdx).style.display = 'none';
					self.nav_visible = false;
					
					// switch to last page (most recent)
					self.current_page = self.total_pages;
				}
				else
				{
					if (!self.nav_visible)
					{
						// show nav bar and set flag
						document.getElementById ('comment_paging_bottom_' + self.panIdx).style.display = '';
						self.nav_visible = true;
					}
					
					// switch page
					self.current_page = myBus.auxData.pagIdx;
				}
				
				// set flag
				self.comments_visible = true;
			}
			// handle form load operation
			else if (myBus.auxData.op == 'load_form')
			{
				// write comments form within container
				document.getElementById ('comment_form_' + self.panIdx).innerHTML = myBus.returnData.text;
				
				// set flag
				self.form_visible = true;
			}
			// handle form submit operation
			else if (myBus.auxData.op == 'submit_form')
			{
				// write submit operation result within form container
				document.getElementById ('comment_form_' + self.panIdx).innerHTML = myBus.returnData.text;
				
				// set flag
				self.form_visible = true;
			}
		}
	};
	
	//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	// LOAD FORM METHOD
	
	this.load_form = function (cmtId, mode)
	{
		// validate parent id
		cmtId = Math.round (parseInt (cmtId));
		cmtId = (cmtId > 0) ? cmtId : 0;
		
		// if not under edit or reply modes
		if (!(mode == 'edit' || mode == 'reply'))
		{
			// use defaults
			cmtId = 0; mode = 'new';
		}
		
		// set form URL
		var formURL = self.form_url_tpl.replace ('{cm_fk_comment}', cmtId);
		formURL = formURL.replace ('{mode}', mode);
		
		// if loader is not ready, fail
		if (!self.loader.usable)
		{
			alert ('ERROR: commentsPanel cannot initialize form load operation, loader is not ready.');
			return false;
		}
		
		// if loader is busy, fail
		if (self.loader.busy)
		{
			alert ('NOTICE: Cannot initialize form load operation, another job is currently underway, please try again later.');
			return false;
		}
		
		// set loading status within comment form container
		document.getElementById ('comment_form_' + self.panIdx).innerHTML = document.getElementById ('comment_loader_' + self.panIdx).innerHTML;
		
		// init load operation, pass aux params for operation
		self.loader.getURL (formURL, null, {op: 'load_form', mode: mode, cmtId: cmtId});
		
		// return success
		return true;
	}
	
	//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	// LOAD PAGE METHOD
	
	this.load_page = function (pagIdx, showAll)
	{
		// validate page index
		pagIdx = Math.round (parseInt (pagIdx));
		pagIdx = (pagIdx > 0) ? pagIdx : 1;
		
		// set comment listing range
		var lim = (pagIdx - 1) * self.rec_pp + '-' + self.rec_pp;
		
		// overwrite range to show all comments, if set to
		if (showAll) lim = '0-' + self.rec_total;
		
		// set list URL
		var pageURL = self.comments_url_tpl.replace ('{lim}', lim);
		pageURL = pageURL.replace ('{all}', (showAll ? 'yes' : 'no'));
		
		// if loader is not ready, fail
		if (!self.loader.usable)
		{
			alert ('ERROR: commentsPanel cannot initialize comments load operation, loader is not ready.');
			return false;
		}
		
		// if loader is busy, fail
		if (self.loader.busy)
		{
			alert ('NOTICE: Cannot initialize load operation, another job is currently underway, please try again later.');
			return false;
		}
		
		// set loading status
		var cont = document.getElementById ('comment_list_' + self.panIdx);
		if (!self.comments_visible) cont.innerHTML = document.getElementById ('comment_loader_' + self.panIdx).innerHTML;
		else cont.innerHTML += '<br>' + document.getElementById ('comment_loader_' + self.panIdx).innerHTML;
		
		// init load operation, pass aux params for operation
		self.loader.getURL (pageURL, null, {op: 'load_page', pagIdx: pagIdx, showAll: showAll});
		
		// return success
		return true;
	}
	
	//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	// SHOW PANEL METHOD
	
	this.show_panel = function ()
	{
		// set loading status within comment list container
		document.getElementById ('comment_list_' + self.panIdx).innerHTML = document.getElementById ('comment_loader_' + self.panIdx).innerHTML;
		
		// hide all besides the comment list and the toggle form bar
		document.getElementById ('comment_toggle_panel_' + self.panIdx).style.display = 'none';
		document.getElementById ('comment_toggle_form_' + self.panIdx).style.display = '';
		document.getElementById ('comment_form_' + self.panIdx).style.display = 'none';
		document.getElementById ('comment_list_' + self.panIdx).style.display = '';
		document.getElementById ('comment_paging_bottom_' + self.panIdx).style.display = '';
		
		// load last comments list page (most recent)
		self.load_page (self.total_pages);
	}
	
	//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	// HIDE PANEL METHOD
	
	this.hide_panel = function ()
	{
		// hide all besides the comment list and the toggle form bar
		document.getElementById ('comment_toggle_panel_' + self.panIdx).style.display = '';
		document.getElementById ('comment_toggle_form_' + self.panIdx).style.display = 'none';
		document.getElementById ('comment_form_' + self.panIdx).style.display = 'none';
		document.getElementById ('comment_list_' + self.panIdx).style.display = 'none';
		document.getElementById ('comment_paging_bottom_' + self.panIdx).style.display = 'none';
		
		// set form and comments display flags
		self.comments_visible = false;
		self.form_visible = false;
		self.nav_visible = true;
	}
	
	//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	// TOGGLE FORM METHOD
	
	this.show_form = function (cmtId, mode)
	{
		// set loading status within comment form container
		document.getElementById ('comment_form_' + self.panIdx).innerHTML = document.getElementById ('comment_loader_' + self.panIdx).innerHTML;
		
		// hide all besides the comment list and the comment form
		document.getElementById ('comment_toggle_panel_' + self.panIdx).style.display = 'none';
		document.getElementById ('comment_toggle_form_' + self.panIdx).style.display = 'none';
		document.getElementById ('comment_form_' + self.panIdx).style.display = '';
		document.getElementById ('comment_list_' + self.panIdx).style.display = '';
		if (self.nav_visible) document.getElementById ('comment_paging_bottom_' + self.panIdx).style.display = '';
		
		// load comment form
		self.load_form (cmtId, mode);
	}
	
	//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	// FORM CANCEL METHOD
	
	this.form_cancel = function (silent)
	{
		if (silent || confirm ('This will close and reset the comment form. Please confirm operation.'))
		{
			// hide all besides the comment list and the toggle form bar
			document.getElementById ('comment_toggle_panel_' + self.panIdx).style.display = 'none';
			document.getElementById ('comment_toggle_form_' + self.panIdx).style.display = '';
			document.getElementById ('comment_form_' + self.panIdx).style.display = 'none';
			document.getElementById ('comment_list_' + self.panIdx).style.display = '';
			document.getElementById ('comment_paging_bottom_' + self.panIdx).style.display = '';
			
			// reset the form
			var frm = document.getElementById ('cmt_frm_' + self.panIdx);
			if (frm) frm.reset ();
			
			// set flag
			self.form_visible = false;
		}
	}
	
	//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	// FORM POST METHOD
	
	this.form_post = function ()
	{
		// get form reference
		var frm = document.getElementById ('cmt_frm_' + self.panIdx);
		if (!frm) return false;
		
		// get form information
		var FRMDATA = get_form_information (frm);
		
		// if loader is not ready, fail
		if (!self.loader.usable)
		{
			alert ('ERROR: commentsPanel cannot submit comment, loader is not ready.');
			return false;
		}
		
		// if loader is busy, fail
		if (self.loader.busy)
		{
			alert ('NOTICE: Cannot submit comment, another job is currently underway, please try again later.');
			return false;
		}
		
		// set loading status within comment form container
		var cont = document.getElementById ('comment_form_' + self.panIdx);
		if (!self.form_visible) cont.innerHTML = document.getElementById ('comment_loader_' + self.panIdx).innerHTML;
		else cont.innerHTML += '<br>' + document.getElementById ('comment_loader_' + self.panIdx).innerHTML;
		
		// init post operation, pass aux params for operation
		self.loader.postURL (FRMDATA['action'], FRMDATA['FIELDS'], {op: 'submit_form'});
		
		// return success
		return true;
	}
	
	//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	// INIT THE OBJECT AUTOMATICALLY
	
	this.init ();
}
	
/*
####################################################################################################
*/