<?php

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

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

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

//-----------------------------------

// update the list


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


for ($i = 0 ; $i < $list_count ; $i++ )
{
	$list_result[$i] = array();
	$list_result[$i] = explode ( "|" , $ie_list[$i] );
	
	// create the output info here
	//$list_out = array();
	//$list_out[$i] = $list_tpl;
}

for ($i = 0 ; $i < $list_count ; $i++ )
{
	
	
	$lt = $list_result[$i];
	if ($lt[0] == $dir)
	{
		//
		$lt[4]++;
	}
	//$lt[6] = str_replace("\n",'',$lt[6]);
	$updated_list .= $lt[0].'|'.$lt[1].'|'.$lt[2].'|'.$lt[3].'|'.$lt[4].'|'.$lt[5].'|'.$lt[6];
	
}

write2_file_new( $img_list , $updated_list );

// -------------------------------------

// get the picture info

// format of picture list

// 0 id
// 1 name
// 2 W*H
// 3 size
// 4 time
// 5 from
// 6 hit
// 7 recommend


$img_list = $img_dir.'/'.$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] );

}


// show the info

for ($i = 0 ; $i < $list_count ; $i++ )
{
	$lt = $list_result[$i];
	
	$star = '';
	for ($ii=0 ;$ii<$lt[7];$ii++)
	{
		$star .= '<img src="images/star.gif"><br><br>';
	}

	$i_src = $img_dir.'/'.$dir.'/tn/'.$lt[0];
	/*
	if (!file_exists($i_src))
	{
		$i_src = 'images/noimg.gif';
	}
	*/
	//¦¸¦¨
	if ($view == 'admin')
	{
		$img_admin = '¦¸<a href="admin_wizard.php?action=modifypic&step=2&style=modify&picid='.$lt[0]."&dir=".$dir.'"> modify </a><br>';
		$img_admin .= '¦¨<a href="admin_wizard.php?action=modifypic&step=2&style=delete&picid='.$lt[0]."&dir=".$dir.'"> delete</a>';
	}
	else
	{
		$img_admin = '';
	}
	$img_link = "ie_showpic.php?picid=".$lt[0]."&dir=".$dir;
	$tmp = new tpl( "ie_pic_box.htm" );
	$tmp->assign( "img_name", $lt[1]);
	$tmp->assign( "img_title", "");
	$tmp->assign( "img_src", $i_src);
	$tmp->assign( "img_time", $lt[4]);
	$tmp->assign( "img_hit", $lt[6]);
	$tmp->assign( "img_from", $lt[5]);
	$tmp->assign( "img_size", $lt[3]);
	$tmp->assign( "img_wh", $lt[2]);
	$tmp->assign( "img_recom", $star);//$lt[7]);//
	$tmp->assign( "img_link", $img_link);
	$tmp->assign( "admin", $img_admin);
	$box_in[$i] = $tmp->parse();
	unset ( $tmp );
}

// 2nd tpl

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

	}
	else
	{
		// more than one page
		$explode = explode( '.' , $c / $pic_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 ) * $pic_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 % $pic_in_page ;
		if ( $end_dir == $start_dir )
		{
			$end_dir = $end_dir = $start_dir + $pic_in_page;
		}
	}
	else
	{
		$end_dir = $page * $pic_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 = 'There is only one page here ...';
	}
	else
	{
		$pager = '[';
		for ( $i=1 ; $i<=$max_page_num ; $i++ )
		{
			if ( $i == $page )
			{
				//
				$pager .= ' '.$i.' ';
			}
			else
			{
				if ($view == 'admin')
				{
					$pager .= ' <a href="ie_showdir.php?&view=admin&page='.$i.'&dir='.$dir.'">'.$i.'</a> ';
				}
				else
				{
					$pager .= ' <a href="ie_showdir.php?page='.$i.'&dir='.$dir.'">'.$i.'</a> ';		
				}

			}
		}
		$pager .= ']';
	}


	//echo $pager;
	$navigate_bar = '<a href="ie_main.php">'.$site_name.'</a>  >>>  '.'<a href="ie_showdir.php?dir='.$dir.'" >'.$dir.'</a>';

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


//--------------------------------------
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>';

}
//--------------------------------------

?>