<?php
if (!defined('IN_CONTEXT')) die('access violation error!');

$cols = 3;
?>
<script type="text/javascript" language="javascript">
<!--
function on_del_success(response) {
    var o_result = _eval_json(response);
    if (!o_result) {
        return on_failure(response);
    }
    
    var stat = document.getElementById("admintpllst_stat");
    if (o_result.result == "ERROR") {
        stat.innerHTML = o_result.errmsg;
        return false;
    } else if (o_result.result == "OK") {
        stat.innerHTML = "<?php _e('OK, refreshing...'); ?>";
        reloadPage();
    } else {
        return on_failure(response);
    }
}

function on_del_failure(response) {
    document.getElementById("admintpllst_stat").innerHTML = "<?php _e('Request failed!'); ?>";
    return false;
}

function delete_tpl(tpl_id) {
    if (confirm("Delete the selected template?")) {
        var stat = document.getElementById("admintpllst_stat");
        stat.style.display = "block";
        stat.innerHTML = "<?php _e('Deleting selected template...'); ?>";
        _ajax_request("mod_template", 
            "admin_delete", 
            {
                tpl_id:tpl_id
            }, 
            on_del_success, 
            on_del_failure);
    }
}

function on_tog_success(response) {
    var o_result = _eval_json(response);
    if (!o_result) {
        return on_failure(response);
    }
    
    var stat = document.getElementById("admintpllst_stat");
    if (o_result.result == "ERROR") {
        stat.innerHTML = o_result.errmsg;
        return false;
    } else if (o_result.result == "OK") {
        stat.innerHTML = "<?php _e('OK, refreshing...'); ?>";
        reloadParent();
    } else {
        return on_failure(response);
    }
}

function on_tog_failure(response) {
    on_del_failure(response);
}

function toggle_default(tpl_id, is_remote) {
    var remote_tpl = "0";
    if (is_remote) {
        remote_tpl = "1";
    }
    var stat = document.getElementById("admintpllst_stat");
    stat.style.display = "block";
    stat.innerHTML = "<?php _e('Setting default template...'); ?>";
    _ajax_request("mod_template", 
        "admin_make_default", 
        {
            tpl_id:tpl_id,
            is_remote:remote_tpl
        }, 
        on_tog_success, 
        on_tog_failure);
}
//-->
</script>
<div class="content_title">
    <h3><?php _e('Template Files'); ?></h3>
</div>
<div class="space"></div>
<div class="content_toolbar">
    <a href="index.php?<?php echo Html::xuriquery('mod_template', 'admin_upload'); ?>" title=""><?php _e('Upload Template'); ?></a>
</div>
<div class="space"></div>
<div class="status_bar">
    <span id="admintpllst_stat" class="status" style="display:none;"></span>
</div>
<div class="space"></div>
<table cellspacing="1" class="list_table" id="admin_tpl_list">
	<thead>
		<tr>
			<td>
				<table cellspacing="0" class="wrap_table">
					<tbody>
						<tr>
							<td><?php _e('Category'); ?>&nbsp;</td>
							<td><?php include_once(dirname(__FILE__).'/_category_switch.php'); ?></td>
						</tr>
					</tbody>
				</table>
			</td>
		</tr>
	</thead>
    <tbody>
	    <tr>
		    <td>
		    <?php
		    $n_tpls = sizeof($templates);
		    if ($n_tpls > 0) {
		    ?>
			    <table id="template_grid" cellspacing="1">
			    	<?php
			        for($i = 0; $i < $n_tpls; $i++) {
			        	if ($i % $cols == 0) {
			        	    echo '<tr>';
			        	}
			    	?>
			            <td width="<?php echo intval(100 / $cols); ?>%">
			            	<img class="tpl_thumb" src=<?php echo $templates[$i]['screenshot_url']; ?> />
			            	<br />
			            	<span class="small">
				            <?php
				            if ($templates[$i]['name'] == DEFAULT_TPL) {
				                _e('Current Default');
				            } else {
				                $remote_flag = isset($templates[$i]['package_url'])?'true':'false';
				                echo '<a href="#" onclick="toggle_default(\''.$templates[$i]['id'].'\', '.$remote_flag.');return false;" title="'.__('Set as default template').'">'.__('Set as default template').'</a>';
				            }
				            ?>
				            &nbsp;
			                <a href="#" onclick="delete_tpl(<?php echo $templates[$i]['id']; ?>);return false;" title="<?php _e('Delete'); ?>"><?php _e('Delete'); ?></a>
			            	</span>
			            </td>
			        <?php
			        	if (($i % $cols) == ($cols - 1)) {
			        	    echo '</tr>';
			        	}
			        }
			        if ($i % $cols != 0) {
				        for ($j = 0; $j < ($cols - $i); $j++) {
				            echo '<td></td>';
				        }
				        echo '</tr>';
			        }
			        ?>
			    </table>
		    <?php } ?>
		    </td>
	    </tr>
    </tbody>
</table>
