<?php

// This is the index page of the site 


// -------------------------------------------------
// included files

include("ie_global.php");
include("ie_config.php");
include("ie_tpl.php");
include("lang.inc.php");
include_once("ie_count.php");

//-----------------------------------
$time_start = getmicrotime();
//-----------------------------------

// update the counter

$ie_counter ++ ;
$updated = '<' . '?php $ie_counter = ' . $ie_counter . ' ; ?'.'>';
write2_file_new( 'ie_count.php' , $updated );

// read the data from the list.php

$img_list = $img_dir.'/list.inc.php';
@$ie_list = file( $img_list );
$list_count = count ( $ie_list );
//echo $list_count;

for ($i = 0 ; $i < $list_count ; $i++ )
{
	$list_result[$i] = array();
	$list_result[$i] = explode ( "|" , $ie_list[$i] );
}

// 2 show the data

// marks here

// {img_id} 0
// {img_dir} 1
// {img_time} 3
// {img_hit} 4
// {img_sum} 5
// {img_ins} 6 
// {img_title} 1
// {img_src} 2  [should be 268*221]
// {img_link}


for ($i = 0 ; $i < $list_count ; $i++ )
{
	$lt = array();
	$lt = $list_result[$i];
	if (strlen($lt[6])>$detail_len)
	{
		$lt[5] = substr($lt[6], 0, $detail_len).'...'; 
	}
	$o_src = $img_dir.'/'.$lt[2];

	if ( !file_exists($o_src))
	{
		$o_src = 'images/noimg.gif';
	}

	$img_link = "ie_showdir.php?dir=".$lt[0];
	
	if ($view == 'admin')
	{
		$img_admin = '¦¸<a href="admin_wizard.php?action=modifycate&step=2&style=modify&cdir='.$lt[0].'"> modify </a><br>';
		$img_admin .= '¦¨<a href="admin_wizard.php?action=modifycate&step=2&style=delete&cdir='.$lt[0].'"> delete</a>';
	}
	else
	{
		$img_admin = '';
	}
	
	$tmp = new tpl( "ie_box.htm" );

	$tmp->assign( "admin", $img_admin);
	$tmp->assign( "img_dir", $lt[1]);
	$tmp->assign( "img_title", "");
	$tmp->assign( "img_src", $o_src);
	$tmp->assign( "img_time", $lt[3]);
	$tmp->assign( "img_hit", $lt[4]);
	$tmp->assign( "img_sum", $lt[5]);
	$tmp->assign( "img_ins", $lt[6]);
	$tmp->assign( "img_link", $img_link);
	$box_in[$i] = $tmp->parse();
	unset ( $tmp );
}

// 2nd tpl

	// page divided
	$c = $list_count ;
	// echo $c;
	if ( $c<=$line_in_page )
	{
		// less than a page
		// do output here

	}
	else
	{
		// more than one page
		$explode = explode( '.' , $c / $line_in_page );
		if ($explode[1] == '')
		{
			$max_page_num = $explode[0];
		}
		else
		{
			$max_page_num = $explode[0]+1;
		}
		
	}

	if( !isset($page) )
	{
		// no parameter 
		$page = 1;
	}
	
	// echo 'page='.$page;
	// the number of dir to show
	$start_dir = ( $page-1 ) * $line_in_page ;

	//echo $start_dir.'ggg<br>'.$page.'pppp<br>';
	if ( $page == $max_page_num )
	{
		// not enough dirs to show in this page
		$end_dir = $start_dir +  $c % $line_in_page ;
		if ( $end_dir == $start_dir )
		{
			$end_dir = $end_dir = $start_dir + $line_in_page;
		}
	}
	else
	{
		$end_dir = $page * $line_in_page;
	}
	
	//show dirs
	for ( $i = $start_dir ; $i < $end_dir ; $i++ )
	{
		//add template here
		$box_out .= $box_in[$i].'<br>';
	}

	//make pager
	//echo  'eee'. $max_page_num;
	if ( $max_page_num == '' )
	{
		$pager = $txt[0];
	}
	else
	{
		$pager = '[';
		for ( $i=1 ; $i<=$max_page_num ; $i++ )
		{
			if ( $i == $page )
			{
				//
				$pager .= ' '.$i ;
			}
			else
			{
				if ($view == 'admin')
				{
					$pager .= ' <a href="ie_main.php?&view=admin&page='.$i.'">'.$i.'</a> ';
				}
				else
				{
					$pager .= ' <a href="ie_main.php?page='.$i.'">'.$i.'</a> ';		
				}
			}
		}
		$pager .= ' ]';
	}


	//echo $pager;

$tmp1 = new tpl( "ie_main.htm" );
$tmp1->assign( "notice", $sys_notice );
$tmp1->assign( "ie_boxs", $box_out );
$tmp1->assign( "page_title", $site_name );
$tmp1->assign( "pager", $pager );
$tmp1->printe();
unset( $tmp1 );

//--------------------------------------
if ($show_runtime == 'on')
{
	$time_end = getmicrotime();
	$time = $time_end - $time_start;
	echo '<br><font face="Arial"><span style="font-size: 7pt"><center>Run time : '.$time.' s Pageview : '. $ie_counter .'</center></span></font>';

}
//--------------------------------------
?>