 			<div class="error">You must enter search criteria.</div>
 			<cfabort>
 		</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>
+		
+
 	</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>

				<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.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>

    	return findIn;
    }
</cfscript>
	<!-- Default mediaSearch sql query-->
	<cfset sql = "SELECT * FROM media_flat ">	
	
	<cfset whr ="WHERE media_flat.media_id > 0">
	<cfset srch=" ">
	<cfif isdefined("srchType") and srchType is "key">

		<cfif isdefined("keyword") and len(keyword) gt 0>

			<cfif not isdefined("kwType") ><cfset kwType="all"></cfif>
			<cfif kwType is "any">
				<cfset kwsql="">
