/*******************************
 *
 *	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)
	{
		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('detail');
				
				// Overview inc title, location, salary
				var olOverview = new Element('ol');
				
				// Title
				var liJobTitle = new Element('li');
				var aJobTitle = new Element('a');
				aJobTitle.setHTML(arrResults[i].fTitle);
				aJobTitle.setAttribute('href', 'results.php?'+(arrResults[i].fIsJob?'job':'candidate')+'=1&id='+arrResults[i].ID);
				aJobTitle.addEvent('click', function()
											{
												prSearch.addToHistory();
											});
				liJobTitle.adopt(aJobTitle);
				olOverview.adopt(liJobTitle);
				
				// Description (Short)
				var pSummary = new Element('p');
				var strContent = "";
				
				if(!arrResults[i].fIsJob && arrResults[i].fSummary != "")
				{
					strContent = arrResults[i].fSummary;
				}
				if(strContent == "")
				{
					strContent = arrResults[i].fShortDescription + "...";	
				}
				pSummary.setHTML(strContent.replace("&amp;lsquo;", "&lsquo;").replace("&amp;rsquo;", "&rsquo;").replace("&amp;pound;", "&pound;"));
					
				// Tags
				var divTags	= new Element('div');
				divTags.addClass('base');
				
				var ulTags = new Element('ul');
				ulTags.addClass('floatleft');
				
				// Location
				if(arrResults[i].fIsJob && arrResults[i].fLocation)
				{
						var liLocation = new Element('li');
						liLocation.setHTML("Based in " + arrResults[i].fLocation);
						olOverview.adopt(liLocation);
				}
				else if(!arrResults[i].fIsJob && arrResults[i].fArea)
				{
						var liLocation = new Element('li');
						liLocation.setHTML("Based in " + arrResults[i].fArea);
						olOverview.adopt(liLocation);
				}
				
				// if the job / candidate is hot
				if(arrResults[i].fHot)
				{
					var liHot = new Element('li');
					liHot.addClass('hot');
					liHot.setAttribute('title', 'This '+(arrResults[i].fIsJob?'job':'candidate')+' is hot!');
					liHot.setHTML('This '+(arrResults[i].fIsJob?'job':'candidate')+' is hot!');
					ulTags.adopt(liHot);
				}
				
				if(arrResults[i].fPerm)
				{
					var liPerm = new Element('li');
					liPerm.addClass('perm');
					liPerm.setAttribute('title', (arrResults[i].fIsJob?'This vacancy is for permanent employment':'This candidate is seeking permanent employment'));
					liPerm.setHTML((arrResults[i].fIsJob?'This vacancy is for permanent employment':'This candidate is seeking permanent employment'));
					ulTags.adopt(liPerm);
				}
				
				if(arrResults[i].fTemp)
				{
					var liTemp = new Element('li');
					liTemp.addClass('temp');
					liTemp.setAttribute('title', (arrResults[i].fIsJob?'This vacancy is for temporary employment':'This candidate is seeking temporary employment'));
					liTemp.setHTML((arrResults[i].fIsJob?'This vacancy is for temporary employment':'This candidate is seeking temporary employment'));
					ulTags.adopt(liTemp);
				}
					
					
					
				if(arrResults[i].fIsJob)
				{
					// Salary / Rate
					var liSalaryRate = new Element('li');
					
					var strSalaryMin = arrResults[i].fMinSalary.toString();
					var strSalaryMax = arrResults[i].fMaxSalary.toString();
					
					if((strSalaryMin.indexOf(".") > -1) && ((strSalaryMin.length - strSalaryMin.indexOf(".")) < 3))
					{
						strSalaryMin += "0";	
					}
					if((strSalaryMax.indexOf(".") > -1) && ((strSalaryMax.length - strSalaryMax.indexOf(".")) < 3))
					{
						strSalaryMax += "0";	
					}
					
					liSalaryRate.setHTML("Salary/Rate &pound;"+strSalaryMin+(strSalaryMin != strSalaryMax ? " &ndash; &pound;"+strSalaryMax : ""));
					olOverview.adopt(liSalaryRate);
					
					var dtNow 	= new Date();
					var m7Days	= 604800000;
					var m30Days = 2592000000;
					
					if(arrResults[i].fDateAdded > (dtNow - m7Days)/1000)
					{
						var liWeek = new Element('li');
						liWeek.addClass('new_w');
						liWeek.setAttribute('title', 'This job new this week');
						liWeek.setHTML('This job new this week');
						ulTags.adopt(liWeek);
					}
					
					/*if(arrResults[i].fDateAdded > (dtNow - m30Days)/1000)
					{
						var liMonth = new Element('li');
						liMonth.addClass('new_m');
						liMonth.setAttribute('title', 'This job new this month');
						liMonth.setHTML('This job new this month');
						ulTags.adopt(liMonth);
					}*/
					
					if(arrResults[i].fSynergy)
					{
						var liSynergy = new Element('li');
						liSynergy.addClass('synergy');
						liSynergy.setAttribute('title', 'This job is from a Synergy Status Client');
						liSynergy.setHTML('This job is from a Synergy Status Client');
						ulTags.adopt(liSynergy);
					}
					
					
				}
				else
				{
					if(arrResults[i].fGrad)
					{
						var liGrad = new Element('li');
						liGrad.addClass('grad');
						liGrad.setAttribute('title', 'This candidate is a graduate');
						liGrad.setHTML('This candidate is a graduate');
						ulTags.adopt(liGrad);
					}
				}
				
				var aView = new Element('a');
				aView.setHTML('View');
				aView.setAttribute('title', 'See more information about this '+(arrResults[i].fIsJob?'job':'candidate'));
				aView.setAttribute('href', 'results.php?'+(arrResults[i].fIsJob?'job':'candidate')+'=1&id='+arrResults[i].ID);
				aView.addClass('floatright');
				aView.addEvent('click', function()
										{
											prSearch.addToHistory();
										});
				
				var divClear = new Element('div');
				divClear.addClass('clear');
				
				if(ulTags.childNodes.length > 0)
					divTags.adopt(ulTags);
				divTags.adopt(aView);
				divTags.adopt(divClear);
				
				// compile everything
				divDetail.adopt(olOverview);
				divDetail.adopt(pSummary);
				divDetail.adopt(divTags);
				
				$('results').adopt(divDetail);
				
				iRowCount++;
				
				if(iRowCount%3 == 0)
				{
					var divRowClear = new Element('div');
					divRowClear.addClass('clear');
					$('results').adopt(divRowClear);
				}
			}
		}
				
		// 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;
		
		// clear out the pagination list
		if($('pagination'))
			$('pagination').empty();
		
		if(iNumPages <= 1 && $('pagination'))
			$('pagination').addClass('hidden');
		else if($('pagination'))
		{
			$('pagination').removeClass('hidden');
		
			// create a new set of links
			var liPreviousPage 	= new Element('li');
			
			if(iCurrentPage > 0)
			{
				var aPreviousPage	= new Element('a');
				aPreviousPage.setAttribute('title', 'Previous Page ('+(iCurrentPage-1)+')');
				aPreviousPage.setAttribute('href', 'javascript:void(0)');
				aPreviousPage.addEvent('click', function() { prSearch.getResults(iCurrentPage-1); });
				aPreviousPage.setHTML('Previous page');
				liPreviousPage.adopt(aPreviousPage);
			}
			
			liPreviousPage.addClass('page_prev');
			
			// add it into the list
			$('pagination').adopt(liPreviousPage);
			
			var iMinWin = Math.ceil(Math.max(0, iCurrentPage - 5));
			var iMaxWin = Math.ceil(Math.min(iNumPages, iMinWin + iWindowSize));
			
			// if we're at the end
			if(iMaxWin - iMinWin < iWindowSize)
				iMinWin = Math.max(0, iMaxWin - iWindowSize);
				
			for(var i = iMinWin; i < iMaxWin; i++)
			{
				// if this is our current page
				// simply add the page number
				// and title, otherwise add
				// an anchor
				var strPageNum	= (i+1);
				var liPage 		= new Element('li');
				if(i == iCurrentPage)
				{
					liPage.setAttribute('title', 'Page '+ strPageNum);
					liPage.setHTML(strPageNum);
				}
				else
				{
					var aPage = new Element('a');
					aPage.setAttribute('title', 'Page '+ strPageNum);
					aPage.setAttribute('href', 'javascript:void(0)');
					aPage.iPage	 = i;
					aPage.addEvent('click', function()
											  {
												  prSearch.getResults(this.iPage);
												  return false;
											  });
					aPage.setHTML(strPageNum);
					liPage.adopt(aPage);
				}
				
				// add it into the list
				$('pagination').adopt(liPage);
			}
			
			// create a new set of links
			var liNextPage 	= new Element('li');
			if(iCurrentPage < iNumPages - 1)
			{
				var aNextPage	= new Element('a');
				aNextPage.setAttribute('title', 'Next Page ('+(iCurrentPage+1)+')');
				aNextPage.setAttribute('href', 'javascript:void(0)');
				aNextPage.addEvent('click', function()
												  {
													  prSearch.getResults(iCurrentPage+1);
													  return false;
												  });
				aNextPage.setHTML('Next page');
				liNextPage.adopt(aNextPage);
			}
			
			// add the page next class
			liNextPage.addClass('page_next');
			
			// add it into the list
			$('pagination').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 '+(iMode == 0 ?'jobs':'candidates')+' matching your search term');
		$('results').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()
												{
													$('search_ajax_field').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)
	{
		$('search_ajax_field').value = strSearchValue;
	},
	
	// clear everything out
	clearResults: function()
	{
		$('results').empty();
	}
});