/*hooks for the  DOM and Event objects in the Yahoo! library*/
var Dom = YAHOO.util.Dom;
var Event = YAHOO.util.Event;

/*used for the multipage form*/
var currentLayer = 'page1';

/*showLayer - show the next page, or layer, of the form
@param lyr -string- the id of the layer to show
*/
function showLayer(lyr) {
   hideLayer(currentLayer);
   document.getElementById(lyr).style.visibility = 'visible';
   currentLayer = lyr;
}

/*hideLayer - hide a page, or layer, of the form
@param lyr -string- id of the layer to hide
*/
function hideLayer(lyr) {
   document.getElementById(lyr).style.visibility = 'hidden';
}

/*initNavButton - initialize a single page navigation button
@param buttonId - ID of the button we're initializing
@param toPage - page, or layer, that this button will navigate to
*/
function initNavButton(buttonId, toPage){
	if (!document.getElementById) return false;
	document.getElementById(buttonId).onclick = function() { showLayer(toPage); };
	document.getElementById(buttonId).onkeypress = function() { showLayer(toPage); };
}

/*initAllNavButtons - initialize all navigation buttons with class = className
@param className -string- name of the class for navigation buttons
*/
function initAllNavButtons(className){
	if (!document.getElementsByTagName) return false;
	var navbuttons = Dom.getElementsByClassName('navbutton', 'input');
	
	//initialize each nav button
	for (var i = 0; i < navbuttons.length; i++){
		var page = Math.floor( (i + 1) / 2 ) + 1;
		if (navbuttons[i].value == "Back"){
			initNavButton( navbuttons[i].id, "page" + (page - 1) );
		} else {
			initNavButton( navbuttons[i].id, "page" + (page + 1) );
		}
	}
}



/*variables to use on all pages for holding the number of questions, the sliders and all the answers*/
var numquestions; //int, number of questions
var questSliders; //array for holding all the slider objects
var answers; //array for holding the arrays of answers

/*displayNewValue - displays a value in a div based on the slider thumb's distance from its starting position
@param offsetFromStart -int- how many pixels the thumb is from its starting position
@param divID- string-  the id of the div where the new value should be displayed
@param answersforquestion -array- an array holding the possible values to be displayed
@param scale -int/float-  value determining how many pixels is equal to 1
*/
function displayNewValue(offsetFromStart, divID, answersforquestion, scale)
{
	if(window.opera) offsetFromStart = offsetFromStart - 40;
	document.getElementById(divID).innerHTML = answersforquestion[ Math.round(offsetFromStart * scale) ];
}

/*getCheckedValue - finds which radio button is checked out of a group
@param radioObj -object- a group of radio buttons
@return -if one of the radio buttons is checked, return its value, else return empty string
*/
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}	

/*initHorizSlider - initializes a new Yahoo! slider bar with displayNewValue occuring on a change event and size/increment specified
@param bgDivID -string- the id of the div being used as the slider bg
@param handleDivID -string- the id of the div being used as the slider handle/thumb
@param answerDivID- string- the id of the div being used to show the answers
@param answersforquestions -array- an array holding all of the values you want to display when the slider is slid
@param size -int- the length of the slider in pixels
@param increment -int- how many pixels the handle/thumb should move, the distance between ticks
@return - a YUI slider object with change subscribed to displayNewValue
*/
function initHorizSlider(bgDivID, handleDivID, answerDivID, answersforquestion, size, increment)
{
	var horizSlider = YAHOO.widget.Slider.getHorizSlider(bgDivID, handleDivID, 0, size, increment);
	
	horizSlider.subscribe("change", function(offsetFromStart){ displayNewValue(offsetFromStart, answerDivID, answersforquestion, 1/increment) });

	return horizSlider;
}

/*init - initialize all the sliders on a page
@param questSliders -slider array- array to contain all the sliders
@param answers- array of arrays of answers- contains all the answers to the questions
@param size -int- the size of each slider
note that for this function to work with page, slider background and handle and answer div must have appropriate classes
*/
function init(questSliders, answers, size)
{

	var bgDivIds = new Array(numquestions);
	var handleDivIds = new Array(numquestions);
	var answerDivIds = new Array(numquestions);
	size = size - 40; //correct for the size of the thumb
	
	if (!document.getElementsByTagName) return false;
	var temp = document.getElementsByTagName("div");
	
	var questionCount = 0;
	
	for(var i=0; i < temp.length; i++){
		if( temp[i].className.match("questionWrapper") ){
			if( temp[i + 1].className.match("horizBGDiv") ){
				bgDivIds[questionCount] = temp[i + 1].id
				handleDivIds[questionCount] = temp[i + 2].id
				answerDivIds[questionCount] = temp[i + 3].id
			}
			questionCount++;
		}
	}
	
	for( var i=0; i < questSliders.length; i++ )
	{
		if( typeof(bgDivIds[i]) != "undefined" ){
			//if(answers.length % 2 = 1) then
		
			var increment = Math.floor( size / (answers[i].length - 1) );
			questSliders[i] = initHorizSlider(bgDivIds[i], handleDivIds[i], answerDivIds[i], answers[i], size, increment );
		}
	}



}

/*
Taken from http://simonwillison.net/2004/May/26/addLoadEvent/, thanks to Simon Willison
The addLoadEvent function registers a function with the page's onload event handler
@param func -function- the function to be registered with the onload event
*/
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
