<?php

//global $Debug; $Debug=true;
class ProductView extends Slot {
	var $SectionID = 23;
	var $ModuleID = 2;
	var $sale = 0;
	var $category = 0;
	var $categoryURL = 0;
	var $rootcategory = 0;
	var $productline = array();
	var $productURL = '';	
	var $productcategoryID = 0;

	function __construct($pquery) {
		parent::__construct($pquery);
	}

	function init() {
		if (isset($_POST["ajax"]) && $_POST["ajax"] == 'filterScroll') {
			$id = !empty($this->ID)? $this->ID : (isset($this->LocalTarget[0])? intval($this->LocalTarget[0]) : 0);
			if(empty($id)) exit;
			else {
				$this->Action->processEvent($this->SectionID,'open',array('id' => $id,'ship' => true,'catid' => $this->_ModuleID,'children' => 0),$this);
				$this->getCategory(); 
				if (isset($_POST["sort"]) && in_array($_POST["sort"], array("generation", "level", "core")) ) { 
					$this->getProductLine($_POST["idType"], $_POST["sort"]);
				} else $this->getProductLine($_POST["idType"], 'generation');
				$html = '';
				if (!empty($this->TempFillEls['productline']))
					foreach($this->TempFillEls['productline'] as $key => $value){
						$html .=  '<li'.($value['id'] == $this->PreparedTemplates['id']? ' class="active" style="float:inherit;"   > ':'   style="float:inherit;"  >').
								'<a class="name" href="'.$value['url'].'">'.
								'<img alt="" src="/images/products/'.$value['id'].'/middle.jpg" class="image">'.$value['name'].
								'</a><span class="price">'.$value['price'].'</span></li>';
					}
				print json_encode(array( "status" => "success", "link" => "", "html" => $html ));		
				exit;
			}
		}
		if (isset($_POST["ajax"]) && $_POST["ajax"] == 'ajax-form-expadv'){
   			$this->Action->processEvent($this->SectionID,'sendaskexpert',array($_POST),$this);
   			print json_encode(array( "status" => "success"));		
			exit;
		}
		if (isset($_POST["ajax"]) && $_POST["ajax"] == 'ajax-form-rfq'){ 
   			$this->Action->processEvent($this->SectionID,'sendrfq',array($_POST),$this);
   			print json_encode(array( "status" => "success"));		
			exit;
		}
		if (isset($_POST["ajax"]) && $_POST["ajax"] == 'ajax-form-dealalert'){
   			$this->Action->processEvent($this->SectionID,'senddealalert',array($_POST),$this);
   			print json_encode(array( "status" => "success"));		
			exit;
		}
		if (isset($_POST["ajax"]) && $_POST["ajax"] == 'ajax-form-contactus'){
   			$this->Action->processEvent($this->SectionID,'sendcontactus',array($_POST),$this);
   			print json_encode(array( "status" => "success"));		
			exit;
		}
		
		$id = !empty($this->ID)? $this->ID : (isset($this->LocalTarget[0])? intval($this->LocalTarget[0]) : 0);
		if(empty($id)) header('Location: '.$this->Home.'/Units'.$this->SID);
		else {
			$this->Action->processEvent($this->SectionID,'open',array('id' => $id,'ship' => true,'catid' => $this->_ModuleID,'children' => 0),$this);
			if(!empty($this->PreparedTemplates['_error'])) header('Location: '.$this->Home.'/Units'.$this->SID);
				else if(!empty($this->PreparedTemplates['path2']) && implode('/',$this->Target)!='/'.$this->PreparedTemplates['path2']) header('Location: /'.$this->PreparedTemplates['path2'].$this->SID);
					else $this->Action->processEvent($this->SectionID,'getcart_status',array(),$this);
			$this->Action->processEvent($this->SectionID,'getcat_for_acc',array(),$this);
			$this->acc_category = $this->PreparedTemplates['category'];
			$this->rss = makeLink($this->Home.implode('/',$this->Target).'.xml','RSS',array(),'','','RSS Feed','target="_blank"');
		}
	}
	
	function getGenerationLine($gen) {
		$generation = $this->Db->SelectAny("select distinct name,sort_order from generation order by sort_order" );
		foreach($generation as $key => $value){
			$this->TempFillEls['gen'][] = array(
				'name' => $value['name'],
				'name' => $value['name'],
				'current' => ($value['name'] == $gen)? 'current':'',
			);
		}
	}
	
