/*******************************
 *
 *	Output Class
 *
 *  Version: 1.0
 *
 *	Author: 
 *	The Roundhouse
 *
 *  © The Roundhouse 2007 -
 * 	ALL RIGHTS RESERVED
 */
 
 /* Creates output like this:
 
   <div class="detail">
		<ol>
			<li>Snr Architectural Technican</li>
			<li>Based in Birmingham</li>
			<li>Salary/Rate £30,000 &ndash; £35,000</li>
		</ol>
		<p>An award-winning, multi-disciplinary consultancy that likes to be different in it's chosen sectors...</p>
		
		<div class="base">
			<ul class="floatleft">
				<li class="hot" title="Hot!">This job is hot!</li>
				<li class="new_w" title="New this week">This job new this Week</li>
				<li class="new_m" title="New this month">This job new this Month</li>
				<li class="synergy" title="Synergy Status Client">This job is from a Synergy Status Client</li>
			</ul>
			<a href="#" class="floatright">View</a>
			<div class="clear"></div>
		</div>
	</div>
  */

var Output = new Class({
					   
 	// implementation-specific function to
	// output the search results to the browser
	outputResults: function(arrResults, iStart, iEnd, iResultSize, iPageSize)
	{
		var lang = "gb";
		var site = $('site');
		if(site)
		{
			if(site.hasClass("fr"))
				lang = "fr";
		}
		
		this.clearResults();
		var iRowCount = 0;
		
		for(var i = iStart; i < iEnd; i++)
		{
			if(arrResults[i])
			{
				// create a detail div
				var divDetail = new Element('div');
				divDetail.addClass('productsearchresult');
							
				// Description (Short)
				var aView = new Element('a');
				var strContent = arrResults[i].Name;
				aView.setHTML(strContent);
				aView.setAttribute('title', 'See more information about this product.');
				aView.setAttribute('href', 'product.php?'+'id='+arrResults[i].ID);
				aView.addEvent('click', function()
										{
											prSearch.addToHistory();
										});
				
				var divTop = new Element('div');
				divTop.addClass('panel_top_searchresult');
				
				var divContent = new Element('div');
				divContent.addClass('productsearchresult_content');

				var divLeft = new Element('div');
				divLeft.addClass('product_name floatleft');

				var spanName = new Element('span');
				
				var divCode = new Element('div');
				divCode.addClass('product_code floatleft');
				
				var spanCode = new Element('span');
				spanCode.setHTML(arrResults[i].ProductCode);
				
				var divMore = new Element('div');
				divMore.addClass('product_more floatleft');
				
				var spanMore = new Element('span');
				
				var aMore = new Element('a');
				aMore.setProperty('href','product.php?'+'id='+arrResults[i].ID);
				aMore.setAttribute('title', 'See more information about this product.');
				
				var vwDetails = "View Details"
				
				if(lang == "fr")
					vwDetails = "plus d’infos";
				
				aMore.setHTML(vwDetails);
				
				var divClear = new Element('div');
				divClear.addClass('clear');
				
				var divButton = new Element('div');
				divButton.addClass('btn_more save_icon');
				
				var aButton = new Element('a');
				aButton.setProperty('href','?addproduct='+arrResults[i].MasterProduct);
				aButton.setProperty('title','Save this product');
				aButton.setHTML('Save this product');
				
				spanName.adopt(aView);
				divLeft.adopt(spanName);
				divContent.adopt(divLeft);
				
				divCode.adopt(spanCode);
				divContent.adopt(divCode);
				
				spanMore.adopt(aMore);
				divMore.adopt(spanMore);
				divContent.adopt(divMore);
				
				divButton.adopt(aButton);
				divContent.adopt(divButton);
				
				divTop.adopt(divContent);							
				divTop.adopt(divClear);
				// compile everything
				divDetail.adopt(divTop);
				
				
				if($('productsearchresults_container'))
					$('productsearchresults_container').adopt(divDetail);
			}
		}
				
		// now do paging
		this.outputPages(arrResults, iStart, iPageSize, iResultSize);
	},
	
	// pagination
	outputPages: function(arrResults, iStart, iPageSize, iResultSize)
	{
		// work out which page we are looking
		// at and how many pages there are in total
		var iCurrentPage 	= Math.floor(iStart / iPageSize);
		var iNumPages 		= Math.ceil(iResultSize / iPageSize);
		var iWindowSize		= 10;
		var pageLinks		= false;
		
		var arrLinks = $$('.pagelinks_container');
		if(arrLinks.length == 1)
			pageLinks = arrLinks[0];
		
		// clear out the pagination list
		if(pageLinks)
			pageLinks.empty();
		
		if(iNumPages <= 1 && pageLinks)
			pageLinks.addClass('hidden');
		else if(pageLinks)
		{
			pageLinks.removeClass('hidden');
		
			// create a new set of links
			var ulPageLinks = new Element('ul');
			var liPreviousPage 	= new Element('li');
			
			if(iCurrentPage > 0)
			{
				var aPreviousPage	= new Element('a');
				aPreviousPage.setAttribute('title', 'Go to the previous page');
				aPreviousPage.setAttribute('href', 'javascript:void(0)');
				aPreviousPage.addEvent('click', function() { prSearch.getResults(iCurrentPage-1); });
				aPreviousPageClone = aPreviousPage.clone();
				aPreviousPageClone.setHTML('Previous');
				aPreviousPageClone.addEvent('click', function() { prSearch.getResults(iCurrentPage-1); });
				
				var divPrevious = new Element('div');
				divPrevious.addClass('btn_img previous');
				
				var prevButton = new Element('img');
				prevButton.setProperty('src','/z_images/btns/btn_previous.gif');
				
				divPrevious.adopt(aPreviousPage);
				liPreviousPage.adopt(divPrevious);
				liPreviousPage.adopt(aPreviousPageClone);
				aPreviousPage.adopt(prevButton);
				liPreviousPage.addClass('first-child');
			}
			
			liPreviousPage.addClass('page_prev');
			
			// add it into the list
			pageLinks.adopt(ulPageLinks);
			
			var liPages = new Element('li');
			liPages.setHTML((iCurrentPage+1)+" of "+iNumPages);
			
			// create a new set of links
			var liNextPage 	= new Element('li');
			if(iCurrentPage < iNumPages - 1)
			{
				var aNextPage	= new Element('a');
				aNextPage.setAttribute('title', 'Next');
				aNextPage.setAttribute('href', 'javascript:void(0)');
				aNextPage.addEvent('click', function()
												  {															  
													  prSearch.getResults(iCurrentPage+1);
													  return false;
												  });
				liNextPage.adopt(aNextPage);
				
				var aNextPageClone = aNextPage.clone();
				aNextPageClone.addEvent('click', function()
								  {										  
									  prSearch.getResults(iCurrentPage+1);
									  return false;
								  });
								  
				aNextPage.setHTML('Next');
				aNextPage.addClass('floatleft');
				
				var divNext = new Element('div');
				divNext.addClass('btn_img next');
				
				var nextButton = new Element('img');
				nextButton.setProperty('src','/z_images/btns/btn_next.gif');
				
				divNext.adopt(aNextPageClone);
				liNextPage.adopt(aNextPage);
				liNextPage.adopt(divNext);
				aNextPageClone.adopt(nextButton);
				liNextPage.addClass('last-child');
			}
			
			// add the page next class
			liNextPage.addClass('page_next');
			
			// add it into the list			
			ulPageLinks.adopt(liPreviousPage);
			ulPageLinks.adopt(liPages);	
			ulPageLinks.adopt(liNextPage);					
		}
	},
	
	setMode: function(iMode, strSearchTerm)
	{
		if(iMode == 0)
		{
			//$('mode_iam').setStyle('backgroundPositionY', '-25px');
			if($('mode_iam'))
				$('mode_iam').setStyle('backgroundPosition', '0 0');
			
			if($('job_filters'))
				$('job_filters').setStyle('display', 'block');
			
			if($('candidate_filters'))
				$('candidate_filters').setStyle('display', 'none');
			
			if($('jobseeker'))
				$('jobseeker').addClass('active');
			
			if($('employer'))
				$('employer').removeClass('active');
			
			if(!strSearchTerm || strSearchTerm == "")
				this.isLatest(iMode);
			else
				this.isSearch();
		}
		else
		{
			if($('mode_iam'))
				$('mode_iam').setStyle('backgroundPosition', '0 18px');
			
			if($('job_filters'))
				$('job_filters').setStyle('display', 'none');
			
			if($('candidate_filters'))
				$('candidate_filters').setStyle('display', 'block');
			
			if($('jobseeker'))
				$('jobseeker').removeClass('active');
			
			if($('employer'))
				$('employer').addClass('active');
			
			if(!strSearchTerm || strSearchTerm == "")
				this.isLatest(iMode);
			else
				this.isSearch();
		}
	},
	
	isLatest: function(iMode)
	{
		if(iMode == 0)
		{
			if($('live_search_title'))
			{
				$('live_search_title').addClass('ourlatestjobs');
				$('live_search_title').removeClass('ourlatestcandidates');
			}
		}
		else
		{
			if($('live_search_title'))
			{
				$('live_search_title').removeClass('ourlatestjobs');
				$('live_search_title').addClass('ourlatestcandidates');
			}
		}
	},
	
	isSearch: function()
	{
		if($('live_search_title'))
		{
			$('live_search_title').removeClass('ourlatestjobs');
			$('live_search_title').removeClass('ourlatestcandidates');
		}
	},
	
	noResults: function(iMode)
	{
		this.clearResults();
		var pNoResults = new Element('p');
		pNoResults.addClass('resultstatus');
		pNoResults.setHTML('Sorry, we couldn\'t find any products matching your search term');
		$('productsearchresults_container').adopt(pNoResults);
	},
	
	// chuck out the history
	// and start again
	outputHistory: function(arrHistory)
	{
		// start by clearing out the history
		if($('search_history'))
			$('search_history').empty();
		
		// set true if we have any history
		var bHistory = false;
		
		// now add in each history item
		for(var i in arrHistory)
		{
			if(arrHistory[i].bHist)
			{
				/*
				<div class="floatleft">
				   <div class="tl">
				   <div class="bl">
				   <div class="tr">
				   <div class="br">
				    ....
				   </div>
				   </div>
				   </div>
				   </div>
				</div>
				*/
				
				// we have something in the history
				bHistory 			= true;
				
				var liHistEntry 	= new Element('li');
				
				var divFloatLeft	= new Element('div');
				var divTopLeft		= new Element('div');
				var divBottomLeft	= new Element('div');
				var divTopRight		= new Element('div');
				var divBottomRight	= new Element('div');
				var ulHistEntry 	= new Element('ul');
				var liStrEntry 		= new Element('li');
				var liAncRemove		= new Element('li');
				var aStrEntry		= new Element('a');
				var aAncRemove		= new Element('a');
				
				divFloatLeft.adopt(divTopLeft);
				divTopLeft.adopt(divBottomLeft);
				divBottomLeft.adopt(divTopRight);
				divTopRight.adopt(divBottomRight);
				
				divFloatLeft.addClass('floatleft');
				divTopLeft.addClass('tl');
				divBottomLeft.addClass('bl');
				divTopRight.addClass('tr');
				divBottomRight.addClass('br');
				
				liAncRemove.addClass('remove');
				
				aStrEntry.setHTML(arrHistory[i].strSearchTerm);
				aAncRemove.setHTML('Remove');
				
				aStrEntry.setAttribute('title', 'Search for \''+arrHistory[i].strSearchTerm+'\'');
				aAncRemove.setAttribute('title', 'Remove \''+arrHistory[i].strSearchTerm+'\' from your search history');
				
				aStrEntry.setAttribute('href', 'javascript:void(0)');
				aAncRemove.setAttribute('href', 'javascript:void(0)');
				
				aStrEntry.strHistoryVal 		= arrHistory[i].strSearchTerm;
				aAncRemove.strHistoryVal 		= arrHistory[i].strSearchTerm;
				
				aStrEntry.addEvent('click', 	function()
												{
													$('productsearchfield').value = this.strHistoryVal;
													prSearch.doSearch(this.strHistoryVal, true);
													return false;
												});
				
				aAncRemove.addEvent('click', 	function()
												{
													prSearch.removeFromHistory(this.strHistoryVal);
													return false;
												});
												
				
				liStrEntry.adopt(aStrEntry);
				liAncRemove.adopt(aAncRemove);
				ulHistEntry.adopt(liStrEntry);
				ulHistEntry.adopt(liAncRemove);
				divBottomRight.adopt(ulHistEntry)
				liHistEntry.adopt(divFloatLeft);
				
				if($('search_history'))
					$('search_history').adopt(liHistEntry);
			}
		}
		
		if(!bHistory && $('history_search'))
			$('history_search').addClass('hidden');
		else if($('history_search'))
			$('history_search').removeClass('hidden');
	},
	
	setSearchBox: function(strSearchValue)
	{
		if($('productsearchfield'))
			$('productsearchfield').value = strSearchValue;
	},
	
	// clear everything out
	clearResults: function()
	{
		if($('productsearchresults_container'))
			$('productsearchresults_container').empty();
	}
});