<?php // no direct access

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

//K2
$showCommentCount	= $params->get('showCommentCount','yes');
$renderPlugin	= $params->get('renderPlugin','render');

if(!($mainframe->getCfg('caching')) && !($framework)) {
	$document->addStyleSheet($modbase.'css/microblog.css');
	
	if($colorBox){
		$document->addStyleSheet($modbase.'css/cbtheme'.$cbTheme.'.css');
		$document->addScript($modbase . "js/jquery.colorbox-min.js");
	}
}

// Triggers for filter option
?>

<div class="mbContainer filter" <?php if ($mbHeight !="") { ?> style="height:<?php echo $mbHeight ?>px" <?php } ?>>

<ul id="filter">
	<li class="current triggerall"><a href="#">All</a></li>
	<?php if (!$trigger1==""){?> <li><a href="#"><?php echo $trigger1; ?></a></li><?php } ?>
	<?php if (!$trigger2==""){?> <li><a href="#"><?php echo $trigger2; ?></a></li><?php } ?>
	<?php if (!$trigger3==""){?> <li><a href="#"><?php echo $trigger3; ?></a></li><?php } ?>
	<?php if (!$trigger4==""){?> <li><a href="#"><?php echo $trigger4; ?></a></li><?php } ?>
	<?php if (!$trigger5==""){?> <li><a href="#"><?php echo $trigger5; ?></a></li><?php } ?>
	<?php if (!$trigger6==""){?> <li><a href="#"><?php echo $trigger6; ?></a></li><?php } ?>
	<?php if (!$trigger7==""){?> <li><a href="#"><?php echo $trigger7; ?></a></li><?php } ?>
	<?php if (!$trigger8==""){?> <li><a href="#"><?php echo $trigger8; ?></a></li><?php } ?>
	<?php if (!$trigger9==""){?> <li><a href="#"><?php echo $trigger9; ?></a></li><?php } ?>
