<?php

/**
* @version $Id: function for mod_sliding_minifp
* Erwin Schro + Andy Sikumbang
* http://www.templateplazza.com
* @based on mod_section_thumbnail, mod_ultraDGM_News, mod_mostreadrecent, mod_mostread
* @package Joomla 1.0.12
*/

/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

global $mosConfig_offset, $mosConfig_live_site, $mainframe;


echo '<!-- Sliding Mini Front Page  Module by TemplatePlazza.com -->';

//---------------------------------Functions for thumbnails utility---------------------------------------
if (!function_exists("fptn_thumb_size_sliding_minifp"))
{
	function fptn_thumb_size_sliding_minifp($file, $wdth, $hgth, &$image, &$xtra, $class, $aspect)
	{
		global $mosConfig_absolute_path, $mosConfig_live_site;
		if($class!='') $xtra .= ' class="'.$class.'"';
		
		// Find the extension of the file
		$ext = substr(strrchr(basename($mosConfig_absolute_path.$file), '.'), 1);
		$thumb = str_replace('.'.$ext, '_sliding_thumb.'.$ext, $file);
		$image = '';
		$image_path = $mosConfig_absolute_path.$thumb;
		$image_site = $mosConfig_live_site.$thumb;
		$found = false;
		
		if (file_exists($image_path))
		{
			$size = '';
			$wx = $hy = 0;
			if (function_exists( 'getimagesize' ))
			{
				$size = @getimagesize( $image_path );
				if (is_array( $size ))
				{
					$wx = $size[0];
					$hy = $size[1];
					$size = 'width="'.$wx.'" height="'.$hy.'"';
				}
	    	}
//	    	if ($wx == $wdth && $hy == $hgth)
	    	if ( $wx == $wdth )
	    	{
				$found = true;
				$image= '<img src="'.$image_site.'" '.$size.$xtra.' />';
			}
		}
	
		if (!$found)
		{
			$size = '';
			$wx = $hy = 0;
			$size = @getimagesize( $mosConfig_absolute_path.$file );
			if (is_array( $size ))
			{
				$wx = $size[0];
				$hy = $size[1];
			}
			fptn_calcsize_sliding_minifp($wx, $hy, $wdth, $hgth, $aspect);
			switch ($ext)
			{
				case 'jpg':
				case 'jpeg':
				case 'png':
					fptn_thumbIt_sliding_minifp($mosConfig_absolute_path.$file,$image_path,$ext,$wdth,$hgth);
					$size = 'width="'.$wdth.'" height="'.$hgth.'"';
					$image= '<img  src="'.$image_site.'" '.$size.$xtra.' />';
					break;
	
				case 'gif':
					if (function_exists("imagegif")) {
						fptn_thumbIt_sliding_minifp($mosConfig_absolute_path.$file,$image_path,$ext,$wdth,$hgth);
						$size = 'width="'.$wdth.'" height="'.$hgth.'"';
						$image= '<img src="'.$image_site.'" '.$size.$xtra.' />';
						break;
	        		}
					
				default:
					$size = 'width="'.$wdth.'" height="'.$hgth.'"';
					$image= '<img src="'.$mosConfig_live_site.$file.'" '.$size.$xtra.' />';
					break;
			}
		}
	}

	function fptn_thumbIt_sliding_minifp ($file, $thumb, $ext, &$new_width, &$new_height) 
	{
		
		$img_info = getimagesize ( $file );
		$orig_width = $img_info[0];
		$orig_height = $img_info[1];
		
		if($orig_width<$new_width || $orig_height<$new_height)
		{
			$new_width = $orig_width;
			$new_height = $orig_height;
		}
		
		switch ($ext) {
			case 'jpg':
			case 'jpeg':
				$im  = imagecreatefromjpeg($file);
				$tim = imagecreatetruecolor ($new_width, $new_height);
				fptn_ImageCopyResampleBicubic_sliding_minifp($tim, $im, 0,0,0,0, $new_width, $new_height, $orig_width, $orig_height);
				imagedestroy($im);
	
				imagejpeg($tim, $thumb, 75);
				imagedestroy($tim);
				break;

			case 'png':
				$im  = imagecreatefrompng($file);
				$tim = imagecreatetruecolor ($new_width, $new_height);
				fptn_ImageCopyResampleBicubic_sliding_minifp($tim, $im, 0,0,0,0, $new_width, $new_height, $orig_width, $orig_height);
				imagedestroy($im);

				imagepng($tim, $thumb, 9);
				imagedestroy($tim);
				break;

			case 'gif':
				if (function_exists("imagegif")) {
					$im  = imagecreatefromgif($file);
					$tim = imagecreatetruecolor ($new_width, $new_height);
					fptn_ImageCopyResampleBicubic_sliding_minifp($tim, $im, 0,0,0,0, $new_width, $new_height, $orig_width, $orig_height);
					imagedestroy($im);

					imagegif($tim, $thumb, 75);
					imagedestroy($tim);
    			}
				break;

			default:
				break;
		}
	}

	function fptn_ImageCopyResampleBicubic_sliding_minifp (&$dst_img, &$src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) 
	{
		if ($dst_w==$src_w && $dst_h==$src_h) {
			$dst_img = $src_img;
			return;
		}
  		ImagePaletteCopy ($dst_img, $src_img);
		$rX = $src_w / $dst_w;
		$rY = $src_h / $dst_h;
		$w = 0;
		for ($y = $dst_y; $y < $dst_h; $y++) 
		{
			$ow = $w; $w = round(($y + 1) * $rY);
			$t = 0;
			for ($x = $dst_x; $x < $dst_w; $x++) 
			{
				$r = $g = $b = 0; $a = 0;
				$ot = $t; $t = round(($x + 1) * $rX);
				for ($u = 0; $u < ($w - $ow); $u++) 
				{
					for ($p = 0; $p < ($t - $ot); $p++) 
					{
						$c = ImageColorsForIndex ($src_img, ImageColorAt ($src_img, $ot + $p, $ow + $u));
						$r += $c['red'];
          				$g += $c['green'];
          				$b += $c['blue'];
          				$a++;
        			}
				}
				if(!$a) $a = 1;
				ImageSetPixel ($dst_img, $x, $y, ImageColorClosest ($dst_img, $r / $a, $g / $a, $b / $a));
			}
		}
	}

	function fptn_calcsize_sliding_minifp($srcx, $srcy, &$forcedwidth, &$forcedheight, $aspect) 
	{
		if ($forcedwidth > $srcx)  $forcedwidth = $srcx;
		if ($forcedheight > $srcy) $forcedheight = $srcy;
		if ( $forcedwidth <=0 && $forcedheight > 0) {
			$forcedwidth = round(($forcedheight * $srcx) / $srcy);
		} 
		else if ( $forcedheight <=0 && $forcedwidth > 0) {
			$forcedheight = round(($forcedwidth * $srcy) / $srcx);
		}
		else if ( $forcedwidth/$srcx>1 && $forcedheight/$srcy>1) {
			//May not make an image larger!
			$forcedwidth = $srcx;
			$forcedheight = $srcy;
		}
		else if ( $forcedwidth/$srcx<1 && $aspect) {
			//$forcedheight = round(($forcedheight * $forcedwidth) /$srcx);
			$forcedheight = round( ($srcy/$srcx) * $forcedwidth );
			$forcedwidth = $forcedwidth;
		}
	}


	function fptn_limittext_sliding_minifp($txt,$limit)
	{
	    $len=strlen($txt);
	    if ($len <= $limit)
	        return $txt;
	    else
	    {
	        $txt = substr($txt,0,$limit);
	        $pos = strrpos($txt," ");
	        if($pos >0)
			{
		        $txt = substr($txt,0,$pos);
		    	if (($tpos =strrpos($txt,"<")) >  strrpos($txt,">") && $tpos>0)
		    	{
			  		$txt = substr($txt,0,$tpos-1);
			  	}
			}
	        return $txt . "...";
	    }
	}
}