	function getLevelLine($gen) {
		$generation = $this->Db->SelectAny("select distinct name,sort_order from level order by sort_order" );
		foreach($generation as $key => $value){
			$this->TempFillEls['level'][] = array(
				'name' => $value['name'],
				'name' => $value['name'],
				'current' => ($value['name'] == $gen)? 'current':'',
			);
		}
	}
	
	function getCoreLine($gen) {
		$generation = $this->Db->SelectAny("select distinct name,sort_order from core order by sort_order" );
		foreach($generation as $key => $value){
			$this->TempFillEls['core'][] = array(
				'name' => $value['name'],
				'name' => $value['name'],
				'current' => ($value['name'] == $gen)? 'current':'',
			);
		}
	}

	function getProductLine($generation, $type) {
		global $REGISTRY;
		$this->productline = $this->Db->SelectAny("	select p.id, p.name, pr.value as price
								from product p left join categorylineitem cl on p.id = cl.item
								     left join price pr on p.id = pr.ownerid   
								where pr.current = 1 and p.".$type." in (select id from ".$type." where name like '".$generation."') and p.is_active = 1 and cl.ownerid = " .$this->category);
		foreach($this->productline as $key => $value)
			foreach($REGISTRY['VirtualSlotTarget']['main'] as $k => $v) 
				if( $v  == 'ProductView:object?id='.$value['id']){
					$this->productline[$key]['url'] = $k;
					break;
				}
		foreach($this->productline as $key => $value){
			$this->TempFillEls['productline'][] = array(
				'id' => $value['id'],	
				'name' => $value['name'],
				'price' => '$ '.number_format($value['price'], 2,'.',','),
				'url' => $value['url'],
				'active' => ($value['id'] == $this->PreparedTemplates['id'])? array(array()):array(),
			);
		}
	}

	function getAllProductLine() {
		global $REGISTRY;
		$this->productline = $this->Db->SelectAny("	select p.id, p.name, pr.value as price
								from product p left join categorylineitem cl on p.id = cl.item
									left join price pr on p.id = pr.ownerid  
								where pr.current = 1 and p.is_active = 1 and cl.ownerid = " .$this->category);
		foreach($this->productline as $key => $value)
			foreach($REGISTRY['VirtualSlotTarget']['main'] as $k => $v) 
				if( $v  == 'ProductView:object?id='.$value['id']){
					$this->productline[$key]['url'] = $k;
					break;
				}
		foreach($this->productline as $key => $value){
			$this->TempFillEls['productline'][] = array(
				'id' => $value['id'],	
				'name' => $value['name'],
				'price' => '$ '.number_format($value['price'], 2,'.',','),
				'url' => $value['url'],
				'active' => ($value['id'] == $this->PreparedTemplates['id'])? array(array()):array(),
			);
		}
	}
	
	function fillElements() {
		global $REGISTRY;
		$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID, 'open');
		$this->getCategory();
		$this->TempFillEls = array(
			'id' => $this->PreparedTemplates['id'],
			'path' => $this->PreparedTemplates['path'],
			'type' => $this->PreparedTemplates['item'],
			'name' => $this->PreparedTemplates['name'],
			'distance_chart_activity' => !empty ( $this->PreparedTemplates['distance_chart_activity']) ?  $this->PreparedTemplates['distance_chart_activity'] : 0,
			'nav_name' => $this->PreparedTemplates['nav_name'],
			'short_desc' => $this->PreparedTemplates['short_desc'],
			'short_desc2' => $this->PreparedTemplates['short_desc2'],			
			'video_desc' => $this->PreparedTemplates['video_desc'],
			'full_desc' => $this->PreparedTemplates['full_desc'],
			'pid' => $this->PreparedTemplates['pid'],
			'Warranty' =>  $this->PreparedTemplates['warranty']['name'],
			'exp' =>  ($this->PreparedTemplates['export'] == 1? array():  array(array()) ),
			'policy' => $this->PreparedTemplates['policy']['id']? array(array('text' => $this->PreparedTemplates['policy']['text'])) : array(),		
			'Export' => $this->PreparedTemplates['export_restriction']['id']? array(array('id' => $this->PreparedTemplates['export_restriction']['id'],'name' => $this->PreparedTemplates['export_restriction']['name'])) : array(),
			'return_url' => $this->Home.$REGISTRY['Request']['RawTarget'],
			'features_retailer' => $this->PreparedTemplates['special_offer'] != ' ' && !empty($this->PreparedTemplates['special_offer'])?  array(array('text' => $this->PreparedTemplates['special_offer'])) : array(),  
		);		
		if ($this->rootcategory==56 || $this->category == 52) { // thermal imaging
			if ($this->PreparedTemplates['rate']['id'] != 0 ) $this->TempFillEls['generation'] =  array(array('Generation' => $this->PreparedTemplates['rate']['name']));
		} 
//------added for digital 
		else if ($this->rootcategory==166 ) { // digital
			if ($this->PreparedTemplates['level']['id'] != 0 ) $this->TempFillEls['generation'] =  array(array('Level' => $this->PreparedTemplates['level']['name']));
		}
//------
		  else 
		 	if ($this->PreparedTemplates['generation']['id'] != 0 )$this->TempFillEls['generation'] =  array(array('Generation' => $this->PreparedTemplates['generation']['mask']));
			
		$selected = $this->Db->SelectAny('select name from category where id = '.$this->category);
		$this->TempFillEls['cat_name'] = $selected[0]['name'];  					
		$this->TempFillEls['cat_url'] = $this->categoryURL;
		$path = $this->ImagePath.'/products/'.$this->PreparedTemplates['id'];
		//if(file_exists($path.'/ug.pdf')) $this->TempFillEls['UG'][0]['link'] = '"'.$this->Home.'/NightVisionDownload/'.$this->PreparedTemplates['path'].'/DownloadUserGuidePdf"          onclick="win_open(\'NightVisionPDF\',\''.$this->PreparedTemplates['path'].'/'.$this->PreparedTemplates['path'].'_UserGuide.pdf\');return true"';
		if(!empty($this->PreparedTemplates['files'])) {
				foreach($this->PreparedTemplates['files'] as $file_key => $file_value) {	//printArray($file_value);exit;
					if($file_value['filetype']==7) { 
						if(file_exists($path.'/userguides/'.date2pg($file_value['number']).'.pdf')) 
							$this->TempFillEls['UG'][] = array(
											'link' => '"'.$this->Home.'/NightVisionDownload/'.$this->PreparedTemplates['path'].'/DownloadUserGuidePdf/'.date2pg($file_value['number']).'"          onclick="win_open(\'NightVisionPDF\',\''.$this->PreparedTemplates['path'].'/'.$this->PreparedTemplates['path'].'_UserGuide_'.date2pg($file_value['number']).'.pdf\');return true"',
											'description'  => $file_value['description'],
							);
					}
				}
		}		
		
		if(file_exists($path.'/ss.pdf')) $this->TempFillEls['SS'][0]['link'] = '"'.$this->Home.'/NightVisionDownload/'.$this->PreparedTemplates['path'].'/DownloadSpecificationSheetPdf" onclick="win_open(\'NightVisionPDF\',\''.$this->PreparedTemplates['path'].'/'.$this->PreparedTemplates['path'].'_SpecSheet.pdf\');return true"';
		$this->TempFillEls['ALLZip'][0]['link'] = makeLink($this->Home.'/NightVisionDownload/'.$this->PreparedTemplates['path'].'/DownloadAllZip',makeImage($this->ImagePath.'/buttons/download.gif',$this->ImageWPath.'/buttons/download.gif',(!empty($this->PreparedTemplates['nav_name'])? $this->PreparedTemplates['nav_name'] : $this->PreparedTemplates['name']).' - Download Package'),array(),'',' onclick="win_open(\'DownloadAllZip\',\''.$this->PreparedTemplates['path'].'/'.$this->PreparedTemplates['path'].'_All.zip\');return true"');

		if(!empty($this->PreparedTemplates['prices']) && $this->PreparedTemplates['prices'][0]['value'] != 0) {
			if ($this->PreparedTemplates['callforquote'] == 0) {
				$this->TempFillEls['prices'][0]['price'] =   $this->getPrice($this->PreparedTemplates['prices'], false);
				$this->TempFillEls['price_NOcallforquote'] = array(array('navname' => $this->PreparedTemplates['nav_name'],
																		  'id' => $this->PreparedTemplates['id'] 
																 ));
			}
			if ($this->PreparedTemplates['callforquote'] == 1) {
				$this->TempFillEls['prices'][0]['price'] = '';
				$this->TempFillEls['price_callforquote'] = array(array(
																'name' => $this->PreparedTemplates['name'],
																'pid' => $this->PreparedTemplates['pid'],
															));
			} 
			// for google checkout
/*			$this->TempFillEls['google_price'] = str_ireplace('\$|<span>|</span>| ','',$this->getPrice($this->PreparedTemplates['prices'],true));
			preg_match('/\d+\D\d+/', $this->TempFillEls['google_price'], $matches); 
			$this->TempFillEls['google_price'] = number_format( str_replace(' ','',$matches[0]),2,'.','') ;*/
		}
		$video = 0;
		if(!empty($this->PreparedTemplates['video_desc'])) {
				$this->PreparedTemplates['video_desc'] = rtrim(str_replace(";", "", $this->PreparedTemplates['video_desc'] ), ";");
				$this->TempFillEls['video'][0] = array( 'video_desc' => $this->PreparedTemplates['video_desc'] );
				$this->TempFillEls['images'][0]['video_prew'][0]['video_desc'] = $this->PreparedTemplates['video_desc'];
				$video = 1;  
		}
		 		
		if(!empty($this->PreparedTemplates['productlineitems'])) {
			$acc_opt = array();
			$acc_inc = array();
			$count = count($this->PreparedTemplates['productlineitems']);
			for($i = 0; $i < $count; $i++) {
				if($this->PreparedTemplates['productlineitems'][$i]['optional'])
					$acc_opt[] = $this->PreparedTemplates['productlineitems'][$i]['lineitem'];
				else $acc_inc[] = $this->PreparedTemplates['productlineitems'][$i]['lineitem'];
			}
			$count = count($acc_inc);
			for($i = 0; $i < $count; $i++) {
				$id_path = $acc_inc[$i]['id'];
				foreach($acc_inc[$i]['files'] as $acc_key => $acc_value) {
					if($acc_value['filetype']==8) {
						$id_path = !empty($acc_value['image_link'])? $acc_value['image_link'] : $acc_inc[$i]['id'];
					}
				}
				$i_path = file_exists($this->ImagePath.'/products/'.$id_path.'/prew.jpg')? '/products/'.$id_path.'/prew.jpg' : '/products/NA/middle.png';
				$this->TempFillEls['Accessories_inc'][0]['accessory_inc'][$i] = array(
					'image' => makeImage($this->ImagePath.$i_path, $this->ImageWPath.$i_path, $acc_inc[$i]['name'], '', array(), 0, array('115') ),
					'name' => $acc_inc[$i]['name'],
				);
			}
			$count = count($acc_opt);
			$j = 2;
			for($i = 0; $i < $count; $i++) {
					$id_path = $acc_opt[$i]['id'];
					foreach($acc_opt[$i]['files'] as $acc_key => $acc_value) {
						if($acc_value['filetype']==8) {
							$id_path = !empty($acc_value['image_link'])? $acc_value['image_link'] : $acc_opt[$i]['id'];
						}
					}
					$i_path = file_exists($this->ImagePath.'/products/'.$id_path.'/prew.jpg')? '/products/'.$id_path.'/prew.jpg' : '/products/NA/middle.png';
					$image = makeImage($this->ImagePath.$i_path, $this->ImageWPath.$i_path, $acc_opt[$i]['name'], '', array(), 0, array('115')  );
					$acc_path = $this->Home.$this->getCategoryPath($this->acc_category).'/'.$this->Target[1];
					$this->TempFillEls['Accessories_opt'][0]['accessory_opt'][$i] = array(
						'image' => is_numeric($acc_opt[$i]['prices'][0]['value'])? makeLink( $acc_path,$image,array(),'acc-'.$acc_opt[$i]['id'],'thumb_img') : $image,
						'name' => is_numeric($acc_opt[$i]['prices'][0]['value'])? makeLink($acc_path,$acc_opt[$i]['name'],array(),'acc-'.$acc_opt[$i]['id']) : $acc_opt[$i]['name'],
						'price' => is_numeric($acc_opt[$i]['prices'][0]['value'])? '$ '.number_format($acc_opt[$i]['prices'][0]['value'],2,'.',',') : 'Call for quote',
						'price2' => is_numeric($acc_opt[$i]['prices'][0]['value'])? '$ '.number_format($acc_opt[$i]['prices'][0]['value'],2,'.',',') : 'Call for quote',
						'id' => $acc_opt[$i]['id'],
						'product_price' => str_ireplace('\$|<span>|</span>| ','',$this->getPrice($this->PreparedTemplates['prices'],true)),
						'display' => $this->sale && is_numeric($acc_opt[$i]['prices'][0]['value'])? 'inline' : 'none',
					);
					if(is_numeric($acc_opt[$i]['prices'][0]['value'])) {
						$accessory_calc[] = array(
							'name' => $acc_opt[$i]['name'],
							'id' => $acc_opt[$i]['id'],
						);
					}
				//}
			}
		}
		/*if($this->sale) {
			$this->TempFillEls['Calculator'][0] = array(
				'name' => $this->PreparedTemplates['name'],
				'total_price' => $this->getPrice($this->PreparedTemplates['prices'],false),
				'prod_price' => $this->getPrice($this->PreparedTemplates['prices'],false),
				'accessory_calc' => !empty($accessory_calc)? $accessory_calc : array(),
			);
		}*/
		if(!empty($this->PreparedTemplates['item']['children'])) {
			$children = array();
			$count = count($this->PreparedTemplates['item']['children']);
			for($i = 0; $i < $count; $i++) {
				if($this->PreparedTemplates['item']['children'][$i]['id']!=$this->PreparedTemplates['id']) {
					$children[$this->PreparedTemplates['item']['children'][$i]['generation']['id']][] = array(
						'id' => $this->PreparedTemplates['item']['children'][$i]['id'],
						'name' => $this->PreparedTemplates['item']['children'][$i]['name'],
						'price' => $this->getPrice( $this->PreparedTemplates['item']['children'][$i]['prices'], true),
						'generation' => $this->PreparedTemplates['item']['children'][$i]['generation']['name'],
						'sort_order' => $this->PreparedTemplates['item']['children'][$i]['generation']['sort_order'],
						'path' => $this->PreparedTemplates['item']['children'][$i]['path'],
					);
				}
			}
			foreach($children as $key => $value) {
				$count = count($value);
				for($i = 0; $i < $count; $i++) {
					$link = '';
					foreach($REGISTRY['VirtualSlotTarget']['main'] as $path => $handler) {
						if($handler == 'ProductView:object?id='.$value[$i]['id']) {
							$link = $path;
							continue;
						}
					}
					$this->TempFillEls['Children'][0]['children'][$key]['child'][$i] = array(
						'link' => makeLink($this->Home.$link, $value[$i]['name'], array(), '', 'class="n"'),
						'price' => $value[$i]['price'],
						'compare' => '/'.strtolower(str_replace(' ','-',$this->PreparedTemplates['color']['label'])).'_'.$this->PreparedTemplates['path'].'_compare_'.$value[$i]['path'],
					);
				}
			}
		}
		//-------This system is also available in
		if(!empty($this->PreparedTemplates['relatedlineitems'])) {
			$children = array();
			$count = count($this->PreparedTemplates['relatedlineitems']);
			for($i = 0; $i < $count; $i++) {
				if($this->PreparedTemplates['relatedlineitems'][$i]['lineitem']['id']!=$this->PreparedTemplates['id']) {
					$children[$this->PreparedTemplates['relatedlineitems'][$i]['lineitem']['generation']['id']][] = array(
						'id' => $this->PreparedTemplates['relatedlineitems'][$i]['lineitem']['id'],
						'name' => $this->PreparedTemplates['relatedlineitems'][$i]['lineitem']['name'],
						'price' => $this->getPrice( $this->PreparedTemplates['relatedlineitems'][$i]['lineitem']['prices'], false),
						'generation' => $this->PreparedTemplates['relatedlineitems'][$i]['lineitem']['generation']['name'],
						'sort_order' => $this->PreparedTemplates['relatedlineitems'][$i]['lineitem']['generation']['sort_order'],
						'path' => $this->PreparedTemplates['relatedlineitems'][$i]['lineitem']['path'],
					);
				}
			}
			foreach($children as $key => $value) {
				$count = count($value);
				for($i = 0; $i < $count; $i++) {
					$link = '';
					foreach($REGISTRY['VirtualSlotTarget']['main'] as $path => $handler) {
						if($handler == 'ProductView:object?id='.$value[$i]['id']) {
							$link = $path;
							continue;
						}
					}
					$this->TempFillEls['Children'][0]['children'][$key]['child'][$i] = array(
						'link' => makeLink($this->Home.$link, $value[$i]['name'], array(), '', 'class="n"'),
						'price' => $value[$i]['price'],
						'compare' => '/'.strtolower(str_replace(' ','-',$this->PreparedTemplates['color']['label'])).'_'.$this->PreparedTemplates['path'].'_compare_'.$value[$i]['path'],
					);
				}
			}
		}
		//-----------------------
		if(!empty($this->PreparedTemplates['parameters'])) {
			$count = count($this->PreparedTemplates['parameters']);
			$ind = 0;
			for($i = 0; $i < $count; $i++) {
				if (!empty($this->PreparedTemplates['parameters'][$i]['value'])){
					$this->TempFillEls['parameters'][0]['parameter'][$i] = array(
						'name' => $this->PreparedTemplates['parameters'][$i]['name'],
						'value' => $this->PreparedTemplates['parameters'][$i]['value'],
						'class' =>  $ind%2? 'class="odd"' : '',
					);
					$ind++;
				}
			}
		}
		if(!empty($this->PreparedTemplates['features'])) {
			$count = count($this->PreparedTemplates['features']);
			for($i = 0; $i < $count; $i++) {
				$this->TempFillEls['features'][0]['feature'][$i] = array(
					'value' => $this->PreparedTemplates['features'][$i]['value'],
				);
			}
		}
		if(!empty($this->PreparedTemplates['reviews'])) {
			$count = count($this->PreparedTemplates['reviews']);
			for($i = 0; $i < $count; $i++) {
				$this->TempFillEls['reviews'][0]['review'][$i] = array(
					'url' => $this->PreparedTemplates['reviews'][$i]['url'],
					'image_url' => $this->PreparedTemplates['reviews'][$i]['image_url'],
					'image_alt' => $this->PreparedTemplates['reviews'][$i]['image_alt'],
					'image_title' => $this->PreparedTemplates['reviews'][$i]['image_title'],
					'description' => $this->PreparedTemplates['reviews'][$i]['description'],
				);
			}
		}
		
		if(!empty($this->PreparedTemplates['articlelineitems'])) {
			$count = count($this->PreparedTemplates['articlelineitems']);
			for($i = 0; $i < $count; $i++) {
				$this->TempFillEls['articles'][0]['article'][$i] = array(
					'value' => makeLink($this->Home.'/ExpertOpinion/'.$this->PreparedTemplates['articlelineitems'][$i]['lineitem']['path'],$this->PreparedTemplates['articlelineitems'][$i]['lineitem']['text_in_link'],array(),'','target="_blank"'),
				);
			}
		}
		if(!empty($this->PreparedTemplates['files'])) {
			$path = '/products/'.$this->PreparedTemplates['id'];
			$count = count($this->PreparedTemplates['files']);
			$images_big = array();
			$images_middle = array();
			$images_small = array();
			$j = 0;
			$count_3d = 0;
			for($i = 0; $i < $count; $i++) {
				if($this->PreparedTemplates['files'][$i]['filetype']==2) {
					$this->TempFillEls['big_image_id'] = !empty($this->PreparedTemplates['files'][$i]['image_link'])? $this->PreparedTemplates['files'][$i]['image_link'] : $this->PreparedTemplates['id'];
					$this->TempFillEls['butt_image_id'] = !empty($this->PreparedTemplates['files'][$i]['image_link'])? $this->PreparedTemplates['files'][$i]['image_link'] : $this->PreparedTemplates['id'];
					$big_path = '/products/'.$this->TempFillEls['big_image_id'];
					$butt_path = '/products/'.$this->TempFillEls['butt_image_id'];
				}
				if($this->PreparedTemplates['files'][$i]['filetype']==3 && validate(trim($this->PreparedTemplates['files'][$i]['imagetype']))) {
					$images_big[$j] = $path.'/images/big/'.date2pg($this->PreparedTemplates['files'][$i]['number']).'.jpg';
					$images_middle[$j] = $path.'/images/middle/'.date2pg($this->PreparedTemplates['files'][$i]['number']).'.jpg';
					$images_small[$j] = $path.'/images/small/'.date2pg($this->PreparedTemplates['files'][$i]['number']).'.jpg';
					$images_butt[$j] = $path.'/images/butt/'.date2pg($this->PreparedTemplates['files'][$i]['number']).'.gif';
					$j++;
				}				
			}
			$this->TempFillEls['3d_display'] = 'none';
			$this->TempFillEls['big_display'] = 'block';
			if(!empty($images_big) ) {
				$count = count($images_big);
				$this->TempFillEls['Slides'][0]['slide_previous'] = $count > 3? array(array()) : array();
				$this->TempFillEls['Slides'][0]['slide_next'] = $count > 3? array(array()) : array();
				for($i = 0; $i < $count; $i++) {
					$this->TempFillEls['js_middle'][$i+1]['value'] = '"'.$this->Home.$this->ImageWPath.$images_middle[$i].($i+1==$count? '"' : '",');
					$this->TempFillEls['js_middle2'][$i+1]['value'] = '"'.$this->ImageWPath.$images_middle[$i].'"';
					$this->TempFillEls['middle_images'][$i+1]['value'] = $this->ImageWPath.$images_middle[$i];
					$this->TempFillEls['middle_images'][$i+1]['alt'] = htmlentities($this->PreparedTemplates['name']).' - Middle Image '.($i+2);
					$this->TempFillEls['js_middle2'][$i+1]['id'] = $i+2;
					$this->TempFillEls['js_big'][$i+1]['value'] = '"'.$this->Home.$this->ImageWPath.$images_big[$i].($i+1==$count? '"' : '",');
					$this->TempFillEls['js_big2'][$i+1]['value'] = '"'.$this->ImageWPath.$images_big[$i].'"';
					$this->TempFillEls['big_images'][$i+1]['value'] = $this->ImageWPath.$images_big[$i];
					$this->TempFillEls['big_images'][$i+1]['alt'] = htmlentities($this->PreparedTemplates['name']).' - Big Image '.($i+2);
					$this->TempFillEls['js_big2'][$i+1]['id'] = $i+2;
					$this->TempFillEls['images'][$i]['middle_images'][0]['value'] = $this->ImageWPath.$images_middle[$i];
					$this->TempFillEls['images'][$i]['small_images'][0]['value'] = $this->ImageWPath.$images_small[$i];
					$this->TempFillEls['images'][$i]['small_images'][0]['alt'] = htmlentities($this->PreparedTemplates['name']).' - Small Image '.($i+2);
					$this->TempFillEls['images'][$i]['big_images'][0]['value'] = $this->ImageWPath.$images_big[$i];
					$this->TempFillEls['images'][$i]['num'] = sprintf("%02s", ($i+1+$video)); 
					
					if($i < 3) {
						$this->TempFillEls['Slides'][0]['slide'][$i+1]['item'][0] = array(
							'image' => makeImage($this->ImagePath.$images_middle[$i],$this->ImageWPath.$images_middle[$i],(!empty($this->PreparedTemplates['nav_name'])? $this->PreparedTemplates['nav_name'] : $this->PreparedTemplates['name']).' - Image '.($i+2),'',array(),0,'','thumb_img_'.($i+2),'onclick="show('.($i+1).')" class="png"'),
							'id' => $i+2,
						);
					}
				}
			}
		}
		if(!empty($this->PreparedTemplates['technologylineitems'])) {
			$technologies = $this->Db->SelectGuide(array('technology g'),array('g.id','g.name'));
			$count = count($this->PreparedTemplates['technologylineitems']);
			for($i = 0; $i < $count; $i++) {
				$this->TempFillEls['Technologies'][0]['Technology'][$i] = array(
					'value' => makeImage($this->ImagePath.'/guides/technology/'.$this->PreparedTemplates['technologylineitems'][$i]['lineitem']['id'].'.png',$this->ImageWPath.'/guides/technology/'.$this->PreparedTemplates['technologylineitems'][$i]['lineitem']['id'].'.png',$technologies[$this->PreparedTemplates['technologylineitems'][$i]['lineitem']['id']],'',array(),0,'','','class="png"'),
				);
			}
		}
		if(!empty($this->PreparedTemplates['recomenduselineitems'])) {
			$recomenduse = $this->Db->SelectGuide(array('recomenduse g'),array('g.id','g.name'));
			$count = count($this->PreparedTemplates['recomenduselineitems']);
			for($i = 0; $i < $count; $i++) {
				$this->TempFillEls['Rec_Uses'][0]['Rec_Use'][$i] = array(
					'value' => makeImage($this->ImagePath.'/guides/recomenduse/'.$this->PreparedTemplates['recomenduselineitems'][$i]['lineitem']['id'].'.png',$this->ImageWPath.'/guides/recomenduse/'.$this->PreparedTemplates['recomenduselineitems'][$i]['lineitem']['id'].'.png',$recomenduse[$this->PreparedTemplates['recomenduselineitems'][$i]['lineitem']['id']]),
					'name' => $recomenduse[$this->PreparedTemplates['recomenduselineitems'][$i]['lineitem']['id']],
				);
			}
		}
		//$this->getProductLine($this->PreparedTemplates['generation']['name']);
		//$this->getGenerationLine($this->PreparedTemplates['generation']['name']);
		//----use different groups of products
		if ($this->rootcategory == 56) { // thermal imaging
				$this->getProductLine($this->PreparedTemplates['core']['name'],'core');
				$this->getCoreLine($this->PreparedTemplates['core']['name']);
		} elseif ($this->rootcategory == 166 ){	    // digital night vision
			$this->getProductLine($this->PreparedTemplates['level']['name'],'level');
			$this->getLevelLine($this->PreparedTemplates['level']['name']);
		  } elseif ($this->rootcategory == 2 ){ 	 // Night_Vision
		  	  $this->getProductLine($this->PreparedTemplates['generation']['name'],'generation');
			  $this->getGenerationLine($this->PreparedTemplates['generation']['name']);
			} else {
				$this->getAllProductLine();
			}
		//------------------------------------
		$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'getcart_status');
		$this->TempFillEls['Shopping_status'][0] = array(
			'qty' => $this->PreparedTemplates['qty'],
			'total' => number_format($this->PreparedTemplates['total'],2,'.',' '),
		);
		$this->TempFillEls['rss'] = $this->rss;
	}

