var QueryURL, url, zSr;
//required args:
var config, source, ctxtUrl, ctxtId, sensitive
//query context args
// p16
//conditional args:
//ctxtId => needs to be set on the page itself, indicating the page category
//var ctxtCat, ctxtId, ctxtKeywords, type, keywordCharEnc, outputCharEnc, maxCount
var ctxtCat, ctxtKeywords, type, keywordCharEnc, outputCharEnc, maxCount

function getctxtId(urlPart) {
var ctxtId;
var ix;
switch(urlPart) {
	case "advocate":
		ctxtId = "blogs_social";
		ix=0;
	break;
	case "autos":
		ctxtId = "cars";
		ix=1;
	break;
	case "bestof":
		ctxtId = "entertainment";
		ix=2;
		break;
	case "bestof2007":
		ctxtId = "entertainment";
		ix=3;
	break;
	case "bestofvideo":
		ctxtId = "photos_video";
		ix=4;	
	break;
	case "blogs":
		ctxtId = "blogs_social";
		ix=5;	
	break;
	case "business":
		ctxtId = "business";
		ix=6;	
	break;
	case "calendar":
		ctxtId = "entertainment";
		ix=7;	
	break;
	case "capitalnd":
		ctxtId = "business";
		ix=8;	
	break;
	case "capitaland2003":
		ctxtId = "business";
		ix=9;
	break;
	case "capitaland2004":
		ctxtId = "business";
		ix=10;
	break;
	case "capitaland2005":
		ctxtId = "business";
		ix=11;
	break;
	case "capitalcars":
		ctxtId = "cars";
		ix=12;
	break;
	case "capitol":
		ctxtId = "news";
		ix=13;
	break;
	case "careers":
		ctxtId = "employment";
		ix=14;
	break;
	case "celebrations":
		ctxtId = "lifestyle";
		ix=15;
	break;
	case "classifieds":
		ctxtId = "classifieds_tier_2";
		ix=16;
	break;
	case "centralave":
		ctxtId = "entertainment";
		ix=17;
	break;
	case "centralavenue":
		ctxtId = "entertainment";
		ix=18;
	break;
	case "comics":
		ctxtId = "entertainment";
		ix=19;
	break;
	case "communityforum":
		ctxtId = "blogs_social";
		ix=20;
	break;
	case "election":
		ctxtId = "news";
		ix=21;
	break;
	case "election2006":
		ctxtId = "news";
		ix=22;
	break;	
	case "elections":
		ctxtId = "news";
		ix=23;
	break;
	case "entertainment":
		ctxtId = "entertainment";
		ix=24;
	break;
	case "entertainment2":
		ctxtId = "entertainment";
		ix=25;
	break;
	case "halfmoon":
		ctxtId = "news";
		ix=26;
	break;
	case "harrypotter":
		ctxtId = "entertainment";
		ix=27;
	break;
	case "highschoolsports":
		ctxtId = "sports";
		ix=28;
	break;
		case "homes":
		ctxtId = "lifestyle";
		ix=29;
	break;
		case "homes2":
		ctxtId = "lifestyle";
		ix=30;
	break;
	case "jobfair":
		ctxtId = "employment";
		ix=31;
	break;
	case "jobs":
		ctxtId = "employment";
		ix=32;
	break;
	case "kitchenskills":
		ctxtId = "lifestyle";
		ix=33;
	break;
	case "LatestLocalNews":
		ctxtId = "news";
		ix=34;
	break;
	case "life":
		ctxtId = "lifestyle";
		ix=35;
	break;
	case "local":
		ctxtId = "news";
		ix=36;
	break;
	case "locallinks":
		ctxtId = "news";
		ix=37;
	break;
	case "lottery":
		ctxtId = "lottery";
		ix=38;
	break;
	case "mondayconcerts":
		ctxtId = "entertainment";
		ix=39;
	break;
	case "mondaynights":
		ctxtId = "entertainment";
		ix=40;
	break;
	case "movies":
		ctxtId = "entertainment";
		ix=41;
	break;	
	case "multimedia":
		ctxtId = "photos_video";
		ix=42;
	break;
	case "nation":
		ctxtId = "news";
		ix=43;
	break;
	case "nation-world":
		ctxtId = "news";
		ix=44;
	break;
	case "news":
		ctxtId = "news";
		ix=45;
	break;	
	case "olympics":
		ctxtId = "sports";
		ix=46;
	break;	
	case "opinion":
		ctxtId = "news";
		ix=47;
	break;
	case "personals":
		ctxtId = "personals";
		ix=48;
	break;
	case "photogalleries":
		ctxtId = "photos_videos";
		ix=49;
	break;
	case "pickthepros":
		ctxtId = "sports";
		ix=50;
	break;
	case "place":
		ctxtId = "entertainment";
		ix=51;
	break;
	case "politics":
		ctxtId = "news";
		ix=52;
	break;
	case "politics_comp":
		ctxtId = "news";
		ix=53;
	break;
	case "poll":
		ctxtId = "news";
		ix=54;
	break;
	case "profootballpickem":
		ctxtId = "sports";
		ix=55;
	break;
	case "readandreact":
		ctxtId = "blogs_social";
		ix=56;
	break;
	case "readernetwork":
		ctxtId = "blogs_social";
		ix=57;
	break;
	case "realestate":
		ctxtId = "real_estate";
		ix=58;
	break;
	case "religion":
		ctxtId = "lifestyle";
		ix=59;
	break;
	case "reportcard":
		ctxtId = "news";
		ix=60;
	break;
	case "reportcard2005":
		ctxtId = "news";
		ix=61;
	break;
	case "reportcard2006":
		ctxtId = "news";
		ix=62;
	break;
	case "restaurants":
		ctxtId = "entertainment";
		ix=63;
	break;
	case "Saratoga":
		ctxtId = "news";
		ix=64;
	break;
	case "schoolclosings":
		ctxtId = "news";
		ix=65;
	break;
	case "search":
		ctxtId = "news";
		ix=66;
	break;
	case "searchalbany":
		ctxtId = "news";
		ix=67;
	break;
	case "seen":
		ctxtId = "entertainment";
		ix=68;
	break;
	case "seen2":
		ctxtId = "entertainment";
		ix=69;
	break;
	case "shapeup":
		ctxtId = "sports";
		ix=70;
	break;
	case "shopping":
		ctxtId = "shopping";
		ix=71;
	break;
	case "specialreports":
		ctxtId = "news";
		ix=72;
	break;
	case "sports":
		ctxtId = "sports";
		ix=73;
	break;
	case "styles":
		ctxtId = "lifestyle";
		ix=74;
	break;
	case "travel":
		ctxtId = "travel";
		ix=75;
	break;
	case "tvguide":
		ctxtId = "entertainment";
		ix=76;
	break;
	case "videocontest":
		ctxtId = "photos_videos";
		ix=77;
	break;
	case "videoplayer":
		ctxtId = "photos_videos";
		ix=78;
	break;
	case "world":
		ctxtId = "news";
		ix=79;
	break;
	case "yourphotos":
		ctxtId = "photos_videos";
		ix=80;
	break;
	case "yourstuff":
		ctxtId = "classifieds_tier_2";
		ix=81;
	break;

	default:
	ctxtId = "misc";
	ix=82;
	}
	return ctxtId;
}
function getTier(ctxtId) {
	var tier;
	var source;
	switch(ctxtId) {
	case "announcements":
		tier = '2';
	break;
	case "blogs_social":
		tier = '2';
	break;
	case "business":
		tier = '1';
	break;
	case "calendar":
		tier = '2';
	break;
	case "cars":
		tier = '1';
	break;
	case "classifieds_tier_2":
		tier = '2';
	break;
	case "education":
		tier = '1';
	break;
	case "employment":
		tier = '1';
	break;
	case "entertainment":
		tier = '1';
	break;
	case "family":
		tier = '2';
	break;
	case "finance":
		tier = '1';
	break;
	case "health":
		tier = '1';
	break;
	case "home_page":
		tier = '2';
	break;
	case "lifestyle":
		tier = '1';
	break;
	case "lottery":
		tier = '2';
	break;
	case "misc":
		tier = '2';
	break;
	case "news":
		tier = '2';
	break;
	case "opinion":
		tier = '2';
	break;
	case "personals":
		tier = '2';
	break;
	case "pets":
		tier = '2';
	break;
	case "photos_videos":
		tier = '2';
	break;
	case "real_estate":
		tier = '1';
	break;
	case "recreation":
		tier = '2';
	break;
	case "rentals":
		tier = '1';
	break;
	case "shopping":
		tier = '2';
	break;
	case "sports":
		tier = '2';
	break;
	case "tech":
		tier = '1';
	break;
	case "travel":
		tier = '1';
	break;
	case "web_site_pages":
		tier = '2';
	break;
	default:
		tier='2';
	}
	
	if (tier == '1') {
		source= 'npc_hearst_albanytimesunion_t1_ctxt';
	}
	if (tier == '2') {
		source='npc_hearst_albanytimesunion_t2_ctxt';
	}
//	source= source + "_test";
	return source;
}

