<?php
/**
 * @version     1.0
 * @package     Content - JLex Review
 * @copyright   Copyright (C) 2013 joomla-extensions.info. All rights reserved.
 * @license     GNU/GPL or later
 * @author      JLEX team
 */
defined ( '_JEXEC' ) or die ( 'Restricted access' );
jimport ( 'joomla.filesystem.file' );

class plgDJCatalog2JLexReview extends JPlugin {
    public function __construct(& $subject, $config) {
        JHTML::_ ( 'behavior.keepalive' ); // Keep session
        parent::__construct ( $subject, $config );
        $this->loadLanguage ();
    }
    public function onafterDJCatalog2DisplayTitle(&$row, &$params, $page = 0, $context = 'item') {
        $option = 'djcatalog2';
        $factory = JPATH_ROOT . '/components/com_jlexreview/load.php';
        if (! JFile::exists ( $factory ))
            return '';
        require_once $factory;
		$html = '<div class="djc_jlexreview_top"><div class="template-reviews-link">';
		$html.= '<a class="modal" rel="{handler: \'adopt\', target: \'template-reviews\', size: {x: 800, y: \'100%\'}, '
				.'onOpen: function() { initTemplateReviews(); window.addEvent(\'resize\', function(){ this.resize({x: (window.getSize().x > 800 ? 800 : window.getSize().x - 100), y: window.getSize().y - 100}, true); ' 
				.'var reviewsInner = document.id(\'sbox-content\').getElement(\'.template-reviews\'); '
				.'reviewsInner.setStyle(\'width\', this.size.x); '
				.'reviewsInner.setStyle(\'height\', this.size.y); }.bind(this) ); ' 
				.'window.fireEvent(\'resize\'); }, '
				.'onClose: function() { window.location.reload() }}" >';
		preg_match('/data-count="(\d+)"/', $html, $match);
		$count = isset($match[1]) ? $match[1] : 0;
		$html.= 'Reviews ('.$count.')</a>';
		$html.= JLexReviewLoad::quick_init ( $option, $row->id );
		$html.= '</div></div>';
		
		return $html;
    }
    public function onAfterDJCatalog2DisplayContent(&$row, &$params, $page = 0, $context = 'item') {
        $option = 'djcatalog2';
        
        $factory = JPATH_ROOT . '/components/com_jlexreview/load.php';
        if (! JFile::exists ( $factory ))
            return '';
        $id = JFactory::getApplication()->input->getInt( 'id' );
        if ($id != $row->id)
            return '';
        
        require_once $factory;
		
		$html = '<div class="djc_jlexreview_bottom"><div id="template-reviews" class="template-reviews" style="position: relative;">';
		$html.= JLexReviewLoad::init ( $row->name, $option, $row->id );
		$html.= '</div></div>';
		
		return $html;
    }
}