<?php
/**
* JB Type
* @version 1.1
* @author Joomla Bamboo
* http://www.joomlabamboo.com
* Based on JW AllVideos Plugin by Joomlaworks.gr and Xtypo from www.templateplazza.com
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
* 
**/

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin' );

class plgSystemJbtype extends JPlugin {
	function onAfterRender() {  

	// Get Plugin info
	  $plugin		=& JPluginHelper::getPlugin('system','jbtype');
	  $param		= new JParameter( $plugin->params );
	  $iconStyle = $param->get('iconStyle','coquette'); 
	  $document =& JFactory::getDocument();
	  $app = JFactory::getApplication();
	  $doctype = $document->getType();
	  $output = JResponse::getBody();
	  $urlOption = JRequest::getVar('option','none');
	  $urlTask = JRequest::getVar('task','none');
	  $modbase = ''.JURI::base().'plugins/system/jbtype/';
	  require_once ('jbtype/styles.php');
	
		if($app->isAdmin()) {
		    return;
		}   
	
		if($doctype !== 'html') {
		    return;
		}   
		
		if(($urlOption == 'com_content') and ($urlTask == 'edit')) {
			return;
		}
	

	$jbListPattern = "#{jb_list.*}(.*?){/jb_list.*}#s";
	$jbListRegex = "#(^(.*?)\|)|(\|(.*?)\|)|(\|(.*?)$)#s";
	$jbListReplace = "<li>***listcode***</li>";

	$regex = array(
	// Boxes
	"jb_blackbox" => array("<p class=\"jb_blackbox\">***code***</p>","#{jb_blackbox}(.*?){/jb_blackbox}#s") ,
	"jb_greenbox" => array("<p class=\"jb_greenbox\">***code***</p>","#{jb_greenbox}(.*?){/jb_greenbox}#s") ,
	"jb_bluebox" => array("<p class=\"jb_bluebox\">***code***</p>","#{jb_bluebox}(.*?){/jb_bluebox}#s") ,
	"jb_redbox" => array("<p class=\"jb_redbox\">***code***</p>","#{jb_redbox}(.*?){/jb_redbox}#s") ,
	"jb_yellowbox" => array("<p class=\"jb_yellowbox\">***code***</p>","#{jb_yellowbox}(.*?){/jb_yellowbox}#s") ,
	"jb_brownbox" => array("<p class=\"jb_brownbox\">***code***</p>","#{jb_brownbox}(.*?){/jb_brownbox}#s") ,
	"jb_purplebox" => array("<p class=\"jb_purplebox\">***code***</p>","#{jb_purplebox}(.*?){/jb_purplebox}#s") ,

	// Typography
	"jb_dropcap" => array("<span class=\"jb_dropcap\">***code***</span>","#{jb_dropcap}(.*?){/jb_dropcap}#s") ,
	"jb_quote" => array("<blockquote><p>***code***</p></blockquote>","#{jb_quote}(.*?){/jb_quote}#s") ,
	"jb_quoteleft" => array("<div class=\"jb_quoteleft\"><p>***code***</p></div>","#{jb_quoteleft}(.*?){/jb_quoteleft}#s") ,
	"jb_quoteright" => array("<div class=\"jb_quoteright\"><p>***code***</p></div>","#{jb_quoteright}(.*?){/jb_quoteright}#s") ,

	// Layout
	"jb_left45" => array("<div class=\"jb_left45\">***code***</div>","#{jb_left45}(.*?){/jb_left45}#s") ,
	"jb_right45" => array("<div class=\"jb_right45\">***code***</div>","#{jb_right45}(.*?){/jb_right45}#s") ,
	"jb_clear" => array("<div class=\"jbclear\">***code***</div>","#{jb_clear}(.*?){/jb_clear}#s") ,

	// Spans
	"jb_black" => array("<span class=\"jb_black\">***code***</span>","#{jb_black}(.*?){/jb_black}#s") ,
	"jb_blue" => array("<span class=\"jb_blue\">***code***</span>","#{jb_blue}(.*?){/jb_blue}#s") ,
	"jb_red" => array("<span class=\"jb_red\">***code***</span>","#{jb_red}(.*?){/jb_red}#s") ,
	"jb_green" => array("<span class=\"jb_green\">***code***</span>","#{jb_green}(.*?){/jb_green}#s") ,
	"jb_yellow" => array("<span class=\"jb_yellow\">***code***</span>","#{jb_yellow}(.*?){/jb_yellow}#s") ,
	"jb_white" => array("<span class=\"jb_white\">***code***</span>","#{jb_white}(.*?){/jb_white}#s") ,
	"jb_brown" => array("<span class=\"jb_brown\">***code***</span>","#{jb_brown}(.*?){/jb_brown}#s") ,
	"jb_purple" => array("<span class=\"jb_purple\">***code***</span>","#{jb_purple}(.*?){/jb_purple}#s") ,
	
	
	// Twitter Search
	"twitter_search" => array("<span class=\"twitter_search\">***code***</span>","#{twitter_search}(.*?){/twitter_search}#s") ,
	
	// Lists
	"jb_listblack" => array("<ul class=\"jb_black\">***code***</ul>","#{jb_listblack}(.*?){/jb_listblack}#s") ,
	"jb_listblue" => array("<ul class=\"jb_blue\">***code***</ul>","#{jb_listblue}(.*?){/jb_listblue}#s") ,
	"jb_listred" => array("<ul class=\"jb_red\">***code***</ul>","#{jb_listred}(.*?){/jb_listred}#s") ,
	"jb_listgreen" => array("<ul class=\"jb_green\">***code***</ul>","#{jb_listgreen}(.*?){/jb_listgreen}#s") ,
	"jb_listyellow" => array("<ul class=\"jb_yellow\">***code***</ul>","#{jb_listyellow}(.*?){/jb_listyellow}#s") ,
	"jb_listwhite" => array("<ul class=\"jb_white\">***code***</ul>","#{jb_listwhite}(.*?){/jb_listwhite}#s") ,
	"jb_listbrown" => array("<ul class=\"jb_brown\">***code***</ul>","#{jb_listbrown}(.*?){/jb_listbrown}#s") ,
	"jb_listpurple" => array("<ul class=\"jb_purple\">***code***</ul>","#{jb_listpurple}(.*?){/jb_listpurple}#s") ,

	// Iconic Icons
	"jb_iconic_info" => array("<span class=\"jb_iconic_info\">***code***</span>","#{jb_iconic_info}(.*?){/jb_iconic_info}#s") ,
	"jb_iconic_star" => array("<span class=\"jb_iconic_star\">***code***</span>","#{jb_iconic_star}(.*?){/jb_iconic_star}#s") ,
	"jb_iconic_heart" => array("<span class=\"jb_iconic_heart\">***code***</span>","#{jb_iconic_heart}(.*?){/jb_iconic_heart}#s") ,
	"jb_iconic_tag" => array("<span class=\"jb_iconic_tag\">***code***</span>","#{jb_iconic_tag}(.*?){/jb_iconic_tag}#s") ,
	"jb_iconic_arrival" => array("<span class=\"jb_iconic_arrival\">***code***</span>","#{jb_iconic_arrival}(.*?){/jb_iconic_arrival#s") ,
	"jb_iconic_truck" => array("<span class=\"jb_iconic_truck\">***code***</span>","#{jb_iconic_truck}(.*?){/jb_iconic_truck}#s") ,
	"jb_iconic_arrow" => array("<span class=\"jb_iconic_arrow\">***code***</span>","#{jb_iconic_arrow}(.*?){/jb_iconic_arrow}#s") ,
	"jb_iconic_article" => array("<span class=\"jb_iconic_article\">***code***</span>","#{jb_iconic_article}(.*?){/jb_iconic_article}#s") ,
	"jb_iconic_email" => array("<span class=\"jb_iconic_email\">***code***</span>","#{jb_iconic_email}(.*?){/jb_iconic_email}#s") ,
	"jb_iconic_beaker" => array("<span class=\"jb_iconic_beaker\">***code***</span>","#{jb_iconic_beaker}(.*?){/jb_iconic_beaker}#s") ,
	"jb_iconic_book" => array("<span class=\"jb_iconic_book\">***code***</span>","#{jb_iconic_book}(.*?){/jb_iconic_book}#s") ,
	"jb_iconic_bolt" => array("<span class=\"jb_iconic_bolt\">***code***</span>","#{jb_iconic_bolt}(.*?){/jb_iconic_bolt}#s") ,
	"jb_iconic_box" => array("<span class=\"jb_iconic_box\">***code***</span>","#{jb_iconic_box}(.*?){/jb_iconic_box}#s") ,
	"jb_iconic_calendar" => array("<span class=\"jb_iconic_calendar\">***code***</span>","#{jb_iconic_calendar}(.*?){/jb_iconic_calendar}#s") ,
	"jb_iconic_comment" => array("<span class=\"jb_iconic_comment\">***code***</span>","#{jb_iconic_comment}(.*?){/jb_iconic_comment}#s") ,
	"jb_iconic_tick" => array("<span class=\"jb_iconic_tick\">***code***</span>","#{jb_iconic_tick}(.*?){/jb_iconic_tick}#s") ,
	"jb_iconic_cloud" => array("<span class=\"jb_iconic_cloud\">***code***</span>","#{jb_iconic_cloud}(.*?){/jb_iconic_cloud}#s") ,
	"jb_iconic_document" => array("<span class=\"jb_iconic_document\">***code***</span>","#{jb_iconic_document}(.*?){/jb_iconic_document}#s") ,
	"jb_iconic_image" => array("<span class=\"jb_iconic_image\">***code***</span>","#{jb_iconic_image}(.*?){/jb_iconic_image}#s") ,
	"jb_iconic_quote" => array("<span class=\"jb_iconic_quote\">***code***</span>","#{jb_iconic_quote}(.*?){/jb_iconic_quote}#s") ,
	"jb_iconic_lightbulb" => array("<span class=\"jb_iconic_lightbulb\">***code***</span>","#{jb_iconic_lightbulb}(.*?){/jb_iconic_lightbulb}#s") ,
	"jb_iconic_search" => array("<span class=\"jb_iconic_search\">***code***</span>","#{jb_iconic_search}(.*?){/jb_iconic_search}#s") ,
	"jb_iconic_mail" => array("<span class=\"jb_iconic_mail\">***code***</span>","#{jb_iconic_mail}(.*?){/jb_iconic_mail}#s") ,
	"jb_iconic_dash" => array("<span class=\"jb_iconic_dash\">***code***</span>","#{jb_iconic_dash}(.*?){/jb_iconic_dash}#s") ,
	"jb_iconic_movie" => array("<span class=\"jb_iconic_movie\">***code***</span>","#{jb_iconic_movie}(.*?){/jb_iconic_movie}#s") ,
	"jb_iconic_download" => array("<span class=\"jb_iconic_download\">***code***</span>","#{jb_iconic_download}(.*?){/jb_iconic_download}#s") ,
	
	// Icons
	"jb_new" => array("<span class=\"jb_new\">***code***</span>","#{jb_new}(.*?){/jb_new}#s") ,
	"jb_code" => array("<span class=\"jb_code\">***code***</span>","#{jb_code}(.*?){/jb_code}#s") ,
	"jb_attachment" => array("<span class=\"jb_attachment\">***code***</span>","#{jb_attachment}(.*?){/jb_attachment}#s") ,
	"jb_calculator" => array("<span class=\"jb_calculator\">***code***</span>","#{jb_calculator}(.*?){/jb_calculator}#s") ,
	"jb_cut" => array("<span class=\"jb_cut\">***code***</span>","#{jb_cut}(.*?){/jb_cut}#s") ,
	"jb_dollar" => array("<span class=\"jb_dollar\">***code***</span>","#{jb_dollar}(.*?){/jb_dollar}#s") ,
	"jb_euro" => array("<span class=\"jb_euro\">***code***</span>","#{jb_euro}(.*?){/jb_euro}#s") ,
	"jb_dollar" => array("<span class=\"jb_dollar\">***code***</span>","#{jb_dollar}(.*?){/jb_dollar}#s") ,
	"jb_pound" => array("<span class=\"jb_pound\">***code***</span>","#{jb_pound}(.*?){/jb_pound}#s") ,
	"jb_support" => array("<span class=\"jb_support\">***code***</span>","#{jb_support}(.*?){/jb_support}#s") ,
	"jb_next" => array("<span class=\"jb_next\">***code***</span>","#{jb_next}(.*?){/jb_next}#s") ,
	"jb_previous" => array("<span class=\"jb_previous\">***code***</span>","#{jb_previous}(.*?){/jb_previous}#s") ,
	"jb_calculator" => array("<span class=\"jb_calculator\">***code***</span>","#{jb_calculator}(.*?){/jb_calculator}#s") ,
	"jb_cart" => array("<span class=\"jb_cart\">***code***</span>","#{jb_cart}(.*?){/jb_cart}#s") ,
	"jb_save" => array("<span class=\"jb_save\">***code***</span>","#{jb_save}(.*?){/jb_save}#s") ,
	"jb_sound" => array("<span class=\"jb_sound\">***code***</span>","#{jb_sound}(.*?){/jb_sound}#s") ,
	"jb_info" => array("<span class=\"jb_info\">***code***</span>","#{jb_info}(.*?){/jb_info}#s") ,
	"jb_warning" => array("<span class=\"jb_warning\">***code***</span>","#{jb_warning}(.*?){/jb_warning}#s") ,
	"jb_camera" => array("<span class=\"jb_camera\">***code***</span>","#{jb_camera}(.*?){/jb_camera}#s") ,
	"jb_comment" => array("<span class=\"jb_comment\">***code***</span>","#{jb_comment}(.*?){/jb_comment}#s") ,
	"jb_chat" => array("<span class=\"jb_chat\">***code***</span>","#{jb_chat}(.*?){/jb_chat}#s") ,
	"jb_document" => array("<span class=\"jb_document\">***code***</span>","#{jb_document}(.*?){/jb_document}#s") ,
	"jb_accessible" => array("<span class=\"jb_accessible\">***code***</span>","#{jb_accessible}(.*?){/jb_accessible}#s") ,
	"jb_star" => array("<span class=\"jb_star\">***code***</span>","#{jb_star}(.*?){/jb_star}#s") ,
	"jb_heart" => array("<span class=\"jb_heart\">***code***</span>","#{jb_heart}(.*?){/jb_heart}#s") ,
	"jb_mail" => array("<span class=\"jb_mail\">***code***</span>","#{jb_mail}(.*?){/jb_mail}#s") ,
	"jb_film" => array("<span class=\"jb_film\">***code***</span>","#{jb_film}(.*?){/jb_film}#s") ,
	"jb_pin" => array("<span class=\"jb_pin\">***code***</span>","#{jb_pin}(.*?){/jb_pin}#s") ,
	"jb_recycle" => array("<span class=\"jb_recycle\">***code***</span>","#{jb_recycle}(.*?){/jb_recycle}#s") ,
	"jb_lightbulb" => array("<span class=\"jb_lightbulb\">***code***</span>","#{jb_lightbulb}(.*?){/jb_lightbulb}#s") ,
	);

		// prepend and append code
		$startcode = "";
		$endcode = "";

		$classes = array();
		$uniqueClasses = array();

		if ( !$param->get( 'enabled', 1 ) ) {		
			foreach ($regex as $key => $value) {
				$output = preg_replace( $regex[$key][1], '', $output );
			}
			return;
		}


		foreach ($regex as $key => $value) {  // searching for marks   		
			if (preg_match_all($regex[$key][1], $output, $matches, PREG_PATTERN_ORDER) > 0) {      			 
				foreach ($matches[1] as $match) {	
					$classes[]= $key;

					if (preg_match($jbListPattern,$regex[$key][1])){
						$listMatches = explode("|",$match);
						$listCode = "";
						foreach ($listMatches as $listMatch) {
							$listCode .= str_replace("***listcode***", $listMatch, $jbListReplace );
						}
						$code = str_replace("***code***", $listCode, $regex[$key][0] );			
					}
					else{
						$code = str_replace("***code***", $match, $regex[$key][0] );
					}
					$a = preg_quote($match);
					$a = str_replace("'", "\'", $a);
					$output = preg_replace("'{".preg_quote($key)."}".$a."{/".preg_quote($key)."}'s", $startcode.$code.$endcode , $output );


		    	}
		 	}	    	
		}

		if (!empty($classes)) {
			$uniqueClasses = array_unique($classes);
			$pageStyles = getTypeCss($iconStyle,$uniqueClasses,$jbTypeStyles);
			$document->addCustomTag($pageStyles);
			$output   = str_replace('</head>', $pageStyles .'</head>', $output);
			
				
		}
		JResponse::setBody($output);
		
		return true;
	}
}


?>