<?php

	include_once( PLOG_CLASS_PATH."class/plugin/pluginbase.class.php" );
    include_once( PLOG_CLASS_PATH."class/plugin/pluginmanager.class.php" );

    /**
     * Implementation of a very simple plugin file.
     */
    class PluginJsDictionary extends PluginBase {

    var $_jsCode = "<script language=\"javascript1.2\">
        var NS = (navigator.appName == \"Netscape\") ? 1 : 0;
if (NS) document.captureEvents(Event.DBLCLICK);

// register the events we're going to use
document.ondblclick = dict;
document.onkeydown = register;

var newwin;
function dict() {
if (NS) {
t = document.getSelection();
opennewwin(t);
}
else {
t = document.selection.createRange();
if(document.selection.type == 'Text' && t.text != '') {
document.selection.empty();
opennewwin(t.text);
      }
   }
}
function opennewwin(text) {
if (text > '') {
var url = '%s';
var dict_url = url.replace( /%word%/ig, escape(text));
//window.alert(dict_url);
newwin = window.open(dict_url, 'dictionary', 'width=800, height=600, resizable=yes, menubar=yes, toolbar=yes, scrollbars=yes');
setTimeout('newwin.focus()', 100);
   }
}

function register(e)
{
	if(!e) e = window.event;
	if( e.keyCode == 13 ) {
		selText = getSel();
		if( selText != '' ) opennewwin(selText);
	}
}

function getSel()
{
	var str = '';
	if(document.getSelection) {
		str = document.getSelection();
	}
	else if( document.selection && document.selection.createRange ) {
		range = document.selection.createRange();
		if (range) str = range.text;
	}

	if(!str) return'';
	// replace unneeded characters
	str=cleanString( str );
	return str;
}

function cleanString( str )
{
	return str.replace(/^[^A-Za-z]+|[^A-Za-z]+\$/g,'');
}


</script>
";

		var $_sites =  Array(
            Array( "desc" => "Dictionary.com",
                   "url"  => "http://dictionary.reference.com/search?q=%word%" ),
        	Array( "desc" => "ILN.net Dictionary",
                   "url"  => "http://www.iln.net/main/resources/dictionary/default.asp?Word=%word%" ),
            Array( "desc" => "Cambridge Advanced Learner's Dictionary",
                   "url"  => "http://dictionary.cambridge.org/results.asp?searchword=%word%&image.x=0&image.y=0" ),
            Array( "desc" => "Merrian Webster Dictionary",
                   "url"  => "http://www.m-w.com/cgi-bin/dictionary?book=Dictionary&va=%word%" ),
            Array( "desc" => "Google",
                   "url"  => "http://www.google.com/search?q=%word%&ie=UTF-8&oe=UTF-8&hl=en" ),
            Array( "desc" => "Yahoo! Search",
                   "url"  => "http://search.yahoo.com/search?fr=fp-pull-web-t&p=%word%" ),
            Array( "desc" => "Wikipedia",
                   "url"  => "http://en.wikipedia.org/wiki/%word%" ),
            Array( "desc" => "IMDB.com Movie Search",
                   "url"  => "http://www.imdb.com/find?tt=on;nm=on;mx=20;q=%word%" ),
            Array( "desc" => "Babelfish English to French translator",
                   "url"  => "http://babelfish.altavista.com/babelfish/tr?doit=done&tt=urltext&intl=l&urltext=%word%&lp=en_fr" ),
            Array( "desc" => "Babelfish English to German translator",
                   "url"  => "http://babelfish.altavista.com/babelfish/tr?doit=done&tt=urltext&intl=l&urltext=%word%&lp=en_de" ),
            Array( "desc" => "Babelfish English to Spanish translator",
                   "url"  => "http://babelfish.altavista.com/babelfish/tr?doit=done&tt=urltext&intl=l&urltext=%word%&lp=en_es" ),
            Array( "desc" => "Babelfish English to Italian translator",
                   "url"  => "http://babelfish.altavista.com/babelfish/tr?doit=done&tt=urltext&intl=l&urltext=%word%&lp=en_it" ),
            Array( "desc" => "Babelfish English to Portuguese translator",
                   "url"  => "http://babelfish.altavista.com/babelfish/tr?doit=done&tt=urltext&intl=l&urltext=%word%&lp=en_pt" ),
            Array( "desc" => "Babelfish English to Japanese translator",
                   "url"  => "http://babelfish.altavista.com/babelfish/tr?doit=done&tt=urltext&intl=l&urltext=%word%&lp=en_ja" ),
            Array( "desc" => "Babelfish English to Chinese translator",
                   "url"  => "http://babelfish.altavista.com/babelfish/tr?doit=done&tt=urltext&intl=l&urltext=%word%&lp=en_zh" ),
            Array( "desc" => "Babelfish English to Korean translator",
                   "url"  => "http://babelfish.altavista.com/babelfish/tr?doit=done&tt=urltext&intl=l&urltext=%word%&lp=en_ko" ),
            Array( "desc" => "Babelfish French to English translator",
                   "url"  => "http://babelfish.altavista.com/babelfish/tr?doit=done&tt=urltext&intl=l&urltext=%word%&lp=fr_en" ),
            Array( "desc" => "Babelfish German to English translator",
                   "url"  => "http://babelfish.altavista.com/babelfish/tr?doit=done&tt=urltext&intl=l&urltext=%word%&lp=de_en" ),
            Array( "desc" => "Babelfish Spanish to English translator",
                   "url"  => "http://babelfish.altavista.com/babelfish/tr?doit=done&tt=urltext&intl=l&urltext=%word%&lp=es_en" ),
            Array( "desc" => "Babelfish Italian to English translator",
                   "url"  => "http://babelfish.altavista.com/babelfish/tr?doit=done&tt=urltext&intl=l&urltext=%word%&lp=it_en" ),
            Array( "desc" => "Babelfish Portuguese to English translator",
                   "url"  => "http://babelfish.altavista.com/babelfish/tr?doit=done&tt=urltext&intl=l&urltext=%word%&lp=pt_en" ),
            Array( "desc" => "Babelfish Japanese to English translator",
                   "url"  => "http://babelfish.altavista.com/babelfish/tr?doit=done&tt=urltext&intl=l&urltext=%word%&lp=ja_en" ),
            Array( "desc" => "Babelfish Chinese to English translator",
                   "url"  => "http://babelfish.altavista.com/babelfish/tr?doit=done&tt=urltext&intl=l&urltext=%word%&lp=zh_en" ),
            Array( "desc" => "Babelfish Korean to English translator",
                   "url"  => "http://babelfish.altavista.com/babelfish/tr?doit=done&tt=urltext&intl=l&urltext=%word%&lp=ko_en" ),
            Array( "desc" => "Babelfish Russian to English translator",
                   "url"  => "http://babelfish.altavista.com/babelfish/tr?doit=done&tt=urltext&intl=l&urltext=%word%&lp=ru_en" )
        );


    	function PluginJsDictionary()
        {
        	$this->PluginBase();

            $this->desc   = "Allows readers to get a instant definition of any word from the blog by
                             <b>double clicking</b> any of them. Also, readers can select any arbitrary section
                             of text and press the <b>Enter</b> key in order to perform a search <br/><br/>
                             In order to get this plugin working:<br/><br/>
                             <b>1.</b> First, make sure that the plugin is enabled by clicking the \"configure\"
                             link on the right. Choose also a dictionary from the ones provided or
                             use a custom one if you wish.<br/><br/>
                             <b>2.</b> Add the following to the header.template file of the template set you are
                             currently using:<br/><br/>
                             {\$jsdictionary->enable()}
                             <br/><br/>
                             Please remember that this plugin needs a Javascript 1.2-compatible browser in order to
                             work properly.";
            $this->author = "The LifeType team";
        }

        /**
         * Enables the dictionary
         */
        function enable()
        {
        	// do the necessary transformations so that we get the right javascript code

            // if the plugin has been disabled, then quit
            if( $this->blogSettings->getValue( "plugin_js_dictionary_enabled" ) == false )
            	return;

            // first of all, fetch the url of the site we're currently using
            $siteId = $this->blogSettings->getValue( "plugin_js_dictionary_site" );
            if( $siteId == -1 )
            	$url = $this->blogSettings->getValue( "plugin_js_dictionary_custom_site" );
            else
            	$url = $this->_sites[$siteId]["url"];

            // the mechanism we use to get the right javascript code is a bit clunky
            // but... it works O:)
            return str_replace( "%s", $url, $this->_jsCode );
        }

        /**
         * generates a valid configuration dialog
         */
        function getConfigurationMessage()
        {
        	$configMessage = '<h1>JsDictionary Plugin Configuration</h1>
                              <form name="configureJsDictionaryPlugin" method="post">';

            if( $this->blogSettings->getValue( "plugin_js_dictionary_enabled" ) == true )
            	$configMessage .= 'Enable the plugin: <input type="checkbox" name="pluginJsDictionaryEnabled" value="1" checked="checked" />';
            else
            	$configMessage .= 'Enable the plugin: <input type="checkbox" name="pluginJsDictionaryEnabled" value="1" />';

            $configMessage .= '<br/><br/>Dictionary site: <select name="pluginJsDictionarySite">';
            foreach( $this->_sites as $id => $site ) {
            	$configMessage .= '<option value="'.$id.'"';
                if( $this->blogSettings->getValue( "plugin_js_dictionary_site" ) == $id )
                	$configMessage .= ' selected="selected" ';
                $configMessage .= '>'.$site["desc"].'</option>';
            }

            $configMessage .= '<option value="-1"';
            if( $this->blogSettings->getValue( "plugin_js_dictionary_site" ) == -1 )
            	$configMessage .= ' selected="selected" ';
            $configMessage .= '>Custom (use field below)</option>
                               </select><br/><br/>
                               Custom dictionary site: <input size="80" type="text" name="pluginJsDictionaryCustomSite" value="'.$this->blogSettings->getValue( "plugin_js_dictionary_custom_site" ).'" /><br/>
                               <i>Use \'%word%\' to specify where the selected word should be included in the request</i>
                               <br/><br/>
                               <input type="hidden" name="op" value="updatePluginConfig" />
                               <input type="hidden" name="pluginId" value="jsdictionary" />
                               <input type="submit" name="Save Settings" value="Save Settings" />
                               </form>';

            return $configMessage;
        }

        /**
         * Configures the plugin.
         */
        function configure( $params )
        {
        	// check if the user wants to enable or disable the plugin
        	$pluginEnabled = $params->getValue( "pluginJsDictionaryEnabled" );
            if( $pluginEnabled == "" )
            	$pluginEnabled = false;

            $dictionarySite = $params->getValue( "pluginJsDictionarySite" );
            if( $dictionarySite == "" )
            	$dictionarySite = 0;

            $customSite = $params->getValue( "pluginJsDictionaryCustomSite" );
            if( $dictionarySite == "-1" && $customSite == "" )
            	$dictionarySite = 0;

            $this->blogSettings->setValue( "plugin_js_dictionary_enabled", $pluginEnabled );
            $this->blogSettings->setValue( "plugin_js_dictionary_site", $dictionarySite );
            $this->blogSettings->setValue( "plugin_js_dictionary_custom_site", $customSite );

            $this->updateBlogSettings();

            return true;
        }
    }

    PluginManager::registerPlugin( "jsdictionary", "PluginJsDictionary" );
?>
