<?php
function sessinit(){
	// Let's set the category we are currently working on
	if(isset($_POST['category'])){
		$_SESSION['cat_ID']=$_POST['category'];
		$cat = $_SESSION['cat_ID'];
	}elseif(isset($_SESSION['cat_ID'])){
		$cat = $_SESSION['cat_ID'];
	}else{
		$_SESSION['cat_ID'] = 0;
	}
	
	// Have we chosen a gender yet?
	if(isset($_POST['gender'])){
		$_SESSION['gender']=$_POST['gender'];
		$gen = $_SESSION['gender'];
	}elseif(isset($_SESSION['gender'])){
		$gen = $_SESSION['gender'];
	}else{
	$_SESSION['gender'] = 0;
	}
	
	// Set the Image creation functions
	if(isset($_POST['saveimg']) > 0){
		$_SESSION['saveimg']=$_POST['saveimg'];
		$img = $_SESSION['saveimg'];
		CreateImg();
	}elseif(isset($_SESSION['saveimg'])){
		$img = $_SESSION['saveimg'];
	}else{
		$_SESSION['saveimg'] = 0;
	}
}

function pagination($cat,$gen,$per_page=19,$page=1,$url='?'){

	global $conn;
	
    $query = "SELECT COUNT(*) as num FROM avtr_items WHERE cat_ID=" . $cat . " AND gender=". $gen ."";
    $row = mysqli_fetch_array(mysqli_query($conn,$query));
    $total = $row['num'];
    $adjacents = "2"; 
     
    $prevlabel = "&lsaquo; Prev";
    $nextlabel = "Next &rsaquo;";
	$lastlabel = "Last &rsaquo;&rsaquo;";
     
    $page = ($page == 0 ? 1 : $page);  
    $start = ($page - 1) * $per_page;                               
     
    $prev = $page - 1;                          
    $next = $page + 1;
     
    $lastpage = ceil($total/$per_page);
     
    $lpm1 = $lastpage - 1; // //last page minus 1
     
    $pagination = "";
    if($lastpage > 1){   
        $pagination .= "<ul class='pagination'>";
             
            if ($page > 1) $pagination.= "<li><a href='{$url}page={$prev}'>{$prevlabel}</a></li>";
             
        if ($lastpage < 7 + ($adjacents * 2)){   
            for ($counter = 1; $counter <= $lastpage; $counter++){
                if ($counter == $page)
                    $pagination.= "<li><a class='current'>{$counter}</a></li>";
                else
                    $pagination.= "<li><a href='{$url}page={$counter}'>{$counter}</a></li>";                    
            }
         
        } elseif($lastpage > 5 + ($adjacents * 2)){
             
            if($page < 1 + ($adjacents * 2)) {
                 
                for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++){
                    if ($counter == $page)
                        $pagination.= "<li><a class='current'>{$counter}</a></li>";
                    else
                        $pagination.= "<li><a href='{$url}page={$counter}'>{$counter}</a></li>";                    
                }
                $pagination.= "<li><a href='{$url}page={$lpm1}'>{$lpm1}</a></li>";
                $pagination.= "<li><a href='{$url}page={$lastpage}'>{$lastpage}</a></li>";  
                     
            } elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) {
                 
                $pagination.= "<li><a href='{$url}page=1'>1</a></li>";
                $pagination.= "<li><a href='{$url}page=2'>2</a></li>";
                for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) {
                    if ($counter == $page)
                        $pagination.= "<li><a class='current'>{$counter}</a></li>";
                    else
                        $pagination.= "<li><a href='{$url}page={$counter}'>{$counter}</a></li>";                    
                }
                $pagination.= "<li><a href='{$url}page={$lpm1}'>{$lpm1}</a></li>";
                $pagination.= "<li><a href='{$url}page={$lastpage}'>{$lastpage}</a></li>";      
                 
            } else {
                 
                $pagination.= "<li><a href='{$url}page=1'>1</a></li>";
                $pagination.= "<li><a href='{$url}page=2'>2</a></li>";
                for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) {
                    if ($counter == $page)
                        $pagination.= "<li><a class='current'>{$counter}</a></li>";
                    else
                        $pagination.= "<li><a href='{$url}page={$counter}'>{$counter}</a></li>";                    
                }
            }
        }
         
            if ($page < $counter - 1) {
				$pagination.= "<li><a href='{$url}page={$next}'>{$nextlabel}</a></li>";
				$pagination.= "<li><a href='{$url}page=$lastpage'>{$lastlabel}</a></li>";
			}
         
        $pagination.= "</ul>";        
    }
     
    return $pagination;
}

