/* usemedia.com . joes koppers . 09.2005 */
/* thnx for reading this code */

//version 24.07.06


/* search, filter and display items */

//tag html
tag_title = '<span class=thing_tag>&lt;title&gt;</span> ';
tag_what = ' <span class=thing_tag>&lt;what&gt;</span> ';
tag_where = ' <span class=thing_tag>&lt;where&gt;</span> ';
tag_when = ' <span class=thing_tag>&lt;when&gt;</span> ';
tag_who = ' <span class=thing_tag>&lt;who&gt;</span> ';
tag_tech = ' <span class=thing_tag>&lt;tech&gt;</span> ';
tag_short = ' <span class=thing_tag>&lt;short&gt;</span> ';

tag_title = '';

function find_init()
{
	for (var s=0; s<items.length; s++)
	{
		/* collect data to search through */
		
		var itemstr = '';
		itemstr+= items[s].title+' '+items[s].mediatype+' '+items[s].location+' '+items[s].longdate+' '+items[s].tech+' '+items[s].people+' '+items[s].desc+' '+items[s].content+' ';
		//add image captions
		for (var i=0; i<items[s].captions.length; i++) itemstr+= ' '+items[s].captions[i];
		//add qtl info
		for (var i=0; i<items[s].qtl_titles.length; i++) itemstr+= ' movie quicktime '+items[s].qtl_titles[i];
		//exclude '<a href="..">' from search data
		var offset=0;
		while (itemstr.indexOf("<a",offset)!=-1)
		{
			//get href portion
			var startindex = itemstr.indexOf("<a",offset);
			var endindex = itemstr.indexOf(" >",startindex);
			itemstr = itemstr.substr(0,startindex) + itemstr.substr(endindex);
			offset = startindex+1;
		}
		items[s].searchdata = itemstr;
	}
}

function find() //transition to find. mode
{
	if (docLoaded && maxsize!=.3)
	{
		//remember usetext state
		usetext_stopped = stopped;
		//hide display
		document.getElementById('display').style.display = 'none';
		//hide google extra
		if (google_query) document.getElementById('google').style.display = 'none';
		//extra margin for USE.
		document.getElementById('use').style.marginLeft = '5px';
		//transition to found pane
		do_transition(0.3,-1);
	}
}

function first_input()
{
	var searchstr = document.forms['findform'].searchinput.value;

	if (searchstr=='find.')
	{
		//clear form & change font to black
		document.forms['findform'].searchinput.value = '';
		document.getElementById('searchinput').style.color = '#000000';
	}
}

function last_input()
{
	//adds space when enter is pressed, this renders the last word as a 'whole word' (wordbounderies in actual query)
	document.forms['findform'].searchinput.value = document.forms['findform'].searchinput.value+" ";
}

function items_count(num)
{
	document.getElementById('items_count').innerHTML = (num==1)? num+' item':num+' items';
}



quoted = new Array();
searchstrings = new Array();

