



	var searchPageSize = 4;
var searchThisPage = 1;
var searchData = '';
var searchFirstResult = 1;
var searchTotalResults = 0;
var searchTarget = null;
var queryDefaultValue = null;
var queryText = "";

$(function(){
	//$('#news_Search').attr('action', '/news/search_82576cf-vcd-Ph.html');
	//$('.ajaxSearch').submit(searchAction);
	//$('.ajaxSearch button').click(searchAction);
	
	// add the hidden start form field if it doesn't exist
	$('.ajaxSearch').not('input[name=start]').each(function() {
		$(this).append('<input type="hidden" name="start" value="1" />');
	});
	
	if (!(typeof searchTargetId === "undefined" )) {
		searchTarget = $('#' + searchTargetId);
	}
});

/**
 * Return cookieValue
 * @param formId
 * @return
 */
function getSavedSearch(formId) {
	var cookieValue = $.cookie(COOKIE_NAMES.SEARCH_QUERY);
	if (typeof cookieValue === "undefined" || cookieValue == null) {
		// If there is no previous search, we're done
		return null;
	} else {
		var storedFormId = cookieValue.split(',')[0];
		if (storedFormId == formId) {
			// If there is a previous search on this form, re-use it
			searchThisPage = parseInt(cookieValue.split(',')[2]);
			queryText = cookieValue.split(',')[3];
			return cookieValue;
		} else {
			$.cookie(COOKIE_NAMES.SEARCH_QUERY, null);
			return null;
		}
	}
}

function submitForm(form, searchLinkNoCache, searchLinkCache, isAjax, isFromCookie) {
	var cmid = cmArr['ctxId'];
	var cat = [];
	
	if (!isFromCookie) // If we pressed the button, reset 'this page' to 1
		searchThisPage = 1;
	
	if (isAjax || isFromCookie) {
		form.action = ajaxFormLink; // This is definined in Collection.column_AJAXDYNAMIC.jsp
		searchAction(form, cmid, isFromCookie);
		return false;
	} else { // It's a straightforward form submit
		return true;
	}
}

function searchAction(form, cmid, isFromCookie) {
	var formData = null;
	var cookieValue = null;
	var queryDefaultValue = '';
	
	if (!(typeof form.searchBox === "undefined") && form.searchBox.value.indexOf("--") != -1) {
		// Keep it to restore visually
		queryDefaultValue = form.searchBox.value;
		form.searchBox.value = "";
	}
	
	if (isFromCookie) { // If it is from a cookie, try to retrieve it
		cookieValue = getSavedSearch(cmid);
		if (cookieValue != null) {
			formData = cookieValue.split(',')[1]; // The 2nd part of the csv is the formdata
			form.searchBox.value = cookieValue.split(',')[3]; // The 4th part is the query text to repopulate
		}
	}
	if (formData == null) {
		formData = $(form).map(function(){ return $.makeArray(this.elements); });
	}
	
	$.get(form.action, formData, function(data){
		
		// evaluate scripts in the HTML payload (can't use text() so use DOM on first script)
		eval($(data).parent().find('script')[0].text);
		
		// build list of result items
		searchData = $(data).find('.lining');
		
		// display required result items and related paging
		var firstInPage = getFirstResultIndexFromPage(searchThisPage, searchPageSize) - searchFirstResult;
		updateSearchResults(searchData.slice(firstInPage, firstInPage + searchPageSize));
	});	
	
	// If this came from a cookie, rewrite the same cookie to keep it fresh
	// otherwise write new cookie from form data
	var newCookieValue = cookieValue;
	if (newCookieValue == null) { 
		newCookieValue = cmid + ',' + $(form).serialize() + ',' + searchThisPage + ',' + form.searchBox.value;
	}
	// store search query in a cookie for a couple minutes
	$.cookie(COOKIE_NAMES.SEARCH_QUERY, newCookieValue);  // was { expires: 1 / (24 * 30) }
	
	if (!(typeof form.searchBox === "undefined") && form.searchBox.value == "") // If we're doing a default search, reset it
		form.searchBox.value = queryDefaultValue;
}

function getSearchPage(page) {
	
	// update the form field specifying index of first result and resubmit form
	$('input[name=start]')[0].value = getFirstResultIndexFromPage(page, searchPageSize);
	searchThisPage = page;
	searchAction($('input[name=start]').parents('form')[0],cmArr['ctxId'],false);
}