function CreateImg(){

	global $conn;
	
		$BodyID = (isset($_SESSION['BodyID']) ? $_SESSION['BodyID'] : 'blankavatar.png');
		$BackID = (isset($_SESSION['BackID']) ? $_SESSION['BackID'] : 'blankavatar.png');
		$EyesID = (isset($_SESSION['EyesID']) ? $_SESSION['EyesID'] : 'blankavatar.png');
		$GlassesID = (isset($_SESSION['GlassesID']) ? $_SESSION['GlassesID'] : 'blankavatar.png');
		$HairID = (isset($_SESSION['HairID']) ? $_SESSION['HairID'] : 'blankavatar.png');
		$MouthID = (isset($_SESSION['MouthID']) ? $_SESSION['MouthID'] : 'blankavatar.png');
		$Face_ExtrasID = (isset($_SESSION['Face_ExtrasID']) ? $_SESSION['Face_ExtrasID'] : 'blankavatar.png');
		$GlovesID = (isset($_SESSION['GlovesID']) ? $_SESSION['GlovesID'] : 'blankavatar.png');
		$ShoesID = (isset($_SESSION['ShoesID']) ? $_SESSION['ShoesID'] : 'blankavatar.png');
		$Pants_ShortsID = (isset($_SESSION['Pants_ShortsID']) ? $_SESSION['Pants_ShortsID'] : 'blankavatar.png');
		$ShirtsID = (isset($_SESSION['ShirtsID']) ? $_SESSION['ShirtsID'] : 'blankavatar.png');
		$JewelryID = (isset($_SESSION['JewelryID']) ? $_SESSION['JewelryID'] : 'blankavatar.png');
		$JacketsID = (isset($_SESSION['JacketsID']) ? $_SESSION['JacketsID'] : 'blankavatar.png');
		$Right_HandID = (isset($_SESSION['Right_HandID']) ? $_SESSION['Right_HandID'] : 'blankavatar.png');
		$Left_HandID = (isset($_SESSION['Left_HandID']) ? $_SESSION['Left_HandID'] : 'blankavatar.png');
		$CloaksID = (isset($_SESSION['CloaksID']) ? $_SESSION['CloaksID'] : 'blankavatar.png');
		$CostumesID = (isset($_SESSION['CostumesID']) ? $_SESSION['CostumesID'] : 'blankavatar.png');
		$HatsID = (isset($_SESSION['HatsID']) ? $_SESSION['HatsID'] : 'blankavatar.png');
		$PetsID = (isset($_SESSION['PetsID']) ? $_SESSION['PetsID'] : 'blankavatar.png');
		
	$avtrBld = array(
		$BodyID,
		$BackID,
		$EyesID,
		$GlassesID,
		$HairID,
		$MouthID,
		$Face_ExtrasID,
		$GlovesID,
		$ShoesID,
		$Pants_ShortsID,
		$ShirtsID,
		$JewelryID,
		$JacketsID,
		$Right_HandID,
		$Left_HandID,
		$CloaksID,
		$CostumesID,
		$HatsID,
		$PetsID
	);	
	
	$baseImg = imagecreatetruecolor(120, 150);
	$col = imagecolorallocate($baseImg, 255, 255, 255);
	imagecolortransparent($baseImg,$col);
	imagealphablending($baseImg, false);
	imagefilledrectangle($baseImg, 0, 0, 120, 150, $col);
	imagealphablending($baseImg, true);	
	
	$imgchk = mysqli_query($conn, "SELECT * FROM avtr_categories");
	$imgbld = mysqli_fetch_array($imgchk, MYSQLI_ASSOC);

	$cnt = 	1;
	foreach ($avtrBld as $img){
	
		$query2 = mysqli_query($conn,"SELECT catName
						   FROM avtr_categories 
						   WHERE ID = $cnt") or die(mysql_error());
		$img2 = mysqli_fetch_assoc($query2);
		
		$imgPath = 'catImages/'.$img2['catName'].'/'.$img.'';
     	$mask = imagecreatefrompng($imgPath);
     	MergeImages($baseImg,$mask);
     	imagedestroy($mask);
		
		$cnt++;
   	}
	
	if($_SESSION['saveimg'] == 1){
		header("Content-type: image/gif");
		header("Content-Disposition: attachment; filename=avatar.gif");
	
		imagegif($baseImg);
		imagedestroy($baseImg);
		$_POST['saveimg'] = 0;
		$_SESSION['saveimg'] = 0;
	}elseif($_SESSION['saveimg'] == 2){
		header("Content-type: image/jpeg");
		header("Content-Disposition: attachment; filename=avatar.jpg");
	
		imagejpeg($baseImg);
		imagedestroy($baseImg);
		$_POST['saveimg'] = 0;
		$_SESSION['saveimg'] = 0;
	}elseif($_SESSION['saveimg'] == 3){
		header("Content-type: image/png");
		header("Content-Disposition: attachment; filename=avatar.png");
	
		imagepng($baseImg);
		imagedestroy($baseImg);
		$_POST['saveimg'] = 0;
		$_SESSION['saveimg'] = 0;
	}
}

function MergeImages($base,$mask){
    imagecopy($base, $mask, 0, 0, 0, 0, imagesx($mask),imagesy($mask));
    return $base;
}
?>