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

	class PluginLunar extends PluginBase
	{
		var $pluginEnabled;
		
		function PluginLunar()
		{
			$this->PluginBase();

			$this->id      = "lunar";
			$this->author  = "Mark Wu";
			$this->desc    = "Show Chinese Lunar Calendar date information in template.";

			$this->locales = Array( "en_UK" , "zh_TW" );

			$this->init();
		}

		function init()
		{
            $this->registerBlogAction( "lunarCalendar", "ShowLunarCalendarAction" );
            $this->registerAdminAction( "lunar", "PluginLunarConfigAction" );
			$this->registerAdminAction( "updateLunarConfig", "PluginLunarUpdateConfigAction" );
			
			$menu =& Menu::getMenu();
			if( !$menu->entryExists( "/menu/controlCenter/manageAppearancePlugins" ))						
				$this->addMenuEntry( "/menu/controlCenter", "manageAppearancePlugins", "", "", true, false );			
            $this->addMenuEntry( "/menu/controlCenter/manageAppearancePlugins", "Lunar", "?op=lunar", "" );
		}

		function register()
		{
		    $blogSettings = $this->blogInfo->getSettings();
			$this->pluginEnabled = $blogSettings->getValue( "plugin_lunar_enabled" );
		}

	    function isEnabled()
	    {
	        return $this->pluginEnabled;
	    }
	    
	    function show()
	    {
            $locale = $this->blogInfo->getLocale();
            $langId = $locale->getLanguageId();

			$rg = $this->blogInfo->getBlogRequestGenerator();
			$baseUrl = $rg->getBaseUrl();

	    	$str = '';
	    	$str = '<script type="text/javascript" src="'.$baseUrl.'/plugins/lunar/js/lunar.js"></script>';
	    	$str .= '<script type="text/javascript">CalConv("'.$langId.'");</script>';
	    	$str .= '... <a href="#" onclick="javascript:window.open(\''.$this->getCalendarUrl($langId);
	    	$str .= '\',\''.$locale->tr("calendar").'\',\'scrollbars=no,resizable=no,toolbar=no,height=400,width=800\');">'.$locale->tr("detail").'</a>';
	    	return $str;
		}

        function getCalendarUrl( $langId )
        {
 			if ( $langId == "zh_CN" || $langId == "zh-CN" )
 				$langId = "zh-CN";
 			else
 				$langId = "zh-TW";
 			
 			$rg = new RawRequestGenerator($this->blogInfo);
            
        	$rg->addParameter( "op", "lunarCalendar" );
        	$rg->addParameter( "show", "calendar_".$langId);
            $rg->addParameter( "blogId", $this->blogInfo->getId());

            $templatePage = $rg->getIndexUrl().$rg->getRequest();

            return $templatePage;
        }				
	}
?>