if (!function_exists("fptn_get_id_sliding_minifp"))
{

	function fptn_get_id_sliding_minifp ($id, $option="com_content") 
	{
		global $database, $Itemid;
	
		$_Itemid = "";
	
		// Content Item Link
		if ($_Itemid == "") {
			$database->setQuery( "SELECT id "
				."FROM #__menu "
				."WHERE type='content_item_link' AND published='1' AND link='index.php?option=$option&task=view&id=$id'" );
			$_Itemid = $database->loadResult();
		}
	
		// Typed Content Link
		if ($_Itemid == "") {
			$database->setQuery( "SELECT id "
				."FROM #__menu "
				."WHERE type='content_typed' AND published='1' AND link='index.php?option=$option&task=view&id=$id'" );
			$_Itemid = $database->loadResult();
		}
	
		// Content Section List
		if ($_Itemid == "") {
			$database->setQuery( "SELECT m.id "
				."FROM #__content AS i "
				."LEFT JOIN #__sections AS s ON i.sectionid=s.id "
				."LEFT JOIN #__menu AS m ON m.componentid=s.id "
				."WHERE m.type='content_section' AND m.published='1' AND i.id='$id'" );
			$_Itemid = $database->loadResult();
		}
	
		// Content Category List
		if ($_Itemid == "") {
			$database->setQuery( "SELECT sectionid, catid "
				."FROM #__content WHERE id='$id'" );
			$row = null;
			$database->loadObject( $row );
	
			$database->setQuery("SELECT id "
				."FROM #__menu "
				."WHERE type='content_category' AND published='1' AND link='index.php?option=$option&task=category&sectionid=$row->sectionid&id=$row->catid'");
			$_Itemid = $database->loadResult();
		}
	
		// Content Section Blog (specific)
		if ($_Itemid == "") {
			$database->setQuery( "SELECT m.id "
				."FROM #__content AS i "
				."LEFT JOIN #__sections AS s ON i.sectionid=s.id "
				."LEFT JOIN #__menu AS m ON m.componentid=s.id "
				."WHERE m.type='content_blog_section' AND m.published='1' AND i.id='$id'" );
			$_Itemid = $database->loadResult();
		}
	
		// Content Category Blog (specific)
		if ($_Itemid == "") {
			$database->setQuery( "SELECT m.id "
				."FROM #__content AS i "
				."LEFT JOIN #__categories AS c ON i.catid=c.id "
				."LEFT JOIN #__menu AS m ON m.componentid=c.id "
				."WHERE m.type='content_blog_category' AND m.published='1' AND i.id='$id'" );
			$_Itemid = $database->loadResult();
		}
	
		// Content Section Blog (global)
		if ($_Itemid == "") {
			// Search in global blog section
			$database->setQuery( "SELECT id "
				."FROM #__menu "
				."WHERE type='content_blog_section' AND published='1' AND componentid='0'" );
			$_Itemid = $database->loadResult();
		}
	
		// Content Category Blog (global)
		if ($_Itemid == "") {
			$database->setQuery( "SELECT id "
				."FROM #__menu "
				."WHERE type='content_blog_category' AND published='1' AND componentid='0'" );
			$_Itemid = $database->loadResult();
		}
	
		//echo $_Itemid;
	
		if ($_Itemid != "") {
			return $_Itemid;
		} else {
			return $Itemid;
		}
	}
}

