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

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

	this.elementHeight = 0;

	this.spaceBetweenImages = 0;
	this.imagesSpace = 0;

	this.images = new Array();
	this.inputs = new Array();
	
	this.imagesArr = new Array();
};

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

componentNumberImages.prototype.onBaseCreate = function(obj) {
	this.captureMouseEvents();
};

componentNumberImages.prototype.onCreate = function(obj)
{
	if ( PageInterface.GetValue("state") != psDesigning && lms && this.disableOnlineOfflineMix != false )
	{
		this.resourcesStatus 	= lms.resourcesStatus();
		this.resourcesURL 		= lms.resourcesURL();
	} else this.resourcesURL = resourcesURL;

	var textHeight = this.getTextHeight(this.text)+20;
	var tempX = this.x + 3;
	var tempY = this.y + textHeight;

	this.imagesArr = this.content.split('||');
	var randomIndexes = this.randomElements(this.imagesArr);

	var imagesWidthHeight = this.getImageWidthHeight(this.imagesArr);

	var lastX = this.x;

	this.imagesSpace = this.w/this.imagesArr.length;

	for(var i=0; i < this.imagesArr.length; i++){
		var imageX = lastX + this.imagesSpace/2 - this.imageW/2;
		var inputX = lastX + this.imagesSpace/2 - 12;

		var image = new __componentImage;
		image.myParent = this;
		image.id = this.id+'_image_'+i;
		image.x = imageX;
		image.y = tempY;
		image.w = this.imageW;
		image.h = this.imageH;
		image.src = this.imagesArr[i];
		
		page.addChild(image);
		this.images.push(image);

		var input = new componentInput;
		input.parentText = this;
		input.id = this.id+'_Inp_'+i;
		input.x = inputX;
		input.y = this.y + this.h - 30;
		input.w = 25;
		input.h = 25;
		input.caseSensitive = this.caseSensitive;
		input.fieldType = 'text';
		input.inputBgColor = '#ffffff';
		input.borderColor = '#000000';
		input.fontFamily = 'Arial';
		input.fontSize = 2;
		input.fontColor = '#000000';
		input.textAlign = 'center';		
		input.maxLength = 2;
		input._setHTMLDef = this.setInputHTMLDef;
		input.getAnswer = this.__getAnswer;
		input.checkAnswer = this.__checkAnswer;
		input.caseSensitive = false;
		
		var inputComp = page.addChild(input);
		this.inputs.push( inputComp );
		
		inputComp.correctAnswer = (eval(parseInt(randomIndexes[i])+1)).toString();
		inputComp.showCorrectAnswerIndicator = this.showCorrectAnswerIndicator;
		inputComp.showAnswerIndicatorAnyTime = this.showAnswerIndicatorAnyTime;
		inputComp.correctColor = this.correctColor;
		inputComp.incorrectColor = this.incorrectColor;
		
		lastX += this.imagesSpace;
	};
};

componentNumberImages.prototype.onRepaint = function() 
{
	//po to aby nie zmieniala sie czcionka pytania po nacisnieci na OK
	return;
}

componentNumberImages.prototype.get_random = function(from, to) {
    var ranNum = Math.floor( Math.random() * (to-from+1) ) + from;
    return ranNum;
}

componentNumberImages.prototype.randomElements = function(arr) {
	var indexes = new Array();
	var sizes = new Array();
	
	for (var i=0; i < arr.length; i++) {
		indexes.push(i);
		sizes.push(arr[i]);
	};
	var randomIndexes = new Array();
	while( indexes.length != 0 ) {
		randomIndexes.push( indexes.splice( this.get_random(0, indexes.length-1),1 ) );
	};
	for (var i=0; i < arr.length; i++) {
		arr[i] = sizes[ randomIndexes[i] ];
	};
	this.imagesArr = arr;
	return randomIndexes;
};

