/**
 * @author Kristian Rasmussen
 */
window.addEvent('domready', function() {
	if($('ask-question-preview') != null){
		$('ask-question-preview').addEvent('click', function(){
			load('loader.php/?action=preview-question&cat=' + $('ask-question-cat').getProperty('value') + '&question=' + $('ask-question-input').getProperty('value') + '&desc=' + $('ask-question-desc').getProperty('value'), 
				'question-preview-container');
		});
	}
	
	if($('ask-question-form') != null){
		$('ask-question-form').addEvent('submit', function(e) {
			new Event(e).stop();
		})
	}
	
	if($('idea-container') != null){
		addSlideEffectToQuestions();	
	}
	
	if($('category-list-container') != null){
		addCategoryLinksAction();
		addQuestionLinksAction();		
	}
	
});

function addQuestionLinksAction(){
	var elements = $('idea-container').getElements('a');

	$each(elements, function(element){
    	if(element.hasClass('ask-question-link-open')){
			var linkId = element.getProperty('id');
			var id = linkId.split("-");
			var singleId = id[4];
			$('ask-question-link-open2-' + singleId).addEvent('click', function(e){
				load('loader.php?action=answer&question-id=' + singleId, 'idea-container','answer-question');
			});
			$('ask-question-link-open-' + singleId).addEvent('click', function(e){
				load('loader.php?action=answer&question-id=' + singleId, 'idea-container','answer-question');
			});
		}
	}); 
}

function addCategoryLinksAction(){
	var elements = $('category-list').getElements('li');
	
	$each(elements, function(element){
		$linkId = element.getProperty('id');
		var id = $linkId.split("-");
		var singleId = id[2];
		$('idea-category-' + singleId).addEvent('click', function(e){
			load('loader.php/?action=get-category&category-id=' + singleId, 'idea-container','view-questions');
		});
	}); 
}

function addSlideEffectToQuestions(){
	var elements = $('idea-container').getElements('div');

	$each(elements, function(element){
    	if(element.hasClass('ask-question-slide')){
			$slideId = element.getProperty('id');
			var id = $slideId.split("-");
			
			addSlideEffect(id[3], true);
		}
	}); 
}

function addSlideEffect(id, hidden){
	var mySlide = new Fx.Slide('ask-question-slide-' + id);
	$('ask-question-link-' +id).addEvent('click', function(e){
		e = new Event(e);
		mySlide.toggle();
		e.stop();
	});
	if (hidden) {
		mySlide.hide();
	}
}

function checkAnswer(form){
//	var question = $('answer-question-input').getProperty('value').trim();
//	var errorText = $('error-box-text');
//	
//	if(question.length < 5){
//		errorText.setText('Answer must be 5+ characters');
//		burnError(true);
//		return false;
//	}
	
	form.submit();
}

function checkForQuestion(form){
	var question = $('ask-question-input').getProperty('value').trim();
	var errorText = $('error-box-text');
	
	if(question.length < 10){
		errorText.setText('Question must be 10+ characters');
		burnError(true);
		return false;
	}
	
	if(question.length > 300){
		errorText.setText("Question cannot be over 300 characters");
		burnError(true);
		return false;
	}
	
	if(question.substring(question.length-1) != '?'){
		errorText.setText("Question must end with '?'");
		burnError(true);
		return false;
	}

	var desc = $('ask-question-desc').getProperty('value').trim();
	if(desc.length > 1000){
		errorText.setText("Description cannot be over 1000 characters");
		burnError(true);
		return false;
	}
	form.submit();
}

function replaceNewLine(fieldId) {
	  var _s = $(fieldId)
	  var _t = "<br>";
	  for (_i=0;_i<_s.length;_i++) {
	    if (_s.charCodeAt(_i) != 13 && _s.charCodeAt(_i) != 10)
		  _t += _s.charAt(_i);
	  }
	  document.exf1.ta.value=_t;
}
