//This page needs to be configured for each site

//Base url for content match. Each site has a different hostname
var g_baseOvertureCMUrl = "http://cm.mtvtr3s.overture.com/js_flat_1_0/";
//source paramater for content match. Each site has its own source code
var g_overtureCMSource = "viacom_mtvtr3s_ctxt";
//Config code for each site
var g_overtureCMConfig = "23968379231";
//Base url for partner search. 
var g_baseOvertureSearchUrl = "http://html.viacom.overture.com/d/search/p/viacom/js/v2/";
//partner paramater for search. 
var g_overtureSearchPartner = "viacom_mtvtr3s_search";
if (window.location.search.indexOf("usecm=1") != -1) g_overtureSearchPartner = "viacom_mtvtr3s_ls_search";
var g_baseOvertureLinkspotUrl = "http://cmls.overture.com/ls_js_1_0/";
//default linkspot id.
var g_defaultLinkspotId = "mtv_games";
//source paramater for linkspots.  Each site must configure its own.
var g_overtureLinkspotSource = "viacom_mtvtr3s_kwonly_ctxt";
//Config code for each site.  Each site must configure its own.
var g_overtureLinkspotConfig = "23968379231";

var g_contextIdMap = new Object();
g_contextIdMap["/music/artist"] = "musica";
g_contextIdMap["/news/articles"] = "musica";
g_contextIdMap["/ontv"] = "shows";
g_contextIdMap["/music"] = "musica";
g_contextIdMap["/"] = "musica";

var g_typeIdMap = new Object();
g_typeIdMap["/music/artist"] = "musica";
g_typeIdMap["/news/articles"] = "musica";
g_typeIdMap["/ontv"] = "shows";
g_typeIdMap["/music"] = "musica";
g_typeIdMap["/"] = "musica";

var g_linkspotIdMap = new Object();
g_linkspotIdMap["/music/artist"] = "mtvtr3s_musica";
g_linkspotIdMap["/news/articles"] = "mtvtr3s_musica";
g_linkspotIdMap["/ontv"] = "mtvtr3s_shows";
g_linkspotIdMap["/music"] = "mtvtr3s_musica";
g_linkspotIdMap["/"] = "mtvtr3s_musica";


//Default links to use in case we don't get results back.
zSr_dummy = new Array("Reach 80% of active Internet users with Overture.","","","List your site with Overture","http://www.overture.com/","",
"See the latest Accord offers at the Honda Official Site.",
"",
"http://ypn-100.overture.com/d/sr/?xargs=SjGTYHM2WSfAwg8ep-u2ASFl94ZiTOXSU7PrBi34b0dkMRpxIz_DgoDqDHPG_NIidq9XtjihuvhsnU-qzNGWIEpI0WYkcoPdGWF6vHcW_pmyJkWX7VREKmh2o6lFQdzPF1NYiCCDFPBMzTW1E9ywARKkbBHDxtdkScwJOM-imi-fb8Z061AriZIDa1EDdkfqf3QyaFsV2ZtTR7fzb3-6K7zuu9vXH0AppemWRteQ9SJW7Z1IuIHVxAIrOsp7BDr91kty-i4Ghjhbnh_NMBWcNKIHMwuOzg_br8ZdyL97n9w8jcEZNvfaURyWMRn97xi0YtD5kOy-QPA",
"Honda Vehicles",
"www.honda.com",
"",
"Save up to 40% JVC, Pioneer, Clarion, Alpine, Kenwood.",
"",
"http://ypn-100.overture.com/d/sr/?xargs=k-vWdwvJp78awRZ3Qhx-bNIsqKMyFJwxG5e0pmbgOXJ8w7TZD_t_g3yvErHu8Xl4I3_x8ptDV3A0LIMF7OlPTj5IkMVeVfJ7YImiuZU1-p7rVyQOV4pyMgRkXrvbW_C6knIw1bUvr88gdVHsT-HA77YeNJVDGHkfjWgmNwyqxub9CYdDO8qHDr1b2pamYZbeIy8ZdhbSa1DvmkyROP-YJHS26-bDnYiyF5Aqyi-iI6d3UaXPeWNhzBfWVqBbIRd7wn767p7eqywIgT6oNriUfKJKA9YnOYxl9msgYv75FJF4Yrr5i8pEpPbf8orxrGg5OiPTaHCp4Qw",
"Car Audio/Multimedia SALE",
"carsaudio.co.uk",
"",
"Electronics, Games, Toys, Digital Cameras, and more.",
"",
"http://ypn-100.overture.com/d/sr/?xargs=cc9OZjd2BnLDSEYSO0xFQYOLbyfpbe27TqGUg90i-jKS-gcU8lXMkhuNGMm3DJcx2oriBHlOMItEsUbii_mZooTh1ploLYXV4BBkox4A4Qm4e-UnspwI6DfPw1Y_7s2sDBsD0wcAgFkwVB-VviXebuFDhuCZJseHhCWH6u2I3yH5D1X0OIGIQdhCQIYNL7WVQvzPz69wffyUTyqnItDPX1OFjCZ3Kqnwomn_TFmZeibU291SbvoyNP4mMKIn6VmD3D4rOoid-bTxDP63P19rs3ZAqnClMlJZXOimusbL77w3hK72W7aVtG2VBll-pXPpohzEDJ2nt8A",
"Hundreds of Products at Low Prices",
"www.advancedtechtoys.com",
"");

