<?php
/**
* @version		3.0.0
* @package		JomLand TicketCenter
* @copyright	Copyright (C) 2010 JomLand.com. All rights reserved.
* @link 		www.jomland.com
* @license		GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.view');

class JLTicketCenterViewEvent extends JViewLegacy
{
	
	private $_pathway ;
	private $_pageTitle='' ;
	private $_doc='' ;
	private $_pparams;
	private $_itemid;
	private $_viewType = 0;
	
	function __construct(){
		$app 	= JFactory::getApplication();
		$this->_pathway	= $app->getPathway();
		$this->_doc 	= JFactory::getDocument();
		$this->_pparams = new JLTicketCenterHelperSetup;
		
		
		parent::__construct();
	}
	
	function display($tpl = null)
	{
        $app = JFactory::getApplication();

		$this->_doc->addStyleSheet(JURI::base().'components/com_jlticketcenter/assets/jlticketcenter.css');
		$this->_doc->addStyleSheet(JURI::base().'components/com_jlticketcenter/assets/stacktable.css');

        JHtml::_('jquery.framework',  true, true);
        $this->_doc->addScript(JURI::base().'components/com_jlticketcenter/assets/stacktable.js');
        $this->_doc->addScript(JURI::base().'components/com_jlticketcenter/assets/events_list.js');
		$this->_pparams = new JLTicketCenterHelperSetup;
		

		
		$menuitemid = JRequest::getInt('Itemid');
		 if (empty($menuitemid))
		 {
		 	$menuparams = JComponentHelper::getParams('com_jlticketcenter');
		 	
		 	$menu 		= $app->getMenu();
			$bit_itemid = $menu->getItems('component','com_jlticketcenter');
			$this->_itemid  	= $bit_itemid[0]->id;
			
		 }else{
			$menu = $app->getMenu();
		    $menuparams = $menu->getParams($menuitemid);
		    $this->_itemid  	= $menuitemid ;
		    
			$active_menu = $menu->getActive(); 
			if (!empty($active_menu)){
				$this->getMenuVars($active_menu);
			}
		 }
		
		 
	 	$this->_viewType = $menuparams->get('view_type',0);

		$events = $this->get('Events');
		
		
		$this->assignRef('events', $events);
		$this->assignRef('params', $this->_pparams);
		$this->assignRef('menuparams', $menuparams);
		$this->assignRef('itemid',	$this->_itemid);
		
		
		
		if (JRequest::getInt('performer',0)>0){
			$this->setPerformerDetails();
		}
		elseif(JRequest::getInt('venue',0)>0){
			$this->setVenueDetails();
		}
		elseif(JRequest::getInt('city',0)>0){
			$this->setCityDetails();
		}
		elseif(JRequest::getInt('state',0)>0){
			$this->setStateDetails();
		}
		elseif(JRequest::getInt('country',0)>0){
			$this->setCountryDetails();
		}
		else{
			$this->setCategoryDetails();
		}
		
		if ($this->_viewType==1 || $this->_viewType==2){
			if(file_exists(JPATH_SITE.'/components/com_jlticketcenter/helpers/helper_pro.php')){
				$session = JFactory::getSession();
				
				$session->set('JLTC_events',$events);
				$session->set('JLTC_Itemid',$this->_itemid);
				
				JLoader::import('components.com_jlticketcenter.helpers.helper_pro',JPATH_SITE);
				JLTicketCenterProHelper::setupCalender($menuparams);

				if($this->_viewType==1){
					$this->setLayout('calender');	
				}else{
					$calender_div = "<div id='JLticketCenter_calendar'></div><br />";
					$this->assignRef('calender', $calender_div );	
				}
				
			}else{
				echo '<span color="red">CALENDER VIEW IS WORKING ONLY ON PRO VERSION !</span>';
			}
			
		} 

		parent::display($tpl);
		
		JLTicketCenterLinksHelper::renderinfo();
	}//EOF display
	
	private function setPerformerDetails(){
		$performer = $this->get('Performer');
		if ($performer){
				if ($performer->cat_id && $performer->cat_title){
					$this->_pathway->addItem($performer->cat_title, JRoute::_('index.php?option=com_jlticketcenter&view=category&id='.$performer->cat_id.'&Itemid='.$this->itemid));
					$this->_pageTitle = ' - '.$performer->cat_title;
				}
	 		$this->_pathway->addItem($performer->performer_name, '');
	 		$this->_pageTitle = $performer->performer_name.$this->_pageTitle;
	 		
	 		$this->_doc->setTitle(JText::_('Events').' - '.$this->_pageTitle);	
		}
		
		$this->assignRef('performer', $performer);
		$this->setLayout('performer');
	}//EOF setPerformerDetails
	
	private function setVenueDetails(){
		if ($this->events){
			$venue = current($this->events);
		}
		else{
			$venue = $this->get('Venue');
		}
		
		if (!empty($venue)){
			if ($this->_pparams->get('display_country')>0 && isset ($venue->country)){
				$this->_pathway->addItem($venue->country, JRoute::_('index.php?option=com_jlticketcenter&view=venues&country='.$venue->country_id.'&Itemid='.$this->itemid));
				$this->_pageTitle = ' - '.$venue->country;
			}
		
			if ($venue->state_id){
				$this->_pathway->addItem($venue->state_name, JRoute::_('index.php?option=com_jlticketcenter&view=venues&state='.$venue->state_id.'&Itemid='.$this->itemid));
				$this->_pageTitle = ' - '.$venue->state_name.$this->_pageTitle ;
			}
			
			
			$this->_pathway->addItem($venue->city_name, JRoute::_('index.php?option=com_jlticketcenter&view=event&city='.$venue->city_id.'&Itemid='.$this->itemid));
			$this->_pageTitle = ' - '.$venue->city_name.$this->_pageTitle ;
			
			$this->_pathway->addItem($venue->venue_name, '');
			
			$this->_pageTitle = $venue->venue_name.$this->_pageTitle ; 
			
			$this->_doc->setTitle(JText::_('Events').' - '.$this->_pageTitle);
		}
		
		$this->assignRef('venue', $venue);
		$this->setLayout('venue');
				
		
	}//EOF setVenueDetails
	
	private function setCityDetails(){
		if ($this->events){
			$city  = current($this->events);
		}else{
			$city  = $this->get('City');
		}
		if (!empty($city)){
			if ($this->_pparams->get('display_country')>0){
				$this->_pathway->addItem($city->country, JRoute::_('index.php?option=com_jlticketcenter&view=venues&country='.$city->country_id.'&Itemid='.$this->itemid));
				$this->_pageTitle = ' - '.$city->country;
			}
		
			if ($city->state_id){
				$this->_pathway->addItem($city->state_name, JRoute::_('index.php?option=com_jlticketcenter&view=venues&state='.$city->state_id.'&Itemid='.$this->itemid));
				$this->_pageTitle = ' - '.$city->state_name.$this->_pageTitle ;
			}
			
			$this->_pathway->addItem($city->city_name, JRoute::_('index.php?option=com_jlticketcenter&view=event&city='.$city->city_id.'&Itemid='.$this->itemid));
			$this->_pageTitle = $city->city_name.$this->_pageTitle ;
			
			$this->_doc->setTitle(JText::_('Events').' - '.$this->_pageTitle);
		}
						
		$this->assignRef('city', $city);
		$this->setLayout('city');
	}//EOF setCityDetails
	
	private function setStateDetails(){
		if ($this->events){
			$state  = current($this->events);
		}
		else{
			$state = $this->get('State');
		}
		if (!empty($state)){
			if ($this->_pparams->get('display_country')>0){
				$this->_pathway->addItem($state->country, JRoute::_('index.php?option=com_jlticketcenter&view=venues&country='.$state->country_id.'&Itemid='.$this->_itemid));
				$this->_pageTitle = ' - '.$state->country;
			}else{
				$this->_pathway->addItem($state->state_name, JRoute::_('index.php?option=com_jlticketcenter&view=venues&state='.$state->state_id.'&Itemid='.$this->_itemid));
				$this->_pageTitle = ' - '.$state->state_name.$this->_pageTitle ;
			}
			
			$this->_pageTitle = $state->state_name.$this->_pageTitle ;
			
			$this->_doc->setTitle(JText::_('Events').' - '.$this->_pageTitle);
		}
			
		
		$this->assignRef('state', $state);
		$this->setLayout('state');
		
	}//EOF setStateDetails
	
	private function setCountryDetails(){
		if ($this->events){
			$country  = current($this->events);
		}
		else{
			$country  = $this->get('Country');
		}
		if (!empty($country)){
			
			$this->_pathway->addItem($country->country, JRoute::_('index.php?option=com_jlticketcenter&view=venues&country='.$country->country_id.'&Itemid='.$this->itemid));
			$this->_pageTitle = ' - '.$country->country;
			
			$this->_doc->setTitle(JText::_('Events').$this->_pageTitle);
						
		}
		$this->assignRef('country', $country);
		$this->setLayout('country');
	}//EOF setCountryDetails
	
	private function setCategoryDetails(){
		if ($this->events){
			$category  = current($this->events);
			$this->_pathway->addItem($category->cat_name, JRoute::_('index.php?option=com_jlticketcenter&view=event&category='.$category->cat_id.'&Itemid='.$this->itemid));
			$this->_pageTitle = $category->cat_name.$this->_pageTitle ;
			$this->_doc->setTitle(JText::_('Events').' - '.$this->_pageTitle);
		}
		$this->assignRef('category', $category);
		$this->setLayout('category');
	}//EOF setCategoryDetails

	
	private function getMenuVars($active_menu){

		if (!empty($active_menu->query['layout'])){
			switch ($active_menu->query['layout']){
				case 'venue':
					JRequest::setVar('venue',$active_menu->query['venue']);
				break;
				
				case 'city':
					JRequest::setVar('city',$active_menu->query['city']);
				break;
				
				case 'category':
					$cat = 0;
					if (!empty($active_menu->query['category'])){
						$cat = $active_menu->query['category'];
					}
					JRequest::setVar('category',$cat );
				break;
				
			}
			
			
		}
						
	}
} // EOC VIEW
