function setup() {
	
	
	$('ul .plus').apply(function() {
		
		var plus = this;
		this.$('input').apply(function() {
			if (this.checked) {
				plus.$('.qtext').addClass('tick');
			}
		});
	});
	
	
	
/*	$('.clear').addEvent('click', function() {
		//var el = $(this);
	//	while (el.parent() && el.className != 'expand') {
//			el = el.parent();
		//}

		//el = el.parent();
		
		//el.parent().$('.tick').removeClass('tick');

		//el.$('input').apply(function() {
		//	this.checked = false;
		//});
		
		//recount();	
	});*/
	
	
	$('ul .plus').addEvent('mousedown', function() {
		
			if (this.className == 'plus') {
				this.className = 'minus';
				var type = 'show';
			}
			else {
				this.className = 'plus';
				var type = 'hide';
			}
			
		
			var effect = new ElementEffect(new Effect_Slide);
			var node = this.$('.expand');
			
			
			var self = this;
			this.$('.clear').addEvent('click', function() {
				self.$('input').apply(function() {
					this.checked = false;
				});
				getChart($('select[name="chart"]').value);
				recount();
			}, true);
			
			node.addEvent('mousedown', function() {}, true);
			effect.run(node, type);
		}
	);
	
	$('input').addEvent('click', recount );
}

$.ready(setup);

function recount(gc) {
	$('.qtext').removeClass('tick');
	
		//Find all checked boxes:
	
	$('.wizard input').apply(function() {
		if (this.checked) {
			var el = this;
			while (el.parent()) {
				if (el.className == 'minus' || el.className == 'plus') break;
				else el = el.parent();
			}
			
			if (el) {
				el.$('.qtext').addClass('tick');
			}
		}
	});
	
	var ajax = new Ajax({
		url: 'search/count/',
		params: document.forms[0],
		callback: function(result) {
		//	alert(result);
			if (!result) result = {message: 0}
			if (!result.message) result.message = 0;
			
			$('.stats .found').setText(result.message);
			$('.stats .percent').setText(Math.round((parseInt(result.message) / parseInt($('.stats .total').item(0).getText()))*100));	
		}
	});
	
	
	if (gc) getChart($('select[name="chart"]').value);
	
	
	
}

var chartMode = 1;

function getChart(id) {

	if (!$('#chartarea') || $('#chartarea').count() == 0)  return;
	
	if (!id) id = 1;
	
	
	if ($('#chartarea img').count() > 0) {
			$('#chartarea img').fade({
				type: 'hide'
			});
	}
	
	
	$('#chartarea').apply(function() {
		this.style.backgroundImage = 'url(/img/loading.gif)';
		this.style.backgroundRepeat = 'no-repeat';
		this.style.backgroundPosition = 'center';
	});
		
	new Ajax({url: 'chart/getChart/' + id + '/' + chartMode, method: 'POST', params: document.forms[0],
		resultMode: Ajax.RESULT_TEXT,
		callback: function(result) {
		$('map').remove();
			$('#chartarea').empty();
			
			
			$('#chartarea').style.backgroundImage = '';
			
			$('#chartarea').innerHTML = result;
			var mapname = 'chart' + Math.round(Math.random()*100);
		
			var img = new Image();
			//img.setAttribute('usemap', 'chart');;
			img.useMap = '#chart';
			img.onload = function() {
				$('#chartarea').appendChild(img);				
				$(this).fade({type: 'show'});
			}
			
			
			var sessionid = document.cookie.split('PHPSESSID=')[1].split(';')[0];
			$(img).setOpacity(0);
			img.src = '/charts/' + sessionid + '.gif?' + Math.random(); 
		}	
	});
	
	
	
	//Get selection
	
	
	var selection = new Object();
	
	$('input[checked]').apply(function() {
		
		var el = this.parent();
		while (el.parent() && el.className != 'plus' && el.className != 'minus') {
			el = el.parent();
		}
		
		var label = el.$('.qtext').getText();
		if (!selection[label]) selection[label] = new Array();		
		selection[label].push(this.title);
	});
	
	
	$('#myselection').empty();
	
	for (var s in selection) {
		var div = new $e('div');
		var span = new $e('span');
		span.style.fontWeight = 'bold';
		span.add(s + ': ');
		div.add(span);
		
		var span = new $e('span');
		span.add(selection[s].join(', '));
		div.add(span);
		
		$('#myselection').add(div);
	}
	
}


function setChartMode(el, num) {
	chartMode = num;
	$('.disabled').removeClass('disabled');
	$(el).addClass('disabled');
	getChart($('select[name="chart"]').value);
}

function printChart() {
	var iframe = new $e('iframe');
	//iframe.style.display = 'none';
	
	iframe.src = '/' + document.cookie.split('PHPSESSID=')[1].split(';')[0] + '.gif';
	iframe.style.width = '700px'
	iframe.style.height = '700px'
	//iframe.style.visibility = 'hidden';
	iframe.style.position = 'absolute';
	iframe.style.left = '-1400px';
	//iframe.contentWindow.focus();
	document.body.appendChild(iframe);
		
	iframe.contentWindow.onload = function() {
		this.focus();
		this.print();
	}
	
}


function checkDecade(el) {
	$(el).parent().$('input').apply(function() {
		this.checked = el.checked;
	});
	
	recount();
}

function expandAll() {
	$('.expand').slide({type: 'show'});
	$('.plus').addClass('minus');
	$('.plus').removeClass('plus');
}

function resetAll() {
	$('.expand').slide({type: 'hide'});
	$('.minus').addClass('plus');
	$('.minus').removeClass('minus');
}


function showDetails(el) {
	//alert($(el.parentNode).$('.details').item(0));
	$(el.parentNode).$('.details').toggle({type: 'slide'});
	
}


function unselectAll() {
	$('input').apply(function() {
		this.checked = false;
		
	}
	
	);
	
	$('.stats .found').setText($('.stats .total').item(0).getText());
	$('.stats .percent').setText('100');
	$('.tick').removeClass('tick');
}


function showMore(link, el) {
	if (el.style.height == '1.5em') {
		el.style.height = 'auto';
		link.firstChild.nodeValue = '[less]';
	}
	else {
		el.style.height = '1.5em';
		link.firstChild.nodeValue = '[more]';
	}
}

function setSortOrder(order) {
	$('#sortorder').value = order;
	document.forms[0].submit();
}

function clickBar(title, value) {
//	alert(title + ' ' + value);
	$('input[name="' + title + '[]"]').apply(function() {
		//alert(this.value);
		if (this.value == value) {
			this.checked = true;
		//	recount();
			
		}
		else this.checked = false;
	});
	
	getChart($('select[name="chart"]').value);
	recount(false);
}

function in_array(needle, haystack) {
	for (var i = 0; i < haystack.length; i++) {
		if (haystack[i] == needle) return true;
	}
	return false;
}

var pieFilter = new Array();
var pieFilterType;

function clickPieOther(title, values) {
	if (title !== pieFilterType) {
		pieFilter = new Array();
		pieFilterType = title;
	}
	
	for (var i = 0; i < values.length; i++) pieFilter.push(values[i]);
	
	
	$('input[name="' + title + '[]"]').apply(function() {
		//alert(this.value);
		this.checked = false;
		
		if (in_array(this.value, pieFilter))	 {
			this.checked = false;
		}
		else this.checked = true;
	});
	
	getChart($('select[name="chart"]').value);
	recount(false);
}

