<script type="text/javascript">
$(document).ready( function() {
    // Dialog Link
    $('#tbladdalbum').click(function () {
        $('#addalbumgallery').dialog('open');
        return false;
    });
});
</script>

<script type="text/javascript">
$(document).ready( function() {
    $('.addalbum').dialog({
        autoOpen: false,
        width: 400,
		modal: true
    });
});
</script>

<script type="text/javascript">
$(document).ready(function () {
    $('.addtoform').click(function(){
		var name = $(this).attr("id");
		$('#customDialog').dialog("close");
		$('#picture').val(name);
    });
});
</script>

<script type="text/javascript">
$(document).ready(function () {
	$('#picture').keypress(function(e){
		e.preventDefault();    
	});
    $('#btn_uploadfpc').click(function(){
		$('#uploadfpc').show();
		$('#uploadfweb').hide();
		$('#picture').val('');
    });
    $('#btn_uploadfweb').click(function(){
		$('#uploadfpc').hide();
		$('#uploadfweb').show();
		$('#picture').val('');
    });
});
</script>

<script type="text/javascript">
$(document).ready(function(){
	$('#paging_container').pajinate({
		items_per_page : 5,
		num_page_links_to_display : 5
	});
});
</script>

<script type="text/javascript">
$(document).ready( function() {
var oTable = $('.dTable').dataTable();
    // Dialog Link
    oTable.$('.tbledit').click(function () {
		var idisi = $(this).attr('id');
        $('#editdata'+idisi).dialog('open');
        return false;
    });
});
</script>

<script type="text/javascript">
$(document).ready( function() {
    $('.editdata').dialog({
        autoOpen: false,
        width: 400,
		modal: true
    });
});
</script>

<?php
//Author  	= 'Jenuar Dwi Putra Dalapang';
//Contact 	= 'mailto:djenuar@gmail.com';

