var feedPrefix = 'http://aps1.philly.com/yahoo/sponsor_ads.php?q=';
var adARR = new Array();

function getVariable(myKey) {
	var fullURL = window.location.toString();
	var startPoint = fullURL.indexOf("?");
	var queryString = fullURL.substr(startPoint+1,fullURL.length);
	var keyValuePairs = queryString.split("&");
	var myValue;
	for(var x = 0; x < keyValuePairs.length; x++) {
		if(keyValuePairs[x].indexOf(myKey) != -1) {
			myValue = keyValuePairs[x].substr(keyValuePairs[x].indexOf("=")+1,keyValuePairs[x].length);
		}
	}
	return myValue;
}

function getFeedData(myKeywords){
	var feedURL = feedPrefix + myKeywords + '&callback=?';
	var adOBJ = new Object();
	jQuery.getJSON(feedURL,
		function(json) {
			var counter = 0;
			for(sponsorAds in json["ResultSet"]["Listing"]) {
				var tempOBJ = json["ResultSet"]["Listing"][sponsorAds];
				if(tempOBJ != undefined) {
					adOBJ[counter] = new Object();
					adOBJ[counter].rank = tempOBJ["@attributes"].rank;
					adOBJ[counter].title = tempOBJ["@attributes"].title;
					adOBJ[counter].description = tempOBJ["@attributes"].description;
					adOBJ[counter].site = tempOBJ["@attributes"].siteHost;
					adOBJ[counter].rating = tempOBJ["@attributes"].adultRating;
					adOBJ[counter].clickURL = tempOBJ["ClickUrl"];
					adARR.push(adOBJ[counter]);
					counter++;
				}
			}
		}
	);
}

function createAd(myOBJ) {
	var tempHTML = "<dl id=\"web_results_sponsor\"><dt>";
	tempHTML += "<a href=\"" + myOBJ.clickURL + "\">" + myOBJ.title + "<\/a>";
	tempHTML += "<\/dt><dd>";
	tempHTML += myOBJ.description + "<br\/>";
	tempHTML += "<a href=\"" + myOBJ.clickURL + "\">" + myOBJ.site + "<\/a>";
	tempHTML += "<\/dd><\/dl>";
	return tempHTML;
}

if(getVariable("q") != undefined){getFeedData(getVariable("q"));}
else{getFeedData("philadelphia");}
