var text_ads = [];
function getJSFromOverture (url) {
	var count = 12;
	if ($("#ad_topcenter").size() == 0) {
		count -= 3;
	}
	if ($("#ad_bottomcenter").size() == 0) {
		count -= 4;
	}
	if ($("#ad_sidebar").size() == 0) {
		count -= 5;
	}
	if (count > 0) {
		url += "&maxCount=" + count;
		$.getScript(url, function () {
			setTextAdList();
			if ($("#ad_topcenter").size() > 0) {
				setTextAd("ad_topcenter", 3);
			}
			if ($("#ad_bottomcenter").size() > 0) {
				setTextAd("ad_bottomcenter", 4);
			}
			setTextAd("ad_sidebar", 5);
	  });
  }
}
function setTextAdList () {
	var start = 6;
	var end = zSr.length;
	text_ads = [];
	for (var i=start; i<end; i) {
		var descr = zSr[i++];
		var unused1 = zSr[i++];
		var href = zSr[i++];
		var title = zSr[i++];
		var sitehost = zSr[i++];
		var unused2 = zSr[i++];
		href = (href == undefined) ? '' : href;
		title = (title    == undefined) ? '' : title;
		descr = (descr    == undefined) ? '' : descr;
		sitehost = (sitehost == undefined) ? '' : sitehost;
		if (href != '') { text_ads.push({ asp:'overture', href:href, title:title, descr:descr, sitehost:sitehost, beacon:'' }); }
	}
  for (var i = text_ads.length - 1; i >= 0; i--) {
    var j = Math.floor(Math.random() * (i + 1))
    var tmp = text_ads[i];
    text_ads[i] = text_ads[j];
    text_ads[j] = tmp;
  }
  /*
  var cnt = textads_supleks.length;
	for (var i=0; i<cnt; i++) {
		text_ads.push(textads_supleks[i]);
	}
  */
}
function setTextAd (place, count) {
	var url = [];
	if (document.getElementById(place) != undefined) {
		for (var i=0; i<count; i++) {
			if (text_ads.length > 0) {
				var ad = text_ads[0];
				var href = ad.href;
				var title = ad.title;
				var descr = ad.descr;
				var sitehost = ad.sitehost;
				if (ad.beacon != '') {
					url.push('<img src="' + ad.beacon + '" width="1" height="1" border="1" />');
				}
				if (place == 'ad_topcenter') {
					url.push('<a href="' + href + '" target="_new" class="textad"><div class="header"><span class="title">' + title + '</span><span class="host">' + sitehost + '</span></div><div class="description">' + descr + '</div></a>');
				} else if (place == 'ad_bottomcenter') {
					url.push('<a href="' + href + '" target="_new" class="textad"><div class="header"><span class="title">' + title + '</span><span class="host">' + sitehost + '</span></div><div class="description">' + descr + '</div></a>');
				} else if (place == 'ad_sidebar') {
					url.push('<a href="' + href + '" target="_new" class="textad-sidebar"><div class="adtitle">' + title + '</div><div class="description">' + descr + '</div></a>');
				}
				text_ads.splice(0, 1);
			}
		}
		if (ad.asp == "overture") {
			if (place != 'ad_sidebar') {
				$("#" + place + "_sign").html('[PR] インタレストマッチ - <a href="http://ov.yahoo.co.jp/service/int/index.html?o=IM0028" style="text-decoration:underline;color:#666;">広告の掲載について</a>');
			} else {
				$("#" + place + "_sign").html('インタレストマッチ - <a href="http://ov.yahoo.co.jp/service/int/index.html?o=IM0028" style="text-decoration:underline;color:#fff;">広告の掲載について</a>');
			}
		} else {
			$("#" + place + "_sign").html('[PR] <a href="http://pitatto.rakuten.co.jp/" style="text-decoration:underline;color:#666;">楽天ぴたっとアド</a>');
		}
		$("#" + place).html(url.join(""));
	}
}
