/*global $, $$, Class, doNifty */
// JavaScript Document

var HelpDesk = function () {
	var self = {
		addDropDown: function (){
			for(var i = 0; i < self.faqQuestions.length; i++){
				self.addShowHide($$(self.faqQuestions[i]), 'a');
				if (self.faqQuestions[i].hasClassName('faqSection')) {
					self.showHide(self.faqQuestions[i], 'a');
				}
			}
		},
		addShowHide: function (el, whichdiv){
			el.addClick(self.showHide.bind(self, el, whichdiv));
		},
		deleteComment: function () {
			for (var i=0; i < self.deletecomments.length; i++) {
	//			if (this.deletecomments[i].className.indexOf("delComment") != -1) {
					self.addDeleteEvents($$(this.deletecomments[i]));
	//			}
			}
		},
		addDeleteEvents: function (el) {
			var me = self;
			el.addClick(me.delCom.bind(me, el.id));
		},
		showHide: function (el, whichdiv){
			var answerid = el.id.substring(1),
				answerdiv = $(whichdiv+answerid),
				parent = $$(el.parentNode),
				children = parent.getChildNodes(true),
				childanswer, j, i;

			if (answerdiv.isHidden()) {
				if (el.id.substring(0,1) != 't') {
					for (j = 0; j < children.length; ++j) {
						if (children[j].hasClassName('faqQuestion')) {
							children[j].removeClassName('orange');
							children[j].removeClassName('darkgray');
							children[j].addClassName('lightgray');
							$(whichdiv+children[j].id.substring(1)).hide();
						}
					}
					el.removeClassName('darkgray');
					el.addClassName('orange');
				}
				answerdiv.show();
				return;
			}
			if (!answerdiv.isHidden()) {
				if (el.id.substring(0,1) != 't') {
					for (j = 0; j < children.length; ++j) {
						if ($$(children[j]).hasClassName('faqQuestion')){
							children[j].removeClassName('lightgray');
							children[j].addClassName('darkgray');
						}
					}
					el.removeClassName('orange');
					el.addClassName('darkgray');
				}
				answerdiv.hide();
				return;
			}
		},
		checkUrl: function (){
			var urlArg = $_GET('q');
			if(urlArg && urlArg.intval && urlArg > 0){
				var urlEl = $('q'+urlArg);
				self.showHide(urlEl, 'a');
			}
		},
		delCom: function (commentid) {
			if (window.confirm("are you sure you want to remove this comment?")) {
				//var comObj = $(commentid);
				//var comParent = comObj.parent;
				var pars = 'cid=' + commentid,
					delAjax = new Ajax( self.delUrl,
					{	method: 'get',
						parameters: pars,
						onComplete: function (request) {
							var response = JSON.parse(request.responseText), comDiv;
							if (response.status == 1) {
								comDiv = $('c_'+response.id);
								if (comDiv) { comDiv.remove(); }
							}
							else { statusBar.error("Could not remove comment"); }
						}
					});
				delAjax.send();
			}
		}
	};
	function setup() {
		doNifty("div#activeContent,div#helpContent");
		doNifty("div#quickMenu,div#subQuickMenu", "small");
		if (!window.ie) {
			doNifty("div#tabs div", "small transparent top");
		}
		self.faqQuestions = document.getElementsByClassName('faqQuestion');
		self.addDropDown();
		self.checkUrl();
		self.delUrl = Constants.ajax() + 'delComment';
		var tabs = $('tabs');
		var tabLinks = tabs.getChildNodes(true);
		for (var i=0, tabLink; tabLink = $$(tabLinks[i]); i++) {
			(function(t) {
				tabLink.addEvent('mouseover', function () { t.addClassName('hover'); });
				tabLink.addEvent('mouseout', function () { t.removeClassName('hover'); });
			})(tabLink);
		}
		self.deletecomments = document.getElementsByClassName("delComment", "a");
		self.deleteComment();
	}
	window.addLoad(setup);
	return self;
}();

