
Array.prototype.clone = function() {
  var newObj = (this instanceof Array) ? [] : {};
  for (i in this) {
    if (i == 'clone') continue;
    if (this[i] && typeof this[i] == "object") {
      newObj[i] = this[i].clone();
    } else newObj[i] = this[i]
  } return newObj;
};

var bijoy=unijoy.clone();

bijoy['d']='\u200d'+'\u09BF'; // hrossho i kar
bijoy['c']= '\u200d'+'\u09C7'; // e kar
bijoy['C'] = '\u200d'+'\u09C8'; //Oi Kar

function parsebijoy(evnt){
	// main bijoy parser
	var t = document.getElementById(activeta); // the active text area
	var e = (window.event) ? event.keyCode : evnt.which; // get the keycode

	if (e=='32'||e=='71') //format bijoy when pressed spacebar or dari
	{formatBijoy();}

	if (e=='113')
	{
		//switch the keyboard mode
		if(ctrlPressed){
			switched = !switched;
			//alert("H"+switched);
			return true;
		}
	}

	if (switched) return true;

	if(ctrlPressed)
	{
		// user is pressing control, so leave the parsing
		e=0;
	}

	var char_e = String.fromCharCode(e); // get the character equivalent to this keycode

	if(e==8 || e==32)
	{
		// if space is pressed we have to clear the carry. otherwise there will be some malformed conjunctions
		carry = " ";
		old_len = 1;
		return;
	}

	lastcarry = carry;
	carry += "" + char_e;	 //append the current character pressed to the carry
	
	/* added from sain */
	if(typeof isEnglishOn!='undefined'){
		if(isEnglishOn=="true"){
			if(char_e.length!=0&&e!=0){
			insertAtCursor(char_e);old_len=lastInserted.length;
			}
			return false;
		}
	}
	/* */

	bangla = parsebijoyCarry(carry); // get the combined equivalent
	tempBangla = parsebijoyCarry(char_e); // get the single equivalent

	if (tempBangla == ".." || bangla == "..") //that means it has sibling
	{
		return false;
	}
	
	if (char_e=="g")
	{
		if(carry=="gg")
		{
			// check if it is a plus sign
			insertConjunction('\u09CD' + '\u200c',old_len);
			old_len=1;
			return false;
		}
		//otherwise this is a simple joiner
		insertAtCursor("\u09CD");old_len = 1;
		carry="g";
		return false;
	}

	else if(old_len==0) //first character
	{
		// this is first time someone press a character
		insertConjunction(bangla,1);
		old_len=1;
		return false;

	}

	else if(char_e=="A")
	{
		//process old style ref
		newChar = bijoy['v']+ '\u09CD';
		insertAtCursor(newChar);
		old_len = 1;
		return false;
		
	}


	else if((bangla == "" && tempBangla !="")) //that means it has no joint equivalent
	{

		// there is no joint equivalent - so show the single equivalent.
		bangla = tempBangla;
		if (bangla=="")
		{
			// there is no available equivalent - leave as is
			carry ="";
			return;
		}

		else
		{
			// found one equivalent
			carry = char_e;
			insertAtCursor(bangla);
			old_len = bangla.length;
			return false;
		}
	}
	else if(bangla!="")//joint equivalent found
	{
		// we have found some joint equivalent process it

		insertConjunction(bangla, old_len);
		old_len = bangla.length;
		return false;
	}
}

function parsebijoyCarry(code)
{
	//this function just returns a bangla equivalent for a given keystroke
	//or a conjunction
	//just read the array - if found then return the bangla eq.
	//otherwise return a null value
	if (!bijoy[code])  //Oh my god :-( no bangla equivalent for this keystroke

	{
		return ''; //return a null value
	}
	else
	{
		return ( bijoy[code]);  //voila - we've found bangla equivalent
	}

}

function formatBijoy()
{
	activeTextAreaInstance = document.getElementById(activeta);
	text = activeTextAreaInstance.value;

	malformedKars = [bijoy['c'],bijoy['d'],bijoy['C']];
	correctKars = ['\u09C7','\u09BF','\u09C8'];
	for($j=0;$j<3;$j++){
		var malformedKar=malformedKars[$j]
		while(karPos = text.indexOf(malformedKar)){
			if (karPos==-1) break;
			//alert(karPos);
			start = karPos+2;
			end = karPos+9;
			for($i=start;$i<end;$i++)
			{
				if(text.substr($i,1)=="\u09CD") continue
				else if(text.substr(($i+1),1)=="\u09CD") continue
				else
				{
					//replace
					newtext = text.substr(0,karPos)+text.substr(start,(($i+1)-start))+correctKars[$j]+text.substr(($i+1)); //yak, i wrote it??? f**k!! i cant read what i've written!!!
					text=newtext;
					break;
				}
			}
		}
	}
	activeTextAreaInstance.value = text;
}

function makeBijoyEditor(textAreaId)
{
	activeTextAreaInstance = document.getElementById(textAreaId);
	activeTextAreaInstance.onkeypress = parsebijoy; 
	activeTextAreaInstance.onkeydown = checkKeyDown; 
	activeTextAreaInstance.onkeyup = checkKeyUp;
	activeTextAreaInstance.onblur = formatBijoy;
	activeTextAreaInstance.onfocus = function(){activeta=textAreaId;};
}




