Index: trunk/gallery.php
===================================================================
--- trunk/gallery.php	(revision 265)
+++ trunk/gallery.php	(revision 270)
@@ -16,4 +16,7 @@
 		$_GET["level"] = $resolved_path["level"];
 		$_GET["id"] = $resolved_path["id"];
+		if (isset($resolved_path['mode'])) {
+			$_GET['mode'] = $resolved_path['mode'];
+		};
 
 		// get page number from url, if present
Index: trunk/plog-load_config.php
===================================================================
--- trunk/plog-load_config.php	(revision 264)
+++ trunk/plog-load_config.php	(revision 270)
@@ -35,5 +35,5 @@
 };
 
-// remote admin/ from the end, if present .. is there a better way to determine the full url?
+// remove admin/ from the end, if present .. is there a better way to determine the full url?
 if (substr($config["baseurl"],-6) == "admin/") {
 	$config["baseurl"] = substr($config["baseurl"],0,-6);
Index: trunk/plog-functions.php
===================================================================
--- trunk/plog-functions.php	(revision 268)
+++ trunk/plog-functions.php	(revision 270)
@@ -22,5 +22,5 @@
 
 			$album_name = SmartStripSlashes($row["name"]);
-			$album_link = generate_url("album",$row["id"],$row["name"]);
+			$album_link = generate_url("album",$row["id"]);
 						
 			$query = "SELECT * FROM `".TABLE_PREFIX."collections` WHERE `id`='".$row["parent_id"]."'";
@@ -28,5 +28,5 @@
 			$row = mysql_fetch_assoc($result);
 			
-			$collection_link = '<a accesskey="/" href="' . generate_url("collection",$row["id"],$row["name"]) . '">' . $row["name"] . '</a>';
+			$collection_link = '<a accesskey="/" href="' . generate_url("collection",$row["id"]) . '">' . $row["name"] . '</a>';
 
 			if ($GLOBALS['plogger_mode'] == "slideshow") 
@@ -48,5 +48,5 @@
 			$row = mysql_fetch_assoc($result);
 			
-			$album_link = '<a accesskey="/" href="' . generate_url("album",$row["id"],$row["name"]) . '">' 
+			$album_link = '<a accesskey="/" href="' . generate_url("album",$row["id"]) . '">' 
 			. SmartStripSlashes($row["name"]) . '</a>';
 			
@@ -55,5 +55,5 @@
 			$row = mysql_fetch_assoc($result);
 			
-			$collection_link = '<a href="' . generate_url("collection",$row["id"],$row["name"]) . '">' 
+			$collection_link = '<a href="' . generate_url("collection",$row["id"]) . '">' 
 			. SmartStripSlashes($row["name"]) . '</a>';
 			
@@ -173,10 +173,10 @@
 
 			if ($row["collection_id"] != $last_collection) {
-				$output .= '<option value="'.generate_url("collection",$row["collection_id"],$row["collection_name"]).'">'.$row["collection_name"].'</option>';
+				$output .= '<option value="'.generate_url("collection",$row["collection_id"]).'">'.$row["collection_name"].'</option>';
 				$last_collection = $row["collection_id"];
 			};
 
 
-			$output .=  '<option value="'.generate_url("album",$row["album_id"],$row["album_name"]).'">'.SmartStripSlashes($row["collection_name"]).' : '.SmartStripSlashes($row["album_name"]);
+			$output .=  '<option value="'.generate_url("album",$row["album_id"]).'">'.SmartStripSlashes($row["collection_name"]).' : '.SmartStripSlashes($row["album_name"]);
 			$output .=  '</option>';
 		}
@@ -274,37 +274,38 @@
 	global $config;
 	$id = $GLOBALS["plogger_id"];
-	
+
+	$fields = array(
+		'date' => 'Date Submitted',
+		'date_taken' => 'Date Taken',
+		'caption' => 'Caption',
+		'filename' => 'Filename',
+		'number_of_comments' => 'Number of Comments',
+	);
+
 	if ($level == "album"){
-		
+	
+		// I think this should be a single form and I really should move the javascript functions
+		// into a separate file
+
+		// I need to merge those 2 forms. and since I'm realoading stuff anyway, I can just
+		// create correct urls. oh yeah, baby.
 		$output .= '
 			<form action="#" method="get">
 				Sort by:
 				<select id="change_sortby" name="change_sortby" 
-				onchange="document.location.href = \''.$config["baseurl"].
-				'?sortby=\'+this.options[this.selectedIndex].value+\'&level='.$level.'&amp;id='.$id.'&amp;n=0\';">
-				
-				<option value="date"';
-				if($_SESSION["plogger_sortby"] == 'date') $output .= ' selected="selected"';
-				$output .= '>Date Submitted</option><option value="date_taken"';
-				
-				if($_SESSION["plogger_sortby"] == 'date_taken') $output .= ' selected="selected"';
-				$output .= '>Date Taken</option>
-				
-				<option value="caption"';
-				if($_SESSION["plogger_sortby"] == 'caption') $output .= ' selected="selected"';
-				$output .= '>Caption</option>
-				
-				<option value="filename"';
-				if($_SESSION["plogger_sortby"] == 'filename') $output .= ' selected="selected"';
-				$output .= '>Filename</option>
-				
-				<option value="number_of_comments"';
-				if($_SESSION["plogger_sortby"] == 'number_of_comments') $output .= ' selected="selected"';
-				$output .= '>Number of Comments</option>
-				
-				</select>
-			</form>';
-	}
-	
+				onchange="document.location.href = this.options[this.selectedIndex].value;">
+				';
+
+				foreach($fields as$fkey => $fval) {
+					$value = generate_url("album",$id,array(1 => 'sorted','sortby' => $fkey,'sortdir' => $_SESSION['plogger_sortdir']));
+					$output .= "<option value='$value'";
+					if ($_SESSION["plogger_sortby"] == $fkey) {
+						$output .= " selected='selected'";
+					};
+					$output .= ">$fval</option\n";
+				}
+			
+		$output .= "</select> </form>\n";
+	}
 	return $output;
 }