//zSr is the array that is populated by the javacsript call to Overture
var zSr;
var mapkey;
var mapkey_dummy=[{title: 'placeholder', keywords: 'fitness equipment, fitness, diet program, weight loss program, home gyms, vitamin, exercise bike, nutrition, healthy diet, fitness program'}];


//Below are some example funtions to draw the acutall link boxes
//Each site can use this same pattern to define new functions that can draw the links
//in various ways.

function titleCase(str) {
        return str.toLowerCase().replace(/\b\w+\b/g, cnvrt);
        function cnvrt() {
            if (arguments[arguments.length -2] == 0)
                return arguments[0].replace(/^[a-z]/, cnvrt2);
            else if (/^(a|about|after|an|and|at|by|for|from|in|into|nor|of|on|onto|over|the|to|up|with|within)$/.test(arguments[0]) )
                return arguments[0];
            else
                return arguments[0].replace(/^[a-z]/, cnvrt2);
        }
        function cnvrt2() {
            return arguments[0].toUpperCase();
        }
    }

//A function for drawing a set of sponsored links
function showLinks1(linkData, startIndex, endIndex, placement) {
  if (startIndex == undefined) {
	startIndex = 0;
  }
  var links = linkData.getItems();
  var outString = '<ol class="slat rich sponsored-results">\n';
  var arrLength = links.length;
  if (endIndex == undefined || endIndex == 0) {
    endIndex = arrLength;
  }
  else {
    if (endIndex > arrLength) {
	endIndex = arrLength;
    }
  }

  var emptyText = "";
  if (endIndex <= startIndex) {
     //If there are not links to show, then return an alternative text
     return emptyText;
  }

  var i = startIndex;
  while ( i <= endIndex && links[i] != null) {
   var currItem = links[i];
   
   var ctText = currItem.getTitle().replace(eval('/' + keywords.toUpperCase() + '/g'), '<eM>' + keywords.toUpperCase() + '</eM>');
   ctText = ctText.replace(eval('/' + keywords.toLowerCase() + '/g'), '<eM>' + keywords.toLowerCase() + '</eM>');
 	ctText = ctText.replace(eval('/' + titleCase(keywords) + '/g'), '<eM>' + titleCase(keywords) + '</eM>');
 	
 	var cdText = currItem.getDescription().replace(eval('/' + keywords.toUpperCase() + '/g'), '<em>' + keywords.toUpperCase() + '</eM>');
   cdText = cdText.replace(eval('/' + keywords.toLowerCase() + '/g'), '<eM>' + keywords.toLowerCase() + '</eM>');
 	cdText = cdText.replace(eval('/' + titleCase(keywords) + '/g'), '<eM>' + titleCase(keywords) + '</eM>');
   
   
   if (!(currItem == undefined)) {
     if (i == endIndex) outString += "<li class=\"last\">\n";
     else outString += "<li>\n";
     outString += "<p class=\"info\"><strong><a href=\"" + currItem.getClickUrl() + "\" target=\"_blank\">" + ctText + "</a></strong><a href=\"" + currItem.getClickUrl() + "\" target=\"_blank\">" + cdText + "</a></p>";
     outString += "<p class=\"meta\"><a href=\"" + currItem.getClickUrl() + "\" target=\"_blank\">" + currItem.getSitehost() + "</a></p>\n";
   	 outString += "</li>\n";
   }
   i++;
  } 
  outString += '</ol>\n';
  outString += '<p class="spons-footer"><strong>Sponsored Results</strong></p>\n';
  return outString;
}




