<?php
$allow_guests = false;

require_once('global.php');
require_once('INC/class.session.php');

$pagename = "Badges";
$pageid = "badges";
$body_id = "badges";

//require_once('templates/community_subheader.php');
require_once('templates/shop_header.php');


$checkUserBadges = mysql_query("SELECT * FROM user_badges WHERE user_id = '" . $my_id . "'");
$userBadges = mysql_fetch_assoc($checkUserBadges);
$sql = mysql_query("SELECT * FROM badges ORDER by id ASC");
$getBadges = mysql_query("SELECT * FROM badges");

$badge = Filter($_GET['buybadge']);

if(isset($_GET['buybadge']))
{
	$getBadgeCost = mysql_query("SELECT * FROM badges WHERE badge_id = '" . $badge . "'");
	$badgeCost = mysql_fetch_assoc($getBadgeCost);
	$doesBadgeExist = mysql_query("SELECT badge_id FROM badges WHERE badge_id = '" . $badge . "'");
        
        $badge_exists = mysql_num_rows(mysql_query("SELECT * FROM user_badges WHERE badge_id ='".$badge."' AND user_id = '".$my_id."'"));
        $secu = mysql_num_rows(mysql_query("SELECT null FROM badges WHERE badge_id ='".$badge."'"));
	
	/*if(!file_exists("http://cdn.habbo.ma/c_images/album1584/" . $badge . ".gif"))
	{
		$msg = "<div class=\"alert\"><img src='".$path."/web-gallery/v2/images/shop/icon-error.gif' /> Ce badge n'est pas disponible au Badgeshop!</div>";  
            
	}*/
	if(!mysql_num_rows($doesBadgeExist))
	{
		$msg = "<div class=\"alert\"><img src='".$path."/web-gallery/v2/images/shop/icon-error.gif' /> Ce badge n'est pas disponible au Badgeshop!</div>";  
            
	}
        elseif($badge_exists > 0) {
                $msg = "<div class=\"alert\"><img src='".$path."/web-gallery/v2/images/shop/icon-error.gif' /> Tu as déjà ce badge!</div>";
        }
        elseif(!$secu > 0) {
                $msg = "<div class=\"alert\"><img src='".$path."/web-gallery/v2/images/shop/icon-error.gif' /> Ce badge n'est pas disponible au Badgeshop!</div>";     
        }
	else
	{
		if($myrow['piks'] < $badgeCost['price'])
		{
			$msg = "<div class=\"alert\"><img src='".$path."/web-gallery/v2/images/shop/icon-error.gif' /> Vous n'avez pas assez de Piks. <a href='piks'>«En acheter plus»</a>.</div>";
		}
		else
		{
			mysql_query("INSERT INTO user_badges (user_id, badge_id, badge_slot) VALUES ('" . $my_id . "', '" . $badge . "', '0')");
			mysql_query("UPDATE users SET piks = piks - " . $badgeCost['price'] . " WHERE username = '" . $myrow['username'] . "'");
			     $msg = "<div class=\"alert\"><img src='".$path."/web-gallery/v2/images/shop/icon-good.gif' /> Tu viens d'acheter le badge: <b>".$badge."</b> pour <u>".$badgeCost['price']." Piks</u>.</div>";
		}
	}
}

    
?>
<div id="container">
    <div class="shop">
        <div class="title blue">Boutique - Badges (<?php echo $piks; ?><img src="<?php echo $path; ?>/web-gallery/v2/images/vipcoin.gif" />)</div> 
        <?php if(isset($msg)) echo $msg; ?>
        
        <div class="offre general">
            <img align="right" src="<?php echo $path;?>/web-gallery/images/shop/bb_red_yellow_blue.gif" /> 
            <h2 class="offre-title">Pack badges [I] </h2>
            
           <?php 	while($badgeShit = mysql_fetch_assoc($sql)) { ?>
        <div class="badges-content">
            
                
	
        <?php 
		echo '<p><a href="?buybadge='.$badgeShit['badge_id'].'">
                <img align="right" src="http://cdn.habbo.ma/c_images/album1584/' . $badgeShit['badge_id'] . '.gif"></a>
            <b>Nom du badge:</b> '.$badgeShit['badge_id'].' <br/>
            <b>Seulement:</b> '.$badgeShit['price'].'  Piks                    
            
 </p>';

            ?>
                
               
                
                
        </div>      
                	<?php } ?>
            <div class="clear"></div>
            
        </div>
        <div class="clear"></div>
        <div class="separator"></div>
        <p class="info"><img src="<?php echo $path; ?>/web-gallery/images/shop/icon_10.gif" /> Pour acheter un badge, vous devez cliquer sur le badge.</p>

    </div></div>

<script>
    $(".badges-content").click(function () {
      $(this).toggleClass("selected");
    });
</script>
<script type="text/javascript">if (!$(document.body).hasClassName('process-template')) { Rounder.init(); }</script></div>

<!--[if lt IE 7]>
<script type="text/javascript">
Pngfix.doPngImageFix();
</script>
<![endif]--> 
<script type="text/javascript"> 
    HabboView.run(); 
</script> 

<?php //include('templates/community_footer.php'); ?>