var loading = '<img src="/resources/images/loading.gif">';
var _comments = new Array();
var _dialog = 0;
var _hover = 0;

function hover(e, el){
	switch(e.type){
		case 'mouseover':
			if(el.firstChild.src.indexOf('images/c_b.gif') > -1)
				el.firstChild.src = '/resources/images/c_r.gif';
			else if(el.firstChild.src.indexOf('images/t_b_r.gif') > -1)
				el.firstChild.src = '/resources/images/t_r_r.gif';
			else if(el.firstChild.src.indexOf('images/t_b_d.gif') > -1)
				el.firstChild.src = '/resources/images/t_r_d.gif';
			break;
		case 'mouseout':
			if(	el.firstChild.src.indexOf('images/c_r.gif') > -1)
				el.firstChild.src = '/resources/images/c_b.gif';
			else if(el.firstChild.src.indexOf('images/t_r_r.gif') > -1)
				el.firstChild.src = '/resources/images/t_b_r.gif';
			else if(el.firstChild.src.indexOf('images/t_r_d.gif') > -1)
				el.firstChild.src = '/resources/images/t_b_d.gif';

			break;
	}
}
function show(el){
	var sub = el.parentNode.getElementsByTagName('ul')[0];
	if(sub.style.display == 'none'){
		sub.style.display = '';
		el.firstChild.src='/resources/images/t_r_d.gif';
	}
	else{
		sub.style.display = 'none';
		el.firstChild.src='/resources/images/t_r_r.gif';
	}
}

function load(){
	//table row shading
	var tables = document.getElementById('body').getElementsByTagName('table');
	for(var i=0; i<tables.length; i++)
		tools.colorRows(tables[i]);
		
	//page-specific load events
	switch(document.location.pathname){
		case '/articles/index.php': //articles arrange review cards
		case '/articles/':
			var w = tools.getSize(document.getElementById('body'));
			var width = Math.floor((w['width'] - 70) / 2);
		
			if(document.getElementById('body').firstChild.firstChild.innerHTML == 'Articles' || (document.getElementById('body').firstChild.id == 'addContent' && document.getElementById('body').childNodes[1].firstChild.innerHTML == 'Articles')){
				//main articles page
				var cards = document.getElementById('body').childNodes;
				var maxHeight = 50;
				if(width < 400) width = 400;
				if(cards.length == 3 && width == 400) width = 600;
				for(var i=0; i<cards.length; i++){
					if(cards[i].className == 'article'){
						cards[i].style.width = width + 'px';
						var w = tools.getSize(cards[i]);
						if(maxHeight < w['height'])
							maxHeight = w['height'];
					}
				}
				for(var i=0; i<cards.length; i++){
					 if(cards[i].className == 'article')
						 cards[i].firstChild.style.height = maxHeight + 'px';
				}
			}
			else{
				//displaying an article
				try{
					//rating box?
					var rows = document.getElementById('rating').childNodes[1].childNodes;
				}
				catch(e){ return; }
				for(var i=0; i<rows.length; i++){
					var m = rows[i].childNodes[1].firstChild.style.width.substring(0, rows[i].childNodes[1].firstChild.style.width.length-2);
					var t = width / 10 * m;
					//alert(m + ' ' + t);
					rows[i].childNodes[1].firstChild.style.width = t + 'px';
				}
			}
			break;
	}
}
var _callStack = new Array();
function bgItemDisplay(id){
	if(id == 'back'){
		_callStack.pop(); 		//pop off the currently displayed element
		id = _callStack.pop();	//pop off the requested element so it can be readded and called
	}
	else if(id.tagName == 'SELECT')
		id = tools.getSelectValue(id);
	_callStack[_callStack.length] = id;
	var target = document.getElementById('displayItem');
	ajax('action=getItem&itemID=' + id, window.location.href, target, 'AJAX request failed');
	target.innerHTML = loading;
}

