document.write('\x3Cscript type="text/javascript" src="http://content.worldnow.com/global/interface/httprequest/httprequest.js">\x3C/script>');
document.write('\x3Cscript type="text/javascript" src="http://dynamic.abc.go.com/streaming/util/remotePlayer?aff=wjrt">\x3C/script>');

// Forms
function wrapForms() {
  $wn(".wnDVQuickForm, .wnDVQuickSignup").each(function () {
    $wn(this).find('h4:first').wrapInner('<div class="wnDSHeader"><h3><span></span></h3></div>').children().unwrap();
  });
}

// Change default branding header image to animated gif
function swapBrandingImg() {
  if ($wn("img#WNBrandingImage").attr("src") == "http://WJRT.images.worldnow.com/images/static/hdr/hdr_branding.jpg") {
    $wn("img#WNBrandingImage").attr("src", "http://ftpcontent.worldnow.com/wjrtftp/custom/2011/hdr_branding.gif");
  }
}

/* COMMON WEATHER */
var wxCache = {};
function fetchWxFeed(station, callback) {
  if (wxCache[station] !== undefined) {
    callback(wxCache[station]);
    return;
  }

  var wxBase = 'http://services.intellicast.com/200904-01/598901895/Weather/Report/{STATION}?filter=1,4';
  var url = wxBase.replace('{STATION}', station);
  WNHttpRequestManager.makeRequest(url, {
    onSuccess: function () {
      wxCache[station] = this.response.responseXML;
      callback(this.response.responseXML);
    },
    onError: function () {}
  });
}

/* WEATHER BRANDING */
function weatherBranding() {
  var wxList = {
    'USMI0295': 'Flint',
    'USMI0739': 'Saginaw',
    'USMI0061': 'Bay City'
  };

  var $div = $wn('<div id="csWxBranding" />');
  $div.append('<h3>Right Now in Mid-Michigan:</h3>');
  $wn('#WNBranding').append($div);

  for (var station in wxList) {
    var $cell = $wn('<div class="csWxBrandingCell" />');
    $cell.append('<h4>' + wxList[station] + ':</h4>');
    $cell.append('<span id="' + station + '" class="csTemp">&nbsp;</span>');
    $div.append($cell);

    (function (id) { //closure
      fetchWxFeed(station, function (xml) {
        var $data = $wn(xml);
        var currentTemp = parseInt($data.find('City:first CurrentObservation').attr('TempF'), 10);
        if (!isNaN(currentTemp)) {
          $wn('#csWxBranding #' + id).html(currentTemp + '&deg;');
        }
      });
    })(station);
  }
}

/* ADD "WATCH ABC SHOWS" BUTTON TO MEMEBER CENTER */
function addWatchLink() {
  $ins = $wn('<div id="WNMemberCenter-watch"><a href="http://www.abc12.com/category/229038/abc-video-player"><img src="http://ftpcontent.worldnow.com/wjrtftp/custom/2011/header_watch.png"></a></div>');
  $wn('#WNSearchBox-headertop').prepend($ins);
}

/* MOVE "MORE" LINKS */
function moveMoreLinks() {
  $wn('#WNDS80 .wnDSContainer-standard, #WNDS27 .wnDSContainer-standard, #WNDS78 .wnDSContainer-standard').each(function () {
    $wn(this).find('a.wnMoreLink').appendTo($wn(this));
  });
}

/* BREAKING NEWS */
function wrapBreakingNews() {
  $wn('#DisplaySizeId-30 ul.wnGroup li.wnItem').each(function () {
    $wn(this).prepend($wn(this).find('div.summaryImage'));
  });

  $wn('#DisplaySizeId-30').prepend('<div class="wnDSHeader"><h3>Breaking News</h3></div>');
}

/* CUSTOM DYNAMIC NAV */
var menuItemBackupImg = "http://WJRT.images.worldnow.com/images/620296_G.jpg";
var dMenuItem = [];
dMenuItem[0] = ["228906", "news", "menu2022_2-C215759"];
dMenuItem[1] = ["228909", "sports", "menu2022_4-C210513"];
dMenuItem[2] = ["228907", "weather", "menu2022_3-C210510"];

