<?php

/**
 * DioneSoft Company
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the DioneSoft EULA that is bundled with
 * this package in the file GPL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/copyleft/gpl.html
 *
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@dionesoft.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade the extension
 * to newer versions in the future. If you wish to customize the extension
 * for your needs please refer to http://www.dionesoft.com/ for more information
 * or send an email to sales@dionesoft.com
 *
 * @category   DioneSoft
 * @package    Dione Text Animator
* @copyright Copyright (C) 2010 DioneSoft Company (http://www.dionesoft.com)
* @license GNU/GPL http://www.gnu.org/copyleft/gpl.html
*/

defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin' );
 
class plgContentDioneTextAnimator extends JPlugin
{
	function onPrepareContent( &$article, &$params, $page=0)
	{
		global $mainframe;
		
		// A database connection is created
        $db = JFactory::getDBO();		
		$view  = JRequest::getCmd('view');
		
		if(!$page) {
			$page = 0;
		}		
		
		// get current document
		$document =& JFactory::getDocument();
		
		// Get Plugin info
		$plugin			=& JPluginHelper::getPlugin('content', 'dionetextanimator');
		$pluginParams	= new JParameter( $plugin->params );		
		
		// simple performance check to determine whether bot should process further
		if ( JString::strpos( $article->text, 'dionetextanimator' ) === false ) {
			return true;
		}
		
		$document->addCustomTag('<script type="text/javascript">jQuery.noConflict();</script>' );
		
		$plugin_block_regex = '/({dionetextanimator.*?}.*?{\/dionetextanimator})/s';
		
		$begin_block_regex = '/{dionetextanimator\s+id="([^"]+)?"\s+text="([^"]+)?"\s+parameters="([^"]+)?"}/'; # pattern match the regex like {dionetextanimator id="..." text="..." parameters="..."}
		$end_block_regex = '/{\/dionetextanimator}/';
		
		if ( !$pluginParams->get( 'enabled', 1 ) ) {
			$article->text = preg_replace( $plugin_block_regex, '<br />', $article->text );
			return true;
		}		
		
		$plugin_text_matches = array();
		
		preg_match_all( $plugin_block_regex, $article->text, $plugin_text_matches );
		
		// Number of plugins
		$count = count( $plugin_text_matches[0] );				
			
		if ( $count ) {
			# parameters from the Back-End
			
			$rate  		 		 = $this->params->get('rate');
			$delay  		 	 = $this->params->get('delay');
			$fontSize  			 = $this->params->get('fontSize');
			$fontColor  		 = $this->params->get('fontColor');
			
			# add the declaration for the JQuery Library and Library
			$document->addScript("plugins/content/dionetextanimator/js/jquery-1.4.2.min.js");
			$document->addScript("plugins/content/dionetextanimator/js/jquery.jticker.js");			
			
			# begin of the JS-code's defenition
			$script = '$(document).ready(function(){';
			
			for ( $i = 0; $i < $count; $i++ ){
				$plugin_id = -1;
				$plugin_text_option = "";
				$plugin_text = $plugin_text_matches[0][$i];
				
				if (preg_match( $begin_block_regex, $plugin_text)){
					$head_matches = array();
					preg_match( $begin_block_regex, $plugin_text, $head_matches);
					
					$plugin_tag = $head_matches[0];
					$plugin_id = $head_matches[1];
					
					if (isset($head_matches[2])){
						$plugin_text_option = $head_matches[2];
					}
					
					$plugin_params = "";
					if (isset($head_matches[3])){
						$plugin_params = $head_matches[3];
					}
					
					if ($plugin_params != ""){
						// Parse of input params from the template
						$params_matches = preg_split( "/,/", $plugin_params);
						
						for ( $j = 0; $j < count( $params_matches); $j++ ){
							$params_parts_matches = preg_split( "/:/", $params_matches[$j]);
							
							$param_name = $params_parts_matches[0];
							$param_value = $params_parts_matches[1];
							
							$param_name = trim($param_name);
							$param_value = trim($param_value);
							
							if ($param_name == "rate"){
								$param_value = str_replace("'", "", $param_value);
								$rate = $param_value;
							}
							
							if ($param_name == "delay"){
								$param_value = str_replace("'", "", $param_value);
								$delay = $param_value;
							}
							
							if ($param_name == "fontSize"){
								$param_value = str_replace("'", "", $param_value);
								$fontSize = $param_value;
							}
							
							if ($param_name == "fontColor"){
								$param_value = str_replace("'", "", $param_value);
								$fontColor = $param_value;
							}							
						}
					}
				}
				
				if (preg_match( $end_block_regex, $plugin_text)){
					$document->addStyleDeclaration('#dionetextanimator_'.$plugin_id.' .cursor { display: inline-block; background: #565c61; width: 0.6em; height: 1em; text-align: center;}', 'text/css');
				
					$replaced_text = "<div id=\"dionetextanimator_".$plugin_id."\"><p style=\"font-size: ".$fontSize."; color: ".$fontColor.";\">".$plugin_text_option."</p></div>";
					
					$script .= 'jQuery("#dionetextanimator_'.$plugin_id.'").ticker({';
					$script .= '	cursorList:  " ",';
					$script .= '	rate: '.$rate.',';
					$script .= '	delay: '.$delay.'';
					$script .= '}).trigger("play").trigger("stop");';					
					
					$article->text = str_replace( $plugin_text, $replaced_text, $article->text );					
				}
			}
			
			# end of the JS-code's defenition
			$script .= '});';
			$document->addScriptDeclaration($script);			
		}
		
		return true;
	}
}