/* ****************************************************

	@file		  resultats.js
	@descriptio   Résultats
	@author		  remi (ixmedia.com)
	@version	  20081113

***************************************************** */


var datasets = {};

var buzzz_max = 0;
var buzzz_min = 0;

$(document).ready(function() {

	$(window).scroll(function() {
		/*$('#forms').css('margin-left', $(this).scrollLeft()+'px' );
		$('#forms').animate(
		{
			'marginLeft' : $(this).scrollLeft()+'px'
		},
		{
			duration: 200,
			queue: false
		}
		);
		*/
	}).resize(function() {
		$('.infos').width( $(window).width() );
		$('.infos .texte').width( $(window).width() - 500 );
	});
	$('.infos').width( $(window).width() );
	$('.infos .texte').width( $(window).width() - 500 );


	$('.graphs .status').text('Veuillez patienter...').hide();

	if ($.browser.msie) {
		$('.graphs .status').text('Veuillez patienter, votre navigateur va probablement être inopérable...').show();
	}

	$('.overlay').addClass('mini');
	$('.graph *').remove();
	$('.graphs .status').show();
	$('#graph2').hide();
	$('fieldset.graph2 .champs').hide();
	requestDataset('graph1');
	buzzz_max = 0;
	buzzz_min = 0;
	calculateMaxMin();
	updateGraphiques();

	$('#graph2-actif').toggle(function() {
		$(this).val('Cacher');
		$('.overlay').removeClass('mini');
		$('#graph2').slideDown();
		$('fieldset.graph2 .champs').slideDown();
		$('.graph *').remove();
		$('.graphs .status').show();
		requestDataset('graph1');
		requestDataset('graph2');
		calculateMaxMin();
		updateGraphiques();
	}, function() {
		datasets.graph2 = null;
		$(this).val('Afficher');
		$('.overlay').addClass('mini');
		$('#graph2').slideUp();
		$('fieldset.graph2 .champs').slideUp();
		$('.graph *').remove();
		$('.graphs .status').show();
		requestDataset('graph1');
		buzzz_max = 0;
		buzzz_min = 0;
		calculateMaxMin();
		updateGraphiques();
	})

	$('button').not('.actif button').click(function() {

		var id_graph = '';
		if ($(this).parents('fieldset').is('.graph1')) {
			id_graph = "graph1"
		}
		if ($(this).parents('fieldset').is('.graph2')) {
			id_graph = "graph2"
		}
		$('.graph *').remove();
		$('.graphs .status').show();
		requestDataset(id_graph);
		buzzz_max = 0;
		buzzz_min = 0;
		calculateMaxMin();
		updateGraphiques();
		return false;
	});

	$('.champ input').click(function() {
		var ul = $(this).parents('ul');
		if (ul.find('input:checked').length == 0) {
			$(this).attr('checked', 'checked')
		}
	})

	$('.overlay').css('opacity', 0.3);
	$('#parole div span').mouseover(function() {

		var span_left = parseInt($(this).css('left').replace(/px/, ''));
		var span_width = $(this).width();

		$('#o2').css({
			left : '0',
			width : span_left+'px'
		});

		$('#o1').css({
			left : (span_left+span_width)+'px',
			width : 2462-span_left-span_width+'px'
		});

		$('.overlay:animated').stop();
		$('.overlay').show();

		}
	).bind('mouseleave', function() {
		$('.overlay').hide(0)
	});

});

function calculateMaxMin() {
	$.each(datasets, function(n,i) {
		if (i != null) {
			var allvalues = i.g1;
			allvalues = allvalues.toString().split(',');
			var maxvalues = $.grep(allvalues, function(i){
				return i > 0;
			});

			var minvalues = $.grep(allvalues, function(i){
				return i <= 0;
			});

			var max = Math.max.apply(Math, maxvalues);
			var min = Math.min.apply(Math, minvalues);


			if (max < Math.abs(min)) {
				if (isFinite) {
					max = Math.abs(min); // Magie!
				}
			}
			else {
				min = max * -1;
			}

			if (isNaN(parseInt(min))) {
				min = -10;
			}

			if (isNaN(parseInt(max))) {
				max = 0;
			}

			if (max > buzzz_max || min < buzzz_min) {
				buzzz_max = max;
				buzzz_min = min;
			}
		}
	})

}

function buildParams(id) {
	var params = {};
	$('fieldset.'+id+' .champ input').each(function() {
		if ($(this).is(':checked')) {
			params[$(this).attr('id')] = 1;
		}
	})
	return params;
}

function requestDataset(id) {
	$.ajax({
		type: 'POST',
		url : 'api.php',
		async: false,
		data : buildParams(id),
		dataType: 'json',
		success : function(json){
			populateDataset(id, json.g1, json.g2, json.nombreUsagers);
		}
	})
}

function populateDataset(id, g1, g2, usercount) {
	datasets[id] = {}
	datasets[id].g1 = g1;
	datasets[id].g2 = g2;
	datasets[id].usercount = usercount;
}

function updateGraphiques() {
	$.each(datasets, function(i,n) {
		if (n != null) {
			$('#' + i + ' .g1 .graph').sparkline(n.g1, {
				type: 'bar',
				barWidth: 4,
				barColor: '#aeef81',
				barColorPast: '#aeef81',
				negBarColor: '#fb8585',
				negBarColorPast: '#fb8585',
				height: 120,
				zeroAxis: true,
				barSpacing: 1,
				secondaire: (i == "graph2") ? true : false
			});
			$('#' + i + ' .g2 .graph').sparkline(n.g2, {
				type: 'bar',
				barWidth: 4,
				barColor: '#ffca87',
				barColorPast: '#ffca87',
				negBarColor: '#ffffff',
				negBarColorPast: '#ffffff',
				height: 120,
				zeroAxis: true,
				barSpacing: 1
			});
			$('#'+i+' .tagline span').text('('+n.usercount+' utilisateurs)');
		}
	})
	$('.graphs .status').hide();
}