<?php
/**
 * @package Iyosis Google Translate Module for Joomla! 1.5
 * @author Remzi Degirmencioglu
 * @copyright (C) 2011 www.iyosis.com
 * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
**/

class modGoogleTranslateHelper
{
	function GoogleTranslate($params,$baseURL,$languages)
	{
	$defaultLang = $params->get('defaultLang', 'en');
	$noConflict = $params->get('noConflict', '0');
	$jquery = $params->get('jquery', '1');
	$flagSize = $params->get('flagSize', '16');

	$flags = " ";
	$flags_first_element = true;
	foreach ($languages as $langcode => $language) {
		if ($params->get($langcode, '1')=='2') {
			if ($flags_first_element==true) {
				$flags = "<div id=\"flagcontainer\">";
				$flags_first_element = false;
			}
			$flags .= "<a class=\"flag".$langcode."\" title=\"".JText::_($language)."\" href=\"javascript:startTranslation('".$defaultLang."', '".$langcode."');\"><div id=\"flag\"></div></a>";
		}
	}
	if ($flags_first_element==false) $flags = $flags."</div>";

	$dropdown = " ";
	$dropdown_first_element = true;
	foreach ($languages as $langcode => $language) {
		if ($params->get($langcode, '1')=='1') {
			if ($dropdown_first_element==true) {
				$dropdown =  $dropdown."<select id=\"translate\" name=\"translate\" onchange=\"startTranslation('".$defaultLang."', value);\"><option value=\"\">Select a language</option>";
				if ($params->get($defaultLang, '1')=='1') $dropdown =  $dropdown."<option value=\"".$defaultLang."\">".JText::_($languages[$defaultLang])."</option>";
				$dropdown_first_element = false;
			}
			if ($langcode!=$defaultLang) $dropdown = $dropdown."<option value=\"".$langcode."\">".JText::_($language)."</option>";
		}
	}
	if ($dropdown_first_element==false) $dropdown = $dropdown."</select><span id=\"loading\" style=\"display: none;float: right;\"><img src=\"".$baseURL."modules/mod_iyosis_google_translate/tmpl/images/loading.gif\"></span>";

	$script = "
		<script type=\"text/javascript\" src=\"".$baseURL."modules/mod_iyosis_google_translate/jquery.cookie.min.js\"></script>
		<script type=\"text/javascript\" src=\"".$baseURL."modules/mod_iyosis_google_translate/jquery.translate.min.js\"></script>
		<script type=\"text/javascript\">
		";
	if ($noConflict) $script .= "jQuery.noConflict();";
	$script .= "
		function startTranslation(translateFrom,translateTo){
			jQuery('body').translate(translateFrom,translateTo, {
				not: 'select',
				start: function(){   jQuery('#loading').show() },
				complete: function(){   jQuery('#loading').hide()   },
				error: function(){   jQuery('#loading').hide()   }
			});
			jQuery.cookie(\"translateFrom\", translateFrom);
			jQuery.cookie(\"translateTo\", translateTo);
		}

		var translateFrom = jQuery.cookie('translateFrom');
		var translateTo = jQuery.cookie('translateTo');

		if (translateFrom != translateTo) {
			if (translateFrom || translateTo) {
				jQuery(document).ready(function(){
					startTranslation( translateFrom,translateTo );
				});
			}
		}
	</script>
	";
	if ($jquery) $script = "<script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js\"></script>".$script;

	$html = $script.$flags.$dropdown;

	return $html;
	}
}
?>

