<?php 
require_once 'library/top_application.php';
require_once 'library/logged.php';

//權限 
$sql_right = mysql_query('SELECT `manage_banner` FROM `'.TBL_USER.'` WHERE `uid` = '.$_SESSION['Logged_Uid']);
$row_right = mysql_fetch_assoc($sql_right);

if($row_right['manage_banner'] != 1){
	header("Location: listUser.php");
}


$sql = mysql_query('SELECT * FROM `banner` ORDER BY `rank` ASC')or die(mysql_error());
$row = mysql_fetch_assoc($sql);

$targetName = array(1 => 'Self',
					2 => 'New Window'
					)
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Artech Creative</title>

<link href="css/css_admin.css?rnd=13" rel="stylesheet" type="text/css">
<link href="../css/tiny.css" rel="stylesheet" type="text/css">
<link href="css/drag_list.css" rel="stylesheet" type="text/css">

<script type="text/javascript" src="../js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="../js/jquery-ui-min.js"></script>
<script type="text/javascript" src="../js/tinybox_product.js"></script>
<!--[if IE]>
    <script src="../js/html5.js"></script>
<![endif]--> 
<script type="text/javascript">
  // When the document is ready set up our sortable with it's inherant function(s)
$(document).ready(function() {
	rerank()
	
	var table_name = 'banner';
	var rank_name = 'rank';
	var id_name = 'banner_id';
    $("#content-list").sortable({
      handle : '.handle',
      update : function () {
		  	rerank()
		  var order = $('#content-list').sortable('serialize');
  		$("#info").load('ajax/update_rank.php?'+order+'&sortList='+table_name+'&rank_name='+rank_name+'&id_name='+id_name);
      }
    });
	var _preventDefault = function(evt) { evt.preventDefault(); };
	$(".handle").bind("dragstart", _preventDefault).bind("selectstart", _preventDefault);
	
	
	$('.show_edit').each(function(){
		var btn_show = 	$('.showing', this);
		var btn_not_show = $('.not_showing', this);
		var this_id_name = 'banner_id';
		var this_table = 'banner';
		var this_tr = $(this).closest("table");

		btn_show.click(function(){
			var this_field = $(this).attr('rel');
			var this_id = parseInt($(this).attr('rev'));
			$.ajax({
				type: "POST",
				url: "ajax/update_yesno.php",
				data: {"id_name" : this_id_name, "id" : this_id, "update_field" :this_field, "show" : 2, "table": this_table},
				cache: false,
				success: function(html){
					btn_show.hide();
					btn_not_show.show();
					this_tr.css('background', '#FFB6B6');
				}
			});
		})
		
		btn_not_show.click(function(){
			var this_field = $(this).attr('rel');
			var this_id = parseInt($(this).attr('rev'));
			$.ajax({
				type: "POST",
				url: "ajax/update_yesno.php",
				data: {"id_name" : this_id_name, "id" : this_id, "update_field" :this_field, "show" : 1, "table": this_table},
				cache: false,
				success: function(html){
					btn_show.show();
					btn_not_show.hide();
					this_tr.css('background', '#ccc');
				}
			});
		})
	})
	
	$('.btn_delete').click(function(){
		var this_id = parseInt($(this).attr('rel'));
		var table_name = 'banner';
		var this_id_name = 'banner_id';
		var this_table = $(this).closest("table");
		
		if (confirm("Confirm to delete this Banner?")){
			$.ajax({
				type: "POST",
				url: "ajax/delete.php",
				data: {"table" : table_name, "id_name" : this_id_name, "del_id" : this_id},
				cache: false,
				success: function(html){
					$('#info').html(html);
					if(html.indexOf('Delete Success') != -1){
						this_table.fadeOut(300, function(){
							this_table.remove();
							rerank()
						});
					}
				}
			});
		} else {
			return false;
		}
	})
});

