/*	- created: 2007-07-18 - scarrillo
	- updated: 2007-11-06 - scarrillo - getUrlPath() - getContextPath()
*/

function trim(str) {
	return str.replace(/^\s+|\s+$/g, '');
}

function getUrlPath(url) {
	var urlInfo = url.replace('//', '/').split('/');
	var domain = urlInfo.splice(0, 2).join("/");
	var docInfo = urlInfo.join("/");
	//alert("getUrlPath: "+domain+" | "+docInfo);
	return docInfo;
}

function getContextPath(url, context, removeExtension, removeTrailingIndex) {
	var path = getUrlPath(url);
	
	// remove the file extension if a page is specified: file.jhtml
	if(removeExtension && path.lastIndexOf(".") >= 0) {
		path = path.substring(0, path.lastIndexOf("."));
	}
	
	// remove assigned context from url. context = "games" > url = /games/spongebob/game.jhtml > spongebob/game
	context = context == null || context == "" ? "" : context;
	var contextMatch = new RegExp("^"+context+"\/", "i")
	path = path.replace(contextMatch, "");;

	// remove trailing "/index" - to combine - "/some/url/index" & "/some/url/"
	if(removeTrailingIndex) path = path.replace(/\/index$/i, "");

	// remove trailing forward slash
	path = path.replace(/\/$/, "");
	
	if(path.indexOf("/") >= 0){
		path = path.replace(/\/$/, "");
	}
		
	if(path == ""){
		path = "hub";
	}
	//alert("getContextType: "+path+" | "+url);
	path = context + "_" + path;
	return path;
} 


function doUrl(url) {
	if(!url) return false;

	if(g_enableBumpers && g_bumperUrl) {
		url = g_bumperUrl + escape(url);
	}
	//window.location.href = url;	
	window.open(url);
	return false;
}

function doLinkSpotUrl(url) {
	if(!url) return false;
	window.location.href = url;	
	return false;
}

function trimArray(arr, toLower) {
	if(arr == null) return null;

	for(var i = 0; i < arr.length; i++) {
		arr[i] = toLower ? arr[i].toLowerCase() : arr[i];
		arr[i] = trim(arr[i]);
	}
	return arr;
}

function getFilteredKeywords(keywords, filteredKeywords) {
	if(keywords == null) return "";
	if(filteredKeywords == null) filteredKeywords = new Array();

	trimArray(keywords, true);
	trimArray(filteredKeywords, true);

	var found = false;
	var keywordsBuffer = "";
	for(var i = 0; i < keywords.length; i++) {
		for(var j = 0; j < filteredKeywords.length; j++) {
			//document.writeln("<br/>Keyword Compare: "+filteredKeywords[j]+" | "+keywords[i]+"<br />");
			if(keywords[i] != "" && keywords[i] == filteredKeywords[j]) {
				//document.writeln("<br/>Keyword Filtered: "+filteredKeywords[j]+"<br />");
				found = true;
			}
		}
		if(!found) keywordsBuffer += ", "+keywords[i];
		found = false;
	}
	return keywordsBuffer;
}

// Second argument is an array. String will be changed into an array of characters.
function getFilteredLinks(linkData, keywords) {
	if(linkData == null || !linkData.getItems) return null;
	if(keywords == null) keywords = new Array();

	trimArray(keywords, true);
	var links = linkData.getItems();

	var found = false;
	var filteredLinks = new Array();
	var savedLinks = new Array();
	var title = null;
	var description = null;

	//alert("getFilteredLinks: "+links.length);
	for(var i = 0; i < links.length; i++) {

		title = links[i].getTitle();
		description = links[i].getDescription();

		for(var j = 0; j < keywords.length; j++) {
			if(isEmpty(keywords[j])) continue;

			//alert("Keyword Compare: "+links[i]+" | "+keywords[j]+"<br />");
			if((!isEmpty(title) && title.toLowerCase().indexOf(keywords[j]) >= 0) ||
				(!isEmpty(description) && description.toLowerCase().indexOf(keywords[j]) >= 0)) {
				//alert("Keyword Filtered: "+links[i]+"<br />");
				found = true;
			}
		}

		if(found) savedLinks.push(links[i]);
		else filteredLinks.push(links[i]);

		found = false;
	}

	var filtered = new Object();
	filtered["filtered"] = filteredLinks;
	filtered["forbidden"] = savedLinks;

	return filtered;
}

// keywords is space delimited
// 2007-08-06: added maxResults. Yahoo started limiting results to two by default.
function doSearch(keywords, urlFilters, termFilters, maxResults) {
	var conf = new overtureSearchConf();
	conf.setKeywords(keywords);

	if(maxResults != null && maxResults >= 0) {
		conf.setMaxCount(maxResults);
	}

	if(urlFilters != null) conf.setUrlFilters(urlFilters);
	if(termFilters != null) conf.setTermFilters(termFilters);
	loadOvertureSearchInclude(conf, false);
}

