/*
 * Copyright (C) 1995-2011 by 4system Polska Sp. z o.o.
 * All Rights Reserved. v4
 */

componentPollSingleAnswer = function(name) {
	this.__componentText = __componentText;
	this.__componentText(name);

	this.waitingTryCount = 0;

	this.resourcesStatus = "online";
	this.resourcesURL = "";

	this.interactions 		= true;
	this.interactionsType 	= "fill-in"; 
};

componentPollSingleAnswer.prototype = new __componentText;
componentPollSingleAnswer.prototype.type = "componentPollSingleAnswer";

componentPollSingleAnswer.prototype.onCreate = function(obj)
{
	this.captureMouseEvents();
	
	this.resourcesStatus = "online";
	this.resourcesURL = resourcesURL;
	if (PageInterface.GetValue("state") != psDesigning && lms && lms.name != "lmsOffline") {
		this.resourcesStatus = lms.resourcesStatus();
		if (this.disableOnlineOfflineMix) this.resourcesURL = resourcesURL;
		else this.resourcesURL = lms.resourcesURL();
	}
		
	this.resourcesURL = unescape(this.resourcesURL);
	
	this.imagesNameArr = [	this.imgChecked, this.imgNotChecked ];
	this.getPreloadImagesArr();
	
	this.pollQuestion = this.initDynamicContent(this.pollQuestion);
	this.choiceValues = this.initDynamicContent(this.choiceValues);
	this.inputTitle = this.initDynamicContent(this.inputTitle);
	
	var arrOpt = this.choiceValues.split("||");
	var arrLength = arrOpt.length;

	var width = this.w - this.borderWidth*2;
	var t = '<table style="width:' + width + 'px; height:100%;" id="polls_radio_tab_' + this.id + '" dir="'+this.dir+'">';
	t += '<tr><td colspan="2" style="font-color:' + this.fontColor + '; font-family:' + this.fontFamily + '; font-size:' + this.fontSize + '">' + this.pollQuestion + '</td></tr>';

	for(var i = 0; i < arrLength; i++) {
		t += '<tr><td width="1%"><img id="' + this.id + '_img_' + i.toString() + '" src="' + this.resourcesURL  + this.imgNotChecked + '" name="' + this.id + '_img_' + i.toString() + '" onclick="this.owner.changeImg(this);"></td><td align='+this.textAlign+'  style="color:' + this.fontColor + '; font-family:' + this.fontFamily + '; font-size:' + this.fontSize + ';">' + arrOpt[i] + '</td></tr>';
	};
	
	if (this.inputVisible) {
		t += '<tr><td colspan="2">';
		t += '<table><tr><td style="color:' + this.fontColor + '; font-family:' + this.fontFamily + '; font-size:' + this.fontSize + ';">';
		t += this.inputTitle + '</td>';
		t += '<td align='+this.textAlign+'><input id="' + this.id + '_openQuestion" style="width:'+this.inputWidth+';"></td></tr></table>';
		t += '</td></tr>';
	}
	t += '</table>';  

  this.setHTML(t);
	
};

componentPollSingleAnswer.prototype.checkAnswer = function() { 
  return(this.parseCheckAnswer(this));
};

componentPollSingleAnswer.prototype.getAnswer = function() {
  var form = document.getElementById( "polls_radio_tab_" + this.id );
  var i, obj;
  var valStr= "";
  var indexArr = this.choiceValues.toString().split("||");

  if (form) {
  	for (i=0; i<indexArr.length; i++) {
  			obj = document.getElementsByName(this.id+"_img_"+i.toString());
  		if ( obj[0].src.match(this.imgChecked) ) {
  			valStr = indexArr[i].toString();
  			break;
  		}
  	}
  }
  
  if (this.inputVisible) {
	obj = document.getElementById(this.id + "_openQuestion");
  	if(obj.value!="") valStr += "||[%Open%]:" + obj.value;
  };

  return(valStr);
};

