/*
+----------------------------------------------------------------+
|																							|
|	WordPress 2.8 Plugin: WP-Polls 2.50										|
|	Copyright (c) 2009 Lester "GaMerZ" Chan									|
|																							|
|	File Written By:																	|
|	- Lester "GaMerZ" Chan															|
|	- http://lesterchan.net															|
|																							|
|	File Information:																	|
|	- Polls Javascript File															|
|	- wp-content/plugins/wp-polls/polls-js.js									|
|																							|
+----------------------------------------------------------------+
*/


// Variables
var poll_id = 0;
var poll_answer_id = '';
var is_being_voted = false;
pollsL10n.show_loading = parseInt(pollsL10n.show_loading);
pollsL10n.show_fading = parseInt(pollsL10n.show_fading);

// When User Vote For Poll 
function poll_vote(current_poll_id) {
	if(!is_being_voted) {
		set_is_being_voted(true);
		poll_id = current_poll_id;
		poll_answer_id = '';
		poll_multiple_ans = 0;
		poll_multiple_ans_count = 0;
		if(jQuery('#poll_multiple_ans_' + poll_id).length) {
			poll_multiple_ans = parseInt(jQuery('#poll_multiple_ans_' + poll_id).val());
		}
		jQuery('#polls_form_' + poll_id + ' :checkbox, #polls_form_' + poll_id + ' :radio').each(function(i){
			if (jQuery(this).is(':checked')) {					
				if(poll_multiple_ans > 0) {
					poll_answer_id = jQuery(this).val() + ',' + poll_answer_id;
					poll_multiple_ans_count++;
				} else {
					poll_answer_id = parseInt(jQuery(this).val());
				}
			}
		});
		if(poll_multiple_ans > 0) {
			if(poll_multiple_ans_count > 0 && poll_multiple_ans_count <= poll_multiple_ans) {
				poll_answer_id = poll_answer_id.substring(0, (poll_answer_id.length-1));
				poll_process();
			} else if(poll_multiple_ans_count == 0) {
				set_is_being_voted(false);
				alert(pollsL10n.text_valid);
			} else {
				set_is_being_voted(false);
				alert(pollsL10n.text_multiple + ' ' + poll_multiple_ans);
			}
		} else {
			if(poll_answer_id > 0) {
				poll_process();
			} else {
				set_is_being_voted(false);
				alert(pollsL10n.text_valid);
			}
		}
	} else {
		alert(pollsL10n.text_wait);
	}
}

function multiple_ans_checkbox(poll_id, ans_id){
			

		if(jQuery('#poll_multiple_ans_' + poll_id).length) {
			
                        poll_multiple_ans = parseInt(jQuery('#poll_multiple_ans_' + poll_id).val());
			

			var que = new Queue();
			alert();
			if(poll_multiple_ans == que.getSize()){
				$('#' + que.dequeue()).checked= false ;
			}
	                alert(ans_id);
                }


			
				
}

// Process Poll (User Click "Vote" Button)
function poll_process() {
	if(pollsL10n.show_fading) {
		jQuery('#polls-' + poll_id).fadeTo('def', 0, function () {
			if(pollsL10n.show_loading) {
				jQuery('#polls-' + poll_id + '-loading').show();
			}
			jQuery.ajax({type: 'POST', url: pollsL10n.ajax_url, data: 'vote=true&poll_id=' + poll_id + '&poll_' + poll_id + '=' + poll_answer_id, cache: false, success: poll_process_success});
		});
	} else {
		if(pollsL10n.show_loading) {
			jQuery('#polls-' + poll_id + '-loading').show();
		}
		jQuery.ajax({type: 'POST', url: pollsL10n.ajax_url, data: 'vote=true&poll_id=' + poll_id + '&poll_' + poll_id + '=' + poll_answer_id, cache: false, success: poll_process_success});
	}
}

// Poll's Result (User Click "View Results" Link)
function poll_result(current_poll_id) {
	if(!is_being_voted) {
		set_is_being_voted(true);
		poll_id = current_poll_id;
		if(pollsL10n.show_fading) {
			jQuery('#polls-' + poll_id).fadeTo('def', 0, function () {
				if(pollsL10n.show_loading) {
					jQuery('#polls-' + poll_id + '-loading').show();
				}
				jQuery.ajax({type: 'GET', url: pollsL10n.ajax_url, data: 'pollresult=' + poll_id, cache: false, success: poll_process_success});
			});
		} else {
			if(pollsL10n.show_loading) {
				jQuery('#polls-' + poll_id + '-loading').show();
			}
			jQuery.ajax({type: 'GET', url: pollsL10n.ajax_url, data: 'pollresult=' + poll_id, cache: false, success: poll_process_success});
		}
	} else {
		alert(pollsL10n.text_wait);
	}
}