</ul>
<ul class="portfolio">
<?php

		if($list)
		{
		$numMB = sizeof($list);	
		foreach ($list as $item) :  

		// Grabs the first image from the intro text
		$firstImage = $item->firstImage;
		$sTitle = $item->title;
		if($titleLimit > 0)
		{
			if(strlen($sTitle) > $titleLimit)
			{
				//$sTitle = substr( $sTitle, 0, strrpos(substr($sTitle, 0, $titleLimit), ' ')).$titleSuffix;
				$sTitle = substr($sTitle, 0, $titleLimit).$titleSuffix;
			}
		}

// Assigns the last image in the row to have 0 margin		
		$imageNumber++;
		if($addMargin){
			$imgRightMargin = $autoRightMargin;
		}
		else{
			$imgRightMargin = $rightMargin.'px';
		}

		$rowFlag = ($imageNumber % $imagesPerRow) ? 0 : 1;
		// Plugin processing code from Joomlaworks.gr
		if ($renderPlugin == 'render'){
			/*
			* Process the prepare content plugins
			*/
			$plgparams 	   =& $mainframe->getParams('com_content');
			$dispatcher	   =& JDispatcher::getInstance();
			JPluginHelper::importPlugin('content');
			$item->text = $item->fulltext;
			$results = $dispatcher->trigger('onPrepareContent', array (& $item, & $plgparams));
			$item->fulltext = $item->text;			
			$item->text = $item->introtext;
			$results = $dispatcher->trigger('onPrepareContent', array (& $item, & $plgparams));
			$item->introtext = $item->text;
			
		} elseif ($renderPlugin == 'strip') {

			$item->introtext = preg_replace('/{([a-zA-Z0-9\-_]*)\s*(.*?)}/i','', $item->introtext);
			$item->fulltext = preg_replace('/{([a-zA-Z0-9\-_]*)\s*(.*?)}/i','', $item->fulltext);
		}


// Logic for link behaviour
if ($colorBox && $popupGallery) {
$linkbehaviour ="id='inline".$item->id.$moduleID."' class='cBox".$moduleID."' rel='mbgallery".$moduleID."' href='#mbpopup".$item->id.$moduleID."' title='".$item->title."'";
$linkbehaviourImage ="id='inlineIMG".$item->id.$moduleID."' class='cBoxi".$moduleID."' rel='mbgalleryi".$moduleID."' href='#mbpopup".$item->id.$moduleID."' title='".$item->title."'";
$linkbehaviourReadon ="id='inlineRO".$item->id.$moduleID."' class='cBoxr".$moduleID." readon' rel='mbgalleryr".$moduleID."' href='#mbpopup".$item->id.$moduleID."' title='".$item->title."'";
} 

if ($colorBox && !($popupGallery)) {
$linkbehaviour ="id='inline".$item->id.$moduleID."' class='cBox".$moduleID."'  href='#mbpopup".$item->id.$moduleID."' title='".$item->title."'";
$linkbehaviourImage ="id='inlineIMG".$item->id.$moduleID."' class='cBoxi".$moduleID."' href='#mbpopup".$item->id.$moduleID."' title='".$item->title."'";
$linkbehaviourReadon ="id='inlineRO".$item->id.$moduleID."' class='cBoxr".$moduleID." readon' rel='mbgalleryr".$moduleID."' href='#mbpopup".$item->id.$moduleID."' title='".$item->title."'";
} 

if (!($colorBox)) {
$linkbehaviour ="href='".$item->link."'";
$linkbehaviourReadon ="href='".$item->link."' class='readon'";
$linkbehaviourImage ="href='".$item->link."'";
} 
?>
				<li class="<?php echo $item->metakey; ?> <?php if (($imageNumber == $numMB) or ($rowFlag)) {?>last<?php }?>" style="width:<?php echo $colWidth ?>;margin-right:<?php echo $imgRightMargin ?>;margin-bottom: <?php echo $bottomMargin ?>px">
				<?php if (!($firstImage == "")) { ?>
						<div class="mbFilterImage">
								<a <?php echo $linkbehaviourImage ?>>
										<?php if($resizeImage) { ?>
												<img src="<?php echo resizeImageHelper::getResizedImage($firstImage, $image_width, $image_height, $option); ?>" width="<?php echo $image_width;?>px" height="<?php echo $image_height;?>px" alt="<?php echo $item->title; ?>" class="mbFilterImage <?php if ($fadeEffect) { ?>fade<?php } ?>" />
										<?php } else { ?>
										<img src="<?php echo $firstImage ?>" alt="<?php echo $item->title; ?>" class="mbImage <?php if ($fadeEffect) { ?>fade<?php } ?>" />
										<?php }?>	
								</a>
						</div>
				<?php } ?>	
				<div class="mbClear"></div>
				<?php if ($titlelink) { ?>
					<<?php echo $titleClass ?>>
							<?php if ($titlelink == '1') { ?><a <?php echo $linkbehaviour ?>><?php }?>
										<?php echo $sTitle; ?>
							<?php if ($titlelink == '1') { ?></a><?php }?>
					</<?php echo $titleClass ?>>
					<?php if (!($colClearDiv)) { ?>				
					<div class="mbClear"></div>	
					<?php } ?>	
				<?php }?>		
				<?php if ($showdate) { ?>
					<span class='mbFilterdate'>
						<?php echo $item->dateCreated; ?>
					</span>
				<?php }?>
				<?php if ($showintro) : ?>
				<!-- Intro text -->
						<div class="mbFilterIntro">
								<?php  echo $item->introtext; ?>
						</div>
				<?php endif; ?>
				</li>
<?php if ($colorBox) { ?>

			<!-- Start Joomla Bamboo Microblog Inline Content -->
			<div style="display:none">
				<div id="mbpopup<?php echo $item->id.$moduleID; ?>">
					
					
					<?php // View for displaying k2 Video
					
					if ($justImage == "k2video") { ?>
						<?php echo $item->video;
			
						
						// Fallback to display the item if there is no video available
				 		if ($item->video =="") { ?>
								<h2 class="popupTitle"><?php echo $item->title; ?></<?php echo $titleClass ?>>
								<div class="popupIntroText">
										<?php echo $item->introtext ?>
								</div>
								<div class="mbClear"></div>
								<div class="popupFullText">
										<?php echo $item->fulltext ?>
								</div>
						<?php }
					}
					
					// Displays just the item image
					elseif ($justImage =="1" && !($firstImage == "")) { ?>
						<img src="<?php echo $firstImage; ?>" alt="<?php echo $item->title; ?>" class="mbImageFlat" />
					<?php } 
								
					// Displays just the fulltext
					if ($justImage == "fulltext") { ?>
						<h2 class="popupTitle"><?php echo $item->title; ?></<?php echo $titleClass ?>>
						<div class="popupFullText">
								<?php echo $item->fulltext ?>
						</div>
					<?php }					
					
					// Displays the intro and the fulltext
					elseif(!($justImage)) { ?>
						<h2 class="popupTitle"><?php echo $item->title; ?></<?php echo $titleClass ?>>
						<div class="popupIntroText">
								<?php echo $item->introtext ?>
						</div>
						<div class="mbClear"></div>
						<div class="popupFullText">
								<?php echo $item->fulltext ?>
						</div>
					<?php } ?>

				</div>
			</div>

				<!-- End Joomla Bamboo Microblog Inline Content -->
		<?php }
endforeach; }
?>
</ul>
</div>

