//A globally scoped string to hold the HTML that renders the ads.
var slm_html="";
var slm_html_1="";

//Set the SLM request parameters
var params = {callback:'', sch:'', ssch:'', squery:'', spage:'', snum:'', of:'', z:'', city:'', state:'', spage:'', rv:'', stest:''};
params.callback='done';

// Some default values in case we can't get any info back
var isClient = 0;
var coBrand = "aolde";
var coType = "aolde";
var subClient = "prt";
var clientIdArray = {
	"svc" : "afs-de-vertical",
	"prt" : "afs-de-vertical-prt",
	"jprt" : "afs-de-vertical-jprt"
};
 
// Try to get the value back from the API's
if (typeof ProductBranding != "undefined") {
	// Using the Product API
	var coBrand = ProductBranding.coBrand;
	var coType = ProductBranding.coType;
	var isClient = ProductBranding.isClient;
} else if (typeof AOL != "undefined") {
	// Using the Partner API
	var coBrand = AOL.cobrand;
	var coType = "undefined";
	var isClient = AOL.client;
}
 
if (isClient != 0) {
	var subClient = "svc";
} else if (coBrand.indexOf("aol") >= 0) {
	var subClient = "prt";
} else {
	var subClient = "jprt";
}
 
// Get the actual client ID
var clientId = clientIdArray[subClient];
 
// Rest of the SLM configuration here:
params.sch= clientId;
params.ssch='engadget';
params.squery=bsQuery;
params.snum='3';
params.of='js';
params.city=''.replace(/ /g,'+');
params.state=''.replace(/ /g,'+');
params.rv='1.2';
params.spage = sPageNum;
params.stest = 'off';
params.ssafe = 'low';
params.locale = 'de_DE';
get_links();//Request the Sponsored Links
curPage = curPageNum;

//This is the callback funtion which is called when the ads are available.
//sponsorData is an array of ads
function done(sponsorData) {
	//If there is at least one result then create some sponsored links
	if(sponsorData.length!=0) {
		//Build up a string of HTML to display the results
		slm_html="";
		slm_html_1="";
		slm_html+='<h1><a id="uk-sponsored-link" target="_blank" href="http://suche.aol.de/aol/about#sl" title="Sponsoren Links">Sponsoren Links</a></h1>' +"<ul>";
		slm_html_1+='<h1><a id="uk-sponsored-link" target="_blank" href="http://suche.aol.de/aol/about#sl" title="Sponsoren Links">Sponsoren Links</a></h1>' +"<ul>";
		//For each sponsored link in the results array, add a list item to the unordered list
		for (i=0;i<sponsorData.length;i++) {
			if(i < 3) {
				slm_html+="<li class=\"slblock\"><a target=\"_blank\" href='"
				+sponsorData[i].redirect_url
				+"' onmouseover=\"self.status='"
				+sponsorData[i].url
				+"'; return true\" onmouseout='self.status=\"\"; return true'>"
				+"<h2 class=\"linkh2text\" style=\"border:none;\">"
				+sponsorData[i].title
				+"</h2>"
				+"<h3>"
				+sponsorData[i].d1+" "
				+sponsorData[i].d2+"</h3>"
				+"<h4>"
				+sponsorData[i].url
				+"</h4>"
				+"</a></li>";
			}
			slm_html_1+="<li class=\"slblock\"><a target=\"_blank\" href='"
			+sponsorData[i].redirect_url
			+"' onmouseover=\"self.status='"
			+sponsorData[i].url
			+"'; return true\" onmouseout='self.status=\"\"; return true'>"
			+"<h2 class=\"linkh2text\">"
			+sponsorData[i].title
			+"</h2>"
			+"<h3>"
			+sponsorData[i].d1+" "
			+sponsorData[i].d2+"</h3>"
			+"<h4>"
			+sponsorData[i].url
			+"</h4>"
			+"</a></li>";
		}
		slm_html+="</ul>";
		slm_html_1+="</ul>";
	}
	else {
		//we got no results so do some default behavior or even nothing at all.
		slm_html="";
		slm_html_1="";
	}
	//[slm_html now contains the HTML to draw the ads returned from SLM]
	//stuff the HTML into the DIV we created for it
	showSponsoredLinks();
}
//This is a function to display the ads in the dmn_results DIV
//it handles the cases where the callback happens either before or after
//the DIV is created.
//
function showSponsoredLinks() {
	try {
		if( slm_html != "" && slm_html_1 != "" ) {
			//TODO: Maybe include a check to see if the results have already
			//been filled.
			document.getElementById('dmn_results').innerHTML = slm_html;
			document.getElementById('dmn_results1').innerHTML = slm_html_1;
			slm_html="";
			slm_html_1="";
		}
		else {
			//If the dmn_results DIV is created before the callback runs,
			//slm_html will be empty and we will end up here.
			//That's OK, when the callback runs later, both the slm_html and
			//the DIV will be ready.
 
		}
	}
	catch(err) {
		//If the callback runs before the page is complete,
		//the dmn_results DIV may not exist yet and we end up here.
		//That's OK, this function will be called again when the DIV is created.
		//alert(err);
	}
}