function customMenu() {
  var maxTS = 2;
  var maxFH = 5;
  var content = "";
  $wn("#WNCols234-5").prepend("<div id='newMenuOverlay'></div>");
  for (var aa = 0; aa < dMenuItem.length; aa++) {
    var $target = $wn("#" + dMenuItem[aa][2]);
    $wn("#newMenuOverlay").append("<div id='" + dMenuItem[aa][1] + "' class='subSection'></div>");
    $target.attr("rel", aa).addClass("dyn");
    content = $target.html();
    $wn("#newMenuOverlay .subSection:eq(" + aa + ")").attr("rel", aa).html(content);
    if ($wn("#newMenuOverlay .subSection:eq(" + aa + ") .wnLevel2").length) {
      $wn("#newMenuOverlay .subSection:eq(" + aa + ") .wnLevel2").addClass("itemList");
    }
  }

  $wn(".wnMainMenu .wnLevel1:first .dyn").mouseover(function () {
    var temp = $wn(this).attr("rel");
    $wn("#newMenuOverlay .subSection:visible").hide();
    $wn("#newMenuOverlay .subSection:eq(" + temp + ")").show();
    $wn("#newMenuOverlay").show();
  });

  $wn("#newMenuOverlay").mouseleave(function () {
    $wn(this).find(".subSection").hide();
    $wn(this).hide();
  });
  var itemsArray = new Array();
  var totalItems = 7;

  function requestFeed(url, xx) {
    WNHttpRequestManager.makeRequest(url, {
      onSuccess: {
        callback: menuInit,
        args: [xx]
      },
      onError: menuError
    });
  };

  function menuInit(xx) {
    menuFeed(this, xx);
  }

  function menuFeed(xml, xx) {
    var fullFeed = xml.response.responseXML;
    if ($wn(fullFeed).find("features").children()) {
      var tempHeight = 0;
      var $foundTS = $wn(fullFeed).find("[displaysize='-20']");
      var $foundFH = $wn(fullFeed).find("[displaysize='-7']");
      if ($foundTS.length) {
        var tBox = "<ul id='tsList' class='itemList ts'>";
        $foundTS = $wn(fullFeed).find("[displaysize='-20']:lt(" + maxTS + ")");
        $foundTS.each(function () {
          var type = $wn(this).find("type:first").text();
          if (type == "C") {
            type = "category";
          } else if (type == "S") {
            type = "story";
          } else if (type == "V") {
            type = "clip";
          }
          var id = $wn(this).find("id:first").text();
          var headline = $wn(this).find("headline:first").text();
          if ($wn(this).find("abstractimage").length) {
            var summaryImage = $wn(this).find("abstractimage:first filename").text();
          } else {
            var summaryImage = menuItemBackupImg;
          }
          var pageURL = $wn(this).find("seo:first pageurl").text();
          var fullURL = "/" + type + "/" + id + "/" + pageURL;
          if (type == "clip") {
            tBox += "<li class='item'><a href='" + fullURL + "'><img src='" + summaryImage + "' /><div class='headline video'>" + headline + "</div><div style='clear:both;'></div></a></li>";
          } else {
            tBox += "<li class='item'><a href='" + fullURL + "'><img src='" + summaryImage + "' /><div class='headline'>" + headline + "</div><div style='clear:both;'></div></a></li>";
          }
        });
        tBox += "</ul>";
        $wn("#" + xx).append(tBox);
      }
      if ($foundFH.length) {
        var tBox = "<ul id='fhList' class='itemList fh'><li class='ribbon'>Top Stories</li>";
        $foundFH = $wn(fullFeed).find("[displaysize='-7']:lt(" + maxFH + ")");
        $foundFH.each(function () {
          var type = $wn(this).find("type:first").text();
          if (type == "C") {
            type = "category";
          } else if (type == "S") {
            type = "story";
          } else if (type == "V") {
            type = "clip";
          }
          var id = $wn(this).find("id:first").text();
          var headline = $wn(this).find("headline:first").text();
          var pageURL = $wn(this).find("seo:first pageurl").text();
          var fullURL = "/" + type + "/" + id + "/" + pageURL;
          if (type == "clip") {
            tBox += "<li class='item'><a class='video' href='" + fullURL + "'>" + headline + "</a></li>";
          } else {
            tBox += "<li class='item'><a href='" + fullURL + "'>" + headline + "</a></li>";
          }
        });
        tBox += "</ul>";
        $wn("#" + xx).append(tBox);
      } else {
        $wn("#" + xx).find(".itemList.ts").addClass("noBorder");
      }
      $wn("#" + xx).append("<div style='clear:both;'></div>");
      $wn("#" + xx).show();
      $wn("#" + xx).find(".itemList").show().each(function () {
        if ($wn(this).height() > tempHeight) {
          tempHeight = $wn(this).height();
        }
      });
      $wn("#" + xx).find(".itemList").height(tempHeight);
      $wn("#" + xx).hide();
      $wn("#" + xx).find(".itemList").show().each(function () {
        if ($wn(this).height() > tempHeight) {
          tempHeight = $wn(this).height();
        }
      });
      $wn("#" + xx).find(".itemList").height(tempHeight);

    }
  }

  function menuError() {}

  for (var loadItem = 0; loadItem < dMenuItem.length; loadItem++) {
    requestFeed("http://api.worldnow.com/feed/v2.0/categories/" + dMenuItem[loadItem][0] + "/all?alt=xml", dMenuItem[loadItem][1]);
  }
}