function do_find()
{
	searchstr = document.forms['findform'].searchinput.value;
	var search_close = document.getElementById('search_close');
	//searchmode = 'AND';
	searchmode = 'OR';	

	//show all?
	if (searchstr=='find.' || searchstr=='' || searchstr.length==1)
	{
		search_close.src = (searchstr.length!=1)? 'media/search_close.gif':'media/search_closeX.gif';
		//show all
		sorted_by_relevance = false;
		items_count(items.length);
		found(true);
		return;
	}
	else
	{
		search_close.src = 'media/search_closeX.gif';
		document.getElementById('show_all').style.display = 'inline';
	}
	
	//quoted search?
 	var quote = new RegExp("\"","g");
	quotes = searchstr.match(quote);
	quoted = new Array();
	if (quotes && quotes.length>1)
	{
		//find pairs
		for (var i=0; i<quotes.length-1; i+=2)
		{
			var firstquote = searchstr.indexOf('"');
			var secondquote = searchstr.indexOf('"',firstquote+1);
			//substract quoted portion
			quoted[i/2] = searchstr.substring(firstquote+1,secondquote);
			//update searchstring
			searchstr = searchstr.substr(0,firstquote) + searchstr.substr(secondquote+1);
		}
	}
		
	//perform search
	if (searchstr.length>1 || quoted.length>0)
	{
		if (!sort_overrule) sorted_by_relevance = true;
	
		searchstrings = new Array();
		
		//one or multi search-terms
		if (searchstr.indexOf(" ")==-1) searchstrings[0] = (searchstr.indexOf('"')==-1)? searchstr:searchstr.substr(1); //strip first quote
		else searchstrings = searchstr.split(" ");
	
		//make 2-dimensional results array
		results = new Array(items.length);
			
		for (var s=0; s<items.length; s++)
		{
			results[s] = new Array(2);
			results[s][0] = s; //keep index
			results[s][1] = 0; //default no match
			
			//match??
			searchmode = 'AND';
			quoted_matchloop:
			for (var i=0; i<quoted.length; i++)
			{
				if (quoted[i]=='') continue quoted_matchloop;
				
				// 'AND' searchmode
				if (searchmode=='AND' && i>0 && results[s][1]==0) continue quoted_matchloop;
				
				var regexp = new RegExp('\\b'+quoted[i]+'\\b',"gi"); //with word bounderies
				matches = items[s].searchdata.match(regexp);
				if (matches) results[s][1] += matches.length;
				else if (searchmode=='AND' && i>0) results[s][1] = 0;
			}

			searchmode = 'OR';
			matchloop:
			for (var i=0; i<searchstrings.length; i++)
			{
				if (searchstrings[i].length<=1 || searchstrings[i]==" ") continue matchloop;

				//remove first quote if any
				if (searchstrings[i].indexOf('"')==0) searchstrings[i] = searchstrings[i].substr(1);

				//use wordbounderies if multiple words and not the last word
				if (searchstrings.length!=1 && i<searchstrings.length-1) var regexp = new RegExp('\\b'+searchstrings[i]+'\\b',"gi");
				else var regexp = new RegExp(searchstrings[i],"gi");
				
				matches = items[s].searchdata.match(regexp);
				if (matches) results[s][1] += matches.length;
			}
			items[s].found = (results[s][1]!=0)? true:false;
		}
		
		//sort
		results.sort(descending_by_match);
		
		//display found items
		var found_str = '';
		for (var s=0; s<results.length; s++)
		{
			//stop if no match
			if (results[s][1]==0) break;

			var n = results[s][0];
			
			if (deeplink)
			{
				//expand item in deeplink mode
				items[n].expanded = true;
				//only once!
				deeplink = false;
			}
			var mode = (items[n].expanded)? 'expanded':'normal';
		
			var item_str = '';
			item_str+= '<div id=container_'+items[n].id+' class=container>';
			item_str+= generate_item(n,mode);
			item_str+= '</div>';
		
			items[n].html = item_str;
			items[n].color =  default_item_color;
			found_str += item_str;
		}
		
		items_count(s);
		if (sorted_by_relevance) document.getElementById('found').innerHTML = found_str;
		else found();
		document.getElementById('sorted').innerHTML = (sorted_by_relevance)? 'sorted by relevance | <a href="javascript://sort_by_date" onmousedown="re_sort(\'date\')" onclick="this.blur()">date</a>':'sorted by date | <a href="javascript://sort_by_relevance" onmousedown="re_sort(\'relevance\')" onclick="this.blur()">relevance</a>';
		
		//colorize results
		s--;
		if (s>0)
		{
			var max_match = results[0][1];
			var min_match = results[s][1];
	
			for (s; s>=0; s--)
			{
				//colorshade formula
				if (max_match!=min_match) var color = 180- ((results[s][1]-min_match) * (61/(max_match-min_match)));
				else color = 119; //default_item_color;
				
				items[results[s][0]].color = 'rgb('+parseInt(color)+','+parseInt(color+5)+','+parseInt(color+8)+')';
				//apply
				document.getElementById('thing_bg_'+items[results[s][0]].id).style.backgroundColor = items[results[s][0]].color;
			}
		}
	}
}

