// copyright 1999 Idocs, Inc. http://www.idocs.com
// Distribute this script freely but keep this notice in place
// Restrict input field to alhpanumeric only
function letternumber(e)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);
keychar = keychar.toLowerCase();
// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )
   return true;

// alphas and numbers
if ((("abcdefghijklmnopqrstuvwxyz0123456789 ").indexOf(keychar) > -1))
   return true;
else
   return false;
}

function clickSearchButton() {
	
	// Get the value of the search terms entered by user
	var searchString = $("#searchTermsDisplay").val();
	//alert(searchString);
	// Ensure the search bar does not accept a blank submission
	if ((searchString != "") && (searchString != " ")) {
		// Replace undesirable words
		var wordsToReplace = ['to', 'of', 'for', 'is', 'at', 'it', 'in', 'on', 'the', 'and', 'or', 'not', 'a', 'if', 'what', 'where', 'when', 'why', 'how', 'with', ' '];
		searchString = searchString.replace(new RegExp('\\b(' + wordsToReplace.join('|') + ')\\b', 'g'), ' ');
		// Replace extra spaces with single spaces
		searchString = searchString.replace(/\s{2,}/g, ' ');
		//alert(searchString)
		$("#searchTerms").val(""+searchString+"");
		document.postSearchTerms.submit();
		return false;
	}
}


$(document).ready(function() {	


if ((window.location == "http://www.perspectivescorporation.com/") || (window.location == "http://www.perspectivescorporation.com/index.asp")) {				
	// SLIDESHOW	
	$('#slides').slides({
		start: 1,
		//preload: true,
		preloadImage: 'images/slideshow/loading.gif',
		play: 4000,
		pause: 4000,
		hoverPause: true,
		generatePagination: true,
		paginationClass: 'pagination',
		animationStart: function(current){
			$('.caption').animate({
				bottom:-55
			},100);
			if (window.console && console.log) {
				// example return of current slide number
				console.log('animationStart on slide: ', current);
			};
		},
		animationComplete: function(current){
			$('.caption').animate({
				bottom:0
			},200);
			if (window.console && console.log) {
				// example return of current slide number
				console.log('animationComplete on slide: ', current);
			};
		},
		slidesLoaded: function() {
			$('.caption').animate({
				bottom:0
			},200);
		}
	});
}

	// SEARCH BAR

	$("#searchTermsDisplay").focus(function() {
		$(this).css("color","#000000");
	});
		
	$.ajax({
		type: "GET",
		url: "sitemap.xml",
		dataType: "xml",				
		success: function(xml) {
			$("#siteMap").val("");
			var howManyUrlsTotal = $(xml).find('url').length;			
			var howManyUrlsProcessed = 0;			
			$(xml).find('url').each(function(){
				var location = $(this).children('loc').text();
				location = location.replace(/https/gi, 'http');
				howManyUrlsProcessed = howManyUrlsProcessed + 1;
				// Populate #siteMap with a list of urls and the filename minus the extension to create an ID for the page
				if (howManyUrlsProcessed == howManyUrlsTotal) {
					$("#siteMap").val($("#siteMap").val() + location + "");
				}
				else {
					$("#siteMap").val($("#siteMap").val() + location + ",");
				}				
			});						
		},
		complete: function() {			
		}
	});		
	
	// SEARCH RESULTS
	
	$("#searchResultUrls div").each( function() {
		// Get the URL from the #siteMap container
		var pageLocation = $(this).text();
		// If the word "none" appears as a searchResultUrl (seeded from the searchProcess.asp)
		if (pageLocation == "none") {
			$("#searchResultDescriptions").append("<div class='sectionTitle templateBlue'>no results found</div>");
		}
		else {
			// Load the keywords meta tag from the URL
			$('#searchResultDescriptionHolder').load(''+pageLocation+' meta[name="description"], meta[name="notes"]', function() {	
				var metaDescription = $("#searchResultDescriptionHolder meta[name='description']").attr("content");
				var metaTitle = $("#searchResultDescriptionHolder meta[name='notes']").attr("content");		
				// Parse the pageLocation and get the foldername of the page included in results
				var segements = pageLocation.split("/");
				var searchTermCategory = segements[segements.length - 2];		
				// Add the results to #searchResultsDescriptions, append to the appropriate searchTermCategory
				$('#searchResultDescriptions #'+searchTermCategory+'Results').append("<a class='searchResultLink' href='"+pageLocation+"' title='"+metaTitle+"' target='_blank'><div class='metaTitle templateBlue'>"+metaTitle+"&nbsp;<span class='orangeRaquo'>&raquo;</span></div><div class='pageLocation'>"+pageLocation+"</div><div class='metaDescription'>"+metaDescription+"</div></a>")				
				// Set the category div to display:block
				$("#"+searchTermCategory+"Results").css("display","block");
				// Clear the #searchResultsDescriptionHolder
				$("#searchResultDescriptionHolder").html("");				
			});	
		}
	});

	$(".searchResultLink").livequery( function() {
		$(this).hover(
			function () {
				$(this).children(".pageLocation").addClass("pageLocationHover");					
			}, 
			function () {
				$(this).children(".pageLocation").removeClass("pageLocationHover");
			});
	});	
	
});	
