/*

VoxPopuli

Copyright (c) 2009-2011, EldoS Corporation

For licensing and distribution terms visit http://www.eldos.com/

*/

var vox_script = "vox.php";
var vox_location;

// constants
var VOX_STATUS_PENDING = 1;
var VOX_STATUS_COMPLETE = 3;

var VoxUI = Class.create( {
	
	initialize:
		function(tabContainer, tabClass, pageClass)
		{
			this.activeTabName = "";
			this.activeTab = null;			
			this.contentAjax = null;
			
			// set default values for request variables
			this.numIdeas = 0;
			this.startPage = 0;
			this.searchTerm = "";
			this.onlyUser = 0;
			this.onlyOpen = true;
			this.specificStatus = 0;
			this.category = 1;
			this.langParam = "";
			this.sortOrder = "dd";
			this.fetchDesc = false;
			this.catEditId = 0;
			this.ideaEditId  = 0;
			this.widgetloc = "widgetloc=" + encodeURIComponent(document.location.href);

		},

	/*
	Miscellaneous functions
	*/
	
	updateContent:
		function(value, isHTML)
		{
			var lcontent;
			lcontent = $("vox_tab_content");
			if (isHTML == true)
				lcontent.innerHTML = value;
			else
				lcontent.innerText = value;								
		},
		
	/*
	Progress indicator functions
	*/
	
	hideProgress:
		function ()
		{
			if (voxUI.progressShown == true)
			{
				progress_holder = $("vox_progress_holder");
				if (progress_holder != null)
					progress_holder.setStyle({display:'none'});
				this.progressShown = false;
			}
		},

	showProgress:
		function()
		{	
			tab_contents = $("vox_tab_content");
			progress_holder = $("vox_progress_holder");
			if (progress_holder != null && tab_contents != null)
			{				
				try {
					progress_holder.clonePosition(tab_contents, {setLeft: true, setTop: true, setWidth : false, setHeight : false, offsetLeft : 1, offsetTop : 1});				
				}
				catch (e) {}
				progress_holder.setStyle({display:'block'});
				
				this.progressShown = true;				
			}			
		},

	/*
	Idea description manipulation functions
	*/
		
	hideIdeaDescription: // this function hides the description block of the idea with given ID
		function(id)
		{
			morediv = $("vox_idea_more_" + id);
			if (morediv != null)
			{
				morediv.setStyle({display:'none'});
			}
			morelink = $("vox_idea_more_" + id + "_morelink");
			if (morelink != null)
			{
				morelink.setStyle({display:'inline'});
			}
			lesslink = $("vox_idea_more_" + id + "_lesslink");
			if (lesslink != null)
			{
				lesslink.setStyle({display:'none'});
			}
		},
	
	doShowIdeaDescription: // this function shows the description block of the idea with given ID
		function(id)
		{	
			morediv = $("vox_idea_more_" + id);
			if (morediv != null)
			{
				morediv.setStyle({display:'block'});				
			}
			
			morelink = $("vox_idea_more_" + id + "_morelink");			
			if (morelink != null)
			{
				morelink.setStyle({display:'none'});
			}
			
			lesslink = $("vox_idea_more_" + id + "_lesslink");
			if (lesslink != null)
			{
				lesslink.setStyle({display:'inline'});
			}
		},
	
	setIdeaDescription: // places the received data as the text description of the idea
		function(result)
		{
			id = result.request.options.userData;
			morediv = $("vox_idea_more_" + id);
			if (morediv.innerText != null)
				morediv.innerText = result.responseText;
			else
				morediv.textContent = result.responseText;							
		},
		
	showIdeaDescription: // this function downloads the description of the idea with given ID
		function(id)
		{
			morediv = $("vox_idea_more_" + id);
			if (morediv != null)
			{
				if ((morediv.innerText == "" || morediv.innerText == null) && (morediv.textContent == "" || morediv.textContent == null))
				{
					this.contentAjax = new Ajax.Request(vox_script + '?' + this.widgetloc + '&loc=' + vox_location + '&action=desc&id=' + id,  {
						onSuccess: voxUI.setIdeaDescription,
						onComplete: function(result) {
							voxUI.doShowIdeaDescription(result.request.options.userData);
						},
						method: 'get',
						userData: id
					});
					
				}				
				else
				{
					this.doShowIdeaDescription(id);
				}
			}	
		},
	
	/*
	Category functions
	*/
	
	addCategory:
		function()
		{
			this.catEditId = 0;
			this.showProgress();
			this.contentAjax = new Ajax.Request(vox_script + '?' + this.widgetloc + '&loc=' + vox_location + '&action=getcatform' + this.langParam,  {
				onSuccess: function(result) {
					voxUI.updateContent(result.responseText, true);
				},
				onComplete: function(result) {			
					voxUI.hideProgress();
				}				
			});				
		},
	
	editCategory:
		function(id)
		{
			this.catEditId = id;
			this.showProgress();
			this.contentAjax = new Ajax.Request(vox_script + '?' + this.widgetloc + '&loc=' + vox_location + '&action=getcatform&id=' + id + this.langParam,  {
				onSuccess: function(result) {
					voxUI.updateContent(result.responseText, true);
				},
				onComplete: function(result) {			
					voxUI.hideProgress();
				}				
			});	
		},
	
	deleteCategory:
		function(id, $confirm)
		{
			this.showProgress();
			this.contentAjax = new Ajax.Request(vox_script + '?' + this.widgetloc + '&loc=' + vox_location + '&action=cat_delete&id=' + id + "&confirm=" + $confirm + this.langParam,  {
				onSuccess: function(result) {
					voxUI.updateContent(result.responseText, true);
				},
				onComplete: function(result) {
					voxUI.hideProgress();
				}				
			});	
		},
	
	closeCatSubmitForm:
		function(acceptInput)
		{
			if (acceptInput)
			{
				var params = new Hash();
				
				// specify if we edit or update
				if (this.catEditId != 0)
				{
					action = "cat_update";
					params.set('id', this.catEditId);					
				}
				else
				{
					action = "cat_submit";
				}
				
				params.set('title', $("vox_catsubmit_name").value);
				params.set('forumid', $("vox_catsubmit_forumid").value);

				this.showProgress();				
				this.contentAjax = new Ajax.Request(vox_script + '?' + this.widgetloc + '&loc=' + vox_location + '&action=' + action + this.langParam,  {
					onSuccess: function(result) {
						voxUI.updateContent(result.responseText, true);
					},				
					onFailure: function(result) {
						contentText = "<center><h4>Error " + result.status + "(" + result.statusText + ") occured.</h4><br><input type=\"button\" onclick=\"voxUI.refreshIdeaList();\" value=\"Continue\"></center>";
						voxUI.updateContent(contentText, true);
					},
					onComplete: function(result) {			
						voxUI.hideProgress();
					},
					parameters: params
				});	
			}
			else
			{
				$("vox_tab_content").textContents = "";
				this.refreshIdeaList();
			}
		},
	
	
	/*
	Idea operations
	*/

	addIdea: // this function downloads the idea submission form from the server
		function()
		{
			this.ideaEditId = 0;
			this.showProgress();
			this.contentAjax = new Ajax.Request(vox_script + '?' + this.widgetloc + '&loc=' + vox_location + '&action=getideaform&cat=' + this.category + this.langParam,  {
				onSuccess: function(result) {
					voxUI.updateContent(result.responseText, true);
				},
				onComplete: function(result) {			
					voxUI.hideProgress();
				}				
			});	
		},
	
	deleteIdea:
		function(id, confirm)
		{
			this.showProgress();
			this.contentAjax = new Ajax.Request(vox_script + '?' + this.widgetloc + '&loc=' + vox_location + '&action=delete&id=' + id + "&confirm=" + confirm + this.langParam,  {
				onSuccess: function(result) {
					voxUI.updateContent(result.responseText, true);
				},
				onComplete: function(result) {
					voxUI.hideProgress();
				}				
			});	
		},

		
	editIdea:
		function(id)
		{
			this.ideaEditId = id;
			this.showProgress();
			this.contentAjax = new Ajax.Request(vox_script + '?' + this.widgetloc + '&loc=' + vox_location + '&action=getideaform&id=' + id + this.langParam,  {
				onSuccess: function(result) {
					voxUI.updateContent(result.responseText, true);
				},
				onComplete: function(result) {
					voxUI.hideProgress();
				}				
			});	
		},

	moveIdea:
		function(id)
		{
			this.showProgress();
			this.contentAjax = new Ajax.Request(vox_script + '?' + this.widgetloc + '&loc=' + vox_location + '&action=getmoveform&id=' + id + this.langParam,  {
				onSuccess: function(result) {
					voxUI.updateContent(result.responseText, true);
				},
				onComplete: function(result) {
					voxUI.hideProgress();
				}				
			});	
		},

		
	setIdeaStatus:
		function(id, status)
		{
			this.showProgress();
			this.contentAjax = new Ajax.Request(vox_script + '?' + this.widgetloc + '&loc=' + vox_location + '&action=getstatusform&id=' + id + "&status=" + status + this.langParam,  {
				onSuccess: function(result) {
					voxUI.updateContent(result.responseText, true);
				},
				onComplete: function(result) {
					voxUI.hideProgress();
				}
			});	
		},
	
	closeStatusChangeForm:
		function(acceptInput)
		{
			if (acceptInput)
			{
				var params = new Hash();
				
				params.set('id', $("vox_ideastatus_id").value);
				params.set('status', $("vox_ideastatus_status").value);
				params.set('comment', $("vox_ideastatus_comment").value);
				if ($("vox_ideastatus_sendupdate").checked)
					params.set('sendupdate', 1);
				else
					params.set('sendupdate', 0);
				
				this.showProgress();
				
				this.contentAjax = new Ajax.Request(vox_script + '?' + this.widgetloc + '&loc=' + vox_location + '&action=setstatus' + this.langParam,  {
					onSuccess: function(result) {
						voxUI.updateContent(result.responseText, true);
					},				
					onFailure: function(result) {
						contentText = "<center><h4>Error " + result.status + "(" + result.statusText + ") occured.</h4><br><input type=\"button\" onclick=\"voxUI.refreshIdeaList();\" value=\"Continue\"></center>";
						voxUI.updateContent(contentText, true);
					},
					onComplete: function(result) {			
						voxUI.hideProgress();
					},
					parameters: params
				});	
			}
			else
			{
				$("vox_tab_content").textContents = "";
				this.refreshIdeaList();
			}
		},
	
	closeIdeaSubmitForm:
		function(acceptInput)
		{
			if (acceptInput)
			{
				var params = new Hash();
				
				// specify if we edit or update
				if (this.ideaEditId != 0)
				{
					action = "update";
					params.set('id', this.ideaEditId);
					catStr = "";
				}
				else
				{
					catStr = "&cat=" + this.category;
					action = "submit";
				}
				
				params.set('title', $("vox_ideasubmit_title").value);
				params.set('desc', $("vox_ideasubmit_desc").value);
				
				nameField = $("vox_ideasubmit_name");
				if (nameField != null)
				{
					params.set('name', nameField.value);
					params.set('email', $("vox_ideasubmit_email").value);
				}
				
				this.showProgress();
				this.contentAjax = new Ajax.Request(vox_script + '?' + this.widgetloc + '&loc=' + vox_location + '&action=' + action + catStr + this.langParam,  {
					onSuccess: function(result) {
						voxUI.updateContent(result.responseText, true);
					},				
					onFailure: function(result) {
						contentText = "<center><h4>Error " + result.status + "(" + result.statusText + ") occured.</h4><br><input type=\"button\" onclick=\"voxUI.refreshIdeaList();\" value=\"Continue\"></center>";
						voxUI.updateContent(contentText, true);
					},
					onComplete: function(result) {			
						voxUI.hideProgress();
					},
					parameters: params
				});	
			}
			else
			{
				$("vox_tab_content").textContents = "";
				this.refreshIdeaList();
			}
		},
	
	closeIdeaMoveForm:
		function(acceptInput)
		{
			if (acceptInput)
			{
						
											
				this.showProgress();
				this.contentAjax = new Ajax.Request(vox_script + '?' + this.widgetloc + '&loc=' + vox_location + '&id=' + $("vox_ideamove_id").value + '&action=move' + "&cat=" + $("vox_ideamove_category_list").value + this.langParam,  {
					onSuccess: function(result) {
						voxUI.updateContent(result.responseText, true);
					},				
					onFailure: function(result) {
						contentText = "<center><h4>Error " + result.status + "(" + result.statusText + ") occured.</h4><br><input type=\"button\" onclick=\"voxUI.refreshIdeaList();\" value=\"Continue\"></center>";
						voxUI.updateContent(contentText, true);
					},
					onComplete: function(result) {			
						voxUI.hideProgress();
					}
				});	
			}
			else
			{
				$("vox_tab_content").textContents = "";
				this.refreshIdeaList();
			}
		},
		
	/*
	Search-related functions
	*/
		
	performSearch:
		function()
		{
			search_input = $("vox_search_input");
			this.searchTerm = search_input.value;
			this.refreshIdeaList();			
		},

	searchInputKeyDown:
		function(e, ca, ci)
		{
			if (e.which == 13)
			{
				this.performSearch();
			}
			else
			{
				search_input = $("vox_search_input");
				if (search_input.value=='Search')
				{
					search_input.value='';
					search_input.style.color=ca;
				}
			}
		},

	/*
	Page navigation functions
	*/
	
	setSortOrder:
		function(order)
		{
			this.sortOrder = order;
			this.refreshIdeaList();
		},
		
	setIdeasPerPage:
		function(ideanum)
		{
			this.numIdeas = ideanum;
			this.startPage = 0;			
			this.refreshIdeaList();
		},
		
	gotoPage:
		function(pagenum)
		{
			this.startPage = pagenum;
			this.refreshIdeaList();
		},

	/*
	Voting-related functions
	*/

		
	voteForIdea:
		function (id, value)
		{
			this.showProgress();
			this.contentAjax = new Ajax.Request(vox_script + '?' + this.widgetloc + '&loc=' + vox_location + '&action=vote&id=' + id + "&vote=" + value + this.langParam,  {
				onSuccess: function(result) {
					if (result.responseText != "")
						voxUI.updateContent(result.responseText, true);
					else
						voxUI.refreshIdeaList();
				},
				onComplete: function(result) {			
					voxUI.hideProgress();
				}				
			});	

		},
		
	/*
	Main UI functions
	*/
	
	showTab:
		function(tabId, param1)
		{
			// switch the tab
			if (this.activeTab != null)
				this.activeTab.removeClassName("vox_tab_active");
			this.activeTabName = tabId;
			this.activeTab = $("vox_tab_" + tabId);			
			this.activeTab.addClassName("vox_tab_active");
			
			// change data settings
			this.startPage = 0;
			this.searchTerm = "";
			this.onlyUser = 0;
			this.onlyOpen = true;
			this.specificStatus = 0;
			
			if (tabId == "all")
			{
				this.onlyOpen = false;
			}
			else
			if (tabId == "open")
			{
				this.onlyOpen = true;
			}
			else
			if (tabId == "my")
			{
				this.onlyUser = true;
				this.onlyOpen = false;
			}
			else
			if (tabId == "complete")
			{
				this.specificStatus = VOX_STATUS_COMPLETE;
			}
			
			if (tabId == "pending")
			{
				this.specificStatus = VOX_STATUS_PENDING;
			}
			
			this.refreshIdeaList();
		},
	
	refreshIdeaList: // sends request for the list of ideas to the server
		function()
		{
			this.showProgress();
			
			var req;
			
			if (this.activeTabName == "cat")
			{
				req = vox_script + '?' + this.widgetloc + '&loc=' + vox_location + '&action=cat_list&' + this.langParam;
			}
			else
			{
				req = vox_script + '?' + this.widgetloc + '&loc=' + vox_location + '&action=list&startfrompage=' + this.startPage + '&num=' + this.numIdeas;
				if (this.fetchDesc)
					req = req + '&fetchdesc=1';
				if (this.onlyOpen)
					req = req + '&onlyopen=1';
				if (this.onlyUser)
					req = req + '&onlyuser=1';
				if (this.searchTerm.length > 0)
					req = req + '&searchterm=' + this.searchTerm;
				if (this.specificStatus > 0)
					req = req + '&onlystatus=' + this.specificStatus;
				req = req + '&sortorder=' + this.sortOrder + '&cat=' + this.category + this.langParam;
			}
		
			this.contentAjax = new Ajax.Request(req,  {
				onSuccess: function(result) { voxUI.updateContent(result.responseText, true); },
				onFailure: function(result) { voxUI.updateContent("<center><h4>Error " + result.status + "(" + result.statusText + ") occured.</h4><br><input type=\"button\" onclick=\"voxUI.refreshIdeaList();\" value=\"Try again\"></center>", true); },
				onComplete: function(result) { if (voxUI.progressShown) voxUI.hideProgress(); }
			});
		},
	
	requestWidgetBlock: // writes the main DIV and requests widget HTML code from the server
		function(category, language, starttab, path_to_script)
		{
			vox_location = path_to_script;
			vox_script = path_to_script + "/vox.php";
			if (language != "")
				this.langParam = "&voxlang=" + language;
			if (starttab == "")
				starttab = 'open';
			this.category = category;

			document.write("<div id=\"vox_container\" style=\"display: none;\"></div>");

			this.contentAjax = new Ajax.Request(vox_script + '?' + this.widgetloc + '&loc=' + vox_location + '&cat=' + this.category + this.langParam,  {
				onSuccess: function(result) {
					aContainer = $("vox_container");
					if (aContainer != null)
					{
						aContainer.innerHTML = result.responseText;
						aContainer.setStyle({display:'block'});
						voxUI.showTab(result.request.options.startTab);
					}
				},
				startTab: starttab
			});
		},

	requestWidgetBlockEx: // writes the main DIV and requests widget HTML code from the server
		function(category, language, starttab, path_to_script, fetch_description)
		{
			this.fetchDesc = fetch_description;
			this.requestWidgetBlock(category, language, starttab, path_to_script);
		},
		
	requestView: // writes the list of top items 
		function(category, language, path_to_script)
		{
			vox_location = path_to_script;
			vox_script = path_to_script + "/vox.php";
			if (language != "")
				this.langParam = "&voxlang=" + language;
			this.category = category;
						
			document.write("<div id=\"vox_container\" style=\"display: none;\"></div>");

			this.contentAjax = new Ajax.Request(vox_script + '?' + this.widgetloc + '&action=view&loc=' + vox_location + '&cat=' + this.category + this.langParam,  {
				onSuccess: function(result) {
					aContainer = $("vox_container");
					if (aContainer != null)
					{
						aContainer.innerHTML = result.responseText;
						aContainer.setStyle({display:'block'});						
					}
				}
			});
		}
	
	
});

var voxUI = new VoxUI();