function descending_by_match(a,b)
{
	return (b[1] - a[1]);
}

function re_sort(how)
{
	var displaystr = (how=='relevance')? 'sorted by relevance | <a href="javascript://sort_by_date" onmousedown="re_sort(\'date\')" onclick="this.blur()">date</a>':'sorted by date | <a href="javascript://sort_by_relevance" onmousedown="re_sort(\'relevance\')" onclick="this.blur()">relevance</a>';
	document.getElementById('sorted').innerHTML = displaystr;
	
	sorted_by_relevance = (how=='relevance')? true:false;
	sort_overrule = (how=='date')? true:false;

	//update display
	found();
}

function show_all()
{
	var searchstr = document.forms['findform'].searchinput.value;
	if (searchstr!='find.' && searchstr!='');
	{
		document.forms['findform'].searchinput.value = 'find.';
		document.getElementById('searchinput').style.color = '#777C7F';
		document.getElementById('search_close').src = 'media/search_close.gif';
		document.getElementById('search_close').style.cursor = 'auto';

		sorted_by_relevance = false;
		items_count(items.length);
		found(true);
	}
}

function found(show_all)
{
	if (show_all)
	{
		document.getElementById('sorted').innerHTML = 'sorted by date';
		document.getElementById('show_all').style.display = 'none';
		//clear prev searchstrings
		quoted = new Array();
		searchstrings = new Array();
	}
	//slider values
	s_img = (slider)? slider[0].value:slider_settings[0];
	s_txt = (slider)? slider[1].value:slider_settings[2];

	var found_str = '';
	
	loop:
	for (n=0; n<items.length; n++)
	{
		if (show_all)
		{
			items[n].color = default_item_color;
			items[n].found = true;
		}
		//skip hidden items
		if (!items[n].found) continue loop;
				
		var mode = (items[n].expanded)? 'expanded':'normal';
	
		var item_str = '';
		item_str+= '<div id=container_'+items[n].id+' class=container>';
		item_str+= generate_item(n,mode);
		item_str+= '</div>';
		
		items[n].html = item_str;
		found_str += item_str;
	}	
	
	if (!sorted_by_relevance) document.getElementById('found').innerHTML = found_str;
	else
	{
		found_str = '';
		for (var n=0; n<results.length; n++)
		{
			if (results[n][1]==0) break;
			found_str += items[results[n][0]].html;
		}
		document.getElementById('found').innerHTML = found_str;
	}
}



function more(n)
{
	items[n].expanded = true;
	document.getElementById('container_'+items[n].id).innerHTML = generate_item(n,'expanded');
	
	if (scrolling_start) window.clearTimeout(scrolling_start);
	if (scrolling) window.clearTimeout(scrolling);

	scrolling_start = window.setTimeout('scroll_into_view('+n+')',350);
}

function scroll_into_view(n)
{
	var top = document.getElementById('container_'+items[n].id).offsetTop;
	var scrollmax = document.getElementById('found').scrollHeight-document.getElementById('found').offsetHeight;
	scrolltop = document.getElementById('found').scrollTop;
	
	direction = (top>scrolltop)? +1:-1;
	
	//pause usetext (for better performance on slower machines)
	pause();
	
	if (top-35>scrollmax) do_scroll(scrollmax,direction);
	else do_scroll(top-35,direction);
}

function do_scroll(destination,direction)
{
	scrolltop = scrolltop + .45*(destination-scrolltop);

	var margin = 3;
	var condition = (direction>0)? (scrolltop<destination-margin):(scrolltop>destination+margin);
	
	if (condition)
	{
		document.getElementById('found').scrollTop = scrolltop;
		scrolling = window.setTimeout('do_scroll('+destination+','+direction+')',20);
	}
	else //done scrolling
	{
		document.getElementById('found').scrollTop = destination;
		if (stopped && !usetext_stopped) startstop();
		//else if (stopped) glow(10,-1);
	}
}


