// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
// horizontal slider control with preset values

document.observe ("dom:loaded", function() {
if 	($('difficulty_track')) {
	new Control.Slider('difficulty_handle', 'difficulty_track', {
				range: $R(1, 10),
				sliderValue: 5, 
				onSlide: function(v){ $('diff_rating').innerHTML = difficulty_word(v) + ' (<strong>' + Math.round(v) + '</strong>)'; },
				onChange: function(v) {$('diff_rating').innerHTML = difficulty_word(v) + ' (<strong>' + Math.round(v) + '</strong>)'; updateValues(v);}
			});
	}

		});

function checkAnswer() {
  if ($('checkanswerinput').value == $('hidden_answer').innerHTML.strip())
  {
  	$('check_puzzle_answer_result').update("Correct! <img style='display:inline;' src='/images/good.png'>").show();
  	 toggleAnswerDiv('true');
	 }
  else
  {
  	$('check_puzzle_answer_result').update("Incorrect. <img style='display:inline;' src='/images/error.png'>").show();
  	$('checkanswerinput').value = '';
  }
} 


			
function difficulty_word(rating) {
		if (rating < 2)
    	{  return "Very easy"; }
    else if (rating >= 2 && rating < 4)
    	  {return "Fairly Easy";}
    else if (rating >= 4 && rating < 6)
    	  {return "Moderate";}
	  else if (rating >= 6 && rating < 8)
    	  {return "Challenging";}
	  else if (rating >= 8 && rating < 9)
	  		{return "Insane";}
	  else if (rating >= 9 )
	 	  {return "Near Impossible";}
    
}
function updateValues(rating) {
	if($('difficulty_rating')) {
			$('difficulty_rating').value = Math.round(rating);	
	}
}
function toggle_explanation () {
	if ($('explanation_needed').checked)
	{
		$('puzzle_explanation').setStyle({height:'5px', border: '1px solid #ccc'}).writeAttribute('disabled', 'disabled');
		$('puzzle_explanation').value='';	
	}
	else
	{
		$('puzzle_explanation').setStyle({height:'', border: ''}).enable();	
	}
	
}
function toggleExplanationDiv () {
	Element.toggle('hidden_explanation');
	if ($('toggle_explanation_link').innerHTML == 'Show')
	{
		$('toggle_explanation_link').innerHTML = 'Hide';
			
	}
	else
	{
		$('toggle_explanation_link').innerHTML = 'Show';
	}
}
function toggleAnswerDiv (forceopen) {
		if ($('toggle_answer_link').innerHTML == 'Show' || forceopen == 'true')
	{
		$('toggle_answer_link').innerHTML = 'Hide';
		Element.show('hidden_answer');
		if ($('checkanswerinput')) {
			Element.hide('checkanswerinput');
			Element.hide('checkanswersubmit');		
		}
		
	}
	
	else
	{
		Element.hide('hidden_answer');
		$('toggle_answer_link').innerHTML = 'Show';
		if ($('checkanswerinput')) {
			$('checkanswerinput').value = '';
			Element.show('checkanswerinput');
			Element.show('checkanswersubmit');
			$('check_puzzle_answer_result').update('');
		}
	}
}
function ToggleComment(link, commentbody)
{
	$(commentbody).toggle();
	if ($(commentbody).visible())
	{
		link.innerHTML = 'hide';
	}
	else
	{
		link.innerHTML = 'show';
	}
}
function is_int(input){
    return (typeof(input)=='number' && parseInt(input, 10) == input);
  }
