<?php
/**
* @version		1.1
* @package		Band in Jomland
* @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
*/

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
JLoader::import('components.com_jlticketcenter.models.event',JPATH_SITE);

class modJLTCenterEventHelper
{
    public static function getEvents($params){
    	$db = JFactory::getDBO();
    	$where_array = array();

		$join ='';
    	JRequest::setVar('num_of_event',$params->get('num_of_event',5));
    	switch ($params->get('display',0)){
    		case 1:
    			JRequest::setVar('jltcenter_order_by',1);
    		break;
    		
    		case 2:
    			JRequest::setVar('jltcenter_feature',1);
    		break;
    	}
    	$join .= " LEFT JOIN #__jltcenter_city city  ON city.`id`=e.`city_id` ";
		$join .= " LEFT JOIN #__jltcenter_venue v  ON v.`id`=e.`venue_id` ";
		$join .= " LEFT JOIN #__jltcenter_state state  ON state.`id`=e.`state_id` ";
		$join .= " LEFT JOIN #__jltcenter_country country ON country.`id`=e.`country_id` ";
	
		$where_array[] ='e.`published`=1';
		$where_array[] ='e.`event_date`>NOW()';
		
		$query = 'SELECT e.*,p.`performer_name`,v.`venue_name`,city.`city_name`,state.`state_name`,country.`country`  AS `country`, cat.`title` as `cat_name`,p.`cat_id`,cat.`description` as `cat_description`  '
				. ' FROM #__jltcenter_events e';
		
		$query .= " LEFT JOIN #__jltcenter_performer p  ON p.`id`=e.`performer_id` ";
		$query .= " LEFT JOIN #__categories cat ON cat.`id`=p.`cat_id`";
		$query .= $join;
		
		
		if(JRequest::getInt('jltcenter_feature',0)){
			$where_array[] ='e.`feature`=1'; 
		}
						
		$query .=" WHERE ".implode(' AND ',$where_array);
		
		switch (JRequest::getInt('jltcenter_order_by',0)){
			case 1: //Popular
				$query .=" ORDER BY `hits`,`event_date`";
			break;
			
			default:
				$query .=" ORDER BY `event_date`";
			break;
		}
		
		if (JRequest::getInt('num_of_event',0)>0){
			$query .=" LIMIT ".JRequest::getInt('num_of_event');
		}
		$db->setQuery($query);
		$events= $db->loadObjectList();

		JRequest::setVar('jltcenter_feature',null);
    	return $events;
    } //EOF getEVents
    
    public static function addJScript($params,$list){
	
		
		$sliderId = $params->get('slider_id','jltc_slider_mask');
		$width = $params->get('width','200');
		$height = $params->get('height','200');
		
		$interval = $params->get('interval','2000');
		$autoPlay = $params->get('autoPlay',0);
		$next_prev_btn= $params->get('prev_next_buttons',1);
		$play_btn= $params->get('play_button',0);
		$stop_btn= $params->get('stop_button',0);
		
		//$sliderCss2 = JURI::root().'modules/mod_jltcenter_events/assest/jltcenter_events.php?width='.$width."&height=".$height."&slider_id=".$sliderId;
		$sliderCss2 = JURI::root().'modules/mod_jltcenter_events/assest/jquery.bxslider.css';
		$sliderJS = JURI::root().'modules/mod_jltcenter_events/assest/jquery.bxslider.min.js';
		
		$doc = JFactory::getDocument();

        JHtml::_('jquery.framework');

		//JHtml::_('behavior.framework');

		$doc->addStyleSheet($sliderCss2);
		$doc->addScript($sliderJS);
		
		$itemCount = count($list);
		
		$items_js ="1";
		$i=2; 
		for ($i;$i<=$itemCount;$i++){
			$items_js .=",$i";
		} 
		
		$sliderInt = "	window.addEvent('domready',function(){
								var hs".$sliderId." = new noobSlide({
									box: $('".$sliderId ."'),
									items: [".$items_js."],
									size:".$width.",
									interval: ".$interval .",
									startItem: 0,
									autoPlay:".$autoPlay; 
		

        if ($next_prev_btn || $play_btn || $stop_btn){
			$sliderInt .=",
						addButtons: {";
			
			if ($next_prev_btn){
					$sliderInt .="previous: $('prev_".$sliderId."'),
								next: $('next_".$sliderId."')";
			}
			if ($play_btn){
				if ($next_prev_btn){
					$sliderInt .=",";
				}
				$sliderInt .="play: $('play1')";
			}
				
			if ($stop_btn){
				if ($next_prev_btn || $play_btn){
					$sliderInt .=",";
				}
				$sliderInt .="stop: $('stop1')";
			}
				
			$sliderInt .=" 	}";
		}
		$sliderInt .="							
								});
							});";


        $sliderInt  = "jQuery(document).ready(function(){
                        jQuery('#".$sliderId."').bxSlider({  nextSelector: '#next_".$sliderId."',
                              prevSelector: '#prev_".$sliderId."',
                              nextText: 'Next &gt;&gt;',
                              prevText: '&lt;&lt; Previous'

                            });

                        });";


		$doc->addScriptDeclaration($sliderInt);
		return;
	} // EOF addJScript
} // EOC
