/**
* Assign the view handler
*/

viewHandler = Challenges;

/**
* Creates a new object with methods used by the Challenges page
*
* @author				Matt Gifford
* @copyright			2008 Timeshifting Interactive Limited
*/
function Challenges()
	{
	// Step 1. Define Properties

	var _instance = this;
	this.selectedArea = 'none';



	// Step 2. Define Public Methods

	/**
	* Sets up the initial page state and event handlers
	*/
	this.init = function()
		{
		// Call generic page init method
		this.base.init.call(this);

		// Set the inital state
		var initialArea = 'Face';
		try
			{
			// Check for initial area get parameter on the url
			var matches = window.location.search.toString().match(/area=(Face|Eyes|Skin|Nose|Lips|Throat|Ears)/);
			var initialArea = matches[1];
			}
		catch (err)
			{
			}
		this.selectChallenge(initialArea);

		// Make the sidebar menu visible
		document.getElementById('challengeTreatments').className = '';
		}


	/**
	* Selects the area and updates problem list.
	*
	* @param		area			the area to see problems for
	* @return		void
	*/
	this.selectChallenge = function(area)
		{
		// Reset the area buttons
		this.resetAreaButtons();

		// Update areas button
		document.getElementById('challengesSelector' + area).className = 'active';

		// Update Problems List
		document.getElementById('challengesProblems').style.visibility = 'visible';
		this.resetProblems();
		this.displayProblems(area);
		}


	/**
	* Resets the area buttons on the people graphic
	*/
	this.resetAreaButtons = function()
		{
		var anchors = document.getElementById('challengesSelector').getElementsByTagName('a');
		for (var x = 0; x < anchors.length; x++)
			{
			anchors[x].className = 'inactive';
			}
		}

	
	/**
	* Displays the problems for the selected gender and area
	*
	* @param		area			the area to display problems for
	*/
	this.displayProblems = function(area)
		{
		// 1. Display the selected introduction
		document.getElementById('challengesProblems' + area).style.display = 'block';

		// 2. Update problems list
		var problems = document.getElementById('challengeTreatments').getElementsByTagName('li');
		var matchArea = ' ' + area.toLowerCase() + ' ';

		for (var x = 0; x < problems.length; x++)
			{
			// Check if problem matches selected gender and area
			var className = ' ' + problems[x].className + ' ';
			if (className.indexOf(matchArea) != -1)
				{
				problems[x].style.display = 'list-item';
				}
			else
				{
				problems[x].style.display = 'none';
				}
			}
		}


	/**
	* Reset the problems list and displays the introductory text
	*/
	this.resetProblems = function()
		{
		// 1. Hide the introduction
		var divs = document.getElementById('challengesProblems').getElementsByTagName('div');
		for (var x = 0; x < divs.length; x++)
			{
			divs[x].style.display = 'none';
			}

		// 2. Hide the problems
		var problems = document.getElementById('challengesProblems').getElementsByTagName('li');
		for (var x = 0; x < problems.length; x++)
			{
			problems[x].style.display = 'none';
			}
		}
	}