function go_top()
{
	pause();
	scrolltop = document.getElementById('found').scrollTop;
	do_scroll(0,-1);
}

function less(n)
{
	items[n].expanded = false;
	document.getElementById('container_'+items[n].id).innerHTML = generate_item(n,'normal');

	if (scrolling_start) window.clearTimeout(scrolling_start);
	if (scrolling) window.clearTimeout(scrolling);

	scrolling_start = window.setTimeout('scroll_into_view('+n+')',350);
}

function expand_all()
{
	for (var n=0; n<items.length; n++)
	{
		if (items[n].found) items[n].expanded = true;
	}
	found();
}

function collapse_all()
{
	for (var n=0; n<items.length; n++) items[n].expanded = false;
	found();
}


function generate_item(n,mode)
{
	var str = '';
	
	
	/* collapsed */
	
	if (mode=='normal') 
	{
		
		//thumbs
		var imgs = Math.min(items[n].images.length,s_img);
		//width
		var t = (s_txt>3)? 2:1;
		var w = Math.min(imgs+t,5);
		if (s_txt==0 && imgs>0 && imgs<5) w = w-1;

		w = (w*120) + (((w*2)+(w-1))*8)-16;

		//text width
		if (imgs>3) t = 1;
		if (s_txt==0) t=0;
		w_txt = w - (Math.min(imgs,5-t)*(120+8));
		if (t==0 && imgs==1) w_txt = 0;
		//height
		h_sizer = (imgs==0 && s_txt==0 && s_img<2)? 92:78;
		if (s_txt==0 && s_img==0) h_sizer=34;
		if ((s_txt>3 && imgs>3) || (imgs>4 && s_txt!=0) || (s_txt==0 && imgs>5)) h_sizer= 93+8+75;


		//generate html

		str+= '<table id=thing_bg_'+items[n].id+' class=thing cellpadding=0 cellspacing=0 width='+(w+16)+' style="background-color:'+items[n].color+'" onmouseover="this.style.backgroundColor=\'rgb(85,90,93)\'" onmouseout="this.style.backgroundColor=items['+n+'].color">';
		str+= '<tr><td height=8><img src=media/tlc.'+imgtype+'></td><td height=8 align=right><img src=media/trc.'+imgtype+'></td></tr>';
		str+= '<tr><td colspan=2 id=item_'+items[n].id+' class=thing_content width='+w+'>';
		//text
		str+= '<div class=thing_text style="border:0px dotted red; display:block; width:'+w_txt+'px; height:auto;">';
		str+= '<img src=media/blank.gif style="float:right; width:1px; height:'+h_sizer+'px;">';
		var title = '<span class=thing_title><a href="javascript://more" onmousedown=more('+n+')>'+items[n].title+'</a></span>';
		switch (s_txt)
		{
			case 0:
				if (imgs==0) str+= title ;
				else str+= '<p>';
				break;
				
			case 1:
				str+= tag_title + title + tag_when + items[n].shortdate;
				break;
				
			case 2:
				str+= tag_title + title; 
				if (items[n].mediatype!='') str+= tag_what + items[n].mediatype;
				str+= tag_when + items[n].shortdate;
				break;
				
			case 3:
				str+= tag_title + title;
				if (items[n].mediatype!='') str+= tag_what + items[n].mediatype;
				if (items[n].location!='') str+= tag_where + items[n].location;
				str+= tag_when + items[n].longdate;
				break;
			
			case 4:
				str+= tag_title + title;
				if (items[n].mediatype!='') str+= tag_what + items[n].mediatype;
				if (items[n].location!='') str+= tag_where + items[n].location;
				str+= tag_when + items[n].longdate;
				str+= tag_short + items[n].desc.substr(0,items[n].desc.length/2) + '...';
				break;
				
			case 5:
				str+= tag_title + title;
				if (items[n].mediatype!='') str+= tag_what + items[n].mediatype;
				if (items[n].location!='') str+= tag_where + items[n].location;
				str+= tag_when + items[n].longdate;
				str+= tag_short + items[n].desc + '...';
				break;
		}
		str+= '</div>';
		//thumbs		
		for (var i=0; i<imgs; i++) 
		{
			var margin = (i==imgs-1)? 'margin-right:'+thumbmargin+'px;':'';
			if ((t>0 && i>3) || i>4) margin += 'margin-top:8px;';
			margin = (margin!='')? 'style="'+margin+'"':'';
			
			//str+= '<img src=media/items/'+items[n].images[i]+'_thumb.jpg class=thumb title="'+items[n].captions[i]+'" '+margin+' onclick="view('+items[n].id+','+items[n].images[i]+','+items[n].images_w[i]+','+items[n].images_h[i]+')" onmouseover="this.style.borderColor=\'#e9e9e9\'" onmouseout="this.style.borderColor=\'#777C7F\'">';
			//the thumbnails in preview mode now expand the item first...
			str+= '<img src=media/items/'+items[n].images[i]+'_thumb.jpg class=thumb title="more&gt;" '+margin+' onclick="more('+n+')" onmouseover="//this.style.borderColor=\'#e9e9e9\'" onmouseout="//this.style.borderColor=\'#777C7F\'">';
		}
		//blank fill img for double row items
		if (h_sizer>100)
		{
			var w_placeholder = (t<1)? (10-imgs)*126:(8-imgs)*126;
			str+= '<div style="float:left"><img src=media/blank.gif onmousedown="return false;" style="width:'+(w_placeholder)+'px; height:76px; margin:8px 0px 0px 0px;"></div>';		
		}
		
		var expand = (s_txt==0 && (s_img==0 || imgs==2 || imgs==3))? '&nbsp;':'more';
		var expand_style = (s_txt==0 && imgs==2)? 'style="float:left"':'';
	
		str+= '<div class=thing_expand '+expand_style+'><a href="javascript://more" onmousedown=more('+n+')>'+expand+'&gt;</a></div>';
		str+= '</td></tr>';
		str+= '<tr><td height=8><img src=media/blc.'+imgtype+'></td><td height=8 align=right><img src=media/brc.'+imgtype+'></td></tr>';
		str+= '</table>';
	}
	
	
	/* expanded */
	
	else if (mode=='expanded') 
	{
		//width
		var w = items[n].w_img + items[n].w_txt;
		var w_txt = items[n].w_txt;
	
		w = (w*120) + (((w*2)+(w-1))*8)-16;
		w_txt = w - (items[n].w_img*(120+8)) - 36;
		if (items[n].w_txt==1 && items[n].w_img==0) w_txt+=33;
	
		//height
		var h_txt = items[n].h_txt;
		h_txt = (h_txt*98) +1//- 23;
	
		str+= '<table id=thing_bg_'+items[n].id+' class=thing cellpadding=0 cellspacing=0 width='+(w+16)+' style="background-color:'+items[n].color+'" onmouseover="item_over(this,true,'+n+');" onmouseout="item_over(this,false,'+n+');">';
		str+= '<tr><td height=8><img src=media/tlc.'+imgtype+'></td><td height=8 align=right><img src=media/trc.'+imgtype+'></td></tr>';
		str+= '<tr><td colspan=2 id=item_'+items[n].id+' class=thing_content width='+w+'>';
		
		infostr = '';
		if (items[n].mediatype!='') infostr+= tag_what + items[n].mediatype;
		if (items[n].location!='') infostr+= tag_where + items[n].location;
		if (items[n].people!='') infostr+= tag_who + items[n].people;
		if (items[n].tech!='') infostr+= tag_tech + items[n].tech;
		
		//header		
		str+= '<div style="border-bottom:1px dotted #e9e9e9; margin-bottom:8px; margin-top:-2px;">';
		str+= '<table border=0 cellpadding=0 cellspacing=0  style="width:'+w+'px; margin-bottom:4px; "><tr valign=top>';
		str+= '<td width=120 class=thing_expanded_header><span class=thing_title><a href="javascript://less" onmousedown=less('+n+')>'+items[n].title+'</a></span><br>'+items[n].longdate+'</td>';
		if (items[n].w_img+items[n].w_txt==1) str+='</tr><td></td></tr><tr>';
		str+= '<td class=thing_expanded_header>'+infostr+'</td>';
		if (items[n].w_img+items[n].w_txt==1) str+='</tr><tr>';
		str+= '<td width=35 align=right class=thing_expand style="padding-bottom:3px;">';
		//item func
		str+= '<a href="javascript://less" onmousedown="less('+n+')">&lt;less</a><div style="height:5px; width:30px; display:block; font-size:0px;">&nbsp;</div>';
		str+= '<a href="javascript://link" onclick="item_link('+n+');this.blur()" title="copy or email URL to this item">&gt;link</a></td>';
		str+= '</tr></table></div>';
		//text util
		if (items[n].content.length!=0)
		{
			var text_utils_w = (items[n].w_txt==1 && items[n].w_img==0)? 120:32;
			str+= '<div class=thing_expand style="width:'+text_utils_w+'px; height:25px; margin:2px 1px 0px 0px;">';
			str+= '<a href="javascript://less" onmousedown="text_size('+items[n].id+')" onclick="this.blur()" title="change text size" >A<span id=thing_increase_'+items[n].id+' style="font-size:9px; vertical-align:top; line-height:.6;">+</span></a>&nbsp;';
			str+= '<span id=thing_inv_'+items[n].id+' class=thing_invert onmousedown="text_invert('+items[n].id+')" title="invert text">A</span>';
			str+= '</div>';
		}
		//text
		str+= '<div id=thing_txt_'+items[n].id+' class=thing_expanded_text style="width:'+w_txt+'px; height:auto; display:block;">';
		str+= '<img src=media/blank.gif style="float:right; background-color:none; width:1px; height:'+h_txt+'px;">';
		str+= '<div style="margin:2px 4px;" >'+hilighted_content(n)+'</div>';

		str+= '</div>';
	
		//thumbs		
		for (var i=0; i<items[n].images.length; i++) 
		{
			//margin = (i>0)? 'style="margin-bottom:8px;"':'';
			margin = 'style="margin-bottom:8px;"'
			if (items[n].w_img+items[n].w_txt==1) margin = margin.substring(0,margin.length-1)+' margin-right:0px;"';
			else if (i==items[n].images.length-1 && items[n].qtls.length==0) margin = margin.substring(0,margin.length-1)+' margin-right:6px;"'; //this is only needed for m$ exploder
			str+= '		<img src=media/items/'+items[n].images[i]+'_thumb.jpg class=thumb title="view&gt; '+items[n].captions[i]+'" '+margin+' onclick="view('+items[n].id+','+items[n].images[i]+','+items[n].images_w[i]+','+items[n].images_h[i]+')" onmouseover="this.style.borderColor=\'#e9e9e9\'" onmouseout="this.style.borderColor=\'#777C7F\'">';
		}

		//qtls
		if (items[n].qtls.length>0)
		{
			for (var i=0; i<items[n].qtls.length; i++)
			{
				var title = (items[n].qtl_titles[i].substring(0,1)=='*')? items[n].qtl_titles[i].substr(1):items[n].qtl_titles[i];
				var qt = (items[n].qtl_titles[i].substring(0,1)=='*')? 1:0;
				str+= qt_embed(n,items[n].qtls[i],title,items[n].qtl_srcs[i],qt);
			}
		}

		//blank img to fill remaining space;
	/* 	if (h_sizer>100) */
	/* 	{ */
	/* 		var w_placeholder = (t<1)? (10-imgs)*126:(8-imgs)*126; */
	/* 		str+= '		<div style="float:left"><img src=media/blank.gif onmousedown="return false;" style="width:'+(w_placeholder)+'px; height:76px; margin:8px 0px 0px 0px;"></div>';		 */
	/* 	} */
		
		str+= '</td></tr>';
		//str+= '<tr><td class=thing_footer><img src=media/blc.'+imgtype+'></td></tr>';
		str+= '<tr><td height=8><img src=media/blc.'+imgtype+'></td><td height=8 align=right><img src=media/brc.'+imgtype+'></td></tr>';
		str+= '</table>';
	
	}
	
	return str;
}