function comments(action, el, linkid, commentid, option1, commentType){
	switch(action){
		case 'view':
			if(!el.nextSibling){
				var commentDiv = document.createElement('div');
				commentDiv.className = 'showComments';
				el.parentNode.appendChild(commentDiv);
				el.nextSibling.style.display = 'none';
				el.nextSibling.id = 'commentBlock' + linkid;
			}
			if(el.nextSibling.style.display == 'none')
				el.nextSibling.style.display = '';
			else{
				el.nextSibling.style.display = 'none';
				return;
			}
			var errorContent = 'AJAX request failed';
			//_comments = new Array();
			ajax('action=viewComment&linkid=' + linkid + '&commentid=' + commentid + '&type=' + commentType, window.location.href, el.nextSibling, errorContent)
			el.nextSibling.innerHTML = loading;
			break;
		case 'reply':
			switch(option1){
				case 'save':
					var t = el.parentNode.childNodes;
					var author;
					if(t[0].getElementsByTagName('input').length == 1)
						author = t[0].getElementsByTagName('input')[0].value;
					var comment = escape(t[1].firstChild.value);
					//_comments = new Array();
					var errorContent = el.parentNode.innerHTML.replace('<span class="error">AJAX request failed</span>', '') + '<span class="error">AJAX request failed</span>';
					ajax('action=replyComment&linkid=' + linkid + '&commentid=' + commentid + '&option=save&author=' + author + '&comment=' + comment + '&type=' + commentType, window.location.href, el.parentNode, errorContent);
					el.parentNode.innerHTML = loading;
					break;
				case 'cancel':
					var t = false;
					for(i=0; i<_comments.length; i++){
						if(_comments[i]['commentID'] == commentid){
							t=true;
							break;
						}
					}
					if(!t){
						//my comment array disappeared?
						document.getElementById('commentBlock' + linkid).style.display = 'none';
						comments('view', document.getElementById('commentBlock' + linkid).previousSibling, linkid, 0, 0, commentType);
					}
					else{
						var t = el.parentNode;
						if(_comments[i]['reply'])
							t.innerHTML = _comments[i]['reply'];
						if(_comments[i]['edit'])
							t.innerHTML += _comments[i]['edit'];
						if(_comments[i]['admin'])
							t.innerHTML += _comments[i]['admin'];
					}
					break;
				default:
					var errorContent = 'AJAX request failed<br>' + el.parentNode.innerHTML;
					ajax('action=replyComment&linkid=' + linkid + '&commentid=' + commentid + '&option=0&type=' + commentType, window.location.href, el.parentNode, errorContent);
					el.parentNode.innerHTML = loading;
					break;
			}
			break;
		case 'edit':
			var errorContent = el.parentNode.innerHTML.replace('AJAX request failed', '') + 'AJAX request failed';
			_comments = new Array();
			ajax('action=editComment&linkid=' + linkid + '&commentid=' + commentid + '&type=' + commentType, window.location.href, el.parentNode, errorContent)
			el.parentNode.innerHTML = loading;
			break;
		case 'editCancel':
			var errorContent = 'AJAX request failed <a href="javascript: void(0);" onclick="comments(\'editCancel\', this, ' + linkid + ', ' + commentid + ')">Retry</a>';
			_comments = new Array();
			ajax('action=editCancel&linkid=' + linkid + '&commentid=' + commentid + '&type=' + commentType, window.location.href, el, errorContent)
			el.parentNode.innerHTML = loading;
			break;
		case 'editSave':
			var errorContent = el.parentNode.innerHTML.replace('AJAX request failed', '') + 'AJAX request failed';
			_comments = new Array();
			var comment = tools.urlEncode(el.parentNode.parentNode.parentNode.firstChild.childNodes[1].firstChild.value);
			ajax('action=editSave&linkid=' + linkid + '&commentid=' + commentid + '&type=' + commentType + '&comment=' + comment, window.location.href, el, errorContent)
			el.parentNode.innerHTML = loading;
			break;
	}
}
function build(linkid, parent, container){
	var html = '';
	for(var i=0; i<_comments.length; i++){
		if(_comments[i]['linkid'] == linkid && _comments[i]['parent'] == parent){
			var c = _comments[i];
			var _class = 'comment';
			if(c['visible'] && c['visible'] == 0)
				_class += ' hidden';
			else if(c['approved'] && c['approved'] == 0)
				_class += ' unapproved';
			if(container != 0)
				html += '<div class="' + _class + '" id="comment' + linkid + '_' + c['commentID'] + '">';
			
			html += '<table>' +
				'<tr><td style="vertical-align: top;">' +
					'<div class="commentDate">' + c['date'] + '</div>' +
					'<div class="commentAvatar"><center>' + c['avatar'] + '</center></div>' +
					'<div class="commentAuthor">' + c['author'] + '</td></div>' +
				'</td>' +
					'<td class="comment">' + c['comment'];
			if(c['signature'].length > 0)
				html += '<div class="signature">' + c['signature'] + '</div>';
			html += '</td>' +
				'</tr>' +
				'<tr><td colspan=2 class="respond">';
			if(c['reply'])
				html += c['reply'];
			if(c['edit'])
				html += c['edit'];
			if(c['admin'])
				html += c['admin'];
			html += '</td>' +
			'</tr></table>';
				
			if(container != 0){
				html += '</div>'	+
					'<div class="subcomment">' + build(linkid, c['commentID']) + '</div>';
			}
		}
	}
	return html;
}
function hoverInfo(el, action, info, e){
	switch(action){
		case 'show':
			if(!_hover){
				_hover = new dialog();
				var content = document.createElement('div');
				content.innerHTML = '<div class="content"></div>';
				_hover.create(0, null, content, true, null);
			}
			_hover.item.firstChild.innerHTML = info;
			_hover.position(tools.getMouse(e));
			break;
		case 'destroy':
			try{ _hover.dispose();} catch(e){}
			_hover = null;
			break;
	}
}

