 				<cfset srch="#srch# AND upper(keywords) like '%#ucase(keyword)#%'">
 			</cfif>
 		</cfif>
+		
 		<cfif isdefined("media_uri") and len(media_uri) gt 0>
 			<cfset srch="#srch# AND upper(media_uri) like '%#ucase(media_uri)#%'">
 		</cfif>
 		<cfif isdefined("tag") and len(tag) gt 0>
+			<cfset whr="#whr# AND media_flat.media_id IN (select media_id from tag)">
+		</cfif>
+		<cfif isdefined("media_type") and len(media_type) gt 0>
+			<cfset srch="#whr# AND media_type IN (#listQualify(media_type,"'")#)">
+		</cfif>
+		
+		<cfif isdefined("media_id") and len(#media_id#) gt 0>
+			<cfset whr="#whr# AND media_flat.media_id in (#media_id#)">
+		</cfif>
+		<cfif isdefined("mime_type") and len(#mime_type#) gt 0>
+			<cfset srch="#whr# AND mime_type in (#listQualify(mime_type,"'")#)">
+		</cfif>
+
+	<cfelse>
+
+		<cfif isdefined("media_uri") and len(media_uri) gt 0>
+			<cfset srch="#srch# AND upper(media_uri) like '%#ucase(media_uri)#%'">
+		</cfif>
+		<cfif isdefined("media_type") and len(media_type) gt 0>
+			<cfset srch="#srch# AND upper(media_type) like '%#ucase(media_type)#%'">
+		</cfif>
+		<cfif isdefined("tag") and len(tag) gt 0>
 			<cfset whr="#whr# AND media.media_id in (select media_id from tag)">
-		</cfif>
-		<cfif isdefined("media_type") and len(media_type) gt 0>
-			<cfset srch="#srch# AND media_type in (#listQualify(media_type,"'")#)">
 		</cfif>
 		<cfif isdefined("media_id") and len(#media_id#) gt 0>
 			<cfset whr="#whr# AND media.media_id in (#media_id#)">
 		</cfif>
 		<cfif isdefined("mime_type") and len(#mime_type#) gt 0>
-			<cfset srch="#srch# AND mime_type in (#listQualify(mime_type,"'")#)">
-		</cfif>
-		<cfset ssql="select * from (#sel# #frm# #whr# #srch# order by media_id) where rownum <=500">
-		<cfquery name="findIDs" datasource="user_login" username="#session.dbuser#" password="#decrypt(session.epw,cfid)#" cachedwithin="#createtimespan(0,0,60,0)#">
-			#preservesinglequotes(ssql)#
-		</cfquery>
-	<cfelse>
-		<cfset sel="select distinct media.media_id,media.media_uri,media.mime_type,media.media_type,media.preview_uri "> 
-		<cfset frm="from media">			
-		<cfset whr=" where media.media_id > 0">
-		<cfset srch=" ">
-		<cfif isdefined("media_uri") and len(media_uri) gt 0>
-			<cfset srch="#srch# AND upper(media_uri) like '%#ucase(media_uri)#%'">
-		</cfif>
-		<cfif isdefined("media_type") and len(media_type) gt 0>
-			<cfset srch="#srch# AND upper(media_type) like '%#ucase(media_type)#%'">
-		</cfif>
-		<cfif isdefined("tag") and len(tag) gt 0>
-			<cfset whr="#whr# AND media.media_id in (select media_id from tag)">
-		</cfif>
-		<cfif isdefined("media_id") and len(#media_id#) gt 0>
-			<cfset whr="#whr# AND media.media_id in (#media_id#)">
-		</cfif>
-		<cfif isdefined("mime_type") and len(#mime_type#) gt 0>
 			<cfset srch="#srch# AND mime_type = '#mime_type#'">
 		</cfif>
+		
 		<cfif not isdefined("number_of_relations")>
 		    <cfif (isdefined("relationship") and len(relationship) gt 0) or (isdefined("related_to") and len(related_to) gt 0)>
 				<cfset number_of_relations=1>

				<cfset number_of_relations=1>
			</cfif>
		</cfif>
		<cfif not isdefined("number_of_labels")>
		    <cfif (isdefined("label") and len(label) gt 0) or (isdefined("label__1") and len(label__1) gt 0)>
				<cfset number_of_labels=1>

			<div class="error">You must enter search criteria.</div>
			<cfabort>
		</cfif>
		

	</cfif><!--- end srchType --->
	
	<!-- Limit results to 500 rows -->
	<cfset srch = "#srch# AND rownum <= 500">
	
	<!-- Finalize query -->
	<cfset ssql="#sql# #whr# #srch# order by media_id">		
	<cfquery name="findIDs" datasource="user_login" username="#session.dbuser#" password="#decrypt(session.epw,cfid)#" cachedwithin="#createtimespan(0,0,60,0)#">
		#preservesinglequotes(ssql)#
	</cfquery>
	
	<cfif findIDs.recordcount is 0>
		<div class="error">Nothing found.</div>
		<cfabort>
