<?php
/**
* @Copyright Copyright (C) 2012- xml/swf
* @license GNU/GPL http://www.gnu.org/copyleft/gpl.html
**/ 
// No direct access
 
defined('_JEXEC') or die('Restricted access'); 

$version = new JVersion;
$jver = $version->getShortVersion();
if(substr($jver, 0, 3) != '1.5'){
	jimport('joomla.html.parameter');
}

if (!function_exists('Images_gcgallerycatpro')) {
function Images_gcgallerycatpro($cat_id, $params, $curr_link)
{
$componentstr = &JComponentHelper::getComponent( 'com_gallerycatpro' );
$params_comg = new JParameter($componentstr->params);
$images_path = $params_comg->get('pic_path', 'images/gallerycatpro/gallery');
$images_path .= '/';
$ret_arr = array ('flag'=>false, 'xml'=>'');
$query = "Select * FROM #__gallerycatpro WHERE catid = " . $cat_id . " AND publish = 1 ORDER BY ordnum ASC ";
	$db = & JFactory::getDBO();//new ps_DB();
	$db->setQuery($query);
	$prod_q_res = $db->loadObjectList();

$xml_image_tags = "";

$price = "";
	foreach ($prod_q_res as $curr_product) 
{
$ret_arr['flag'] = true;
	if ($params->get( 'showdesc', 'yes' ) == 'yes') {
		$description = '<![CDATA['.strip_tags($curr_product->descr).']]>';
	} else {
		$description =	'';
	}

	if ($params->get( 'show_price', 'yes' ) == 'yes') {
	  if($curr_product->price> 0 ){
			$price = $curr_product->price;
	  }else{
			$price = '';
	  }
	}else{
		$price = '';
	}

	$xml_image_tags .= '
	<item>
		<label>'.$curr_product->name.'</label>
		<thumb>'.JURI::root().$images_path.$curr_product->thumb.'</thumb>
		<main>'.JURI::root().$images_path.$curr_product->image.'</main>
		<description>'.$description.'</description>
		<link window="'.$params->get( 'target', '_self' ).'">'.$curr_product->linkname.'</link>
		<price><regular>'.$price.'</regular><!-- <updated> --></price>
	</item>';
	
}
$ret_arr['xml'] = $xml_image_tags;
return $ret_arr;
}
}

//**** Generate product thumbs
if (!function_exists('create_defalultgcgallerycatpro_xml')) {
function create_defalultgcgallerycatpro_xml($params)
{
$componentstr = &JComponentHelper::getComponent( 'com_gallerycatpro' );
$params_comg = new JParameter($componentstr->params);
$cat_images_path = $params_comg->get('cat_path', 'images/gallerycatpro/galleries');
$cat_images_path .= '/';
$database = & JFactory::getDBO();//new ps_DB();
$cat_ids                = trim($params->get( 'category_id', '0' ));
$add_query = '';
if ($cat_ids && $cat_ids != 0) {
	$ids  = explode(",", $cat_ids);
	if (is_array($ids)) {
		foreach ($ids as $curr_id) {
			$add_query_arr[] = " id = " . $curr_id . " ";
		}
		$add_query = " AND " . implode("OR", $add_query_arr);
	}
}

$query = "Select * FROM #__gallerycatproc  WHERE publish = 1 " . $add_query . " ORDER BY ordnum ASC ";
$database->setQuery($query);
$cat_q_res = $database->loadObjectList();

$xml_category_tags = "<content>";
foreach ($cat_q_res as $curr_category) 
{

$ret_arr = Images_gcgallerycatpro($curr_category->id, $params, $curr_category->linkit);
	if ($ret_arr['flag']) {
		$xml_category_tags .='
		<category>
			<title><![CDATA['.$curr_category->name.']]></title>	
			<thumb>'.JURI::root().$cat_images_path.$curr_category->thumb.'</thumb>	
			';			
		
		$xml_category_tags .= $ret_arr['xml'];
		$xml_category_tags .= '</category>';
	}
}
$xml_category_tags .= '</content>';
return $xml_category_tags;
}
}