function closeJS(){
	location.reload();
}
function rerank(){
	var i = 1;
	$('.rank_no').each(function(){
		$(this).text(i);
		i = i+1;
	})
}
</script>
</head>
<body>
<div id="wrap">
    <section id="column_left">
        <?php include 'library/left_menu.php';?>
    </section>
    <section id="column_right">
        <div class="admin_content">
        <a onClick="TINY.box.show({iframe:'addBanner.php',boxid:'frameless',width:960,height:800,fixed:true,maskid:'blackmask',maskopacity:70, closejs:function(){closeJS()}})" id="btn_add"><div id="add_img"></div>新增</a>
        <h1>Slide管理</h1>
        
        <hr>
        <div id="info" class="content">(Drag the <span style="color:red">red square</span> to re-arrange the order.)</div>
       		<table width="100%" border="0" cellpadding="2" cellspacing="1" bgcolor="#CCCCCC" class="content">
              <tr>
                <td width="30" bgcolor="#EEEEEE"></td>
                <td width="40" bgcolor="#EEEEEE" align="center">Order</td>
                <td width="200" bgcolor="#EEEEEE" >Banner thumb</td>
                <td bgcolor="#EEEEEE" >Title</td>
                <td width="90" align="center" bgcolor="#EEEEEE" >target</td>
                <td width="130" align="center" bgcolor="#EEEEEE" >Last Update</td>
                <td width="60" align="center" bgcolor="#EEEEEE" >Action</td>
                <td width="24" align="center" bgcolor="#EEEEEE" ></td>
              </tr>
            </table>
        <ul id="content-list">
              <?php
				$i =1;
				if($row){
                do { 
				if($row['show']==2){
						$b_color = 'FFB6B6';
					} else {
						$b_color = 'cccccc';
					}?>
              <li id="listItem_<?=$row["banner_id"]?>" >
                <table width="100%" border="0" cellpadding="2" cellspacing="1" bgcolor="#<?php echo $b_color;?>" class="content show_edit" >
                  <tr bgcolor="#FFFFFF">
                    <td width="30" align="center"><div class="handle"></div></td>
                    <td width="40" align="center"><div class="rank_no"></div></td>
                    <td width="200"><img src="../upload/slide_images/s_<?php echo $row['ep_pic'];?>" width="200"></td>
                    <td>E: <?php echo $row['ep_subject']; ?><br>
                    繁: <?php echo $row['cp_subject']; ?><br>
					Link: <?php echo $row['ep_link']; ?>
                    </td>
                    <td width="90" align="center" ><?php echo $targetName[$row['target']]; ?></td>
                	<td width="130" align="center" ><?php echo $row['last_update']; ?></td>
                    <td width="60" align="center">
                    <?php if ($row['show'] == 1){?>
                        <a class="showing" rel="show" rev="<?php echo $row['banner_id'];?>" style="display:block"></a>
                        <a class="not_showing" rel="show" rev="<?php echo $row['banner_id'];?>" style="display:none"></a>
					<?php }  else {  ?>
                        <a class="showing" rel="show" rev="<?php echo $row['banner_id'];?>" style="display:none"></a>
                        <a class="not_showing" rel="show" rev="<?php echo $row['banner_id'];?>" style="display:block"></a>
					<?php } ?>
                    
                    	<a href="javascript:TINY.box.show({iframe:'addBanner.php?id=<?=$row['banner_id']?>&action=edit',boxid:'frameless',width:960,height:800,fixed:true,maskid:'blackmask',maskopacity:70, closejs:function(){closeJS()}})" class="btn_edit" title="修改"></a>
                    </td>
                  	<td width="24" align="center"><a class="btn_delete" title="刪除內容" rel="<?php echo $row['banner_id']; ?>"></a></td>
                  </tr>
                </table>
          </li>
              <?
					$i++;
                } while ($row = mysql_fetch_assoc($sql));
			} else {
				echo '<span style="color:red">There is no content in this section.</span>';
			}
				?>
            </ul>
        </div>
	</section>
	
</div>

<?php include 'library/bottom.php'; ?>
</body>
</html>