@@ -314,4 +315,9 @@
 	global $config;
 	$id = $GLOBALS["plogger_id"];
+
+	$orders = array(
+		'asc' => 'Ascending',
+		'desc' => 'Descending',
+	);
 
 	if ($level == "album") {
@@ -320,18 +326,17 @@
 			<form action="#" method="get">
 				<select id="change_sortdir" name="change_sortdir" 
-				onchange="document.location.href = \''.$config["baseurl"].
-				'?sortdir=\'+this.options[this.selectedIndex].value+\'&level='.$level.'&amp;id='.$id.'&amp;n=0\';">
+				onchange="document.location.href = this.options[this.selectedIndex].value;">';
+				foreach($orders as $okey => $oval) {
+					$value = generate_url("album",$id,array(1 => 'sorted','sortby' => $_SESSION['plogger_sortby'],'sortdir' => $okey));
+					$output .= "<option value='$value'";
+					if($_SESSION["plogger_sortdir"] == $okey) {
+						$output .= ' selected="selected"';
+					};
+					$output .= ">$oval</option>\n";
+				}
 				
-				<option value="asc"';
 				
-				if($_SESSION["plogger_sortdir"] == 'asc') $output .= ' selected="selected"';
 				
-				$output .= '>Ascending</option><option value="desc"';
-				
-				if($_SESSION["plogger_sortdir"] == 'desc') $output .= ' selected="selected"';
-				
-				$output .= '>Descending</option>
-				</select>
-			</form>';		
+		$output .= '</select></form>';		
 	}
 	
@@ -590,5 +595,5 @@
 	$path_parts = explode("/",$str);
 
-	$levels = array("collection","album","picture");
+	$levels = array("collection","album","picture","arg1","arg2");
 
 	$current_level = "";
@@ -599,5 +604,5 @@
 	foreach($levels as $key => $level) {
 		if (isset($path_parts[$key])) {
-			$names[$level] = mysql_real_escape_string(urldecode(SmartStripSlashes($path_parts[$key])));
+			$names[$level] = mysql_escape_string(urldecode(SmartStripSlashes($path_parts[$key])));
 			$current_level = $level;
 		};
@@ -609,5 +614,7 @@
 		 $collection = mysql_fetch_assoc($result);
 		 // no such collection, return
-		 if (empty($collection)) return $rv;
+		 if (empty($collection)) {
+		 	return $rv;
+		}
 		 // what if there are multiple collections with same names? I hope there aren't .. this would
 		 // suck. But here is an idea, we shouldn't allow the user to enter similar names
@@ -615,5 +622,5 @@
 	};
 
-	if (!empty($names["album"])) {
+	if (!empty($names['album'])) {
 		$sql = "SELECT * FROM `".TABLE_PREFIX."albums`
 				WHERE path = '" . $names["album"] . "' AND parent_id = " . $collection["id"];
@@ -621,10 +628,28 @@
 		$album = mysql_fetch_assoc($result);
 		// no such album, fall back to collection
-		if (empty($album)) return $rv;
+		if (empty($album)) {
+			return $rv;
+		}	
 
 		// try to detect slideshow. Downside is that you cannot have a picture with that name
-		if ($names["picture"] == "slideshow") return array("level" => "slideshow","id" => $album["id"]);
-		
-		$rv = array("level" => "album","id" => $album["id"]);
+		if ('slideshow' == $names['picture']) {
+			return array('level' => 'album','mode' => 'slideshow','id' => $album['id']);
+		}
+
+		// deal with http://plogger/collection/album/sorted/field/asc and friends
+		if ('sorted' == $names['picture']) {
+			if (isset($names['arg1'])) {
+				$_SESSION['plogger_sortby'] = $names['arg1'];
+			};
+			
+			if (isset($names['arg2'])) {
+				$_SESSION['plogger_sortdir'] = $names['arg2'];
+			};
+
+			return array('level' => 'album','id' => $album['id']);
+
+		}
+		
+		$rv = array('level' => 'album','id' => $album['id']);
 	};
 
@@ -644,6 +669,7 @@
 
 		// no dice, fall back to album
-		if (!$picture) return $rv;
-
+		if (!$picture) {
+			return $rv;
+		}
 		$rv = array("level" => "picture", "id" => $picture["id"]);
 	};
