/**
 * @version 1.0
 *
 * @copyright Copyright 2009 WnG Solutions Sàrl, all rights reserved
 * @author Michaël Ambass <michael DOT ambass AT wng DOT ch>
 * @author Yannick Beuchat <yannick DOT beuchat AT wng DOT ch>
 * @author Dorian Villet <dorian DOT villet AT wng DOT ch>
 * @package pnyx
 */

/**
 * Statistiques des sondages
 */
	
// Fonction de changement des restrictions
function getPolls() {

	// On contrôle qu'il y a bien 3 caractères
	if ($('#searchElement').val().length >= 3) {
	
		// Envoi des données à PHP
		$.ajax({
			type: 'POST',
			url: 'ajax/poll-stats.ajax.php',
			data: 'critera=' + $('#searchElement').val() + '&idpoll=' + $('#idPoll').val(),
			success: function(msg) {
				$('#searchPollResult').html(msg);
			}
	    });
		
	} else {			
		$('#searchPollResult').html('<p class="errorBox">La recherche est limitée à 3 caractères</p>');
	}
}

// Quand la page est chargée
$(document).ready(function() {

	// Sélection des sondages croisés
	$('ul.polls_stats li a[class^=\'poll\']').live('click', function() {
		var pollIdentification = $(this).attr('class').split('-');
	
		$('#searchPollResult').html('<p><strong>Sondage sélectionné :</strong><br /><span id="pollSelected" class="' + pollIdentification[1] + '">' + $(this).html() + '</span></p>');	
	
		$.ajax({
			type: 'POST',
			url: 'ajax/poll-stats-question.ajax.php',
			data: 'idpoll=' + pollIdentification[1],
			success: function(msg) {
				$('#searchPollResult').append(msg);
			}
	    });
		
		$('#searchPollResult').append('<p><a href="#" onclick="getPolls(); return false;">Revenir à la selection du sondage</a></p>');
	});

	// Mise à jour de l'animatoin avec les sondages croisés
	$('ul li a[id^=\'pollid\']').live('click', function() {
		var pollID = $(this).attr('id').split('-');
		var choiceID = $(this).attr('class').split('-');
		
		var animation = document.getElementsByName('resultsViewer');
		animation[0].changeRestrictions('restrictOtherPollChoice', choiceID[1]);
		
		var animationPoll = document.getElementsByName('resultsViewer');
		animationPoll[0].changeRestrictions('restrictOtherPoll', pollID[1]);	
		
		$('#pollChoice').html('<strong>' + $('#pollSelected').html() + '</strong><br /> <img src="images/arrow.gif" alt="Puce" /> ' + $(this).html() + '<br /><br /><a href="#" onclick="return false;" class="endCross">Enlever le croisement</a>');
		$('#pollChoice').css('display', 'block');
		$('#dialogContent').dialog('close');
		$('#dialogContent').dialog();
	});
	
	// ...
	$("a[class='endCross']").live('click', function() {
		var animation = document.getElementsByName('resultsViewer');
		animation[0].changeRestrictions('restrictOtherPollChoice', 0);
		animation[0].changeRestrictions('restrictOtherPoll', 0);
		
		$('#pollChoice').css('display', 'none');
	});
	
	// Gestion des vues du flash
	$('#geographicArea').change(function() {
		var animation = document.getElementsByName('resultsViewer');
		animation[0].changeRestrictions('restrictArea', $(this).val());
	});
	$('#agesClass').change(function() {
		var animation = document.getElementsByName('resultsViewer');
		animation[0].changeRestrictions('restrictAgesClass', $(this).val());
	});

	// Recherche de sondage croisés	
	$('#searchPollBtn').live('click', function() {
		getPolls();
	});
	
	// ...
	$('#resultOtherPoll a').live('click', function() {
		var pollIdentification = $(this).attr('class').split('-');
		
		$.ajax({
			type: 'POST',
			url: 'ajax/poll-stats-question.ajax.php',
			data: 'idpoll=' + pollIdentification[1],
			success: function(msg) {
				$('#resultQuestions').html(msg);
			}
   	    });
    });
	
	// ...
	$('#resultQuestions a').live('click', function() {
		var pollId = $(this).attr('id').split('-');
		var choiceId = $(this).attr('class').split('-');

		$('#otherPoll').html('Sondage : <em>' + $('.poll-' + pollId[1]).html() + '</em><br />Choix sélectionné : <em>' + $(this).html() + '</em>');		
		$('#searchBox').slideUp('slow');
		
    });
	
	// Recherche
	$('#openSearchBox').live('click', function() {
		$('#searchPollResult').html('');
		$('#dialogContent').dialog('open');
		$('#dialogContent').dialog();		
	});
});