/* 
 Copyright CodeCogs 2007-2008
 Written by Will Bateman.
	
 GNU General Public License Agreement
 Copyright (C) 2004-2007 CodeCogs, Zyba Ltd, Broadwood, Holford, TA5 1DU, England.
 This program is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by CodeCogs
 (www.codecogs.com/cart-3.htm). You must retain a copy of this licence in all copies.
 This program is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 PARTICULAR PURPOSE. See the GNU General Public License for more details.
*/

var changed=false;
var elementId; // Aloca id do textarea

// Seta elementId para id do textarea seguinte
function setElementId(element)
{
    elementId=element.next().id;
}

// Clears the main editor window
function cleartext()
{ 
 var id=document.getElementById(elementId);
 id.value = "";
 id.focus(); 
 changed=false;
 document.getElementById('copybutton').className='greybutton'; 
 document.getElementById('renderbutton').className='greybutton';
 document.getElementById('clipboardbutton').className='greybutton';
}

// Tries to inserts text at the cursor position of text area
//  wind = document                <- when inserting text into the current equation editor box  
//  wind = window.opener.document  <- when inserting text into a parent window box
function addText(wind, textbox, txt) 
{
	myField = wind.getElementById(textbox);
  // IE 
  if (wind.selection) 
  {
    myField.focus();
    sel = wind.selection.createRange();
    sel.text = txt;
  }
  // MOZILLA
  else 
  {
		var scrolly=myField.scrollTop;
	  if (myField.selectionStart || myField.selectionStart == '0') 
    {
      var startPos = myField.selectionStart;
      var endPos = myField.selectionEnd;
			var cursorPos = startPos + txt.length;
      myField.value = myField.value.substring(0, startPos) + txt 
					+ myField.value.substring(endPos, myField.value.length);
			pos = txt.length + endPos - startPos;

			myField.selectionStart = cursorPos;
			myField.selectionEnd = cursorPos;
	
			myField.focus();
			myField.setSelectionRange(startPos + pos,startPos + pos);	
    } 
    else 
      myField.value += txt;
			
	  myField.scrollTop=scrolly;
  }
}

function insertText( txt, pos)
{
	// pos = optional parameter defining where in inserted text to put the caret
	// if undefined put at end of inserted text
	// if pos=1000 then using style options and move to just before final }
	// startPos = final position of caret in complete text
	if (pos==1000) {pos=txt.length-1};
	if (pos==undefined) { txt+=' '; pos=txt.length;}; // always insert a space after
	
	// my textarea is called latex_formula
	myField = document.getElementById(elementId);
	if (document.selection) 	{
		// IE
		myField.focus();
		var sel = document.selection.createRange();
		// find current caret position
			
		var i = myField.value.length+1; 
		theCaret = sel.duplicate(); 
		while (theCaret.parentElement()==myField 
		&& theCaret.move("character",1)==1) --i; 
	
		// take account of line feeds
		var startPos = i - myField.value.split('\n').length + 1 ; 
	
		if ((txt.substring(1,5) == "left" || txt.substring(pos-1,pos)=='{') && sel.text.length)	{ 
			// allow highlighted text to be bracketed
			if(txt.substring(1,5) == "left")
			  ins_point=7;
			else
			  ins_point=pos;
				
			pos = txt.length + sel.text.length + 1;
			sel.text = txt.substring(0,ins_point) + sel.text + txt.substr(ins_point);	     
		} else {
			sel.text = txt;
		}
		// put caret in correct position to start editing
		var range = myField.createTextRange();
		range.collapse(true);
		range.moveEnd('character', startPos + pos);
		range.moveStart('character', startPos + pos);
		range.select();
	}
	else
	{
		// MOZILLA
		if (myField.selectionStart || myField.selectionStart == '0')	{
			var startPos = myField.selectionStart;
			var endPos = myField.selectionEnd;
			var cursorPos = startPos + txt.length;
			if ((txt.substring(1,5) == "left" || txt.substr(pos-1,1)=='{') && endPos > startPos)	{ 
				// allow highlighted text to be bracketed
				
				if(txt.substring(1,5) == "left")
				  ins_point=7;
				else
				  ins_point=pos;
				
				pos = txt.length + endPos - startPos + 1;
				txt = txt.substring(0,ins_point) + myField.value.substring(startPos, endPos) + txt.substr(ins_point);			
			}
			myField.value = myField.value.substring(0, startPos) + txt 
							+ myField.value.substring(endPos, myField.value.length);
		
			myField.selectionStart = cursorPos;
			myField.selectionEnd = cursorPos;
					
						// put caret in correct position to start editing
			myField.focus();
			myField.setSelectionRange(startPos + pos,startPos + pos);	
		}
		else	
			myField.value += txt;
	}
	myField.focus();
}

/* ----------- Handle rendering example equation --------------------------- */

// Returns the complete string that describes this particular equation with font sizes etc.
function getEquationStr()
{
	var val=document.getElementById(elementId).value;	
	val=val.replace(/^\s+|\s+$/g,"");
				
	var size = document.getElementById('fontsize');
	if(size)
	{
		var txt=size.options[size.selectedIndex].value;
		if(txt!='')
  		val=txt+' '+val; //.replace(/\+/g,"&plus;");
	}
	
	if(document.getElementById('compressed').checked) 
		val='\\inline '+val; 
	
	return val;
}



