/*

// 					Xhtml structure and CSS design by Firdauz Artiste.
//					Server side scripting by Syuen.
//					Client side scripting by Aizat.
//					
//					A damn nice poll survey system by Josh Lim & Associates.

*/

/* Credit to Peter-Paul Koch of www.quirksmode.org
_____________________________________________________*/

addEventSimple(window, 'load', initSelection);
addEventSimple(window, 'load', initSurveyContent); 
/*addEventSimple(window, 'load', initIFrameHeight); */

function addEventSimple(obj,evt,fn) {
	if (obj.addEventListener)
		obj.addEventListener(evt,fn,false);
	else if (obj.attachEvent)
		obj.attachEvent('on'+evt,fn);
}

function removeEventSimple(obj,evt,fn) {
	if (obj.removeEventListener)
		obj.removeEventListener(evt,fn,false);
	else if (obj.detachEvent)
		obj.detachEvent('on'+evt,fn);
}

/* Selection
_____________________________________________________*/

var ID              = 'pollquestion_';
var IFRAME_ID       = 'box_iframe';
var MARGIN          = 20;
var MARGIN_TB       = 30;
var MARGIN_LR       = 20;
var ANSWER_PICKED   = 'answer-picked';
var ANSWER_UNPICKED = 'answer-unpicked';

/* Makes it easier to click select a choice for the poll, and
 * view what has been selected.
 *
 * This is done by modifying an input's parent 'li' to include an 'onclick'
 * which will modify other siblings by changing their class names to indicate
 * that they have been unchecked. Similarly it modifies itself to indicate that it
 * has been checked.
 */
function initSelection () {
  // Get each answer box
	var answerBoxes = $A($('surveys-container').getElementsByClassName('answer-box'));
  // Itterate through each answerbox
	answerBoxes.each(function(answerbox) {
    // Get each input
		var inputs = $A(answerbox.getElementsByTagName('input'));
    // Itterate through each input
		inputs.each(function(input) {
      // When the 'li' is clicked
			input.parentNode.onclick = function() {
        // Make sure that the input itself has been clicked
				input.checked = true;
        // Itterate through each 'li' element in the set,
        // and remove the classnames such that they are 'unpicked'
				$A(this.parentNode.getElementsByTagName('li')).each(function(li) {
					li.removeClassName(ANSWER_PICKED);
					li.addClassName(ANSWER_UNPICKED);
				});
        // To the clicked element, add the classname such that it has been picked
				this.addClassName(ANSWER_PICKED);				
			}
			
//	  Internet Explorer feature only!
//    In IE, iFrame forms get saved, and this can be used 
//    to highlight the element that was checked
			if (input.checked == true) {
				input.parentNode.addClassName(ANSWER_PICKED);
				input.parentNode.removeClassName(ANSWER_UNPICKED);
			}
		});
	});
}

/* Controls how the polls 'next' and 'previous' button works in terms of effects.
 */
function initSurveyContent() {
	var surveycontents = $A(document.getElementsByClassName('survey-content'));
  // Itterate through each survey content
	surveycontents.each(function(surveycontent) {
    // Get the `id` of the survey content
    // Giving us which position we are in
		var id   = parseInt(surveycontent.id.split(/_/)[1]);
    // Retrieve the elements which contain the nav-next, and nav-previous class
    // indicating their nature
		var next = surveycontent.getElementsByClassName('nav-next')[0];
		var prev = surveycontent.getElementsByClassName('nav-previous')[0];

    // If the `next` element exists.
    // For example  it will not exist for the last poll question
		if (typeof next != 'undefined') {
			next.onclick = function() {
        // Simple validation check, retreive which item has bee nselected
				input = isChecked($A(surveycontent.getElementsByTagName('input')))
        // If nothing has been selected highlight the poll
        // Else go to the next poll
				if (! input) {
					new Effect.Highlight(surveycontent,{startcolor:'#FFCCCC', endcolor:'#FFFFFF'});
				} else {
					toggleNext(id);
				}
			};
		}

    // If the `previous` element exists.
    // For example it may not exist for the first poll
		if (typeof prev != 'undefined') { 
			prev.onclick = function() {
				togglePrevious(id);
			};
		}
	});
}

/* Initialize the default iFrame height
 
function initIFrameHeight() {
	if (document == window.top.document)
		return;
	Element.setStyle(getIFrameElement(), {height: $(ID + 1).getHeight() + MARGIN_TB + 'px'});
}
*/
/* Checked if any of the inputs have been checked.
 * If they are, just return it.
 * Else return false.
 */
function isChecked(inputs) {
	var flag = false;
	inputs.each(function(input) {
		if (input.checked == true) flag = input;
	});
	return flag;
}

/* Retreive the iFrame element from the parent window
 
function getIFrameElement() {
	if (document == window.top.document)
		return;
	return window.top.document.getElementById(IFRAME_ID);
}
*/
/* Previous / Next elements, Resize the iframe
_____________________________________________________*/

/* For toggling the next poll.
 * @param id the `id` of the current poll that will be removed
 */
var global_old_height = new Array();
var demolishcounter = 0;

 function toggleNext(id) {
	var old_height = $(ID + id).getHeight();
	var new_height = $(ID + (id + 1)).getHeight();

	var percentage = new_height / old_height * 100;
    
	global_old_height[demolishcounter] = old_height;
	demolishcounter++;
	
	if (old_height < new_height) {
		var scale  = 'front';
		var shrink = 'end';
	} else {
		var shrink = 'front';
		var scale  = 'end';
	}

	new Effect.Shrink(ID + id, {opacityTransition:Effect.Transitions.linear, queue: shrink});
	new sendPipeMessage(Math.round(percentage));
	
	if (old_height == new_height)
		return;
}

/* For toggling the previous poll.
 * @param id the `id` of the current poll that will be moved
 */
function togglePrevious(id) {
	var old_height = $(ID + id).getHeight();
	var new_height = $(ID + (id - 1)).getHeight();
	
	demolishcounter--;
	var percentage = global_old_height[demolishcounter] / old_height * 100;
	
	if (old_height > new_height) {
		var grow  = 'front';
		var scale = 'end';
	} else {
		var scale = 'front';
		var grow  = 'end';
	}
	
	new Effect.Grow(ID + (id - 1), {opacityTransition:Effect.Transitions.linear, queue: grow});
	new sendPipeMessage(Math.round(percentage));
	
	if (old_height == new_height)
		return;
}