function ajaxResponse(responseBlock){
	_comments = new Array();
	for(var i=0; i<responseBlock.length; i++){
		switch(responseBlock[i].getElementsByTagName('responseType')[0].firstChild.nodeValue){
			case 'empty':
				//most likely cause is disagreement between permissions on front-end and backend... can happen
				//due to cookie/session timeout but no page refresh, or change to username/password on the backend.
				var t = confirm('AJAX failure may be due to lack of permissions.\nReload page?');
				if(t == true)
					document.location.href = document.location.href;
				break;
			case 'comment':
				var linkid = responseBlock[i].getElementsByTagName('linkID')[0].firstChild.nodeValue;
				var author = responseBlock[i].getElementsByTagName('author')[0].firstChild.nodeValue;
				var comment = responseBlock[i].getElementsByTagName('comment')[0].firstChild.nodeValue;
				var date = responseBlock[i].getElementsByTagName('date')[0].firstChild.nodeValue;
				var parent = responseBlock[i].getElementsByTagName('parent')[0].firstChild.nodeValue;
				var commentID = responseBlock[i].getElementsByTagName('commentID')[0].firstChild.nodeValue;
				var avatar;
				try{ avatar = responseBlock[i].getElementsByTagName('avatar')[0].firstChild.nodeValue;} catch(e){}
				var signature;
				try{ signature = responseBlock[i].getElementsByTagName('signature')[0].firstChild.nodeValue;} catch(e){}

				var reply = 0;
				try{ reply = responseBlock[i].getElementsByTagName('reply')[0].firstChild.nodeValue;} catch(e){}
				var edit = 0;
				try{ edit = responseBlock[i].getElementsByTagName('edit')[0].firstChild.nodeValue;}catch(e){}
				
				var i = _comments.length;
				_comments[i] = new Array();
				_comments[i]['linkid'] = linkid;
				_comments[i]['author'] = author;
				_comments[i]['avatar'] = avatar;
				_comments[i]['comment'] = comment;
				_comments[i]['signature'] = signature;
				_comments[i]['date'] = date;
				_comments[i]['parent'] = parent;
				_comments[i]['commentID'] = commentID;
				_comments[i]['reply'] = reply;
				_comments[i]['edit'] = edit;
				break;
			case 'buildComments':
				var linkid = responseBlock[i].getElementsByTagName('linkID')[0].firstChild.nodeValue;
				
				var postedCommentID;
				try{ postedCommentID = responseBlock[i].getElementsByTagName('postedCommentID')[0].firstChild.nodeValue; } catch(e){}
				var reply = responseBlock[i].getElementsByTagName('reply')[0].firstChild.nodeValue;
				var commentPosted = false;
				try{
					if(responseBlock[i].getElementsByTagName('commentPosted')[0].firstChild.nodeValue == 'true')
						commentPosted = true;
				}
				catch(e){}

				var content = build(linkid, 0);
				content += '<div class="respond">' + reply + '</div>';
				document.getElementById('commentBlock' + linkid).innerHTML = content;

				if(commentPosted){
					if(document.getElementById('comment' + linkid + '_' + postedCommentID))
						document.getElementById('comment' + linkid + '_' + postedCommentID).innerHTML += '<div class="message">Your comment will be reviewed prior to being visible</div>';
					else
						document.getElementById('commentBlock' + linkid).innerHTML += '<div class="message">Your comment will be reviewed prior to being visible</div>';
				}
				break;
			case 'replyComment':
				var linkid = responseBlock[i].getElementsByTagName('linkID')[0].firstChild.nodeValue;
				var commentid = responseBlock[i].getElementsByTagName('commentID')[0].firstChild.nodeValue;
				var el;
				if(commentid == 0)
					el = document.getElementById('commentBlock' + linkid).childNodes[document.getElementById('commentBlock' + linkid).childNodes.length - 1];
				else 
					el = document.getElementById('comment' + linkid + '_' + commentid).firstChild.firstChild.childNodes[1].firstChild;
				el.innerHTML = responseBlock[i].getElementsByTagName('content')[0].firstChild.nodeValue;
				break;
			case 'editComment':
				var linkID = responseBlock[i].getElementsByTagName('linkID')[0].firstChild.nodeValue;
				var commentID = responseBlock[i].getElementsByTagName('commentID')[0].firstChild.nodeValue;
				var commentAuthor = responseBlock[i].getElementsByTagName('commentAuthor')[0].firstChild.nodeValue;
				var comment = responseBlock[i].getElementsByTagName('comment')[0].firstChild.nodeValue;
				var ops = responseBlock[i].getElementsByTagName('ops')[0].firstChild.nodeValue;
				
				var els = document.getElementById('comment' + linkID + '_' + commentID).getElementsByTagName('TD');
				for(var k=0; k<els.length; k++){
					switch(els[k].className){
						case 'commentAuthor':
							els[k].innerHTML = commentAuthor;
							break;
						case 'comment':
							els[k].innerHTML = comment;
							break;
						case 'respond':
							els[k].innerHTML = ops;
							break;
					}
				}
				
				/*//el.firstChild.firstChild.innerHTML = commentAuthor;
				el.firstChild.firstChild.firstChild.childNodes[1].firstChild.innerHTML = commentAuthor;
				el.firstChild.firstChild.firstChild.childNodes[2].innerHTML = comment;
				el.firstChild.firstChild.childNodes[1].firstChild.innerHTML = ops;
				//childNodes[2].innerHTML = ops;*/
				break;
			case 'updateComment':
				var linkID = responseBlock[i].getElementsByTagName('linkID')[0].firstChild.nodeValue;
				var commentID = responseBlock[i].getElementsByTagName('commentID')[0].firstChild.nodeValue;
				var parentID = responseBlock[i].getElementsByTagName('parentID')[0].firstChild.nodeValue;
				document.getElementById('comment' + linkID + '_' + commentID).innerHTML = build(linkID, parentID, 0);
				break;
			case 'reload':
				document.location.href = document.location.href;
				break;
			case 'BGItem':
				var content = responseBlock[i].getElementsByTagName('content')[0].firstChild.nodeValue;
				if(_callStack.length > 1)
					content = content + '<a href="javascript: void(0)" onclick="bgItemDisplay(\'back\')">Back</a>';
				document.getElementById('displayItem').innerHTML = content;
				break;
		}
		if(typeof(subAjaxResponse) == 'function')
			subAjaxResponse(responseBlock[i]);
		if(typeof(adminCommentAjaxResponse) == 'function')
			adminCommentAjaxResponse(responseBlock[i]);
		if(typeof(adminAjaxResponse) == 'function')
			adminAjaxResponse(responseBlock[i]);

	}
}