//An example function for drawing a set of content match sponsored links with linksposts
function showCMWithLinkspots(linkData, startIndex, endIndex, linkspotData, rowIndex, maxLinks ) {
  if (startIndex == undefined) {
	startIndex = 0;
  }
  var links = linkData.getItems();
  var outString = '<ol class="slat rich sponsored-results">\n';
  var arrLength = links.length;
  if (endIndex == undefined || endIndex == 0) {
    endIndex = arrLength;
  }
  else {
    if (endIndex > arrLength) {
	endIndex = arrLength;
    }
  }

  var emptyText = "";
  if (endIndex <= startIndex) {
     //If there are not links to show, then return an alternative text
     return emptyText;
  }

  var i = startIndex;
  while ( i <= endIndex && links[i] != null) {
   var currItem = links[i];
   
   var ctText = currItem.getTitle().replace(eval('/' + keywords.toUpperCase() + '/g'), '<eM>' + keywords.toUpperCase() + '</eM>');
   ctText = ctText.replace(eval('/' + keywords.toLowerCase() + '/g'), '<eM>' + keywords.toLowerCase() + '</eM>');
 	ctText = ctText.replace(eval('/' + titleCase(keywords) + '/g'), '<eM>' + titleCase(keywords) + '</eM>');
 	
 	var cdText = currItem.getDescription().replace(eval('/' + keywords.toUpperCase() + '/g'), '<em>' + keywords.toUpperCase() + '</eM>');
   cdText = cdText.replace(eval('/' + keywords.toLowerCase() + '/g'), '<eM>' + keywords.toLowerCase() + '</eM>');
 	cdText = cdText.replace(eval('/' + titleCase(keywords) + '/g'), '<eM>' + titleCase(keywords) + '</eM>');
   
   
   if (!(currItem == undefined)) {
     if (i == endIndex) outString += "<li class=\"last\">\n";
     else outString += "<li>\n";
     outString += "<p class=\"info\"><strong><a href=\"" + currItem.getClickUrl() + "\" target=\"_blank\">" + ctText + "</a></strong><a href=\"" + currItem.getClickUrl() + "\" target=\"_blank\">" + cdText + "</a></p>";
     outString += "<p class=\"meta\"><a href=\"" + currItem.getClickUrl() + "\" target=\"_blank\">" + currItem.getSitehost() + "</a></p>\n";
   	 outString += "</li>\n";
   }
   i++;
  } 
 
  
  outString += "<li class=\"last\">\n";
  outString += "<ul class=\"spons-links\">\n";
  
  var isEmpty = false;
	var ls_keywords;			
	var linkspot = linkspotData.getItem(rowIndex);
	if (linkspot == undefined) {
    isEmpty = true;
  }
  else {
    ls_keywords = linkspot.getKeywords();
  }

  var len = ls_keywords.length;

  if (len <= 0) {
    isEmpty = true;
  }
  
  var endIndex;
  if (len > maxLinks) {
     endIndex = maxLinks;
  }
  else {
     endIndex = len;
  }
 if (!isEmpty) {
 for (var i=0; i < endIndex; i++) {
   var keyword = ls_keywords[i];
   
	outString += "<li><strong><a href=\"/search/?usecm=1&searchterm=" + urlEncode(keyword) + "\">" + keyword + "</a></strong></li>\n";
				
			}	
		}
		else outString += "";
	outString += "</ul>\n";
	 outString += "</li>\n";
	 
	
	 
  
  outString += '</ol>\n';
  outString += '<p class="spons-footer"><strong>Sponsored Links</strong></p>\n';
  return outString;
}