$componentstr = &JComponentHelper::getComponent( 'com_gallerycatpro' );
$params_comg = new JParameter($componentstr->params);
$pic_path = $params_comg->get('pic_path', 'images/gallerycatpro/gallery');

if(substr($jver, 0, 3) == '1.5') {
	$params = &JComponentHelper::getParams('com_gallerycatpro');
} else {
	$app = JFactory::getApplication('site');
	$params =  & $app->getParams('com_gallerycatpro');
}

$bannerWidth                   = intval($params->get( 'bannerWidth', 912 ));
$bannerHeight                  = intval($params->get( 'bannerHeight', 700 ));
$backgroundColor         = trim($params->get( 'backgroundColor', '#CCCCCC' ));
$baseColor         = $params->get( 'baseColor', '0x000000' );
$wmode                 = trim($params->get( 'wmode', 'transparent' )); 

$xml_fname    = 'gallerycatpro';//trim($params->get( 'xml_fname', 'a' ));
$catppv_id = 'xml/' . $xml_fname;

$module_path_swf = JPATH_SITE . DS . 'components'.DS.'com_gallerycatpro'.DS.'sb'.DS.'modules'.DS;

$module_path = JPATH_SITE . DS . $pic_path . DS;
if (!is_dir($module_path .  DS . 'xml')) {
	mkdir($module_path .  DS . 'xml', 0777);
}

$MovieScale=trim($params->get( 'MovieScale'));

$ids    = trim($params->get( 'category_id', 0 ));

if (1 == trim($params->get( 'catppv_flag', '2' ))) {
	$catppv_id .= md5(print_r($params, true));
	//$catppv_id .= str_replace (',', '_', $ids);
}

if (!file_exists($module_path . $catppv_id . '.swf') ) {
	copy($module_path_swf . 'mod_xmlswf_gallery_catpro.swf', $module_path . $catppv_id . '.swf');
}

$show_catdesc_always = $params->get( 'show_catdesc_always', 'yes');

if($show_catdesc_always == 'yes'){
	$generate_show_catdesc_always = 'true';
}
else{
		$generate_show_catdesc_always = 'false';
}

$xml_data = '<?xml version="1.0" encoding="UTF-8"?>