// Poll's Voting Booth  (User Click "Vote" Link)
function poll_booth(current_poll_id) {
	if(!is_being_voted) {
		set_is_being_voted(true);
		poll_id = current_poll_id;
		if(pollsL10n.show_fading) {
			jQuery('#polls-' + poll_id).fadeTo('def', 0, function () {
				if(pollsL10n.show_loading) {
					jQuery('#polls-' + poll_id + '-loading').show();
				}
				jQuery.ajax({type: 'GET', url: pollsL10n.ajax_url, data: 'pollbooth=' + poll_id, cache: false, success: poll_process_success});
			});
		} else {
			if(pollsL10n.show_loading) {
				jQuery('#polls-' + poll_id + '-loading').show();
			}
			jQuery.ajax({type: 'GET', url: pollsL10n.ajax_url, data: 'pollbooth=' + poll_id, cache: false, success: poll_process_success});
		}
	} else {
		alert(pollsL10n.text_wait);
	}
}

// Poll Process Successfully
function poll_process_success(data) {
	jQuery('#polls-' + poll_id).html(data);
	if(pollsL10n.show_loading) {
		jQuery('#polls-' + poll_id + '-loading').hide();
	}
	if(pollsL10n.show_fading) {
		jQuery('#polls-' + poll_id).fadeTo('def', 1, function () {		
			set_is_being_voted(false);	
		});
	} else {
		set_is_being_voted(false);	
	}
}

// Set is_being_voted Status
function set_is_being_voted(voted_status) {
	is_being_voted = voted_status;
}

/* author of this program, Safalra (Stephen Morley), irrevocably releases
 * all rights to this program, with the intention of it becoming part of the
 * public domain. Because this program is released into the public domain, it
 * comes with no warranty either expressed or implied, to the extent permitted
 * by law.
 *
 * For more public domain JavaScript code by the same author, visit:
 *
 * http://www.safalra.com/web-design/javascript/
 */


/* Creates a new Queue. A Queue is a first-in-first-out (FIFO) data structure.
 * Functions of the Queue object allow elements to be enqueued and dequeued, the
 * first element to be obtained without dequeuing, and for the current size of
 * the Queue and empty/non-empty status to be obtained.
 */
function Queue(){

  // the list of elements, initialised to the empty array
  var queue = [];

  // the amount of space at the front of the queue, initialised to zero
  var queueSpace = 0;

  /* Returns the size of this Queue. The size of a Queue is equal to the number
   * of elements that have been enqueued minus the number of elements that have
   * been dequeued.
   */
  this.getSize = function(){

    // return the number of elements in the queue
    return queue.length - queueSpace;

  }

  /* Returns true if this Queue is empty, and false otherwise. A Queue is empty
   * if the number of elements that have been enqueued equals the number of
   * elements that have been dequeued.
   */
  this.isEmpty = function(){

    // return true if the queue is empty, and false otherwise
    return (queue.length == 0);

  }

  /* Enqueues the specified element in this Queue. The parameter is:
   *
   * element - the element to enqueue
   */
  this.enqueue = function(element){
    queue.push(element);
  }

  /* Dequeues an element from this Queue. The oldest element in this Queue is
   * removed and returned. If this Queue is empty then undefined is returned.
   */
  this.dequeue = function(){

    // initialise the element to return to be undefined
    var element = undefined;

    // check whether the queue is empty
    if (queue.length){

      // fetch the oldest element in the queue
      element = queue[queueSpace];

      // update the amount of space and check whether a shift should occur
      if (++queueSpace * 2 >= queue.length){

        // set the queue equal to the non-empty portion of the queue
        queue = queue.slice(queueSpace);

        // reset the amount of space at the front of the queue
        queueSpace=0;

      }

    }

    // return the removed element
    return element;

  }

  /* Returns the oldest element in this Queue. If this Queue is empty then
   * undefined is returned. This function returns the same value as the dequeue
   * function, but does not remove the returned element from this Queue.
   */
  this.getOldestElement = function(){

    // initialise the element to return to be undefined
    var element = undefined;

    // if the queue is not element then fetch the oldest element in the queue
    if (queue.length) element = queue[queueSpace];

    // return the oldest element
    return element;

  }

}

var que = new Queue();
function multiple_ans_checkbox(poll_id, ans_id){

	if(jQuery('#poll-answer-'+ans_id+':checked' ).attr('checked')){ 
                if(jQuery('#poll_multiple_ans_' + poll_id).length ) {

                        poll_multiple_ans = parseInt(jQuery('#poll_multiple_ans_' + poll_id).val());


                        
                        
                        if(poll_multiple_ans == que.getSize()){
				var out = que.dequeue();
                               jQuery('#poll-answer-'+out+':checked' ).attr('checked', false );
			
                        }
			que.enqueue(ans_id);
                        
                }



	}else{
		var que2 = new Queue();
		while(!que.isEmpty()){
			var id = que.dequeue();
			if ( id != ans_id){
			  que2.enqueue(id);
			}
		}
		que = que2;
	}
}