//An example function for drawing one row of linkspots. pass in the linkData, index of the row you want
//(we will pretty much always be using index 0 for the first row), and the max number of link
//that we want to show.



function loadOvertureSrchArray() {

	var conf = new overtureSearchConf();
  	conf.setKeywords(keywords);
  	conf.setMaxCount(7);
  	if (window.location.search.indexOf("usecm=1") != -1) conf.setMaxCount(10);
  	conf.setOutputCharEnc("latin1");

	 if (!isEmpty(urlFilters)) {
    conf.setUrlFilters(urlFilters);
  		}
  	if (!isEmpty(termFilters)) {
    conf.setTermFilters(termFilters);
  		}
  	// var myUrl = createOvertureSearchLink(conf);
  	// document.writeln("<P>url = " + myUrl + "<BR>");
  	loadOvertureSearchInclude(conf);
  	
  	}
	
	
	function loadOvertureCMArray() {
	
	var conf = new overtureContentMatchConf();
 // conf.setCtxtKeywords(keywords);
conf.setOutputCharEnc("latin1");
 
  if (!isEmpty(ctxtId)) {
    conf.setCtxtId(ctxtId);
  }
  else {
     //use the map to get the ctxtId
     conf.setCtxtIdMap(g_contextIdMap);
  }
  if (!isEmpty(type)) {
    conf.setType(type);
  }
  else {
  
	conf.setTypeMap(g_typeIdMap);
  
  }
  if (!isEmpty(keywordCharEnc)) {
    conf.setKeywordCharEnc(keywordCharEnc);
  }
  if (!isEmpty(outputCharEnc)) {
    conf.setOutputCharEnc(outputCharEnc);
  }
  if (!isEmpty(maxCount)) {
    conf.setMaxCount(maxCount);
  }



  // var myUrl = createOvertureCMLink(conf);
  // document.writeln("<P>url = " + myUrl + "<BR>");
  loadOvertureCMInclude(conf);
	
	
	}
  
 
 function generateOvertureArray() {
  	if(zSr) {
     	overture_links = populateOvertureLinks(zSr);
     	if (overture_links == undefined) {
        //If this in not populated use dummy data
        	zSr = zSr_dummy;
     	}
  	}
  	else {
    //If this in not populated use dummy data
    	zSr = zSr_dummy;
  	}
	}
 
 function generateOvertureLSArray() {
 if(mapkey) {
     overtureLinkspots = populateOvertureLinkspots(mapkey);
	 
     if (overtureLinkspots == undefined) {
        //If this in not populated use dummy data
        mapkey = mapkey_dummy;
     }
  }
  else {
    //If this in not populated use dummy data
    mapkey = mapkey_dummy;
  }
 
 
 }
 
 function printOverture_1() {
 
 var arg = printOverture_1.arguments;
 if(arg[0] != undefined) {
   
     document.writeln(showLinks1(arg[0], arg[1], arg[2], arg[3]));
  }
  
  }
  
  function printOverture_ls() {
 
 var arg = printOverture_ls.arguments;
 if(arg[0] != undefined) {
   
     document.writeln(showCMWithLinkspots(arg[0], arg[1], arg[2], arg[3], arg[4], arg[5]));
  }
  
  }