<slideshow>
	<mobileSettings>
		<currencySymbol>'.$params->get( 'currency', '$').'</currencySymbol>
		<width>'.$params->get( 'bannerWidth', '710').'</width>
		<height>'.$params->get( 'bannerHeight', '630').'</height>
	</mobileSettings>
	<baseDef fullscreen="'.$params->get( 'fullscreen', '1').'"  showCatDescriptionAlways="'.$generate_show_catdesc_always.'" 
	transitionTime="'.$params->get( 'transitionTime', '1.5').'" 
	autoSlideTimer="'.$params->get( 'autoSlideTimer', '3').'" 
	autoScale="'.$params->get( 'autoScale', '1').'" 
	autoAlign="'.$params->get( 'autoAlign', '1').'" 
	gradientColor="0x'.$params->get( 'gradientColor', 'E8EBEA').'" 
	gradientColor1="0x'.$params->get( 'gradientColor1', 'CCCCCC').'" 
	CatsWidth="'.$params->get( 'CatsWidth', '96').'" 
	catsHeight="'.$params->get( 'catsHeight', '72').'"  
	catsbackColor="0x'.$params->get( 'catsbackColor', 'E2D289').'" 
	arrowsColor= "0x'.$params->get( 'arrowsColor', '666666').'" 
	CatsTitleBarColor="0x'.$params->get( 'CatsTitleBarColor', '000000').'" CatsTextColor="0x'.$params->get( 'CatsTextColor', 'FFFFFF').'" catsImagebackColor="0x'.$params->get( 'catsImagebackColor', '00CC33').'" catsTextSize="'.$params->get( 'catsTextSize', '10').'" 
	ImageHeight="'.$params->get( 'ImageHeight', '450').'" 
	thumbListColor="0x'.$params->get( 'thumbListColor', '000000').'" thumbListAlpha="'.$params->get( 'thumbListAlpha', '50').'" 
	thumbTextColor="0x'.$params->get( 'thumbTextColor', 'FFFFFF').'" thumbButtonColor="0x'.$params->get( 'thumbButtonColor', '515151').'" thumbButtonOverColor="0x'.$params->get( 'thumbButtonOverColor', '171717').'" thumbTextSize="'.$params->get( 'thumbTextSize', '13').'" 
	descColorBack="0x'.$params->get( 'descColorBack', '000000').'" 
	descTextColor="0x'.$params->get( 'descTextColor', 'FFFFFF').'" backTransperency="'.$params->get( 'backTransperency', '0').'" 
	ActiveCat="'.$params->get( 'ActiveCat', '1').'" 
	categoryStatus="'.$params->get( 'categoryStatus', 'show').'"/>
	<priceTag enabled="'.trim($params->get( 'show_price', 'yes')).'">
			<position>'.trim($params->get( 'price_position', 'TL')).'</position>
			<textSize>'.trim($params->get( 'price_textsize', '22')).'</textSize>
			
			<currency>
				<base>
					<color code="#'.trim($params->get( 'currency_basecolor1', 'FFFFFF')).'" alpha="'.trim($params->get( 'currency_basecolor1alpha', '0.8')).'" />

					<color code="#'.trim($params->get( 'currency_basecolor2', 'CECECE')).'" alpha="'.trim($params->get( 'currency_basecolor2alpha', '0.8')).'" />


				</base>

				<label color="#'.trim($params->get( 'currency_color', 'FF00CC')).'" alpha="'.trim($params->get( 'currency_coloralpha', '0.8')).'" />
				<symbol>'.trim($params->get( 'currency', '$')).'</symbol>
			</currency>

			<price>
				<base color="#'.trim($params->get( 'price_basecolor', 'FF00CC')).'" alpha="'.trim($params->get( 'price_basecoloralpha', '0.5')).'" />
				<label color="#'.trim($params->get( 'price_color', 'FFFFFF')).'" alpha="'.trim($params->get( 'price_coloralpha', '1')).'" />
			</price>
	</priceTag>


		<imageEffect>
			<type>'.trim($params->get( 'imageEffect', '1' )).'</type>
			<time>'.trim($params->get( 'imageEffectTime', '1000' )).'</time>
			<closingEffect>'.trim($params->get( 'imageClosingEffect', '1' )).'</closingEffect>
		</imageEffect>

			<descriptionEffect>
			<type>'.trim($params->get( 'descriptionEffect', '1' )).'</type>
		</descriptionEffect>
';

$xml_data .= create_defalultgcgallerycatpro_xml($params);
$xml_data .= "
</slideshow>";

$xmlslides_filename = $module_path.$catppv_id.'.xml';
$xmlslides_file = fopen($xmlslides_filename,'w');
fwrite($xmlslides_file, $xml_data);
fclose($xmlslides_file);

///////// set chmod 0777 for creating .xml file  if server is not windows
	$os_string = php_uname('s');
	$cnt = substr_count($os_string, 'Windows');
	if($cnt =='0'){
		chmod($xmlslides_filename, 0777);
	}

?>

<div align="center">
<script src="<?php echo JURI::root()?>components/com_gallerycatpro/sb/swfobject.js" language="javascript"></script>
<script type="text/javascript">

		var flashvars = {
			baseColor: "<?php echo $baseColor; ?>",
			xmlfileurl: "<?php echo JURI::root() . $pic_path.'/'.$catppv_id; ?>.xml"
		};
		var params = {
			bgcolor: "<?php echo $backgroundColor; ?>",			
			allowFullScreen: true,
			wmode: "<?php echo $wmode;?>"

		};
		
		var attributes = {
			id: "gallerycatpro"
		};
		
		swfobject.embedSWF("<?php echo JURI::root(). $pic_path.'/'.$catppv_id; ?>.swf", "gallerycatpro", "<?php echo $bannerWidth;?>px", "<?php echo $bannerHeight; ?>px", "9.0.0", false, flashvars, params, attributes);

	</script>

	<div id="gallerycatpro" class="mobile" mobile="<?php echo JURI::root() . $pic_path.'/'.$catppv_id; ?>.xml"></div>
</div>