// keywords is space delimited
function doContentMatch(contextId, keywords, maxContentMatch, outputCharacterEncoding, type) {
	var ctxtId = contextId == null ? 'shows' : contextId;
	//var ctxtCat = contextId == null ? 'shows' : contextId;
	var ctxtCat = '';
	var mkt = '';
	//var type = getContextPath(document.location.href, ctxtId, true, true);
	var loc = document.location.href;
	if(loc.match("/home/search/search_results.jhtml")) {
		type = "search";
	} else if(type == "gettype") {
		type = getContextPath(document.location.href, ctxtId, true, true);
	}

	var keywordCharEnc = '';
	var outputCharEnc = isEmpty(outputCharacterEncoding) ? "" : outputCharacterEncoding;
	var maxCount = maxContentMatch == null ? '5' : maxContentMatch;

	// where load metakeywords was

	//Create a configuration object and add the keywords to the conf.
	var conf = new overtureContentMatchConf();
	if(keywords != null) conf.setCtxtKeywords(keywords);
	if(!isEmpty(ctxtId)) conf.setCtxtId(ctxtId);
	if(!isEmpty(ctxtCat)) conf.setCtxtCat(ctxtCat);
	if(!isEmpty(mkt)) conf.setMkt(mkt);
	if(!isEmpty(type)) conf.setType(type);
	if(!isEmpty(keywordCharEnc)) conf.setKeywordCharEnc(keywordCharEnc);
	if(!isEmpty(outputCharEnc)) conf.setOutputCharEnc(outputCharEnc);
	if(!isEmpty(maxCount)) conf.setMaxCount(maxCount);

	loadOvertureCMInclude(conf, false);
}

function doLinkSpots(maxLinkSpots) {
	//Create a configuration object for linkspots and load the linkspots in
	var lsConf = new overtureLinkspotConf();
	lsConf.setNGrp(1);
	lsConf.setNKw(maxLinkSpots == null ? 10 : maxLinkSpots);
	//This will use the mapping to determine the linkspot id
	lsConf.setLinkspotIdMap(g_linkspotIdMap);
	loadOvertureLinkspotInclude(lsConf, false);
}

// call after doSearch() or doContentMatch() - in a separate <script> block. or zSr wont be loaded.
function getLinks() {
	var links = zSr ? populateOvertureLinks(zSr) : zSr_dummy;
	links = links == undefined ? zSr_dummy : links;
	return links;
}

function getLinkSpots() {
	var overtureLinkspots = mapkey ? populateOvertureLinkspots(mapkey) : populateOvertureLinkspots(mapkey_dummy);
	return overtureLinkspots;
}

// linkData object or an array of links
function getHtml(linkData, startIndex, endIndex, vertical) {
	if(linkData == null) return "";

	var links = !linkData.getItems ? linkData : linkData.getItems();
	if(links == null || links.length <= 0) return "";

	startIndex = startIndex == null ? 0 : startIndex;
	endIndex = endIndex == null || endIndex == 0 || endIndex > links.length ? links.length : endIndex;

	var i = startIndex;
	var outputBuffer = "";
	var link = null;
	var count = 0;

	for(var i = startIndex; i < endIndex; i++) {
		link = links[i];
		if(link == null) continue;

		if(vertical) {
			outputBuffer += "<tr>";
		}

		outputBuffer += "<td valign=\"top\"><a class=\"sponsor_ad\" href='"+ link.getClickUrl() +"' target='_blank' onClick=\"return doUrl(this.href);\">"+ link.getTitle() +"</a><br>"+
			"<a class=\"sponsor_ad_description\" href='"+ link.getClickUrl() +"' target='_blank' onClick=\"return doUrl(this.href);\">"+
			link.getDescription()+
			"</a><br><a class=\"sponsor_ad_client\" href='"+ link.getClickUrl()+"' target='_blank' onClick=\"return doUrl(this.href);\">"+ link.getSitehost() +"</a></td>\n";

		if(vertical) {
			outputBuffer += "</tr>";
		}
		count++;
	}
	if(outputBuffer != "") 
		outputBuffer = "<table>"+ outputBuffer +"</table>";

	return outputBuffer;
}


function getHtmlLinkspots(linkData, rowIndex, maxLinks, vertical) {
	if(linkData == null) return "";

	var linkspot = linkData.getItem(rowIndex);
	var keywords = linkspot == null ? null : linkspot.getKeywords();
	if(keywords == null || keywords.length <= 0) {
		return "<table border=0><tr><td>no links</td></tr></table>";
	}
						
	var outputBuffer = "<table width='100%' border='0'>";
	
	maxLinks = maxLinks == null || maxLinks > keywords.length ? keywords.length : maxLinks;
	var keyword;
	for (var i = 0; i < maxLinks; i++) {
		keyword = keywords[i];
		if(keyword == null) continue;

		if(vertical) {
			outputBuffer += "<tr>";
		}

		outputBuffer += "<td width='20%'><a class=\"sponsor_ad\" href=\""+ linkspotSearchUrl + urlEncode(keyword) + "\" onClick=\"return doLinkSpotUrl(this.href);\">" + keyword + "</a></td>\n";

		if(vertical) {
			outputBuffer += "</tr>";
		}
	}

	outputBuffer += "</tr></table>";
	return outputBuffer;
}

// usage: /home/search/search_results.jhtml
function displaySponsoredLinks(filteredLinks, divName, min, max, vertical, toggleVisibility) {
	//alert("Len: "+filteredLinks.filtered.length+" | "+max);
	var contentDiv = divName + "_content";
	if(filteredLinks && filteredLinks.filtered && filteredLinks.filtered.length > 0) {
		var di = document.getElementById(divName);
		if(di == null) return;

		min = min == null ? 0 : min;
		max = max == null ? 2 : max;

		var content = getHtml(filteredLinks.filtered, min, max, vertical);
		//content += "["+filteredLinks.filtered.length+"]";

		if(toggleVisibility && (content == null || content == "")) {
			toggleDiv(contentDiv, false);
			return;
		}

		toggleDiv(contentDiv, true);
		di.innerHTML = content;
	} else if(toggleVisibility) {
		toggleDiv(contentDiv, false);
	}
}

function toggleDiv(divName, show) {
	var di = document.getElementById(divName);
	if(di == null) return;

	di.style.visibility = show ? "visible" : "hidden";
}

// legacy. do not remove. Possibly in use.
function hideDiv(divName) {
	toggleDiv(divName, false);
}