function qt_embed(n,id,title,src,qt)
{
	var qtstr = '';
	qtstr+= '<div class=qtl onmouseover="this.firstChild.src=\'media/qtl_whiteX.'+imgtype+'\'" onmouseout="this.firstChild.src=\'media/qtl_white.'+imgtype+'\'" onclick="//watch('+items[n].id+','+id+','+qt+')">';
	qtstr+= '<img src="media/qtl_white.'+imgtype+'">';
	qtstr+= '<div class=qtl_title><a href="javascript:watch('+items[n].id+','+id+','+qt+')">'+title+'</a></div>';
	qtstr+= '</div>';
	
	return qtstr;
}


function hilighted_content(n)
{
	if (quoted.length==0 && searchstrings.length==0) return items[n].content;

	content = items[n].content;

	//hilight quoted matches
	quoted_hilightloop:
	for (var i=0; i<quoted.length; i++)
	{
		if (quoted[i]=='') continue quoted_hilightloop;
		
		var regexp = new RegExp('\\b'+quoted[i]+'\\b',"gi"); //with word bounderies
		content = content.replace(regexp,'<strong>'+quoted[i]+'</strong>');
	}
		
	//hilight regular matches
	hilightloop:
	for (var i=0; i<searchstrings.length; i++)
	{
		if (searchstrings[i].length<=1 || searchstrings[i]==" ") continue hilightloop;
		
		//skip linebreaks (<br>)
		if (searchstrings[i]=='br') continue hilightloop;
	
		//use wordbounderies if multiple words and not the last word
		if (searchstrings.length!=1 && i<searchstrings.length-1) var regexp = new RegExp('\\b'+searchstrings[i]+'\\b',"gi");
		else var regexp = new RegExp(searchstrings[i],"gi");
		content = content.replace(regexp,'<strong>'+searchstrings[i]+'</strong>');
	}
	
	//undo hilighs in hrefs (!)
	var offset=0;
	while (content.indexOf("<a",offset)!=-1)
	{
		//get href portion
		var startindex = content.indexOf("<a",offset);
		var endindex = content.indexOf(" >",startindex); //use space before closing a-tag!
		//strip <strong> tags from it
		content = content.substr(0,startindex) + content.substring(startindex,endindex).replace(/<strong>|<\/strong>/g,"") + content.substr(endindex);
		offset = startindex+1;
	}

	return content;
}