componentPollSingleAnswer.prototype.setAnswer = function(valStr) 
{
	var arrOpt = this.choiceValues.split("||");
	var arrLength = arrOpt.length;
	var uAns = valStr.split("||");

	for(var i = 0; i < arrLength; i++) {
		if (uAns[0] == arrOpt[i]){
			var obj = document.getElementById(this.id+"_img_"+i.toString());
			obj.src = this.resourcesURL + this.imgChecked;
		};
	};
	if (this.inputVisible) {
		obj = document.getElementById(this.id + "_openQuestion");
		var inpVal = valStr.split("[%Open%]:")[1];
		if (obj && typeof(inpVal) != 'undefined') obj.value = inpVal;
	}
};

componentPollSingleAnswer.prototype.unselectable = function() 
{
	if (typeof(on) == "undefined") on = true;
	if (is.ie || is.opera) {
		this.obj.unselectable = (on) ? "on" : "";
		for (var i=0; i<this.obj.all.length; i++) {
			var u_obj = this.obj.all[i];
			if (u_obj.isComponentElement) {
				u_obj.unselectable(on);
			} else {
				try {
					if (u_obj.owner == this) {
						u_obj.unselectable = (on) ? "on" : "";
					} else {
					}
				} catch (e) {}
			}
		}
	} else if (is.moz) {
		this.obj.style.setProperty("-moz-user-select", "None", "");
	}
	if (this.inputVisible) {
		var input = document.getElementById(this.id + "_openQuestion");
		if (input){
			input.unselectable = "";
		};
	};
};

componentPollSingleAnswer.prototype.parseCheckAnswer = function(obj) {
  if (!obj) return false;
  return true;
};

componentPollSingleAnswer.prototype.onLock = function() {
	var obj;
	for (var i=0; i< this.choiceValues.split("||").length; i++) {
		obj = document.getElementsByName(this.id+"_img_"+i.toString());
		if (obj[0]) obj[0].onclick = "javascript:void(0); return false;";
	}
	if (this.inputVisible) {
		obj = document.getElementById(this.id+"_openQuestion");
		if (obj) obj.disabled = true;
	}  	
	return(true);
};

componentPollSingleAnswer.prototype.onUnLock = function() {
	var obj;
	for (var i=0; i< this.choiceValues.split("||").length; i++) {
		obj = document.getElementsByName(this.id+"_img_"+i.toString());
		if (obj[0]) obj[0].onclick = "this.owner.changeImg(this); return false;";
	}
	return(true); 
};

componentPollSingleAnswer.prototype.onPaint = function(obj)
{
	return true;
};

componentPollSingleAnswer.prototype.changeImg = function(obj) 
{
	if (this.locked) return;
	
	this.cleanOtherFields(obj);
	obj.src = this.resourcesURL + ((obj.src.match(this.imgChecked))?this.imgNotChecked:this.imgChecked);
  	this.answer = obj.id;
};

componentPollSingleAnswer.prototype.cleanOtherFields = function(obj) {
  	var str = obj.name.split("_");
	var amount = this.choiceValues.split("||").length;
  	
	for (var i=0; i < amount; i++) {
		var tmpObj = document.getElementById(this.id+'_img_'+i);
		tmpObj.src = resourcesURL + this.imgNotChecked;
	};
};

componentPollSingleAnswer.prototype.interactionsCorrectResponses = function()
{
	return "";
};

componentPollSingleAnswer.prototype.interactionsStudentResponse = function()
{
	return this.getAnswer();
};

componentPollSingleAnswer.prototype.interactionsCorrectResponsesAsText = function()
{
	return "";
};

componentPollSingleAnswer.prototype.interactionsStudentResponseAsText = function()
{
	return this.getAnswer();
};

componentPollSingleAnswer.prototype.interactionsCorrectResponsesCount = function()
{
	return 1;
};