function showSearchPage(page) {
	var firstPageInData = getPageFromResultIndex(searchFirstResult, searchPageSize);
	var pagesInData = $(searchData).length / searchPageSize;
	
	// if requested page is in current HTML payload, show it
	if (firstPageInData <= page && page <= firstPageInData + (pagesInData - 1)) {
		var firstNewInPage = getFirstResultIndexFromPage(page, searchPageSize) - searchFirstResult;
		searchThisPage = page;
		updateSearchResults(searchData.slice(firstNewInPage, firstNewInPage + searchPageSize));
	}
	
	// otherwise, make ajax call
	else {
		getSearchPage(page);
	}
	return false;
}

function updateSearchResults(resultsHtml) {
	
	if (searchTarget != null) {
		// insert results including boxItUp styles (in the original payload but lost when selecting only searchPageSize items to show
		searchTarget.html('<div class="boxItUp"><div class="bordRight"><div class="bordLeft"><div class="boxLining"></div></div></div><div class="topRight"><div class="topLeft"><!-- --></div></div><div class="bottomRight"><div class="bottomLeft"><!-- --></div></div></div>');
		$('.boxLining', searchTarget).html(resultsHtml);
	
		// if current page is in search results, highlight it
		// store the url-encoded version of the link as a title in the listingUnit div for comparison
		$('.listingUnit').each(function() {
		  this.title = encodeURI($('a', this)[0].href);
		});
		
		$('.id' + cmArr.id, searchTarget).addClass('selectedListingUnit');
		
		var lastInPage = (searchFirstResult + searchPageSize > searchTotalResults) ? searchTotalResults : (searchFirstResult + searchPageSize - 1);
		var totalPages = getPageFromResultIndex(searchTotalResults, searchPageSize);
		
		var paging = '';
		if (totalPages > 1) {
			paging += '<p class="lining itemNumber">';
			paging += searchFirstResult + ' - ' + lastInPage + ' of ' + searchTotalResults + ' items';
			paging += '</p>';
			paging += '<p class="pages">';
			paging += getPagingLink(1, '&lt;&lt;', false);
			paging += getPagingLink(((searchThisPage == 1) ? 1 : (searchThisPage - 1)), '&lt;',false);
			paging += getPagingLink(1, '1', (searchThisPage == 1));
			
			var pagingLoop = '';
			if (totalPages > 2) {
				var pageLoopStart = 2;
				switch (searchThisPage) {
					case 1:
					case 2:
					case 3:
						pageLoopStart = 2;
						break;
					case totalPages:
					case totalPages - 1:
					case totalPages - 2:
						pageLoopStart = totalPages - 3;
						break;
					default:
						pageLoopStart = searchThisPage - 1;
				}
				pageLoopEnd = (totalPages == 3) ? (totalPages - 1) : ((totalPages == 4) ? (totalPages - 2) : pageLoopStart + 2);
				for (var page = pageLoopStart; page <= pageLoopEnd; page++) {
					paging += getPagingLink(page, page, (searchThisPage == page));
				}
			}
			paging += getPagingLink(totalPages, totalPages, (searchThisPage == totalPages));
			paging += getPagingLink(((searchThisPage == totalPages) ? totalPages : (searchThisPage + 1)), '&gt;', false);
			paging += getPagingLink(totalPages, '&gt;&gt;', false);
			paging += '</p>';
			paging += '<div class="clearer"></div>';
		}
		$('#' + searchTargetId).append('<div class="listingInfos listingFooter"><div id="searchPaging" class="listingFooterExtra">' + paging + '<div class="clearer" style=""/></div></div>');
	}
}

function getFirstResultIndexFromPage(page, pageSize) {
	return ((page - 1) * pageSize) + 1;
}

function getPageFromResultIndex(firstResultIndex, pageSize) {
	return Math.ceil(firstResultIndex / pageSize);
}

function getPagingLink(page, text, showActive) {
	return '<a href="#" onclick="return showSearchPage(' + page + ')"' + (showActive ? ' class="active"' : '') + '>' + text + '</a> ';
}

function clearDefaultKeyword() {
	if (($('#searchBox').val()).indexOf("--") != -1)
		$('#searchBox').val('');
}




<!--generated: Tue Nov 03 18:56:55 PST 2009, host: '42', release: v.3.0.14 r.9747 id:111970 -->