function item_over(obj,show,n)
{
	obj.style.backgroundColor = (show)? 'rgb(85,90,93)':items[n].color;
}

inverted = false;

function text_invert(id)
{
	inverted = !inverted;
	
	document.getElementById('thing_txt_'+id).style.backgroundColor = (inverted)? 'white':'transparent';
	document.getElementById('thing_txt_'+id).style.color = (inverted)? 'black':'#eeeeee';
	
	var A = document.getElementById('thing_inv_'+id)
	A.style.backgroundColor = (inverted)? 'transparent':'#e9e9e9';
	A.style.color = (inverted)? 'white':'black';
	A.style.border = (inverted)? '1px solid #eeeeee':'none';
}

increased = false;

function text_size(id)
{
	increased = !increased;
	
	document.getElementById('thing_txt_'+id).style.fontSize = (increased)? '12pt':'10pt';
	document.getElementById('thing_txt_'+id).style.lineHeight = (increased)? '14.7pt':'12pt';
	document.getElementById('thing_increase_'+id).innerHTML = (increased)? '-':'+';
}

function item_link(n)
{
	linkwin = window.open('link.php?item='+escape(items[n].title),'linkwin','width=500,height=250,scrollbars=0,resizable=0');
}

imgwin = false;

function view(item,id,w,h)
{
	//screen big enough for imagewin?
	if (h>screen.availHeight-40)
	{
		var scale = w/h
		h = screen.availHeight-40;
		w = Math.floor(scale * h);
		scale = true;
	}
	if (w>screen.availWidth)
	{
		var scale = h/w
		w = screen.availWidth;
		h = Math.floor(scale * w);
		scale = true;
	}
	var x = (screen.availWidth - w)/2;
	var y = ((screen.availHeight - h)/2)-15;
	if (x<0) x = 0;
	if (y<0) y = 0;
	if (w<340) w = 340;

	//if img window is still open, do resize	
	if (imgwin && !imgwin.closed) resize='&resize=1';
	else resize = '';

	imgwin = window.open('view.php?item='+item+'&id='+id+'&w='+w+'&h='+h+resize,'imgwin','left='+x+',top='+y+',status=0,scrollbars=0,resizable=1,width='+(w)+',height='+(h+1));
}

