/* 
1: each menu MUST have an ID set. It doesn't matter what this ID is as long as it's there.
2: each menu MUST have a class 'menu' set. If the menu doesn't have this, the JS won't make it dynamic

Optional extra classnames:

noaccordion : no accordion functionality
collapsible : menu works like an accordion but can be fully collapsed
expandfirst : first menu item expanded at page load

<ul id="menu1" class="menu [optional class] [optional class]">
<li><a href="#">Sub menu heading</a>
<ul>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
...
...
</ul>
<li><a href="#">Sub menu heading</a>
<ul>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
...
...
</ul>
...
...
</ul>
*/

var elementCount = 1;

function addAttachment() {
	var answer = document.createElement('input');
	answer.setAttribute('type', 'file');
	answer.setAttribute('class', 'file');
	answer.setAttribute('size', '35');
	answer.setAttribute('name', 'file[' + elementCount + ']');

	var newline = document.createElement('br');
    var extra = document.getElementById('attachments');
    extra.appendChild(answer);
    extra.appendChild(document.createTextNode(' '));
    extra.appendChild(newline);

    elementCount++;
}

function initMenus() {	
	$('ul.treeMenu li a').click(function() {
		var checkElement = $(this).next();
		var parent = this.parentNode.parentNode.id;
		if ($('#' + parent).hasClass('noaccordion')) {
			$(this).next().slideToggle('normal');
			if ($(this).hasClass('plus')) {
				$(this).removeClass('plus');
				$(this).addClass('minus');
			} else {
				$(this).removeClass('minus');
				$(this).addClass('plus');
			}
							
			return false;
		}
	});
}

function initItems() {	
	$('ul.items li a.title').click(function() {
		$('ul.items li a.title').each(function() {
			if ($(this.parentNode).hasClass('itemFolded')) {
				$(this.parentNode).removeClass('itemFolded')
				$(this.parentNode).addClass('itemUnfolded');
			}
			
			var checkElement = $(this).next();
			checkElement.hide();
		});
		
		var checkElement = $(this).next();
		var parent = this.parentNode.parentNode.id;
		var child = this.parentNode.id;
		var qBar  = this.parentNode;
		
		if ($(this.parentNode).hasClass('itemUnfolded')) {
			$(this.parentNode).removeClass('itemUnfolded');
			$(this.parentNode).addClass('itemFolded');

			if ($(this).hasClass('ajax')) {
				return true;
			}

			$(checkElement).show();
			$.ajax({
				url: this.href,
				cache: false,
				success: function(html) {
					var response = eval('(' + html + ')');
					if (!response.SolutionText && !response.ExternalLink && !response.Go) {
						$(checkElement).hide();
						$(qBar).removeClass('itemFolded');
						$(qBar).addClass('itemUnfolded');
						tb_show('', 'contact.php#TB_iframe=true&height=450&width=450')
					} else if (response.ExternalLink) {
						window.location = response.ExternalLink;
					} else if (response.Go) {
						window.location = '?node=' + response.Go;
					} else {
						$(checkElement).find('.itemText').html(response.SolutionText);
					}
					
					return false;
				}
			});
			return false;
		} else {
			$(checkElement).hide();
			
			$(this.parentNode).removeClass('itemFolded');
			$(this.parentNode).addClass('itemUnfolded');
		}
		
		return false;
	});
	
	$('ul.items a.yes').click(function() {
		$.ajax({
			url: this.href + '&answered=true',
			cache: false
		});
		
		$(this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode).slideToggle('normal');
		if($(this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode).hasClass('itemUnfolded')) {
			$(this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode).removeClass('itemUnfolded');
			$(this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode).addClass('itemFolded');
		} else {
			$(this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode).removeClass('itemFolded');
			$(this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode).addClass('itemUnfolded');
		}
		
		return false;
	});
}

$(document).ready(function() {
	initItems();
	initMenus();
	
	$('.bubbleInfo .popup').hide();
	$('.no').mouseover(function() {
		$('.bubbleInfo .popup').css({ top: -135, left: -30 });
		$(this.parent).find('.bubbleInfo .popup').hide();
		$(this.parentNode).find('.bubbleInfo .popup').show();
	});
	
	$('.no + .bubbleInfo .popup').mouseout(function() {
		$(this).hide();
	});
	
	$('.infoTop').click(function() {
	   $(this.parent).find('.bubbleInfo .popup').hide();
	   
	   return false;
	});
});

