/**
 * Common JavaScript used by the RP and CP category find pages.
 * 
 * Expects the following variables:
 * 
 * window.currentChannelId	-	the channel ID
 * window.googleCollection	-	the Google collection key
 * window.quickStartIntro	-	a line of text on the quick start results
 */

if (window.catfind == null) {
	window.catfind = { };
}


/**
 * Shows the secondary content by default, or triggers a search if the "qsQuery" 
 * parameter is set.
 * 
 * The latter is required if a quick start search is initiated from a remote page.
 */
$(document).ready(function() {
	// get qsQuery and qsText parameters
	var match = /[\?&]qsQuery=([^&]+)/.exec(window.location.search);
	if (match != null && match[1] != null) {
		var query = unescape(match[1]);
		
		var text = "";
		match = /[\?&]qsText=([^&]+)/.exec(window.location.search);
		if (match != null && match[1] != null) {
			text = unescape(match[1]);
		}
		
		window.catfind.doQuickStart(query, text);
	}
	else {
		window.catfind.setView("generalContent");
	}
});


/**
 * Sets the visibility of the secondary display content.
 * 
 * There are three possible secondary displays - the general content, the search results
 * and the loading image. The specified one is shown, and the others are hidden.
 * 
 * @param view	-	"generalContent", "searchResults" or "loadingImage"
 */
window.catfind.setView = function (view) {
	if (view == "generalContent") {
		$("#VC-catFindProgress, #VC-catFindResults").css("display", "none");
		$("#VC-catFindSecondary").css("display", "block");
	}
	else if (view == "searchResults") {
		$("#VC-catFindProgress, #VC-catFindSecondary").css("display", "none");
		$("#VC-catFindResults").css("display", "block");
	}
	else if (view == "loadingImage") {
		$("#VC-catFindSecondary, #VC-catFindResults").css("display", "none");
		$("#VC-catFindProgress").css("display", "block");
	}
}


/**
 * Loads the search results screen in the background and places it into the secondary
 * content div.
 *
 * searchQuery	-	the pre-formatted search string, eg &filter1=TopRated
 * text1		-	a line of text to be displayed above the search results
 * text2		-	a second line of text
 */
window.catfind.doSearch = function (searchQuery, text1, text2) {
	// hide existing content and show the progress image
	window.catfind.setView("loadingImage");
	
	var newUrl = window.location.protocol + "//" + window.location.host + 
		"/vgn-ext-templating/mgl/dpm/view/internetProductSearch.jsp?channelId=" +
		window.currentChannelId + (searchQuery == null ? "" : searchQuery) +
		(text1 == null ? "" : "&text1=" + escape(text1)) +
		(text2 == null ? "" : "&text2=" + escape(text2));

	// insert the ajax results into the DOM
	$.get(newUrl, null, function(data) {
		$("#VC-catFindResultsData").html(data);
		$(".productComparison").tablesorter();
		stripeTable();

		// reset visibility
		window.catfind.setView("searchResults");
	});
}


/**
 * Performs a product filter search
 */
window.catfind.doFilter = function (query, description) {
	window.catfind.doSearch(query, description);
}


/**
 * Performs a product quick start search
 */
window.catfind.doQuickStart = function (query, description) {
	window.catfind.doSearch(query, window.quickStartIntro, description);
}


/**
 * Performs a product Google search
 */
window.catfind.doTextSearch = function (text) {
	if (text == null || text == "") return;
	var query = "query=" + escape(text) + "&collection=" + escape(window.googleCollection);
	window.catfind.doTextSearchRaw(query);
}


/**
 * Performs a combined meta + text Google search
 */
window.catfind.doMetaSearch = function (text) {
	if (text == null || text == "") return;
	var query = "query=" + escape(text) + "&collection=" + escape(window.googleCollection) +
		"&meta=" + escape(text);
	window.catfind.doTextSearchRaw(query);
}


/**
 * Performs a Google search in the background and loads the results into the secondary
 * content div.
 * 
 * query	-	the query string
 */
window.catfind.doTextSearchRaw = function (query) {
	
	var url = "/vgn-ext-templating/mgl/dpm/view/internetGoogleSearch.jsp?" + query;

	$.get(url, null, function(data) {
		// insert the ajax results into the DOM
		$("#VC-catFindResultsData").html(data);
		
		// override internal links to callback here
		$("#VC-catFindResultsData a[googleinternal=true]").click(function() {
			var href = $(this).attr("href");
			var query = href.substring(href.indexOf("?") + 1);
			window.catfind.doTextSearchRaw(query);
			return false;
		});

		// reset visibility
		window.catfind.setView("searchResults");
	});
}