function encodeCxtUrl(inURL) {
     var outURL ;

     outURL = escape(inURL);
     outURL = outURL.replace(/\//g,"%2F");
     outURL = outURL.replace(/\?/g,"%3F");
     outURL = outURL.replace(/=/g,"%3D");
     outURL = outURL.replace(/&/g,"%26");
     outURL = outURL.replace(/@/g,"%40");

	return outURL;
 } 

function build() {
	var t = Math.floor(Math.random() * 100000);
	//ctxtUrl = "http://www.timesunion.com"
	//config = '2130893885'
	
	config_reg = '2130893885'; 
	config_sensitive = '8247342110'
	// set the variables:
		config = config_reg;
	if (sensitive == true) {
			config = config_sensitive;
			}else {
				config = config_reg;
		}
	//alert(ctxtId.indexOf("undefined"));

	if ((!ctxtUrl) || (ctxtUrl == '')) {
		ctxtUrl = 'http://www.timesunion.com' + document.URL.substr(document.URL.toUpperCase().indexOf('.COM') + 4);
	}
	ctxtUrl = encodeCxtUrl(ctxtUrl);
	// for testing purposes
	var urlP;
//FOR LIVE SITE
	urlP = document.URL.substr(document.URL.toUpperCase().indexOf('TIMESUNION.COM') + 15)
	//FOR TESTWEB
	//urlP = document.URL.substr(document.URL.toUpperCase().indexOf('TESTWEB.TUNET.COM') + 18)
	urlP = urlP.substring(0, urlP.toUpperCase().indexOf('/'))
	//alert(urlP);
	if ((!ctxtId) || (ctxtId == '')) {
		ctxtId = getctxtId(urlP);
//		alert(ctxtId);
	}
	source= getTier(ctxtId);
//	ctxtId = ctxtId + '_test';
	type = ctxtId;
//	source="npc_hearst_albanytimesunion_search";
	//
	QueryURL = 'http://cm.npc-hearst.overture.com/js_flat_1_0/'
	var a = QueryURL + '?ctxtId=' + ctxtId + '&config=' + config + '&ctxtUrl=' + ctxtUrl + '&source=' + source + '&type=' + type;
	url = a + '&maxCount=3&cb=' + t;
//	alert(url);
	return url;
}

function showListings() {
var i = 6;
var count = 1;
var div1;
var div2;
var div3;
	while (i < zSr.length) {
	//	if (count <4) {

		div1 = "ad" + count + "p1";
		div2 = "ad" + count + "p2";
		div3 = "ad" + count + "p3";
	//	}
		var descr = zSr[i++]; //listing desc
		var unused1 = zSr[i++]; //ignore
		var clickURL = zSr[i++] //listing link
		var title = zSr[i++]; //listing title
		var sitehost=zSr[i++]; //advertiser's domain name
		var unused2 = zSr[i++]; // (ignore)	
	//	document.write('<p><span>');
		document.getElementById(div1).innerHTML = "<a href='" + clickURL + "'>" + title + "</a>";
		//document.write('<a target ="_new" href="' + clickURL + '">');
		//document.write(title + '</a></span>');
		document.getElementById(div2).innerHTML = "<A HREF='" + clickURL + "'>" + descr + "</a>"; 
		document.getElementById(div3).innerHTML = "<A HREF='" + clickURL + "'>" + sitehost + "</a>"; 
		count++;
	}

	//node.innerHTML = "some ad";
}