<?php
 /*=======================================================================*\
 | Copyright (c) 2012, Leo 'Met0do' - http://www.met0do.org               |
 | For more stuffs and data, http://www.met0do.org - leo@met0do.org       |
 | ====================================================================== |
 | This program is free software: you can redistribute it and/or modify   |
 | it under the terms of the GNU General Public License as published by   |
 | the Free Software Foundation, either version 3 of the License, or      |
 | (at your option) any later version.                                    |
 | ====================================================================== |
 | This program is distributed in the hope that it will be useful,        |
 | but WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the           |
 | GNU General Public License for more details.                           |
 \*=======================================================================*/
 
    /* requirements */
	require_once '../config.php';
	
	/* initial data - avatar REAL position */
	if(checkIfContains("direction=", $_POST["posture"]) == true){
		$avatarPosition = explode('=', $_POST["posture"]); $avatarPos = $avatarPosition[1];
	} elseif(checkIfContains("d-", $_POST["posture"]) == true){ $avatarPos = 4; }
		
	
	/* initial avatar data */
	if(!isset($_POST["gender"]) && empty($_SESSION["AF"])){
		$_SESSION["AF"]["ch"] = 'ch-3111-63-62';
		$_SESSION["AF"]["hd"] = 'hd-3103-1';
		$_SESSION["AF"]["hr"] = 'hr-3163-39';
		$_SESSION["AF"]["lg"] = 'lg-285-77';
		$_SESSION["AF"]["sh"] = 'sh-305-78';
		$_SESSION["AF"]["ha"] = null;
		$_SESSION["AF"]["he"] = null;
		$_SESSION["AF"]["ea"] = null;
		$_SESSION["AF"]["fa"] = null;
		$_SESSION["AF"]["cp"] = null;
		$_SESSION["AF"]["ca"] = null;
		$_SESSION["AF"]["cc"] = null;
		$_SESSION["AF"]["wa"] = null;
		$_SESSION["AG"] = "M"; /* avatar initial gender */
	}
		
	/* receiving form vars */
	$get["g"]  =  $_POST["gender"];   /* gender of the avatar */
	$get["st"] =  $_POST["set_type"]; /* hr/wa/sh... */
	$get["si"] =  $_POST["set_id"];   /* the ID of the chosen item */
	$get["p"]  =  $avatarPos;         /* posture of the avatar */

	/* verify if is the sex to change */
	if(isset($get["g"]) != false){
		/* if is girl */
		if($get["g"] == "F"){
			$_SESSION["AF"]["ch"] = 'ch-3005-62';		$_SESSION["AF"]["hd"] = 'hd-600-1';
			$_SESSION["AF"]["hr"] = 'hr-3012-35';		$_SESSION["AF"]["lg"] = 'lg-3018-93';
			$_SESSION["AF"]["sh"] = 'sh-730-95';        $_SESSION["AG"] = "F";
			
			/* if is boy */
		} elseif($get["g"] == "M"){
			$_SESSION["AF"]["ch"] = 'ch-3111-63-62';	$_SESSION["AF"]["hd"] = 'hd-3103-1';
			$_SESSION["AF"]["hr"] = 'hr-3163-39';		$_SESSION["AF"]["lg"] = 'lg-285-77';
			$_SESSION["AF"]["sh"] = 'sh-305-78';        $_SESSION["AG"] = "M";
		}
		
		/* extra data nulled */
		$_SESSION["AF"]["ha"] = null; $_SESSION["AF"]["he"] = null;
		$_SESSION["AF"]["ea"] = null; $_SESSION["AF"]["fa"] = null;
		$_SESSION["AF"]["cp"] = null; $_SESSION["AF"]["ca"] = null;
		$_SESSION["AF"]["cc"] = null; $_SESSION["AF"]["wa"] = null;
		
	}
	/* if isnīt the sex to change, must be a part of the avatar (hair, clothes, skin...) */
	elseif(isset($get["st"]) == true){
		switch(strtolower($get["st"])){
			/* change the head */
			case "hd": if($get["si"] != null){ $val = 'hd-'.$get["si"]; } else { $val = null; } $_SESSION["AF"]["hd"] = $val; break;
			
			/* change the hair */
			case "hr": if($get["si"] != null){ $val = 'hr-'.$get["si"]; } else { $val = null; } $_SESSION["AF"]["hr"] = $val; break;
			
			/* change the hat */
			case "ha": if($get["si"] != null){ $val = 'ha-'.$get["si"]; } else { $val = null; }	$_SESSION["AF"]["ha"] = $val; break;
			
			/* change the hair accessories */
			case "he": if($get["si"] != null){ $val = 'he-'.$get["si"]; } else { $val = null; } $_SESSION["AF"]["he"] = $val; break;
			
			/* change the eye accessory */
			case "ea": if($get["si"] != null){ $val = 'ea-'.$get["si"];	} else { $val = null; }	$_SESSION["AF"]["ea"] = $val; break;
			
			/* change the face accessories */
			case "fa": if($get["si"] != null){ $val = 'fa-'.$get["si"];	} else { $val = null; }	$_SESSION["AF"]["fa"] = $val; break;
			
			/* change the t-shirts */
			case "ch": if($get["si"] != null){ $val = 'ch-'.$get["si"];	} else { $val = null; }	$_SESSION["AF"]["ch"] = $val; break;
			
			/* change the t-shirt printing */
			case "cp": if($get["si"] != null){ $val = 'cp-'.$get["si"]; } else { $val = null; }	$_SESSION["AF"]["cp"] = $val; break;
			
			/* change the jackets */
			case "cc": if($get["si"] != null){ $val = 'cc-'.$get["si"]; } else { $val = null; }	$_SESSION["AF"]["cc"] = $val; break;
			
			/* change the body accessories */
			case "ca": if($get["si"] != null){ $val = 'ca-'.$get["si"];	} else { $val = null; }	$_SESSION["AF"]["ca"] = $val; break;
			
			/* change the pants */
			case "lg": if($get["si"] != null){ $val = 'lg-'.$get["si"]; } else { $val = null; } $_SESSION["AF"]["lg"] = $val; break;
			
			/* change the shoes */
			case "sh": if($get["si"] != null){ $val = 'sh-'.$get["si"]; } else { $val = null; } $_SESSION["AF"]["sh"] = $val; break;
			
			/* change the pants accessories */
			case "wa": if($get["si"] != null){ $val = 'wa-'.$get["si"]; } else { $val = null; } $_SESSION["AF"]["wa"] = $val; break;
		}
	}
	
	/* join all parts of the avatar */
	$avatarFigure = join('.', $_SESSION["AF"]);
	
	/* ignore it - but donīt edit it */
	$avatarFigure = str_replace('..', '.', $avatarFigure);
	$avatarFigure = str_replace('..', '.', $avatarFigure);
	$avatarFigure = str_replace('..', '.', $avatarFigure);
	$avatarFigure = str_replace('..', '.', $avatarFigure);
	
	/* now we gonna store the currently avatar figure in a session */
	unset($_SESSION["IMG"]); /* to avoid clones and figure errors */
	$_SESSION["IMG"] = $avatarFigure; /* saving figure string in session */
	
	/* saving in database the avatar figure of the current id session */
	$storeInData = mysql_query('UPDATE sessions SET figure = "'.$avatarFigure.'" WHERE session = "'.$_SESSION["AS"].'"');
	
	/* if gender changes... letīs update it too */
	if(isset($_POST["gender"])){ mysql_query('UPDATE sessions SET gender = "'.$_POST["gender"].'" WHERE session = "'.$_SESSION["AS"].'"'); }
	
	/* if figure avatar saved in datbase we can continue */
	if($storeInData){
		/* creating message to JSON */
		header('Content-type: application/json');
		$json["status"] = "OK";
		if(isset($get["g"]) == true){switch(strtolower($get["g"])){
			case "m": $json["state"] = array("ch" => "3111-63-62", "hd" => "3103-1", "hr" => "3163-39", "lg" => "285-77", "sh" => "305-78");	break;
			case "f": $json["state"] = array("ch" => "3005-62",    "hd" => "600-1", "hr" => "3012-35", "lg" => "3018-93", "sh" => "730-95");	break;
		   }
		}
		$json["errors"] = null;	$json["data"] = $config["hImaging"].'?figure='.$avatarFigure.'&direction='.$get["p"].'&head_direction='.$get["p"].'';
		/* send messages data in JSON mode */
		echo(json_encode($json)); exit;
	}
?>