
var adGroupInfoMap = {};

$(function() {
  var adRotationMs = 15000;
  var initAdGroupInfo = function(htmlContentMap) {
    $.each(htmlContentMap, function(groupId, contentAr) {
      var groupInfo = {};
      groupInfo.groupId = groupId;
      groupInfo.adContentAr = contentAr;
      groupInfo.placeholder$ = $(".ad_placeholder[rel=" + groupId + "]");
      groupInfo.adWrapper$ = $(".ad_wrapper", groupInfo.placeholder$);
      groupInfo.buttonWrapper$ = $(".ad_buttons", groupInfo.placeholder$);
      groupInfo.isNavigable = groupInfo.placeholder$.hasClass("navigable");
      groupInfo.adCount = groupInfo.adContentAr.length;
      groupInfo.disableRotation = groupInfo.adCount <= 1;
      groupInfo.resumeRotation = new Date();
      adGroupInfoMap[groupId] = groupInfo;
    });
  };

  var showAd = function(groupInfo, index) {
    if (groupInfo.adCount > 0) {
      if (groupInfo.adWrapper$.html().length > 0) {
        groupInfo.adWrapper$.fadeOut(100, function() { showAd_Post(groupInfo, index) });
      }
      else {
        showAd_Post(groupInfo, index);
      }
    }
  };

  var showAd_Post = function(groupInfo, index) {
    if (groupInfo.adCount > 0) {
      groupInfo.adWrapper$.fadeIn(500);
      groupInfo.adWrapper$.html(groupInfo.adContentAr[index]);
      groupInfo.activeAdIndex = index;
      groupInfo.adAnchor$ = $("a", groupInfo.adWrapper$);
      groupInfo.adId = (groupInfo.adAnchor$.length > 0) ? groupInfo.adAnchor$[0].id : -1;
      $("a.active", groupInfo.buttonWrapper$).removeClass("active");
      $($("a", groupInfo.buttonWrapper$)[index]).addClass("active");
      var fixedHeight = groupInfo.adWrapper$.height();
//      if (fixedHeight > 0) {
//        groupInfo.adWrapper$.css("height", fixedHeight);
//      }
    }
  };

  var showNextAd = function(groupInfo) {
    var nextAdIndex = groupInfo.activeAdIndex + 1;
    if (nextAdIndex >= groupInfo.adCount) {
      nextAdIndex = 0;
    }
    showAd(groupInfo, nextAdIndex);
  };


  var initAdButtons = function(groupInfo) {
    var groupInfo = groupInfo;
    if (groupInfo.isNavigable) {
      $("a", groupInfo.buttonWrapper$).each(function(index) {
        var this$ = $(this);
        if (index + 1 > groupInfo.adCount) {
          this$.remove();
        }
      });
      $(".ad_buttons", groupInfo.placeholder$).show();
    }
  };

  var findGroup = function(elem$) {
    return adGroupInfoMap[elem$.parents(".ad_placeholder").attr("rel")];
  }

  var enableRotation = function() {
    findGroup($(this)).disableRotation = false;
  };

  var adsRequested = [];
  var adPlaceholders = $(".ad_placeholder");
  adPlaceholders.each(function() {
    var ad = $(this).attr("rel");
    if (ad != undefined && ad.length > 0) {
      adsRequested.push(ad);
    }
  });
  if (adsRequested.length > 0) {
    $.ajax({
      type: "POST",
      url: UComWebApp.getServerPath() + "Home/GetAllAds_json",
      dataType: "json",
      data: { adsRequested: adsRequested },
      success: function(data) {
        $(".ad_wrapper").css("visibility", "visible" ); // because initially hidden
        initAdGroupInfo(data);
        $.each(adGroupInfoMap, function(groupId, groupInfo) {
          showAd(groupInfo, 0)
          initAdButtons(groupInfo);
        });

        $(".ad_buttons a").hoverIntent(
            function() {
              var this$ = $(this);
              var groupInfo = findGroup(this$);
              showAd(groupInfo, parseInt(this$.text()) - 1);
              groupInfo.disableRotation = true;
              window.enableAdRotation = function() { };
            },
            enableRotation
          );
        $(".ad_wrapper").hoverIntent(
          function() {
            findGroup($(this)).disableRotation = true;
          },
          enableRotation
        );

        $(".ad_wrapper a, .ad_placeholder.navigable .ad_buttons").click(function() {
          ucomAdClick(findGroup($(this)).adId);
          return false;
        });

      } // end success: function(data)
    }); // end ajax
  } // end if

  window.adTimer = function() {
    $.each(adGroupInfoMap, function(groupId, groupInfo) {
      if (!groupInfo.disableRotation) {
        showNextAd(groupInfo);
      }
    });
  };
  setInterval(window.adTimer, adRotationMs);


});         //end $(function()
var currentAdIndex = 0;

function ucomAdClick(elementId) {
  var $target = $("a#" + elementId);
  if ($target.length == 1) {
    window.open($target.attr("href"), $target.attr("target"));
  }  
}

function ucomFlashAdClick(elementId) {
  ucomAdClick(elementId);
}