	function getPrice($prices,$format = true) {
		$price = '';
		$this->sale = 0;
		if(count($prices))
			foreach($prices as $value)
				if($value['current']) {
					if(empty($value['value']) || !is_numeric($value['value']) || (double)$value['value'] < 0.01) {
						$price = 'Email for quote';
						$this->sale = 0;
					}
					else {
						$price = '$<span>'.number_format($value['value'],2,'.',($format? ' ' : ',')).'</span>';
						$this->sale = 1;
					}
				}
		return $price;
	}

	function getCategoryPath($id){
		global $Paths;
		if(empty($Paths)) {
			$query = 'SELECT id, sys_name, ownerid FROM category';
			$selected = $this->Db->SelectAny($query);
			foreach($selected as $value)
				$Paths[$value['id']] = array(
					'path' => $value['sys_name'],
					'parent' => $value['ownerid'],
				);
		}
		$p = '';
		while(1){
			if(empty($Paths[$id])) return null;
			if(empty($Paths[$id]['parent']) || $Paths[$id]['parent'] == 1  ) return '/'.$Paths[$id]['path'].$p;
			$p = '/'.$Paths[$id]['path'].$p;
			$id = $Paths[$id]['parent'];
		}
	}
	
    function getCategory() {
        global $REGISTRY;
        foreach($REGISTRY['VirtualSlotTarget']['main'] as $k => $v) 
             if( $v  == 'ProductView:object?id='.$this->PreparedTemplates['id']){
                $this->productURL = $k;
                break;
            }
		$category  = $this->Db->SelectAny("select ownerid from categorylineitem where item = ".$this->PreparedTemplates['id']." order by ownerid ");
	        $this->category = $category[0]['ownerid'];
		if (count($category) >1) {         // overwrite if the product is NOT Access and Download
	        	foreach($category as $k_cat => $v_cat) {
		            $catname = $this->Db->SelectAny('select name,ownerid from category where id = '.$v_cat['ownerid']);
		            if (  stristr($catname[0]['name'], 'Accessories') === FALSE &&  stristr($catname[0]['name'], 'Download') === FALSE ) {
		                    $this->category = $v_cat['ownerid'];
		                break;
		            }
	        	}
    	}
        $this->categoryURL = $this->getCategoryPath($this->category) ; 
        $rootcategory  = $this->Db->SelectAny("select ownerid from category where id = ".$this->category );
        $this->rootcategory = $rootcategory[0]['ownerid']; 
    }
	
}
?>