componentNumberImages.prototype.setInputHTMLDef = function(valStr) {
	valStr = (valStr ? valStr : "");
	
	var t = '<form name="' + this.id + 'Form" id="' + this.id + 'Form">';
	t += '<input id="' + this.id + '_contentObj" name="' + this.id + 'Input" type="' + this.fieldType + '" value="' + valStr + '" ';
	if (this.maxLength != -1) { 
		t += 'maxlength="' + this.maxLength + '" ';
	};
	if (is.ns4) {
		var cols;
		if (this.w == 0) cols = 0;
		else {
			if (this.w < 25) cols = 1;
			else cols = 1 + (parseInt((this.w - 18) / 8));
		}
		t += ' size="' + cols + '"';
	};
	t += this._getStyleDef();
	
	t += '>';
	t += '</form>';
	this.setHTML(t);
};

componentNumberImages.prototype.getTextHeight = function(text)
{
	var sizeDiv = '<span id="'+this.id+'sizeDiv" style="width: '+this.w+'; visibility: hidden; position: absolute; font-family:'
				+ this.fontFamily+ '; font-size:'+this.getFontSize(this.fontSize, 'px')+'px; color:' + this.fontColor
				+ ';  left:0px; top:0px; border: solid #000000 0px;">'+text+'</span>';

	var obj = document.getElementById(this.id);
	obj.innerHTML += sizeDiv;
	obj.style.lineHeight = this.lineHeight;
	var elem = document.getElementById(this.id+"sizeDiv");

	return elem.offsetHeight;
};

componentNumberImages.prototype.__getAnswer = function()
{
	var input = document.getElementById(this.id+"_contentObj");
	if (input) return input.value;
		else return '';
};

componentNumberImages.prototype.__checkAnswer = function() {
	var splitStr = "||";
	var actAnswer = this.getAnswer().toString();
	var ans = "";
	var result = false;
	var arrCorrAns = new Array();
	arrCorrAns = this.correctAnswer.toString().split(splitStr);
	
	if (!this.caseSensitive) {
		actAnswer = actAnswer.toLowerCase();
	};
	var input = document.getElementById(this.id+"_contentObj");
	
	for (var i = 0; i < arrCorrAns.length; i++) {
		ans = (this.caseSensitive) ? arrCorrAns[i]:arrCorrAns[i].toLowerCase();
		
		if (actAnswer == ans){
			result = true;
			if (this.showAnswerIndicatorAnyTime && this.showCorrectAnswerIndicator){
				if (input) input.style.background = this.correctColor;
			};
		}else{
			if (this.showAnswerIndicatorAnyTime && this.showCorrectAnswerIndicator){
				if (input) input.style.background = this.incorrectColor;
		};
		}
	};
	return result;
};

componentNumberImages.prototype.checkIsCorrect = function(arr, ind)
{
	for (var i=0; i < arr.length; i++){
		if (ind == arr[i]) return 1;
	};
	return 0;
};

componentNumberImages.prototype.getFontSize = function(value, format)
{
	var fontSizesPX = new Array(0,10,13,16,18,24,32,48);
	var fontSizesPT = new Array(0, 8,10,12,14,18,24,36);
	var pxSize = fontSizesPT[parseInt(value)];
	if (format == "px") {
		pxSize = fontSizesPX[parseInt(value)];
	}
	return pxSize;
};

componentNumberImages.prototype.getImageWidthHeight = function(arr)
{
	var newIng = new Image();
	var maxWidth = 0;
	var maxHeight = 0;

	for (var i=0; i < arr.length; i++){
		newIng.src = this.resourcesURL + arr[i];
		if (newIng.width > maxWidth) maxWidth = newIng.width;
		if (newIng.height > maxHeight) maxHeight = newIng.height;
	};

	return [maxWidth, maxHeight];
}

componentNumberImages.prototype.onShow = function()
{
	if (PageInterface.GetValue("state") != psDesigning) {
		for (var i=0; i < this.images.length; i++) {
			this.images[i].show();
			this.inputs[i].show();
		};
	};
};