/* Turns off the wait message, once and equation is loaded */
var initmessage=true;
updateparent=null;
function processEquationChange() 
{
	if(initmessage) 
	  initmessage=false;
	else
	{
    var div = document.getElementById('equationcomment');
	  div.innerHTML = "";	
	}
	if(updateparent!=null) { updateparent(); }
 	updateparent=null;
}

// Triggers the rendering of the equations within the iframe
function renderEqn(callback)
{
	/* Display wait message */
	if(changed)
	{
		var div = document.getElementById('equationcomment');
		div.innerHTML = "Rendering Equation <img src=\"images/wait.gif\" width=\"13\" height =\"13\" border=\"0\" />";	
			
		updateparent=callback;
			
		/* Set render button to grey, since we're now rendering.. */
		document.getElementById('renderbutton').className='greybutton';
	
		var val=document.getElementById(elementId).value;
		
		/* Add to history */
		var sel=document.getElementById('history');
		var j=sel.length;
		sel.length=j+1;
		sel.options[j].text=val.substr(0,15);
		sel.options[j].value=val;
		sel.options[j].title=val;
		
		/* This sytem works by changing the image source to the new LaTeX equation. A seperate file, renders from a url the image, which is then presented here. The image on the main page already has
		an onload event which calls processEquationChange, which merely turns off the wait message. */
		var img = document.getElementById('equationview');
		val=getEquationStr();
		img.src='http://www.codecogs.com/eq.latex?' + val;
		changed=false;
		
		//! Show equation code in yellow box at bottom
		document.getElementById('eqcode').innerHTML='The HTML code to embed this equation directly onto your page is given below:';
		document.getElementById('eqcoderaw').value='<a href="http://www.codecogs.com"><img src="http://www.codecogs.com/eq.latex?' + val.replace(/\s/g,"&space;")+'" alt="'+val+'" border="0"/></a>';
	}
}


/* The following code adds a favorite to the favorite drop down. 
To achieve this, we must update the users cookies */

function addfavorite(dropdown)
{
	text=document.getElementById(elementId).value;
	var sel=document.getElementById(dropdown);
	var num=sel.selectedIndex
  if(num==0 || !confirm("Click Yes to replace the current equation or 'Cancel' to add a new equation"))
	{
		var name = prompt('Please enter a short name for this equation:', '')
		if(name==null || name=='') name=text.substr(0,15); 
		/* Add to favorite */
		var j=sel.length;
		sel.length=j+1;
		sel.options[j].text=name;
		sel.options[j].value=text;
		sel.options[j].title=text;
		/* Make sure cookie name is unique to website */
		document.cookie = "eq_"+escape(name.replace(/\+/g,"&plus;").replace(/\s/g,"&space;"))+"="+escape(text.replace(/\+/g,"&plus;"))+"; path=/";
	}
	else
	{
	  sel.options[num].value=text;
	  sel.options[num].title=text;
	  /* Make sure cookie name is unique to website */
	  document.cookie = "eq_"+escape(name.replace(/\+/g,"&plus;").replace(/\s/g,"&space;"))+"="+escape(text.replace(/\+/g,"&plus;"))+"; path=/";
	}
}

function deletefavorite(dropdown)
{
	var sel=document.getElementById(dropdown);
	var num=sel.selectedIndex
	name=sel.options[num].text;
	
	var mydate = new Date(); 
  mydate.setTime(mydate.getTime() - 1); 
	document.cookie = "eq_"+escape(name.replace(/\+/g,"&plus;").replace(/\s/g,"&space;"))+"=; expires="+mydate.toGMTString()+"; path=/";
	sel.options[num]=null;
}


/* Help to generate a Matrix */
// generate a set of aligned equations - thornahawk
// isNumbered: switches between numbered and not numbered equations
function makeEquationsMatrix(type, isNumbered, isConditional)
{
	if (isNumbered==undefined) isNumbered=false;
	if (isConditional==undefined) isNumbered=false;

  var eqns="\\begin{"+type+((isNumbered)?"":"*")+"}";
	var eqi="\n &"+((isNumbered)?" ":"= ")+((isConditional)?"\\text{ if } x=  ":"");
	var eqEnd="\n\\end{"+type+((isNumbered)?"":"*")+"}";
	var i=0;

	var dim = prompt('Enter the number of lines:', '');

	if (dim != '' && dim != null)	{
		n=parseInt(dim);
		if (!isNaN(n)) {
			for (i=1;i<=n-1;i++)
				eqns=eqns+(eqi+"\\\\ ");
			eqns=(eqns+eqi)+eqEnd;
			
			insertText(eqns,type.length+((isNumbered)?0:1)+9);
		}
	}
}

// generate an array of specified dimensions - thornahawk
// type: sets the type of array, e.g. pmatrix
function makeArrayMatrix( type, start, end )
{
	var matr=start+'\\begin{'+type+'matrix}';
	var row="\n";
	var mend="\n\\end{"+type+"matrix}"+end;
	var i=0

	var dim = prompt('Enter the array dimensions separated by a comma (e.g. "2,3" for 2 rows and 3 columns):', '')

	if (dim!='' && dim!=null)	
	{
		dim=dim.split(',');
		m=parseInt(dim[0]);
		n=parseInt(dim[1]);
		
		if (!isNaN(m) && !isNaN(n)) 
		{
			for (i=2;i<=n;i++)
				row=row+' & ';
			
			for (i=1;i<=m-1;i++)
				matr=matr+row+'\\\\ ';
		
			matr=matr+row+mend;
			insertText(matr,type.length+start.length+15);
		}
	}
}