<?php   
defined('C5_EXECUTE') or die("Access Denied."); 
/*
Concrete Bricks game by John Liddiard (aka JohntheFish)
www.jlunderwater.co.uk
This software is licensed under the terms described in the concrete5.org marketplace. 
Please find the add-on there for the latest license copy.
*/

global $concrete_bricks_repeat_count;
$concrete_bricks_repeat_count++;

if ($concrete_bricks_repeat_count>1){
	// do nothing, only one game per page
}else{

// show the game
$form = Loader::helper('form');

$cell_spec = 'style="width:16px;height:16px;overflow:hidden;"';
$border_spec = 'style="border:1px solid #999999;"';

?>
<div style="width:100%;">
<?php 	if (isset($options['show_heading'])){ ?>
<h1><?php  echo t('Concrete Bricks');?></h1>
<?php  } ?>

<div style="margin:auto;overflow:hidden;">

<div id="jl-js-game-grid" style="float:left;">
<table <?php  echo $border_spec;?>>
<tbody>
<?php 
for ($g_i=0; $g_i<$game_rows;$g_i++){
	echo '<tr>';
	for ($g_j=0; $g_j<$game_cols; $g_j++){
		echo '<td '.$cell_spec.'></td>';
	}
	echo '</tr>';
}

?>
</tbody></table>
</div>

<div style="margin-left:15px;float:left;"><div 
id="jl-js-game-next" 
style="margin-bottom:10px;float:right;<?php  
if (!isset($options['show_next'])){ 
	echo ' display:none;';
}?>">
<table <?php  echo $border_spec;?>>
<tbody>
<?php 
for ($n_i=0; $n_i<4;$n_i++){
	echo '<tr>';
	for ($n_j=0; $n_j<4; $n_j++){
		echo '<td '.$cell_spec.'></td>';
	}
	echo '</tr>';
}
?>
</tbody></table>
</div>

<div style="margin-bottom:10px;float:right;clear:right;font-size:120%;text-align:right;">
<h3>
<span style="float:left;"><?php  echo t('Level');?>:&nbsp;</span><span id="jl_js_game_level" style="float:right;">0</span><br>
<span style="float:left;"><?php  echo t('Lines');?>:&nbsp;</span><span id="jl_js_game_lines" style="float:right;">0</span><br>
<span style="float:left;"><?php  echo t('Score');?>:&nbsp;</span><span id="jl_js_game_score" style="float:right;">0</span>
</h3>
</div>

<div style="margin-bottom:10px;float:right;clear:right;font-size:120%;text-align:right;">
<input id="jl-js-game-start" type="button" value="Start" style="width:60px;"><br>
<input id="jl-js-game-stop" type="button" value="Stop"  style="width:60px;" disabled="disabled">
</div>

<div style="margin-bottom:10px;float:right;clear:right;" id="jl-js-game-info">
<h3 style="cursor:help;"
title="<?php  
echo t('Keys:')." /n";
echo t('Move Left, J or Left Arrow.')." /n";
echo t('Move Right, L or Right Arrow.')." /n";
echo t('Rotate Right, I or Up Arrow.')." /n";
echo t('Drop, K or Down Arrow.');
?>"><?php 
echo t('Keys:');
?></h3>
<?php 
$cmd_spec = 'style="width:25px;height:25px;overflow:hidden;text-align:center;font-weight:bold;cursor:pointer;"';
?>
<table id="jl-js-game-controls">
<tr>
<td>&nbsp;</td>
<td><input <?php  echo $cmd_spec;?> type="button" data-cmd="up" value="&uarr;"></td>
<td>&nbsp;</td>
</tr>
<tr>
<td><input <?php  echo $cmd_spec;?> type="button" data-cmd="left" value="&larr;"></td>
<td><input <?php  echo $cmd_spec;?> type="button" data-cmd="down" value="&darr;"></td>
<td><input <?php  echo $cmd_spec;?> type="button" data-cmd="right" value="&rarr;"></td>
</tr>
</table>
</div>

</div></div></div>

<?php 
/*===================================================================
The dialog that handles best scores, sending them back to the 
controller via a tool.
Note the double div container so I can clone this and preserve the dialog
=====================================================================*/
?>
<div><div class="concret-bricks-game-ScoresDialogContent" style="display:none">
<h3>Concrete Bricks Game - Your Score</h3>
<p>
<?php 
if (isset($for_user)){
	echo t('Congratulations').' <b>';
	echo $for_user->getUserName();
	echo '</b>. ';
	$uID = $for_user->getUserID();
}else{
	$uID = 0;
}
?>
 <?php  echo t('You scored');?> <b><span class="concrete_bricks_game_score">XXX</span></b>
 <?php  echo t('points in a game lasting');?> <b><span class="concrete_bricks_game_time">XXX</span></b> 
 <?php  echo t('by completing');?> <b><span class="concrete_bricks_game_lines">XXX</span></b> 
 <?php  echo t('lines of Concrete Bricks at a game level of');?> <b><span class="concrete_bricks_game_level">XXX</span></b>.
</p>

<?php 
if ($uID>0 || empty ($options['require_login_for_score'])){
	echo '<form  action="'.$action_ajax_save_score.'" method="post">';
	if ($uID>0){
		echo $form->hidden('user', $uID);
	} elseif (empty($options['require_login_for_score'])){ 
		echo $form->label('name', t('Please enter your name:'));
		echo '<br>';
		echo $form->text('name', '',array(	'style' => 'width: 95%',
											'maxlength' => 50,
										) );
	}
	if (isset($options['collect_comments'])){ 
		echo $form->label('comment', t('Would you like to comment?'));
		echo '<br>';
		echo $form->textarea('comment', '', array(	'style' => 'width: 95%',
													'maxlength' => 250,
													) );
		echo '<br>';
	}
	echo $form->hidden('ccm_token', $validate_ajax_save_score);
	echo $form->hidden('blockid', $blockID);
	echo $form->hidden('collectionid', $collectionID);
	echo $form->submit('Save', t('Save'));
	echo '</form>';
} else {
	?><p><?php 
	echo t('If you register as a user you will be able to save your score.');
	?></p><?php 
}
//Now the throbber in a div
?>
<div class="scores-dialog-content-feedback" style="align:center; width:98%">
<br><br><img class="ccm-dialog-loader" 
			 src="<?php  echo ASSETS_URL_IMAGES;?>/throbber_white_32.gif" 
			 width="32" height="32"
			style="display: block; margin: auto;">
</div></div></div>
<?php 
$block_path = $this->getBlockURL();
if (isset($options['iconset']) && $options['iconset'] != 'None'){
	$icons_theme = $block_path.'/icons/'. $options['iconset'];
}else{
	$icons_theme = '';
}
?>

<script type="text/javascript">
$(document).ready(function(){var b="<?php  echo $icons_theme ?>";$(".concret-bricks-game-ScoresDialogContent").hide();var c=function(h,d,g,i,k){if(h<1){null;return}var m=$(".concret-bricks-game-ScoresDialogContent:first").clone();var f=$(".concret-bricks-game-ScoresDialogContent:first").parent();var p=function(){$(f).append(m)};$(".concret-bricks-game-ScoresDialogContent:first span.concrete_bricks_game_score").text(h);var l="";var o=Math.ceil((g-d)/1000);var j=Math.floor(o/60);var e=Math.floor(j/60);if(e>0){j=j%60;l=e+"h "}if(e>0||j>0){o=o%60;l+=j+"m "}l+=o+"s";$(".concret-bricks-game-ScoresDialogContent:first span.concrete_bricks_game_time").text(l);$(".concret-bricks-game-ScoresDialogContent:first span.concrete_bricks_game_lines").text(i);$(".concret-bricks-game-ScoresDialogContent:first span.concrete_bricks_game_level").text(k);var n=function(){$(".concret-bricks-game-ScoresDialogContent:first form").hide();$(".concret-bricks-game-ScoresDialogContent:first .scores-dialog-content-feedback").show()};$(".concret-bricks-game-ScoresDialogContent:first form").ajaxForm({data:{score:h,time_taken:g-d,completed_lines:i,level_achieved:k},complete:function(){$(".concret-bricks-game-ScoresDialogContent:first").dialog.closeTop();$(".concrete_bricks_game_top_scores_table").trigger("concrete_bricks_game_scores_updated")},beforeSend:n});$(".concret-bricks-game-ScoresDialogContent:first").dialog.open({element:".concret-bricks-game-ScoresDialogContent:first",title:"Conctrete Bricks - Game Over",width:350,modal:false,height:200,onClose:p});$(".concret-bricks-game-ScoresDialogContent .scores-dialog-content-feedback").hide()};game_options={score_callback:c};if(b){$.extend(game_options,{shape_icons:[b+"0.png",b+"1.png",b+"2.png",b+"3.png",b+"4.png",b+"5.png",b+"6.png"]})}var a=concrete_bricks_game.init(game_options)});
</script>

<?php  } // End concrete_bricks_repeat_count
?>

