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


//mediaguild search

Layout.prototype.addSearch = function()
{
	var obj = this;

	this.search = 
	{
		items: obj.items.item,

		init: function()
		{
			var obj = this;

			this.inputfield = document.getElementById('searchinput');
			this.inputfield.onkeydown = function(e) { obj.keyDown(e) };
			this.closebutton = document.getElementById('searchclose');
			this.results = document.getElementById('search_results');
			
			this.inputfield.value = 'find';
			
			//fix png
			if (browser.cssfilter) this.closebutton.style.filter = pngBgImage('button_close_disabled').substr(7);
	
			var over = function(show)
			{
				if (obj.closebutton.src.indexOf('disabled')!=-1) return;
				var src = (show)? 'button_closeX':'button_close';
				if (browser.cssfilter) obj.closebutton.style.filter = pngBgImage(src).substr(7);
				else obj.closebutton.src = 'media/'+src+'.png';
			}
			this.closebutton.onmouseover = function() { over(true) }
			this.closebutton.onmouseout = function() { over(false) }
			this.closebutton.onclick = function() { obj.close() }
			
			//enable search input
			document.getElementById('search').style.display = 'block';
		},

		keyDown: function(e)
		{
			if (!e) e = event;
			if (e.keyCode>=37 && e.keyCode<=40) return e.returnValue; //ignore arrow-keys
			else
			{
				var obj = this;
				window.setTimeout(function() { obj.find() },10);
				return e.returnValue;
			}
		},
		
		input: function(type)
		{
			if (type=='first')
			{
				var searchstr = this.inputfield.value;
			
				if (searchstr=='find')
				{
					//clear form & change font to black
					this.inputfield.value = '';
					this.inputfield.style.color = 'black';
				}
				else if (searchstr.length>1)
				{
					this.showResults(1);
				}
			}
			else
			{
				//adds space when enter is pressed, so last word is searched with wordbounderies
				this.inputfield.value = this.inputfield.value+' ';
				return false;
			}
		},
		
		enableClose: function(show)
		{
			var src = (show)? 'button_close':'button_close_disabled';

			if (browser.cssfilter)
			{
				this.closebutton.src = (show)? 'media/blank.gif':'media/button_close_disabled.gif';
				this.closebutton.style.filter = pngBgImage(src).substr(7);
			}
			else this.closebutton.src = 'media/'+src+'.png';
		},
				
		find: function()
		{
			var searchstr = this.inputfield.value;
			var searchmode;
		
			//close search?
			if (searchstr=='find' || searchstr=='' || searchstr.length==1)
			{
				this.enableClose(searchstr.length==1);
				//reset search result
				this.found(0,true);
				return;
			}
			else
			{
				this.enableClose(true);
			}
			
			//quoted search?
			var quote = new RegExp('\"','g');
			var quotes = searchstr.match(quote);
			var 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)
			{
				var 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(' ');
			
				var results = new Array();
					
				var s = 0;
				for (var id in this.items)
				{
					results[s] = { id:id, match:0 };
					
					//find matches
					
					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].match==0) continue quoted_matchloop;
						
						var regexp = new RegExp('\\b'+quoted[i]+'\\b','gi'); //with word bounderies
						matches = this.items[id].searchdata.match(regexp);
						if (matches) results[s].match += matches.length;
						else if (searchmode=='AND' && i>0) results[s].match = 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 = this.items[id].searchdata.match(regexp);
						if (matches) results[s].match += matches.length;
					}
					this.items[id].found = (results[s].match!=0)? true:false;
					
					s++;
				}
				
				//sort
				results.sort( function(a,b) { return b.match-a.match } );
	
				this.found(results);
			}
		},
		
		close: function()
		{
			//reset input
			this.inputfield.style.color = 'rgb(150,150,150)';
			this.inputfield.value = 'find';
			//reset close button
			this.enableClose(false);
			//hide search results
			this.found(0,true);			
		},
		
		showResults: function(show)
		{
			this.results.style.display = (show)? 'block':'none';
		},
		
		found: function(results,rset)
		{
			if (rset)
			{
				this.results.firstChild.innerHTML = '';
				this.results.lastChild.innerHTML = '';				
				this.showResults(0);
				return;
			}

			//best and least match count (for colorize)
			var max_match = results[0].match;
			for (var s=0; s<results.length; s++) if (results[s].match==0) break;
			var min_match = results[s].match;
			
			//display found items
			var str = '';
			for (var s=0; s<results.length; s++)
			{
				//stop if no match
				if (results[s].match==0) break;
				
				//colorize
				if (max_match!=min_match) var c = Math.round(120 + (results[s].match * ((255-120)/(max_match-min_match))));
				else var c = 255;
				var color = 'rgb('+c+','+c+','+c+')';
	
				var id = results[s].id;
				var section = this.items[id].section;
				
				var bullit = '<span style="color:'+obj.sections[section].color+'">&bull;</span> ';
				var title = (this.items[id].title.length>20)? this.items[id].title.substring(0,20)+'..':this.items[id].title;

				str+= '<a href="javascript://" onclick="mg.expand('+id+');mg.search.showResults(0)" style="color:'+color+'" onmouseover="this.style.color=\'rgb(0,177,242)\'" onmouseout="this.style.color=\''+color+'\'">' + bullit + title +'</a>'; // ('+results[s].match+', c='+c+')</a>';
			}
			
			//calculate height for search results pane
			var h = s*21 - ((s-1)*4);
			if (h>174) h = 174;
			
			this.results.style.height = (h+21+1) +'px';
			this.results.lastChild.style.height = h+1 +'px';
	
			var items = (s==1)? ' item':' items';
			var found = (obj.lan=='en')? ' found':' gevonden';
			this.results.firstChild.innerHTML = s+items+found;
			this.results.lastChild.innerHTML = str;				

			//show
			this.showResults(1);
		}
	}
	
	this.search.init();
}