var opera      = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
var msie       = (document.all && !opera) ? true : false;
var ff_new	   = (navigator.userAgent.indexOf("Firefox/3") != -1) ? true : false;
if (msie) {
	var msieold = (window.XMLHttpRequest) ? false : true;
} else {
	var msieold = false;
}

/* fix pro poblikavani obr. pozadi v IE */
if ( msie )
{
	try {
		document.execCommand('BackgroundImageCache', false, true);
	} catch(e) {}
}

kQuest = Class.create();

kQuest.prototype = {
	version : '0.0.1',

	set_value : function(answer_elm, answer_val) {
		if ( answer_elm.tagName.toLowerCase() == "input" ) {
			answer_elm.value = answer_val;
		} else {
			answer_elm.innerHTML = answer_val;
		}	
	},

	toggle : function(event) {
		Event.stop(event);
		if ( this.quest.hasClassName('hide-answers') ) {
			this.quest.removeClassName('hide-answers');

			this.questions = $A($$('.question'));
			var answer = false;
			this.questions.each( function( question ) {
				if ( question.type == "text" ) {
					answer = $(question.id + '-answer');
				} else if ( question.checked ) {
					answer = $(question.name + '-answer');
				} else {
//					console.log(question);
				}

				if ( answer ) {
					this.set_value(answer, question.value);
					answer = false;
				}
			}.bind(this));
		} else {
			this.quest.addClassName('hide-answers');
		}
		return false;
	},

	initialize : function (quest) {
		this.quest = quest;
		this.elm_answers = $('elm-answers');
		this.elm_questions = $('elm-questions');
		Event.observe(this.elm_answers, "click", this.toggle.bindAsEventListener(this));
		Event.observe(this.elm_questions, "click", this.toggle.bindAsEventListener(this));
	}
}

function initkQuest () {
	var quest = $('quest');
	if ( quest ) {
		myKQuest = new kQuest(quest);
	}
}

HideOut = Class.create();

HideOut.prototype = {
	version : '0.0.1',

	toggle : function(e) {
		if (Element.hasClassName(this.parent, 'hideout-hidden')) {
			Element.removeClassName(this.parent, 'hideout-hidden');
		} else {
			Element.addClassName(this.parent, 'hideout-hidden');
		}
		Event.stop(e);
	},

	initialize : function(launcher) {
		this.launcher = launcher;
		var parent    = this.launcher;
		while ((!Element.hasClassName(parent, 'hideout-parent')) && (parent.tagName != 'BODY')) {
			parent = parent.parentNode;
		}
		if (parent) {
			this.parent = parent;
			Event.observe(this.launcher, 'click', this.toggle.bindAsEventListener(this));
		}
	}
}

function initHideOuts() {
	var elements = $A(document.getElementsByClassName('hideout-launcher'));
	elements.each(function(elm) {
		new HideOut(elm);
	});
}

function uncheck(elmName) {
	var elms = document.getElementsByName(elmName);
	if ( elms ) 
	{
		for	( i=0; i<elms.length; i++ )
		{
			elms[i].checked = false;
		}
	}
}



Event.observe(window, "load", initHideOuts);
Event.observe(window, "load", initkQuest );