/* DYNAMIC STATIC NAV */
var navTx;

function wnNavReset() {
  clearTimeout(navTx);
  $wn(".wnLevel2:visible").hide();
  $wn(".cur").show();
}

function CSlaunchDynamicNav() {
  var h = 26;
  var t = 38;
  $wn(".wnMainMenu .wnHorizontal .wnStatic .wnSublevel").css({
    "z-index": "1000"
  });

  $wn(".wnStatic .wnLevel1 li:first").attr("rel", "1");
  $wn(".wnStatic .wnLevel1 li:first").siblings("li").each(function () {
    $wn(this).attr("rel", "1");
    if (!$wn(this).find("ul").length) {
      $wn(this).append("<ul class='wnLevel2 wnSublevel' style='height: " + h + "px; top: " + t + "px; display: none;'><li class='wnFirstNode wnOn'><span><a href=''>&nbsp;</a></span></li></ul>");
    }
  });
  $wn(".wnStatic .wnLevel2 li:first").attr("rel", "2");
  $wn(".wnStatic .wnLevel2 li:first").siblings("li").each(function () {
    $wn(this).attr("rel", "2");
  });

  var subnavPos = $(".wnMenuSpacer").offset();
  if ($wn(".wnLevel2:visible").attr("class") != null) {
    //IF A SUB NAV IS PRESENT FOR THE CURRENT CATEGORY
    $wn(".wnLevel2:visible").addClass("cur");
    $wn(".wnLevel2").css({
      "height": h + "px",
      "top": t + "px"
    });
  } else {
    //IF A SUB NAV IS NOT PRESENT FOR THE CURRENT CATEGORY
    $wn("#WNMenuCol1 .wnMenuSpacer").height(h);
    $wn(".wnLevel2").css({
      "height": h + "px",
      "top": subnavPos.top + "px",
      "left": subnavPos.left + "px",
      "width": ($wn(".wnMenuSpacer").width() - 2) + "px",
      "overflow": "hidden"
    });
  }
  //$wn("ul.wnLevel1 li.wnFirstNode .wnLeft").css("padding-left","3px");
  $wn("ul.wnLevel1 li[rel='1']").bind("mouseenter", function () {
    clearTimeout(navTx);
    $wn(".wnLevel2:visible").hide();
    $wn(this).find(".wnLevel2").addClass("wnOn").css({
      "display": "block"
    });
  });
  $wn("ul.wnLevel1 li[rel='2']").bind("mouseenter", function () {
    clearTimeout(navTx);
  });

  $wn("#WNMenuCol1").bind("mouseleave", function () {
    clearTimeout(navTx);
    navTx = setTimeout(function () {
      wnNavReset();
    }, 500);
  });
}

$wn(document).ready(function() {
  wrapForms();
  swapBrandingImg();
  weatherBranding();
  addWatchLink();
  moveMoreLinks();
  wrapBreakingNews();
  //customMenu();
  CSlaunchDynamicNav();
});

/* AD MANIPULATION */
var wng_pageInfo = wng_pageInfo || {};
$wn(window).load(function () {
  var callLetter = (wng_pageInfo.affiliateName || '').toLowerCase();
  var cc = (wng_pageInfo.contentClassification || '').toLowerCase();
  var ord = Math.random() * 10000000000000000;
  if ($wn("#WNAd41").length > 0) {
    $wn("#WNAd41").append("<iframe width='224' height='90' scrolling='no' frameborder='0' marginheight='0' marginwidth='0' src='http://ad.doubleclick.net/adi/wn.loc." + callLetter + "/" + cc + ";sz=234x60;ord=" + ord + "?' id='leaderboardSide'></iframe>");
  } else if ($wn("#WNAd46").length > 0) {
    $wn("#WNAd46").append("<iframe width='224' height='90' scrolling='no' frameborder='0' marginheight='0' marginwidth='0' src='http://ad.doubleclick.net/adi/wn.loc." + callLetter + "/" + cc + ";sz=234x60;ord=" + ord + "?' id='leaderboardSide'></iframe>");
  }
});

/* ROUNDED CORNERS (FOR OLD IE) */
if ($wn.browser.msie && parseInt($wn.browser.version) < 9) {
  document.write('\x3Cscript type="text/javascript" src="http://ftpcontent.worldnow.com/revenue/js/PIE.js">\x3C/script>');

  var installPIE = function() {
    if (window.PIE) {
      $wn('.wnDSContainer-standard, #WNStoryAssociatedLinks, .wnBlock, .wnDSHeader, .wnBlock .header, #WNAd44, #WNAd44 td')
      .css('position', 'relative').each(function () {
        PIE.attach(this);
      });
    }
  };

  $wn(document).ready(installPIE);
  $wn(window).load(installPIE);
}
