var simpleSwitcher = $.inherit(
	{
		__constructor: function(options){

			options = $.extend({}, this.self.defaults, options ? options : {});

			this.jContainer = $(options.containerClass);
			this.jSwitcher = this.jContainer.find(options.switcherClass);
			this.jTextBlock = this.jContainer.find(options.textBlockClass);

			this.jSwitcher.click(this.doSwitch.scope(this));
		},

		doSwitch: function(event)
		{
			$(event.target).toggleClass('selected');
			this.jTextBlock.toggle();
		}
	},

	{
		defaults: {
			containerClass: '.switched-block',
			switcherClass: '.pseudo.switcher',
			textBlockClass: '.reference'
		}
	}
);


var Quiz = $.inherit(
		{
			__constructor: function(oOptions) {
				this.jResultContainer = oOptions.jResultContainer || $(".quiz-result");
				this.jFormContainer = oOptions.jFormContainer || $(".quiz-form");
				this.jForm = oOptions.jForm || $("#quiz-form");
				this.iQuizId = this.jForm.find("#quiz_id").val();
				this.iQuestionId = this.jForm.find("#question_id").val();
				
				if(this.jForm && this.iQuizId && this.iQuestionId){
					this.init();
				} else {
					this.showResult();
				}
			},
			init: function(){
				// если есть форма для опроса
				// проверяем учавствовал ли пользователь в этом опросе
				var sVoted = $.cookie("qvoted") || "";
				var aVoted = sVoted.split(",");
				if(aVoted.indexOf(""+this.iQuizId) < 0){
					// пользователь еще не отвечал на опрос, поэтому показываем ему форму
					var oThis = this;
					this.jForm.submit(function(e){ oThis.vote(e); return false; })
					this.showForm();
				} else {
					// пользователь уже учавствовал
					this.showResult();
				}
			},
			vote: function(e){
				var q = this.iQuestionId;
				var aVal = [];
				var jOptions = $("input[name=question"+q+"_a]:checked",this.jForm);
				jOptions.each(function(i,j){ aVal[i] = $(j).val(); });
				var hData = {};
				hData.qid = this.iQuizId;
				hData.q = this.iQuestionId;
				hData['q'+q+'_a'] = aVal;

				var oThis = this;
				$.get(
					"/ajax/quiz/",
					hData,
					function(status){
						oThis.setVoted();
						oThis.showResult();
					}
				);
				this.showResult();
			},

			setVoted: function(){
				if(this.iQuizId){
					$.cookie(
						"qvoted",
						$.cookie("qvoted") + ","  + this.iQuizId,
						{
							expires: new Date(2015, 0, 1),
							path: '/'
						}
					);
				}
			},
			showForm: function(){
				this.jFormContainer.show();
				this.jResultContainer.hide();
			},
			showResult: function(){
				this.jFormContainer.hide();
				this.jResultContainer.show();
			}

		}
	);