function watch(item,id,qt)
{
	w=800;
	h=600;
	var x = (screen.availWidth - w)/2;
	var y = ((screen.availHeight - h)/2)-15;
	if (x<0) x = 0;
	if (y<0) y = 0;
	if (w<340) w = 340;
	watchwin = window.open('watch.php?check=1&item='+item+'&id='+id+'&qt='+qt,'watchwin','left='+x+',top='+y+',status=0,scrollbars=0,resizable=0,width='+(w)+',height='+(h+1));

}

function help()
{
	helpwin = window.open('help.php','helpwin','width=550,height=615,scrollbars=0,resizable=0');
}

function mailto()
{
	top.location = 'mailto:use@usemedia.com';
}

//internal item link
function do_search(searchstr,notquoted)
{
	newslink = true;
	document.forms['findform'].searchinput.value = (notquoted)? searchstr:'"'+searchstr+'"'; //default=quoted;
	document.getElementById('searchinput').style.color = '#000000';
	
	if (maxsize!=.3) find();
	else
	{
		newslink = false;
		deeplink = true;
		do_find();
	}
}

//google query
function do_google_query()
{
	var query = document.getElementById('query').value;
	//var query = '"'+query.replace(/ /g,'" "')+'"'; //quote every word of searchstring (more accurate result)
		
	do_search(query+" ",true); //extra space renders the last word as a 'whole word' (for wordbounderies in actual query)
}