<?php if($mainframe->getCfg('caching') && !($framework)) { ?>
	<!-- Microblog CSS | Move these scripts into the head of th etemplate to make this module xhtml valid  -->
	<link href="modules/mod_microblog/css/microblog.css" rel="stylesheet" type="text/css" media="screen" />


	<?php if ($colorBox) { ?>
	<!-- Colorbox Scripts | Move these scripts into the head of th etemplate to make this module xhtml valid -->
	<link href="modules/mod_microblog/css/cbtheme<?php echo $cbTheme?>.css" rel="stylesheet" type="text/css" media="screen" />
	<script type="text/JavaScript" src="modules/mod_microblog/js/jquery.colorbox-min.js"></script>
	<?php }
} ?>


<?php 
	// Code for any of the effects like image fades, filter effects or colorbox
	if ($fadeEffect or $layout =="filter" or $cbScripts == "body" or $cbScripts == 'head') { 
?> 
<script type="text/javascript">
<!--//--><![CDATA[//><!-- 
jQuery.noConflict();
jQuery(document).ready(function(){
<?php
// Fade Effect on thumbnail images
if ($fadeEffect)
{
	// Fade Effects 
	?>
	jQuery('img.fade').fadeTo('slow', 1.0); // This sets the opacity of the thumbs to fade down to 60% when the page loads
	jQuery('img.fade').hover(function(){
	jQuery(this).fadeTo('slow', 0.6); // This should set the opacity to 100% on hover
		},function(){
			jQuery(this).fadeTo('slow', 1.0); // This should set the opacity back to 60% on mouseout
		});
<?php
}
if ($layout=="filter") {
	// Filter View Effects
?>	
			jQuery(document).ready(function(){
				setRightMargin();
			});
			jQuery('ul#filter a').click(function() {
				jQuery(this).css('outline','none');
				jQuery('ul#filter .current').removeClass('current');
				jQuery(this).parent().addClass('current');
				var filterVal = jQuery(this).text().toLowerCase().replace(' ','-');
				if(filterVal == 'all') {
					jQuery('ul.portfolio>li').css('margin-right','#000');
					jQuery('ul.portfolio>li.hidden').removeClass('hidden').fadeIn('slow');
					jQuery('ul.portfolio>li').removeClass('last');
					setRightMargin();
				} else {
					jQuery('ul.portfolio>li').each(function() {
						jQuery(this).removeClass('last');
						if(!jQuery(this).hasClass(filterVal)) {
							jQuery(this).addClass('hidden').fadeOut('slow');
						} else {
							jQuery(this).removeClass('hidden').fadeOut('normal');
							jQuery(this).removeClass('hidden').fadeIn('slow');
						};
					});
					setRightMargin();					
				}
				return false;
			});
			
			function setRightMargin(){
				var tempLast = jQuery('ul.portfolio>li').not('.hidden');
				tempLast.filter(function(index){
					if(!((index + 1) % <?php echo $imagesPerRow;?>)){
						jQuery(this).addClass('last');
					}
				});
			}
<?php
}
if ($colorBox) {
	$colorboxGallery = $popupGallery ? 'mbgallery'.$moduleID : 'nofollow';
	// Colorbox settings
?>
		jQuery('a.cBox<?php echo $moduleID?>, a.cBoxi<?php echo $moduleID?>, a.cBoxr<?php echo $moduleID?>').each(function(){
			var mbTarget = jQuery(this).attr('href');
			jQuery(this).colorbox({
				'inline': true,
				'href': mbTarget,
				'current': '{current} of {total}',
				'opacity': '<?php echo $overlayOpacity?>',
				'scrolling': <?php echo $colorboxScrolling?>,
				'innerWidth': '<?php echo $frameWidth?>',
				'innerHeight': '<?php echo $frameHeight?>'
			});	
	    });
<?php
	if($colorboxScrolling == 'false'){
?>		
			jQuery(document).bind('cbox_complete', function(){
			jQuery.fn.colorbox.resize();
		});
<?php
	}
}
?>
});
 //--><!]]> 
</script>
<?php } ?>
<div class="mbClear"></div>