<?php
/**
 * Fading Content from Category Module for Joomla! 1.5
 * Distributor: mediahof
 * Author: Dominik Gorczyca,
 * xhtml 1.0 strict
 * http://www.mediahof.de/
 **/

defined('_JEXEC') or die('Restricted access');

$categoryid		= intval( $params->get( 'categoryid' ) );
$positionLeft	= intval( $params->get( 'positionLeft' ) );
$positionTop	= intval( $params->get( 'positionTop' ) );
$width			= intval( $params->get( 'width' ) );
$height			= intval( $params->get( 'height' ) );
$fadeSpeed		= intval( $params->get( 'fadeSpeed' ) );
$fadeOutTime	= intval( $params->get( 'fadeOutTime' ) )*1000;

$class			= $params->get( 'class' );
$alias			= $params->get( 'alias' );

$idPrefix = 'idfade';

switch ( $params->get( 'sort' )) {
	case 'az': $sortBy = '`title` DESC';	break;
	case 'za': $sortBy = '`title` ASC';	break;
	case '19': $sortBy = '`id` DESC';	break;
	case '91': $sortBy = '`id` ASC';	break;
	case 'rd': $sortBy = ' RAND()'; break;
}

if ($categoryid > 0) {
	$db = & Jfactory::getDBO();
	$db->setQuery('SELECT * FROM `#__content` WHERE `catid`= '.$categoryid.' AND `state` = 1 ORDER BY '.($alias == 'ja' ? str_replace(array('`title`','`id`'), '`title_alias`', $sortBy) : $sortBy));
	$contents = $db->loadObjectList();

	$divStyle	= 'style="'.
				'position:absolute;'.
				'text-align:left;'.
				'overflow:hidden;'.
				'top:'.($positionTop != 0 ? $positionTop.'px' : 1).';'.
				'left:'.($positionLeft != 0 ? $positionLeft.'px' : 1).';'.
				'width:'.$width.'px;'.
				'height:'.$height.'px;'.
				'z-index:2;'.
				'display:none;'.
				'"';

	$tresc = "\n".'<div'.(!empty($class) ? $class = ' class="'.$class.'" ' : '').'>';
	for ($cn = 0; $cn < count($contents); $cn++) {
		$tresc .= "\n".'<div id="'.$idPrefix.($cn+1).'" '.$divStyle.'>'."\n".($contents[$cn]->fulltext == '' ? $contents[$cn]->introtext : $contents[$cn]->fulltext)."\n".'</div>'."\n";
	}
	ob_start();
?>
<script type="text/javascript">
//<![CDATA[
//----------------------------------------------------------
//  Fade2Next by Dominik Gorczyca - mediahof
//----------------------------------------------------------
function transparent(f2nid , trans){
 document.getElementById(f2nid).style.display = "block";
 var x = document.getElementById(f2nid).style;
 if (window.navigator.appName == "Microsoft Internet Explorer") {
  x.filter = "alpha(opacity="+trans+")"; 
 } else {
  x.opacity = (trans / 100);
 }
}

function fadeOut(f2nid, zeit) {
 var count = 0;
 var trans = 100;
 document.getElementById(f2nid).style.zIndex = 998;
 for(count = 1; count <= 20; count++) {
  trans = 100 - (count * 5);
  setTimeout("transparent('"+f2nid+"',"+trans+" )",(zeit * count));
 }
}

function fadeIn(f2nid, zeit) {
 var count = 0; 
 var trans = 0;
 document.getElementById(f2nid).style.zIndex = 999;
 for(count = 1 ;count <= 20; count++) {
  trans = (count * 5 );
  setTimeout("transparent('"+f2nid+"',"+trans+" )",(zeit * count));
 }
}

function fadek(f2nid) {
 if (k == 1) {
  k = 0;
 } else {
  if (oft == 1) {
   fadeOut(f2nid+oft, <?php echo $fadeSpeed;?>);
   oft = oftc;
   fadeIn(f2nid+oft, <?php echo $fadeSpeed;?>);
   k = 1;
  } else {
   fadeOut(f2nid+oft, <?php echo $fadeSpeed;?>);
   fadeIn(f2nid+(oft - 1), <?php echo $fadeSpeed;?>);
   oft = oft - 1;
  }
 }
}

var oft = <?php echo count($contents);?>;
var oftc = oft;
var k = 0;
function wennallesda() {
 fadeIn('<?php echo $idPrefix;?>'+oft, <?php echo $fadeSpeed;?>);
 var aktiv = window.setInterval("fadek('<?php echo $idPrefix;?>')", <?php echo $fadeOutTime;?>);
}

window.onload = wennallesda;
//]]>
</script>
<?php
	JFactory::getDocument()->addCustomTag(ob_get_clean());
	echo $tresc.'</div>'."\n";
}
?>