@@ -707,5 +733,5 @@
 }
 
-function generate_url($level,$id,$name = ""){
+function generate_url($level,$id,$arg = array()){
 	global $config;
 	if ($config["use_mod_rewrite"]){
@@ -719,5 +745,13 @@
 			$result = run_query($query);
 			$row = mysql_fetch_assoc($result);
-			return $config["baseurl"].rawurlencode($row["collection_path"]) . '/' . rawurlencode($row["album_path"]);
+			$rv = $config["baseurl"].rawurlencode($row["collection_path"]) . '/' . rawurlencode($row["album_path"]);
+
+			// I need to give additional arguments to the url-s
+			if (sizeof($arg) > 0) {
+				foreach($arg as $aval) {
+					$rv .= "/".$aval;
+				};
+			};
+			return $rv;
 		} else if ($level == "picture") {
 			$pic = get_picture_by_id($id);
@@ -729,5 +763,15 @@
 			return $config['baseurl'].'?level=collection&amp;id='.$id;
 		} else if ($level == "album") {
-			return $config['baseurl'].'?level=album&amp;id='.$id;
+			$rv = $config['baseurl'].'?level=album&amp;id='.$id;
+			if (sizeof($arg) > 0) {
+				foreach($arg as $akey => $aval) {
+					// mod_rewrite url-s need /sorted in them, the old style ones do not.
+					// this temporary workaround removes the 'sorted' string
+					if ($aval != 'sorted') {
+						$rv .= "&amp;$akey=$aval";
+					};
+				};
+			}
+			return $rv;
 		} else if ($level == "picture") {
 			return $config['baseurl'].'?level=picture&amp;id='.$id;
@@ -753,5 +797,5 @@
 	while ($row = mysql_fetch_assoc($result)) {
 		// output collection name
-		$collection_link = '<a href="'.generate_url("collection",$row['id'], $row['name']).'">'.$row['name'].'</a>';
+		$collection_link = '<a href="'.generate_url("collection",$row['id']).'">'.$row['name'].'</a>';
 		$output .= "<li>$collection_link</li>";
 		
@@ -761,5 +805,5 @@
 		$output .= '<ul>';
 		while ($albums = mysql_fetch_assoc($result)) {
-			$album_link = '<a href="'.generate_url("albums",$albums['id'], $albums['name']).'">'.$albums['name'].'</a>';
+			$album_link = '<a href="'.generate_url("albums",$albums['id']).'">'.$albums['name'].'</a>';
 			$output .= "<li>$album_link</li>";
 		}
@@ -1171,5 +1215,5 @@
 				$rss_link .= "http://".$_SERVER["SERVER_NAME"]."/".SmartStripSlashes(substr($path,1))."/feed/";
 			else
-				$rss_link .= $config['baseurl']."feed/";
+				$rss_link .= $config['gallery_url']."feed/";
 					
 		} 
@@ -1197,16 +1241,11 @@
 	if ($GLOBALS['plogger_mode'] != "slideshow") {
 		if ($GLOBALS['plogger_level'] == "album") {
-			
-			$ss_url = $config["baseurl"]."?level=album&amp;mode=slideshow&amp;id=".$id;
-			
-			if ($config["use_mod_rewrite"]) {
-				$ss_url = generate_url($GLOBALS['plogger_level'], $GLOBALS['plogger_id']) . "/slideshow";
-			}
-			
+			$ss_url = generate_url('album',$GLOBALS['plogger_id'],array('mode' => 'slideshow'));
 			$ss_tag = "<a href=\"$ss_url\">View as Slideshow</a>";
 		}
 
 		if ($GLOBALS['plogger_level'] == "search") {
-			$ss_url = $p_url . "&mode=slideshow";
+			$ss_url = $config['baseurl'] . '?level=search&amp;searchterms=' . $_GET['searchterms'];
+			$ss_url .= "&amp;mode=slideshow";
 			$ss_tag = "<a href=\"$ss_url\">View as Slideshow</a>";
 		}
@@ -1424,5 +1463,5 @@
 function plogger_get_collection_url() {
 	$row = $GLOBALS["current_collection"];
-	return generate_url("collection",$row["id"],$row["name"]);
+	return generate_url("collection",$row["id"]);
 }
 
@@ -1487,5 +1526,5 @@
 function plogger_get_album_url() {
 	$row = $GLOBALS["current_album"];
-	return generate_url("album",$row["id"],$row["name"]);
+	return generate_url("album",$row["id"]);
 }
 
