function getDetailsPageQueryString(sh_id) {
  var series_name = sh_id_to_series[sh_id];
  return "?vs_cat=news&vs_type=podcastpage&vs_action=podcast_autoplay&vs_f=" + encodeURIComponent(series_name) + "&vs_p=";
}
function addSeriesLinks(sh_id,domBuilderObj) {
  var $ = YAHOO.util.Dom.get;
  var series_name = sh_id_to_series[sh_id];
  var details_page_query_string = getDetailsPageQueryString(sh_id);
  if (series_name!=null && series_name!=undefined && series_to_podcast_data[series_name].length>1) {
    var podcasts_in_series = series_to_podcast_data[series_name];
    var num_podcasts_in_series = podcasts_in_series.length;
    var index_of_current_podcast = 0;
    for (x=0;x<podcasts_in_series.length;x++) {
      var this_sh_id = podcasts_in_series[x][0];
      if (this_sh_id==sh_id) index_of_current_podcast = x;
    }
    var num_of_current_podcast = index_of_current_podcast+1;
    var index_of_prev_podcast = index_of_current_podcast-1;
    var index_of_next_podcast = index_of_current_podcast+1;
    if (index_of_current_podcast==0) {
      index_of_prev_podcast = podcasts_in_series.length - 1;
      if (podcasts_in_series.length==1) {
        index_of_next_podcast = 0;
      }
    }
    if (index_of_current_podcast==(podcasts_in_series.length - 1)) {
      // current podcast is the last podcast in the series
      index_of_next_podcast = 0;
      if (podcasts_in_series.length==1) {
        index_of_prev_podcast = 0;
      }
    }
    var headline_of_prev_podcast = podcasts_in_series[index_of_prev_podcast][2];
    var url_of_prev_podcast = podcasts_in_series[index_of_prev_podcast][1] + details_page_query_string + encodeURIComponent(headline_of_prev_podcast);
    var headline_of_next_podcast = podcasts_in_series[index_of_next_podcast][2];
    var url_of_next_podcast = podcasts_in_series[index_of_next_podcast][1] + details_page_query_string + encodeURIComponent(headline_of_next_podcast);
    var text = domBuilderObj.P('Series: ' + series_name + ' ',
      domBuilderObj.A({href: '../dlls/podcasts/' + url_of_prev_podcast},"<<"),
      ' ' + num_of_current_podcast + ' of ' + num_podcasts_in_series + ' ',
      domBuilderObj.A({href: '../dlls/podcasts/' + url_of_next_podcast},">>")
    );
    $("podcastSeriesLinksDiv").appendChild(text);
  }
}
function podcastSeriesBox(sh_id,domBuilderObj) {
  var $ = YAHOO.util.Dom.get;
  var series_name = sh_id_to_series[sh_id];
  var details_page_query_string = getDetailsPageQueryString(sh_id);
  if (series_name!=null && series_name!=undefined && series_to_podcast_data[series_name].length>1) {
    var podcasts_in_series = series_to_podcast_data[series_name];
    var OLObj = domBuilderObj.OL();
    for (x=0;x<podcasts_in_series.length;x++) {
      var this_sh_id = podcasts_in_series[x][0];
      var url = podcasts_in_series[x][1];
      var headline = podcasts_in_series[x][2];
      if (this_sh_id==sh_id) {
        OLObj.appendChild( domBuilderObj.LI({'style': 'font-weight:bold'}, headline) );
      } else {
        var this_details_page_query_string = details_page_query_string + encodeURIComponent(headline);
        var href = "podcasts/" + url + this_details_page_query_string;
        var AObj = domBuilderObj.A({'href': href},headline);
        var LIObj = domBuilderObj.LI(AObj);
        OLObj.appendChild(LIObj);
      }
    }
    var h3Obj = domBuilderObj.H3("More From This Series");
    var divObj = domBuilderObj.DIV({'class': "linkbox"},h3Obj,OLObj);
    var text = domBuilderObj.DIV({'id': "more-related"},divObj);
    $("podcastSeriesBoxDiv").appendChild(text);
  }
}
YAHOO.util.Event.addListener(window, 'load', function(){
  var domBuilderObj = DomBuilder.apply();
  podcastSeriesBox(sh_id,domBuilderObj);
  addSeriesLinks(sh_id,domBuilderObj);
});
