<?php
if($_GET['display']=='modulo'){
		require($_SERVER['DOCUMENT_ROOT'].'/engine/engine.php');
		$page_id 	= $_GET['page_id'];
		$tipo_gal	= $_GET['tipo_gal'];
		$dir_gal	= $_GET['dir_gal'];
	}
	else
	{
		$page_id 	= PAG_ID;
		$tipo_gal 	= TIPO_GALERIA;
		$dir_gal	= TIPO_GALERIA;
	}

	if(isset($_GET['publicacion_id']) || isset($_GET['publicacion_seo'])){
		$page_id 		= $_GET['publicacion_id'];
	};

if($tipo_gal == 'TIPO_GALERIA'){$tipo_gal = 'contenidos'; $dir_gal	= 'contenidos';}

/* --------------------------------------------- */

$query = "SELECT COUNT(*) FROM galerias WHERE FIND_IN_SET(".$page_id.", galerias.cat) AND galerias.tipo = '".$tipo_gal."'";
$total_items = $engine->DB->QueryToArray($query);
;
$count = $total_items[0]['COUNT(*)'];

/* CARGA CONTENIDO PAGINATOR*/	
if($_POST['page'])
{
	
			$page = $_POST['page'];
			$cur_page = $page; 
			$page -= TPL_GALERIA_INITPAGE;
			$per_page = TPL_GALERIA_XPAGE;
			$previous_btn = true;
			$next_btn = true;
			$first_btn = true;
			$last_btn = true;
			$start = $page * $per_page;
			$no_of_paginations = ceil($count / $per_page);
			
			$max = 'LIMIT '.$start.','.$per_page;
			
			function list_galerias($max, $tipo_gal, $page_id, $page, $per_page){

												global $engine;
													if($max == ''){
														
														$set_query = "ORDER BY RAND() LIMIT 0";
														
													}else{
														$set_query = "WHERE FIND_IN_SET(".$page_id.", galerias.cat) AND galerias.tipo = '".$tipo_gal."' ORDER BY galerias.id DESC $max";
													};

												
												$query = "SELECT * FROM galerias $set_query";
												$pics = $engine->DB->QueryToArray($query);

												return $pics;}
												

			?>
                <? if($count != '0'):?>
                <div id="gallery">
                    <ul class="contenedor col-md-4">
						<?php if($count != '0'): $i=0; $runlist = list_galerias($max, $tipo_gal, $page_id, $page, $per_page); else: $runlist = list_galerias($max=='', $tipo_gal, $page_id, $page, $per_page); endif; foreach ($runlist as $pic):?>
                                <? if($pic['video']==''): ?>
                                   <li class="col-md-6"><a href="<?=$SITE_URL?>/media/<?=$pic['tipo']?>/<?=$pic['imagen']?>" rel="gallery" class="gallery fancybox"><img src="<?=$SITE_URL?>/media/<?=$pic['tipo']?>/thumbs/<?=$pic['imagen']?>" class="fancybox" /></a></li>
                                <? else: ?>
                                    <li class="col-md-6"><a href="http://www.youtube.com/embed/<?=$pic['video']?>?wmode=transparent" rel="gallery" class="video fancybox.iframe"><img src="<?=$SITE_URL?>/media/<?=$pic['tipo']?>/thumbs/<?=$pic['imagen']?>" class="fancybox" /></a></li>
                                <? endif; ?>
								<? if(($i%2)):?></ul><ul class="col-md-4"><? endif; ?>
                        <?php $i++; endforeach;?>
                    </ul>
                </div>
                <? else: ?>
                
				<? endif; ?>
                
           <script>
				var width_pic = $('#gallery .contenedor').width()/2-2;			
				
				$('li.col-md-6 img').css("width", width_pic+"px");
				$('li.col-md-6 img').css("height", <?=TPL_GALERIA_HEIGHT?>+"px")
           </script>

                <script>
                $(document).ready(function() {
                    $(".video").fancybox({
                        maxWidth	: 792,
                        maxHeight	: 547,
                        fitToView	: true,
                        width		: 792,
                        height		: 547,
                        autoSize	: true,
                        closeClick	: false,
                        scrolling	: 'no',
                        padding		: 0,
                        margin		: 10,
                    });
                });
                
                
                $(document).ready(function() {
                    $(".gallery").fancybox({
                        maxHeight	: 550,
                        fitToView	: true,
                        autoSize	: true,
                        closeClick	: false,
                        scrolling	: 'yes',
                        padding		: 10,
                        margin		: 0,
                    });
                });
                
                </script>
			<?

		if($count > $per_page): include_once($_SERVER['DOCUMENT_ROOT'].'/engine/paginator.php'); endif;
}
?>


<? if ($_POST['page']==''):?>
{literal}
<script>
            $(document).ready(function(){
				
				
                function loading_show(){
                    $('#loading').show();
                }
                function loading_hide(){
                    $('#loading').hide();
                }                
                function loadData(page){
                    loading_show();                    
                    $.ajax
                    ({
                        type: "POST",
						url: '{/literal}{$TEMPLATE_URL}{literal}/modulos/galeria.php?display=modulo&page_id=<?=$page_id?>&tipo_gal=<?=$tipo_gal?>&dir_gal=<?=$dir_gal?>&randomnumber='+ Math.floor(Math.random()*10),
                        data: "page="+page,
                        success: function(msg)
                        {
                            $("#galerias").ajaxComplete(function(event, request, settings)
                            {
                                loading_hide();
                                $("#galerias").html(msg);
                            });
                        }
                    });
                }
                loadData(1);  // For first time page load default results
                $('#galerias .pagination li.active').live('click',function(){
                    var page = $(this).attr('p');
                    loadData(page);
                    
                });           
                $('#go_btn').live('click',function(){
                    var page = parseInt($('.goto').val());
                    var no_of_pages = parseInt($('.total').attr('a'));
                    if(page != 0 && page <= no_of_pages){
                        loadData(page);
                    }else{
                        alert('Enter a PAGE between 1 and '+no_of_pages);
                        $('.goto').val("").focus();
                        return false;
                    }
                    
                });
            });
</script>
{/literal}
<div id="galerias">
</div>
<? endif; ?>