var timeout = null;

function novemberSearch(value)
	{
				
		//if(value) {
		
		clearTimeout(timeout);
		
		timeout = setTimeout(function() {
		
			//$('#srColumnWrapper').load('/search/'+value);
			$.getJSON('/search/index/q:'+value,function(json){
				$('#srColumnWrapper').html('');
				searchLineFormat(json);
			});
			
	//	}
	
		}, 500);
		
		$('#searchResults').css({'left' : '0'});
	}
	
	function searchLineFormat(json)
	{
				
		var firstColHeight = 0;
		var firstRendered = false;
				
		$('#suggestions').hide();
		$('#srColumnWrapper').html('');
		
		if(json.length==0) return false;
		
		$('#srColumnWrapper').append('<br class="break"/><div id="suggestions"></div>');		
		
		$currentLeft = (320*json.length) - 320;
		$categoryName = '';
		$categoryContinued = '';
		$columnCount =0;

		for($i in json)
		{
			$print = 0;

			if(typeof(json[$i])=='object')
			{
		
				$('#suggestions').append('<p id="searchresults'+$i+'" class="searchresults" style="margin-left: 0px;"></p>');
				//$left -= 320;
				if ($i!=0)
				{
					$left = parseInt($('#searchresults'+($i-1)).css('margin-left')) - $currentLeft;
					$currentLeft -= 320;
					$('#searchresults'+($i-1)).css({'margin-left' : $left+'px'});
				}
				$category = json[$i];
				
				$categoryContinued  = ($category[0].category==$categoryName) ? " Cont'd.." : '';

				$categoryName = '';
				$columnLength = $category.length;
				
				//if($currentLeft < -700) continue;
				//$j=0;
				$('#suggestions').show();
				for($j in $category)
				{

					if(typeof($category[$j])=='object')
					{
						//console.debug($category[$j]);
						if($category[$j].padding!=undefined || $category[$j].banner!=undefined)
						{
							if($category[$j].banner!=undefined)
							{
								
								//console.debug('here?');
								
								//console.debug($category[$j].banner);
								$('#searchresults'+$i).append('<a name="banner" href="#" class="banner"><img class="banner" alt="banner" src="'+$category[$j].banner+'" /><br class="break"/></a>');
								// console.debug($category[$j]['bannerLength']);
								
								$print+=$category[$j]['bannerLength'];
								// console.debug($print);
							}
							else
							{
							$height = $category[$j].padding * 16;
							//just check it's not the last of the 3rd column - might need to fill some space
							if($i==2 && $j == ($category.length-1))
							{
								$obj_filler = {
									'print': $print,
									'catLength' : $category.length-2,
									'columnCount' : $columnLength
									};
								// console.debug($columnLength);
								//$missing = Math.floor($print / ($category.length-2));
								$missing = $columnLength - $print ;
								// console.debug($missing);
								for($m=0;$m<$missing;$m++)
								{
									$('#searchresults'+$i).append('<a name="filler">&nbsp;<span class="separator">&nbsp;</span><br class="break"/></a>');
								}
							}
							//console.debug("length = "+$columnCount+" spacer for column"+$i+" - "+$j);
							// $('#searchresults'+$i).append('<span class="category padding" style="height: '+$height+'px;"><a href="/search/advanced/q:'+escape($('#srch_fld').val())+'">More Results <img src="/img/searchMoreArrow.png" alt="&raquo;"/></a><span>');
							}
						}
						else
						{
							
							$print++;
							if($print > $columnCount) $columnCount = $print;
							if($category[$j].category!=$categoryName)
							{	
								$categoryName = $category[$j].category;
								$categoryContinued = '';
								$('#searchresults'+$i).append('<span class="category">'+$categoryName+$categoryContinued+'</span>');
							}
							$image = imageWrapper($category[$j].thumbnail);
							$description = descriptionWrapper($category[$j]['link'],$category[$j].title);
							href = $category[$j]['link'];
							
							$('#searchresults'+$i).append('<a href="'+href+'">'+$image+$description+'<span class="separator">&nbsp;</span><br class="break"/></a>');
													
							$columnCount++;
							
							//just check it's not the last of the 3rd column - might need to fill some space
							if($i==2 && $j == ($category.length-1) && ($category.length-3)!=0)
							{
							
								$missing = Math.floor($print / ($category.length-3));
								//console.debug($print);
								//console.debug(($category.length-3));
								//console.debug($missing);
								/*for($m=0;$m<$missing;$m++)
								{
									$('#searchresults'+$i).append('<a name="filler">&nbsp;<span class="separator">&nbsp;</span><br class="break"/></a>');
								}
								*/
							}
							
						}
						
					}
					else
					{
						$columnLength--;
					}
					
				}
				
				// console.debug($('#searchresults'+$i), 'search results div');
				
				var $__searchResultsDiv = $('#searchresults'+$i);
				var __myHeight = $__searchResultsDiv.height();			
				
				
				
				if($i==0||$i=='0') {
					firstColHeight = __myHeight;
				} else if(__myHeight < firstColHeight) {
					var __difference = firstColHeight - __myHeight;
					$__searchResultsDiv.append('<div class="btmFiller" style="background: #999999; height: ' + __difference.toString() + 'px">&nbsp;</div>');
				}
				else if(__myHeight > firstColHeight && !firstRendered) {
					var __firstDifference = __myHeight - firstColHeight;
					firstRendered = true;
					$('#searchresults0').append('<div class="btmFiller" style="background: #999999; height: ' + __firstDifference.toString() + 'px">&nbsp;</div>');
				}
				
				/*
				console.group($i);
				console.debug(__myHeight, 'height');
				console.debug(firstColHeight, 'first col height');
				console.groupEnd();
				*/
				
				/*if($columnCount!=4)
				{
					//console.debug($columnCount);
					$loop = 4 - $columnCount;
					//console.debug($loop);
					for($k=0;$k < $loop;$k++)
					{
						$('#searchresults'+$i).append('<a name="filler"><span class="searchheading">&nbsp;</span></a>');
					}
				}*/
			}
			
			
			
		}
		$('#suggestions').css({'height': '254px','background-color': '#FFF'});
		$('#suggestions').show();
	}
	
	function imageWrapper(src,alt)
	{
		return '<img src="'+src+'" alt="'+alt+'" width="80" />';
	}

	function descriptionWrapper(href,title)
	{
		return '<span class="searchheading"><strong>'+title+'</strong></span>';
		//<span class="description"></span></span>';
	}