//added by remush
//this fuction to show Thumbnail image

if (!function_exists("showThumbNews_sliding_minifp"))
{
function showThumbNews_sliding_minifp($row_images,$row_image,$params,$row_itemid,$row_id){
	
	$thumb_embed = intval( $params->get( 'thumb_embed', 0 ) );
	$thumb_width = intval( $params->get( 'thumb_width', 32 ) );
	$thumb_height = intval( $params->get( 'thumb_height', 32 ) );
	$aspect = intval( $params->get( 'aspect', 0 ) );
	$default_image_path = "/modules/sliding_minifp/default.gif";
	
	
	if ($thumb_embed == 1) 
	{
		echo '<a href="'. sefRelToAbs( 'index.php?option=com_content&amp;task=view&amp;id=' . 
		$row_id .'&amp;Itemid=' . $row_itemid) .'">';
						
		if (!empty($row_images))
		{
				$img = "/images/stories/" . strtok($row_images,"|\r\n");
				//echo "<BR>Image:".$img."<BR>";
				$class="";
				$extra = ' align="left" style="margin-left: 0.5em; margin-right: 0.5em;" alt="article thumbnail" ';
					   	
				fptn_thumb_size_sliding_minifp($img, $thumb_width, $thumb_height, $image, $extra, $class, $aspect);
					
				echo  $image;
					
		}
		else if ($row_image !="")
		{
			//echo "<BR>Image:".$row_image."<BR>";
			echo '<img src="'.$mosConfig_live_site.'/images/stories/' . $row_image .' " width="' . $thumb_width . '" height="' . $thumb_height .'" style="float: left; margin-left: 0.5em; margin-right: 0.5em;" alt="article image" />';
		}
		else {
						
			$img = $default_image_path;
			$class="";
			$extra = ' align="left" style="margin-left: 0.5em; margin-right: 0.5em;" alt="article thumbnail" ';
					   	
			fptn_thumb_size_sliding_minifp($img, $thumb_width, $thumb_height, $image, $extra, $class, $aspect);
			echo $image;
		}
		echo '</a>';
	}

}
}

//---------------------------------End Functions For thumbnails 




?>