function search_debug()
{
/* 		str = 'unsorted:<br>'; */
/* 		for (var s=0; s<results.length; s++) */
/* 		{ */
/* 			str+= 'index: '+results[s][0] + ' | matches: '+results[s][1]+'<br>'; */
/* 		} */
/* 		results.sort(descending_by_match); */
		for (var s=0; s<results.length; s++) if (results[s][1]==0) break
		var max_match = results[0][1];
		var min_match = results[s-1][1];
		
		tmpstr = 'searchstrings: quoted -&gt;<i>';
		for (i=0; i<quoted.length; i++) tmpstr+= '"'+quoted[i]+'",';
		tmpstr+= ' </i>not quoted -&gt;<i>';
		for (i=0; i<searchstrings.length; i++) tmpstr+= searchstrings[i]+',';

		tmpstr+= '</i><br><br>SORTED:<br>';
		for (var s=0; s<results.length; s++)
		{
			if (results[s][1]==0) break;
			tmpstr+= 'index: '+results[s][0] + ' | matches: '+results[s][1];
			
			if (max_match!=min_match) var color = 180- ((results[s][1]-min_match) * (61/(max_match-min_match)));
			else color = 119;//default_item_color;
			tmpstr+= '&nbsp&nbsp;<span style="background-color:rgb('+parseInt(color)+','+parseInt(color+5)+','+parseInt(color+8)+')">rgb('+parseInt(color)+','+parseInt(color+5)+','+parseInt(color+8)+')</span><br>';

			
		}
	
		document.getElementById('found').innerHTML = tmpstr;
}