session_start();
if (empty($_SESSION['namauser']) AND empty($_SESSION['passuser'])){
	header('location:404.php');
}else{
$aksi="po-component/po-gallery/proses.php";
?>
	<div class="contentTop">
        <span class="pageTitle"><span class="icon-images"></span>Gallery Management</span>
    </div>
    <!-- Breadcrumbs line -->
    <div class="breadLine">
        <div class="bc">
            <ul id="breadcrumbs" class="breadcrumbs">
                <li><a href="admin.php?mod=home"><?php echo $langmenu1; ?></a></li>
                <li class="current"><a>Input, edit, view and delete gallery management</a></li>
            </ul>
        </div>
    </div>
    <!-- Main content -->
    <div class="wrapper">
<?php
switch($_GET[act]){
  default:
?>
<script type="text/javascript">
$(document).ready( function() {
var oTable = $('.dTable').dataTable();
    $('#alertdel').dialog({
		autoOpen: false,
		width: 400,
		modal: true,
		buttons: {
			"<?php echo $langdelete3; ?>": function() {
				var id = $('#delid').val();
				var mod = 'gallery';
				var act =  'deletegallery';
				var dataString = 'id='+ id + '&mod='+ mod + '&act='+ act;
				$.ajax({
					type: "POST",
					url: "po-component/po-gallery/proses.php",
					data: dataString,
					cache: false,
					success: function(){
						$('#del'+id).slideUp('slow', function() {$(this).remove();});
				}
				});
				$( this ).dialog( "close" );
			},
			"<?php echo $langdelete4; ?>": function() {
				$( this ).dialog( "close" );
			}
		}
	});
    oTable.$('.alertdel').click(function () {
		var id = $(this).attr("id");
        $('#alertdel').dialog('open');
		$('#delid').val(id);
        return false;
    });
});
</script>
	<div class="widget">
		<div class="whead"><span class="titleIcon check"><input type="checkbox" id="titleCheck" name="titleCheck" /></span><h6>Gallery Management</h6><div class="clear"></div></div>
            <ul class="tToolbar">
                <li><a href="?mod=gallery&act=addnew" title=""><span class="icos-add"></span>Add New</a></li>
				<li><a href="?mod=gallery&act=album" title=""><span class="icos-images"></span>Album</a></li>
            </ul>
			<div id="dyn" class="hiddenpars">
                <a class="tOptions" title="Options"><img src="images/icons/options" alt="" /></a>
				<form method="post" action="<?php echo $aksi; ?>">
					<input type="hidden" name="mod" value="gallery">
					<input type="hidden" name="act" value="multidelete">
					<input type="hidden" value="0" name="totaldata" id="totaldata">
<?php
				$tablegallery = new PoTable('gallery');
				$gallerys = $tablegallery->findAll(id_gallery, DESC);
				echo "<table cellpadding='0' cellspacing='0' border='0' class='dTable checkAll check' id='dynamic'>
						<thead><tr>
							<th><img src='images/elements/other/tableArrows.png' /></th>
							<th>No</th>
							<th>Album</th>
							<th>Title</th>
							<th>Preview</th>
							<th>Action</th>
						</tr></thead><tbody>";
						$no=1;
						foreach($gallerys as $gallery){
						$valid = $gallery->id_album;
						$tablealbum = new PoTable('album');
						$currentAlbum = $tablealbum->findBy(id_album, $valid);
						$currentAlbum = $currentAlbum->current();
						echo "<tr class='gradeA' id='del$gallery->id_gallery'>
							<td><input type='checkbox' id='titleCheckdel'></td>
							<input type='hidden' class='deldata' name='item[$no][deldata]' value='$gallery->id_gallery' disabled>
							<td>$no</td>
							<td>$currentAlbum->title</td>
							<td>$gallery->title</td>
							<td><div id='gallery'><i class='icon-picture'></i> <a class='lightbox' rel='group' href='../po-content/po-upload/$gallery->picture'>Image</a></div></td>
							<td class='tableActs' align='center' style='width:100px'>
								<a href='?mod=gallery&act=edit&id=$gallery->id_gallery' class='tablectrl_small bDefault tipS' title='$langaction1'><span class='iconb' data-icon='&#xe1db;'></span></a>
								<span id='$gallery->id_gallery' class='alertdel'><a href='$gallery->id_gallery' class='tablectrl_small bDefault tipS' title='$langaction2'><span class='iconb' data-icon='&#xe136;'></span></a></span>
							</td>
						</tr>";
						$no++;
						}
					echo "</tbody>
					<tfoot>
						<tr><td colspan='6'><div class='itemActions'><input class='buttonS bRed' type='submit' value='Delete Selected Item' onClick=\"return confirm('$langdelete2')\"></div></td></tr>
					</tfoot>
				</table>";
?>
				</form>
			</div>
		<div class="clear"></div>
	</div>
	<div id="alertdel" title="<?php echo $langdelete1; ?>" style="display:none;">
		<p><input type='hidden' id='delid' name='delid'><span class='icon-warning'></span> <?php echo $langdelete2; ?></p>
	</div>
<?php
    break;


	case "addnew":
?>
		<form id="usualValidate" class="main" method="post" action="<?php echo $aksi; ?>" enctype="multipart/form-data" autocomplete="off">
            <fieldset>
				<div class="widget fluid">
				<div class="whead"><h6>Add New</h6><div class="clear"></div></div>
					<input type="hidden" name="mod" value="gallery">
					<input type="hidden" name="act" value="inputgallery">
					<?php
					$tablealbums = new PoTable('album');
					$albums = $tablealbums->findAll(id_album, DESC);
					$numalbums = $tablealbums->numRow();
					if ($numalbums > 0){
					echo "<div class='formRow'>
						<div class='grid3'><label>Album</label></div>
						<div class='grid9 searchDrop'>";
							echo "<select data-placeholder='Choose a Album' class='select' name='id_album' style='width:280px;'>";
								foreach($albums as $album){
									echo "<option value='$album->id_album'>$album->title</option>";
								}
								echo "</select>
						</div>
						<div class='clear'></div>
					</div>";
					echo "<div class='formRow'>
						<div class='grid3'><label>Or Add New Album</label></div>
						<div class='grid9 searchDrop'>
							<a id='tbladdalbum' class='buttonM bGreen'><span class='icol-add'></span><span>Add New Album</span></a>
						</div>
						<div class='clear'></div>
					</div>";
					}else{
					echo "<div class='formRow'>
						<div class='grid3'><label>Add New Album</label></div>
						<div class='grid9 searchDrop'>
							<a id='tbladdalbum' class='buttonM bGreen'><span class='icol-add'></span><span>Add New Album</span></a>
						</div>
						<div class='clear'></div>
					</div>";
					}
					?>
					<div class="formRow">
						<div class="grid3"><label>Title</label></div>
						<div class="grid9"><input class="required" type="text" id="title" name="title" required></div>
						<div class="clear"></div>
					</div>
					<div class="formRow">
						<div class="grid3"><label>Image</label></div>
						<div class="grid9"><div class="btn-group" data-toggle="buttons-radio">
							<button type="button" id="btn_uploadfpc" class="buttonS bGreen">Image From PC</button>
							<a class="buttonS bRed" id="btn_uploadfweb">Images From Web</a>
						</div></div>
						<div class="clear"></div>
					</div>
					<div class="formRow" id="uploadfpc" style="display:none;">
						<div class="grid3"><label>Select Image</label></div>
						<div class="grid9"><input style="opacity: 0;" size="24" class="fileInput" id="fileInput" name="fupload" type="file"><br /><br /><i>Please upload image type must be jpg</i></div>
						<div class="clear"></div>
					</div>
					<div class="formRow" id="uploadfweb" style="display:none;">
						<div class="grid3"><label>Select Image</label></div>
						<div class="grid9"><input type="text" id="picture" name="picture"></div>
						<div class="clear"></div>
					</div>
					<div class="formRow">
						<input class="buttonM bBlue" type="submit" value="Submit">
						<input class="buttonM bRed formSubmit" type="button" value="Cancel" onclick=self.history.back()><div class="clear"></div>
					</div>
				</div>
            </fieldset>
		</form>
		<!-- Dialog content -->
		<div id="addalbumgallery" class="addalbum" title="Add Album">
			<form id="formaddalbum" method="post" action="<?php echo $aksi; ?>" autocomplete="off">
				<input type="hidden" name="mod" value="gallery">
				<input type="hidden" name="act" value="inputalbum">
				<input type="hidden" id="addalb" name="addalb" value="addnew">
				<label>Title</label><br /><input class="required" type="text" id="title" name="title" style="width:97%;" required>
				<div class="divider"><span></span></div>
				<span class="floatR" style="margin-top:5px; margin-bottom:5px;"><input class="buttonM bBlue" type="submit" value="Submit"></span>
			</form>
		</div>
	</div>
<?php
	break;


	case "edit":
	$valid = $val->validasi($_GET['id'],'sql');
	$table = new PoTable('gallery');
	$currentGallery = $table->findBy(id_gallery, $valid);
	$currentGallery = $currentGallery->current();

	if ($currentGallery == '0'){
?>
		<div class="wrapper" id="errorWrapperadmin">
			<span class="errorNum">404</span>
			<div class="errorContent">
				<span class="errorDesc"><span class="icon-warning"></span>Sorry! Your Request Data Not Found. Please Try Again Submit Your Data</span>
			</div>
		</div>
<?php
	}else{
?>
		<form id="usualValidate" class="main" method="post" action="<?php echo $aksi; ?>" enctype="multipart/form-data" autocomplete="off">
            <fieldset>
				<div class="widget fluid">
				<div class="whead"><h6>Add New</h6><div class="clear"></div></div>
					<input type="hidden" name="mod" value="gallery">
					<input type="hidden" name="act" value="editgallery">
					<input type="hidden" name="id" value="<?php echo $currentGallery->id_gallery; ?>">
					<?php
					$tableselcats = new PoTable('album');
					$selcats = $tableselcats->findBy(id_album, $currentGallery->id_album);
					$selcats = $selcats->current();
					$tablealbums = new PoTable('album');
					$albums = $tablealbums->findNotAll(id_album, $currentGallery->id_album);
					$numalbums = $tablealbums->numRow();
					if ($numalbums > 0){
					echo "<div class='formRow'>
						<div class='grid3'><label>Album</label></div>
						<div class='grid9 searchDrop'>";
							echo "<select data-placeholder='Choose a Album' class='select' name='id_album' style='width:280px;'>";
								echo "<option value='$selcats->id_album'>$selcats->title</option>";
								foreach($albums as $album){
									echo "<option value='$album->id_album'>$album->title</option>";
								}
								echo "</select>
						</div>
						<div class='clear'></div>
					</div>";
					echo "<div class='formRow'>
						<div class='grid3'><label>Or Add New Album</label></div>
						<div class='grid9 searchDrop'>
							<a id='tbladdalbum' class='buttonM bGreen'><span class='icol-add'></span><span>Add New Album</span></a>
						</div>
						<div class='clear'></div>
					</div>";
					}else{
					echo "<div class='formRow'>
						<div class='grid3'><label>Add New Album</label></div>
						<div class='grid9 searchDrop'>
							<a id='tbladdalbum' class='buttonM bGreen'><span class='icol-add'></span><span>Add New Album</span></a>
						</div>
						<div class='clear'></div>
					</div>";
					}
					?>
					<div class="formRow">
						<div class="grid3"><label>Title</label></div>
						<div class="grid9"><input class="required" type="text" id="title" name="title" value="<?php echo $currentGallery->title; ?>" required></div>
						<div class="clear"></div>
					</div>
					<div class="formRow">
						<input class="buttonM bBlue" type="submit" value="Submit">
						<input class="buttonM bRed formSubmit" type="button" value="Cancel" onclick=self.history.back()><div class="clear"></div>
					</div>
				</div>
            </fieldset>
		</form>
		<!-- Dialog content -->
		<div id="addalbumgallery" class="addalbum" title="Add Album">
			<form id="formaddalbum" method="post" action="<?php echo $aksi; ?>" autocomplete="off">
				<input type="hidden" name="mod" value="gallery">
				<input type="hidden" name="act" value="inputalbum">
				<input type="hidden" name="id" value="<?php echo $currentGallery->id_gallery; ?>">
				<input type="hidden" id="addalb" name="addalb" value="edit">
				<label>Title</label><br /><input class="required" type="text" id="title" name="title" style="width:97%;" required>
				<div class="divider"><span></span></div>
				<span class="floatR" style="margin-top:5px; margin-bottom:5px;"><input class="buttonM bBlue" type="submit" value="Submit"></span>
			</form>
		</div>
	</div>
<?php
	}
	break;


	case "album":
?>
<script type="text/javascript">
$(document).ready( function() {
var oTable = $('.dTable').dataTable();
    $('#alertdela').dialog({
		autoOpen: false,
		width: 400,
		modal: true,
		buttons: {
			"<?php echo $langdelete3; ?>": function() {
				var id = $('#delida').val();
				var mod = 'gallery';
				var act =  'deletealbum';
				var dataString = 'id='+ id + '&mod='+ mod + '&act='+ act;
				$.ajax({
					type: "POST",
					url: "po-component/po-gallery/proses.php",
					data: dataString,
					cache: false,
					success: function(){
						$('#dela'+id).slideUp('slow', function() {$(this).remove();});
				}
				});
				$( this ).dialog( "close" );
			},
			"<?php echo $langdelete4; ?>": function() {
				$( this ).dialog( "close" );
			}
		}
	});
    oTable.$('.alertdela').click(function () {
		var id = $(this).attr("id");
        $('#alertdela').dialog('open');
		$('#delida').val(id);
        return false;
    });
});
</script>

<script type="text/javascript">
$(document).ready(function () {
var oTable = $('.dTable').dataTable();
    oTable.$('.tblactive').click(function(){
        var ida = $(this).attr('id');
		var adata = $('#activespan'+ida).html();
		if (adata == "Y"){
			var active = "N";
		}else{
			var active = "Y";
		}
		var mod = 'gallery';
		var act =  'activealbum';
		var dataString = 'id='+ ida + '&mod='+ mod + '&act='+ act + '&active='+ active;
		$.ajax({
			type: "POST",
			url: "po-component/po-gallery/proses.php",
			data: dataString,
			cache: false,
			success: function(html){
				$('#activespan'+ida).html(html);
			}
		});
		return false;
    });
});
</script>
	<div class="widget">
		<div class="whead"><h6>Album Management</h6><div class="clear"></div></div>
            <ul class="tToolbar">
				<li><a href="admin.php?mod=gallery" title=""><span class="icos-images"></span>Back To Gallery</a></li>
            </ul>
			<div id="dyn" class="hiddenpars">
                <a class="tOptions" title="Options"><img src="images/icons/options" alt="" /></a>
<?php
				$tablealbum = new PoTable('album');
				$albums = $tablealbum->findAll(id_album, DESC);
				echo "<table cellpadding='0' cellspacing='0' border='0' class='dTable' id='dynamic'>
						<thead><tr>
							<th>No</th>
							<th>Title</th>
							<th>Active</th>
							<th>Action</th>
						</tr></thead><tbody>";
						$no=1;
						foreach($albums as $album){
						echo "<tr class='gradeA' id='dela$album->id_album'>
							<td>$no</td>
							<td style='width:60%;'>$album->title</td>
							<td><span id='activespan$album->id_album'>$album->active</span></td>
							<td class='tableActs' align='center' style='width:100px'>
								<span id='$album->id_album' class='tblactive'><a href='#active$album->id_album' class='tablectrl_small bDefault tipS' title='Active'><span class='iconb' data-icon='&#xe1f7;'></span></a></span>
								<span id='$album->id_album' class='tbledit'><a href='#editdata$album->id_album' class='tablectrl_small bDefault tipS' title='$langaction1'><span class='iconb' data-icon='&#xe1db;'></span></a></span>
								<span id='$album->id_album' class='alertdela'><a href='#delete$album->id_album' class='tablectrl_small bDefault tipS' title='$langaction2'><span class='iconb' data-icon='&#xe136;'></span></a></span>
							</td>
						</tr>";
						?>
                        <div id="editdata<?php echo $album->id_album; ?>" class="editdata" title="Edit Album">
                            <form id="formedit" method="post" action="<?php echo $aksi; ?>" autocomplete="off">
								<input type='hidden' name='mod' value='gallery'>
								<input type='hidden' name='act' value='editalbum'>
								<input type="hidden" name="id" value="<?php echo $album->id_album; ?>">
								<label>Title</label><input class="required" type="text" id="title" name="title" value="<?php echo $album->title; ?>" style="width:97%;" required>
								<div class="divider"><span></span></div>
								<span class="floatR" style="margin-top:5px; margin-bottom:5px;"><input class="buttonM bBlue" type="submit" value="Submit"></span>
                            </form>
                        </div>
						<?php
						$no++;
						}
					echo "</tbody></table>";
?>
			</div>
		<div class="clear"></div>
	</div>
	<div id="alertdela" title="<?php echo $langdelete1; ?>" style="display:none;">
		<p><input type='hidden' id='delida' name='delida'><span class='icon-warning'></span> <?php echo $langdelete2; ?></p>
	</div>
<?php
    break;
}
?>
		<!-- Dialog content -->
		<div id="customDialog" class="customDialog" title="Images From Web">
			<div id="paging_container">
				<table class="tDefault" cellpadding="0" cellspacing="0" width="100%">
					<thead><tr>
						<td>Image</td>
						<td>Image Name</td>
						<td>Action</td>
					</tr></thead><tbody class="contentmed">
					<?php
						$tablemedia = new PoTable("media");
						$medias = $tablemedia->findAll(id_media, DESC);
						foreach($medias as $media){
							$pecah = explode("/", $media->file_type);
							$fileType = $pecah[0];
							$cari = "$media->file_name";
							$tanda = "popoji";
							$tanda2 = "pogallery";
							if ($fileType=="image"){
					?>
								<tr>
									<td><a href="../po-content/po-upload/<?php echo $media->file_name; ?>" title="" class="lightbox"><img src="../po-content/po-upload/medium-<?php echo $media->file_name; ?>" width="60" height="60" class="img-polaroid" /></a></td>
									<td><?php echo $media->file_name; ?></td>
									<td>
										<?php if ($yangdicari = strstr($cari, $tanda) OR $yangdicari = strstr($cari, $tanda2)){ ?>
											<a id="<?php echo $media->file_name; ?>" class="addtoform"><span class="label label-info"><i class="icon-picture icon-white"></i>Add to Form</span></a>
										<?php }else{ ?>
											<span class="label label-inverse"><i class="icon-picture icon-white"></i>Can't Add</span>
										<?php } ?>
									</td>
								</tr>
					<?php
							}else{}
						}
					?>
					</tbody></table>
				<div class="page_navigation"></div>
			</div>
		</div>
	</div>
<?php
}
?>