function getAds(list) { // ex: {ads_list: ["top", "bottom"], top: "ads_top", bottom: "ads_300x250_bottom"}
  if (list != null) {
    $.getJSON("/ads/get_ads", list, function(data) {
      renderAds(data);
    });
  }
}

function renderAds(data, second_try) {
  if (second_try == null) {second_try = false}
  list = data.ads_list;
  divs = data.divs;
  count = 0;
  for (i in list) {
    ad = list[i];
    div = $('#' + divs[count]);
    if ((div.size() != 1) && (second_try == false)) {
      setTimeout(renderAds(data, true), 50);
    } else {
      div.html(data[ad]);
    }
    count += 1;
  }
	$('script').remove();
}

function loadHiddenAds(list, divs) {
  $('#hidden_ads').contents()[0].location = "/hidden_ads?ads_list="+list+"&divs="+divs;
}

function moveAds(list) {
  list = list.split(",");
  for (i in list) {
		$('#hidden_ads').contents().find('script').remove();
		el = $('#hidden_ads').contents().find('#'+list[i]);
    $('#'+list[i]).replaceWith(el);
  }
}

function reloadAds(keywords) {
	if (keywords == null) {keywords = "myspace";}
	if (keywords.length > 600) {
		keywords = keywords.substring(0,600);
	}
	now = new Date();
	$('#ads_top')[0].src = "ads/top?u_str="+now.getTime()//+"keywords_list="+keywords;
	$('#ads_right')[0].src = "ads/right?u_str="+now.getTime()//+"keywords_list="+keywords;
	$('#ads_left')[0].src = "ads/left?u_str="+now.getTime()//+"keywords_list="+keywords;
	ads_bottom = $('#ads_bottom');
	if (ads_bottom.size() > 0) {
  	$('#ads_bottom')[0].src = "ads/bottom?u_str="+now.getTime()//+"keywords_list="+keywords;
	}
	$('#extreme_tracking')[0].src = "ads/extreme?u_str="+now.getTime();
	ads_pop_under = $('#ads_pop_under');
	if (ads_pop_under.size() > 0) {
		$('#ads_pop_under')[0].src = "ads/pop_under?u_str="+now.getTime();
	}
}