<?
/* 
   This file is part of the 'Science Fair In A Box' project
   SFIAB Website: http://www.sfiab.ca

   Copyright (C) 2005 Sci-Tech Ontario Inc <info@scitechontario.org>
   Copyright (C) 2005 James Grant <james@lightbox.org>

   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 the Free Software Foundation, version 2.

   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.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/
?>
<? 
require_once("ajax.inc.php");
?>
<script type="text/javascript">

var queryInProgress=false;
var translateOpen=false;
var translationObject;
var langstr="<? foreach($config['languages'] AS $l=>$ln){ $lstr.="$l/"; } echo substr($lstr,0,-1)?>";

var jsTranslateLangs=new Array();
<? 
	$x=0;
	foreach($config['languages'] AS $l=>$ln)
	{
		if($l=="en")continue;
		echo "jsTranslateLangs[$x]=\"$l\";\n";
		$x++;
	}
?>

function selectVisibility(vis) {
	//we'll always have form0=translationdropdownform, form1=langselectform, form2...infinity=forms on the page
	for(x=2;x<document.forms.length;x++) {
		var frm=document.forms[x];
		for(y=0;y<frm.elements.length;y++) {
			var e=frm.elements[y];
			if(e.type=="select-one")
				e.style.visibility=vis;
		}
	}
}

function getTranslations(o)
{
	var objItem=document.getElementById(o);
	var url="<?=$config['SFIABDIRECTORY']?>/admin/gettranslation.php?str="+escape(objItem.value);

	if(!queryInProgress)
	{
		queryInProgress=true;
		http.open("GET", url , true);
		http.onreadystatechange = handleResponse;
		http.send(null);
	}
}
		
function handleResponse()
{
	try {

	if(http.readyState==4)
	{
		queryInProgress=false;
		var d=document.getElementById('translationdropdown');
//		alert(completeField);

		try {
			results = http.responseText.split('\n');
			var num=results.length;

				for(i=0;i<results.length;i++)
				{
					if(!results[i]){ num--; continue; } 

					line = results[i].split(':');
					//line[0] has "en" or "fr" or the lang code
					//line[1] has the actual translation for that language
					objInput=document.getElementById('translation['+line[0]+']');
					if(line[1])
					{
						//alert(objInput);
						objInput.value=line[1];
					}
					else
					{
						objInput.value="";
					}
				}
		}
		catch(e)
		{

		}
	}

	}
	catch(e)
	{
	}
}

function translateButton(o)
{
	document.write("<a href=\"#\" onclick=\"return translateDropdown('"+o+"')\"><?=i18n("translations")?></a>");
}
function translateDropdown(o)
{
	translationObject=document.getElementById(o);
	var d=document.getElementById('translationdropdown');
	if(translateOpen) {
		d.style.visibility="hidden";
		selectVisibility("visible");
	}
	else
	{
		getTranslations(o);
		objItem=translationObject;

		var tagheight=objItem.offsetHeight;
		var tagwidth=objItem.offsetWidth+75;

		//alert('tagheight='+tagheight+' tagwidth='+tagwidth);

		var intx=0;
		var inty=0;

		do
		{
			intx+=objItem.offsetLeft;
			inty+=objItem.offsetTop;
			objParent=objItem.offsetParent.tagName;
			objItem=objItem.offsetParent;
		} while(objParent!="BODY");

		d.style.top=inty+tagheight+"px";
		d.style.left=(intx-75)+"px";
		d.style.width=(tagwidth)+"px";
//		d.style.height=dropdownheight+"px";
		d.style.visibility="visible";

		oButton=document.getElementById('buttonsavetranslations');
		oButton.value="<?=i18n("Save Translations")?>";

		selectVisibility("hidden");

	}
	translateOpen=!translateOpen;
	return false;

}

function savetranslations()
{
	var url="<?=$config['SFIABDIRECTORY']?>/admin/settranslation.php?str="+escape(translationObject.value);
	var x;
	for(x=0;x<jsTranslateLangs.length;x++)
	{
		oTmp=document.getElementById('translation['+jsTranslateLangs[x]+']');
		url+="&"+jsTranslateLangs[x]+"="+escape(oTmp.value);
	}
	oButton=document.getElementById('buttonsavetranslations');
	oButton.value="<?=i18n("Saving translations...")?>";

	if(!queryInProgress)
	{
		queryInProgress=true;
		http.open("GET", url , true);
		http.onreadystatechange = handleSaveResponse;
		http.send(null);
	}

	return false;
}

function handleSaveResponse()
{
	try {

	if(http.readyState==4)
	{
		queryInProgress=false;
		var d=document.getElementById('translationdropdown');

		oButton=document.getElementById('buttonsavetranslations');
		oButton.value="<?=i18n("Translations Saved")?>";

		setTimeout("translateDropdown('dummy');",500); //it doesnt matter what obj we use, because we're just clearing it anyways

		//who cares what happens, we're done thats all we care about

	}

	}
	catch(e)
	{
	}
}
</script>

<?
$num=count($config['languages'])-1;  //subtract 1 for english
$divheight=($num*25)+50;
?>
<style type="text/css">
.translationdropdown
{
		position: absolute;
		z-index: 100;
		overflow: auto;
		width: 300px;
		background-color: #669966;
		visibility: hidden;
		border: 0px;
		top: 0px;
		left:0px;
		width:300px;
		height:<?=$divheight?>px;
		text-align: center;
}

</style>
<div class="translationdropdown" name="translationdropdown" id="translationdropdown">
<form name="translationform" method="get">
<table cellspacing=0 cellpadding=0 border=0 style="width: 99%; font-size: 0.8em;">
<?
foreach($config['languages'] AS $l=>$ln)
{
	if($l=="en") continue;
	echo "<tr><td style=\"width: 75px;\">$ln:</td><td><input type=\"text\" style=\"width: 100%;\" id=\"translation[$l]\" name=\"translation[$l]\"></td></tr>";
}
echo "</table>\n";
echo "<input type=\"button\" id=\"buttonsavetranslations\" onclick=\"return savetranslations()\" value=\"".i18n("Save Translations")."\">";
?>
</form>
</div>