<?php
$col = getIndex($attrs, 'col', 2);
$class = 'col-md-'.((int)12/$col);
$criteria = new CDbCriteria();
$criteria->order = ' date_added desc ,priority asc';
$criteria->condition = 'is_template=1';
if (isset($_REQUEST['cat'])) {
	$currentCategory = isset($_REQUEST['cat']) ? $_REQUEST['cat'] : 0;

	$criteria->condition .= ' and category=:category and is_template_active = 1';
	$criteria->params = array(':category' => $currentCategory);
} else {
	$criteria->condition .= ' and is_template_active = 1';
}
$templates = Site::model()->findAll($criteria);
?>

<div id="site_templates">
	<?php if (isset($templates)): ?>
		<?php  $i = 0; foreach ($templates as $template): ?>
			<?php if ($i % $col == 0) {
echo '<div class="row">';
} ?>
			<div class="<?php echo $class;?>">
				<div class="template_preview_wrapper">
					<div class="template_preview thumbnail" >
						<?php
							$template_path = '/websites/'.$template->name.'/screenshot.jpg';
						?>
						<img class="" src="<?php echo Yii::app()->getBaseUrl(true).'/'.$template_path ?>" />
						<div class="overlay"></div>
						
					</div>
					<div class="template_buttons">
						<?php echo CHtml::link('View template', array('template?id='.$template->id), array('class' => 'btn btn-border-white')); ?> 
					</div>
				</div>
			</div>
			<?php if ($i % $col == ($col-1)) {
echo '</div>';
} ?>
			<?php ++$i; ?>
		<?php endforeach; ?>
	<?php endif;?>
</div>
<script type="text/javascript">
	$(document).ready(function()
	{
		$(window).resize(function(event){
			var parent_height = $('.template_preview_wrapper').outerHeight();
			var parent_width = $('.template_preview_wrapper').outerWidth();
			$('.template_buttons').css({display:'block', visibility:'visible'});
			$('.template_buttons').css("top", Math.max(0, ((parent_height - $('.template_buttons').outerHeight()) / 2) 
                                                ) + "px");
			$('.template_buttons').css("left", Math.max(0, ((parent_width - $('.template_buttons').outerWidth()) / 2) 
                                               ) + "px");
			$('.template_buttons').css({display:'', visibility:''});
		});
		$(window).resize();
	});
</script>
				