/* $Rev: 2295 $ $Date: 2008-10-28 17:12:09 -0400 (Tue, 28 Oct 2008) $ $Author: valentin $ */
/**
 *  Displays the Geogebra object
 */
function geogebra_display ( post_id, nr, object, file )
{
	var div = $jq(object).parent().parent();
	if ( ie )
	{
		div.html('<div id="dp' + post_id + '-geogebra' + nr + '"><object id="p' + post_id + '-geogebra' + nr + '" style="width: 550px; height: 400px;" classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"><param name="archive" value="includes/geogebra/geogebra.jar" /><param name="java_code" value="geogebra.GeoGebraApplet" /><param name="java_codebase" value="." /><param name="filename" value="' + file + '" /><param name="framePossible" value="false" /></object></div>');
	}
	else
	{
	    var applet = '<object id="p' + post_id + '-geogebra' + nr + '" style="width: 550px; height: 400px;" type="application/x-java-applet" archive="includes/geogebra/geogebra.jar" ><param name="archive" value="includes/geogebra/geogebra.jar" /><param name="java_code" value="geogebra.GeoGebraApplet" /><param name="java_codebase" value="." /><param name="filename" value="' + file + '" /><param name="framePossible" value="false" /></object>';
	    div.html('<div id="dp' + post_id + '-geogebra' + nr + '">' + applet + '</div>');
	}
    var controls = '<div class="geogebra-c"><input type="button" class="liteoption" value="Reset picture" onclick="geogebra_reset(\'p' + post_id + '-geogebra' + nr +'\');" /><input type="button" class="liteoption" value="Hide picture" onclick="geogebra_hide(' + post_id + ', ' + nr + ', this);" /></div>';
    div.append(controls);
}

/**
 *  Hides the applet
 */
function geogebra_hide( post_id, nr, object )
{
	$jq('#dp' + post_id + '-geogebra' + nr).hide();
    $jq(object).parent().html('<input type="button" class="mainoption" value="Click to view applet" onclick="geogebra_show(' + post_id + ', ' + nr + ', this);" />');
}

/**
 *  Shows the applet
 */
function geogebra_show( post_id, nr, object )
{
	var div = $jq(object).parent().parent();
	// applet container
	$jq('#dp' + post_id + '-geogebra' + nr).show();
	
	$jq(object).parent().remove();
	div.append('<div class="geogebra-c"><input type="button" class="liteoption" value="Reset picture" onclick="geogebra_reset(\'p' + post_id + '-geogebra' + nr +'\');" /><input type="button" class="liteoption" value="Hide picture" onclick="geogebra_hide(' + post_id + ', ' + nr + ', this);" /></div>');
}


/**
 *  Insert the picture in the textarea; requires an AJAX method
 *  The Object tag needs to have the object# specified apparently
 */
function geogebra_insert( prefix, in_wiki, object )
{
    var xml_text = $jq('object#' + prefix )[0].getXML();
	
	// need the ajax call here
	$jq.post(
		'includes/ajax/ajax.php', {
			'class' : 'geogebra',
			'subdir' : 'geogebra',

			'xml_text' : xml_text,
			'in_wiki' : in_wiki
		},
		function(data){
            if ( data.msg != undefined )
			{
			    alert(data.msg);
			    return false;
			}
		    if ( confirm('Click OK if you want to close the current window, or Cancel if you want to keep editing') )
		    {
				geogebra_emoticon(data.bbcode, in_wiki);
				window.close();
			}
	  	},
		"json");
}

/**
 *  Reset button
 */
function geogebra_reset( prefix, flag )
{
	if ( (flag == undefined) || confirm('Are you sure you want to reset this picture?') )
	{
	    $jq('object#' + prefix)[0].reset();
	}
}

/**
 *  Stuff below is taken from other places, edit page mainly
 */
function geogebra_emoticon( text, in_wiki )
{
	if ( opener != null )
	{
		if ( in_wiki )
		{
		    geogebra_emoticon_insert(opener.document.forms['editform'].wpTextbox1, text);
		}
		else
		{
			geogebra_emoticon_insert(opener.document.forms['post'].message, text);
		}
	}
}
function geogebra_emoticon_insert(txtarea, text)
{
	text = ' ' + text + ' ';
	if (txtarea.createTextRange && txtarea.caretPos)
	{
		var caretPos = txtarea.caretPos;
		var baseHeight;
		if ( ie )
		{
			baseHeight = document.selection.createRange().duplicate().boundingHeight;
		}
		if ( baseHeight != txtarea.caretPos.boundingHeight )
		{
			txtarea.focus();
			geogebra_storeCaret(txtarea);
		}
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
		txtarea.focus();
	}
	else if ( (txtarea.selectionEnd | txtarea.selectionEnd == 0) && (txtarea.selectionStart | txtarea.selectionStart == 0) )
	{
		geogebra_mozInsert(txtarea, text, "");
	}
	else
	{
		txtarea.value  += text;
		txtarea.focus();
	}
}
function geogebra_mozInsert(txtarea, openTag, closeTag)
{
	var scrollTop = ( typeof(txtarea.scrollTop) == 'number' ? txtarea.scrollTop : -1 );
	if (txtarea.selectionEnd > txtarea.value.length)
	{
		txtarea.selectionEnd = txtarea.value.length;
	}
	var startPos = txtarea.selectionStart;
	var endPos = txtarea.selectionEnd + openTag.length;

	txtarea.value = txtarea.value.slice(0, startPos) + openTag + txtarea.value.slice(startPos);
	txtarea.value = txtarea.value.slice(0, endPos) + closeTag + txtarea.value.slice(endPos);
	txtarea.selectionStart = startPos + openTag.length;
	txtarea.selectionEnd = endPos;
	txtarea.focus();

	if (scrollTop >= 0)
	{
		txtarea.scrollTop = scrollTop;
	}
}
function geogebra_storeCaret(textEl)
{
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}