if ( typeof ads_host == 'undefined' || ads_host.replace(/\s/g,"") == "" ) {
  var ads_host = "http://ads.techbang.com.tw";
}

var ad_list_url = ads_host + "/positions/list.json";
var ad_clicked_url = ads_host + "/advertises/clicked";

$(function() {
  function is_ie6_or_ie7() {
    return $.browser.msie && ( $.browser.version == "6.0" || $.browser.version == "7.0" );
  }
  function show_advertise(advertise) {
    advertise.css("display", get_display_show_value());
  }
  function get_display_show_value() {
    if ( is_ie6_or_ie7() ) {
      return "block";
    } else {
      return "table-cell";
    }
  }
  function add_styles_to_position(position, position_json) {
    position.addClass("advertising");
    var position_size = position_json["position_size"];
    position.width(position_size.width);
    position.height(position_size.height);
    var position_float = position_json["position_float"];
    if ( position_float && position_float != "" ) {
      position.css("float", position_float);
    }
  }
  function add_click_link_to_advertise(advertise, advertise_json) {
    /* Use for advertise clicked count */
    var click_link_tag = '<a href="' + ad_clicked_url + "?id=" + advertise_json.id + '" class="click"></a>';
    advertise.append(click_link_tag);
    /* Using .trigger('click') will not trigger the native click event, have to simulate a default click */
    var advertise_click = advertise.find("a.click");
    advertise_click.bind('click', function(e) {
      e.preventDefault();
      // window.location.href = this.href;
      window.open(this.href);
      return false;
    });
    /* this is for image/text advertise */
    advertise.click(function() {
      advertise_click.trigger('click');
      return false;
    });
    var position = advertise.parent();
    /* this is for flash advertise (only works on safari, firefox) */
    // position.find("object").click(function() {
    //   advertise_click.trigger('click');
    //   return false;
    // });
    position.mousedown(function() {
      if ( $(this).has("object").length == 1 ) {
        advertise_click.trigger('click').unbind("click");
        return false;
      }
    });
  }
  function add_advertise_to_position(position, position_json) {
    var advertises = position_json["advertises"];
    if ( advertises.length == 0 ) {
      position.hide();
    }
    $.each(advertises, function() {
      var advertise_json = this;
      var advertise = $("<div id='advertise_" + this.id + "'class='advertise-item'></div>");
      advertise.attr("style", advertise_json["container_style"]);
      var content = $(advertise_json["content"]);
      advertise.html(content).hide();
      position.append(advertise);

      add_click_link_to_advertise(advertise, advertise_json)
    });
  }
  function step_no_advertise() {}
  function step_advertises() {
    var position = $(this);
    var advertises = position.children(".advertise-item");
    var index = parseInt(position.data("index"), 10);
    var previous_advertise = advertises.eq(index);
    previous_advertise.fadeOut(300, function() {
      index = ( index + 1 ) % advertises.size();
      var next_advertise = advertises.eq(index);
      next_advertise.css("display", get_display_show_value());
      position.data("index", index + "");
    });
  }
  function play_advertise(position) {
    var advertises = position.children(".advertise-item");
    var first_advertise = advertises.eq(0);
    first_advertise.css("display", get_display_show_value());
    position.data("index", "0");
    if ( advertises.size() <= 1 ) {
      position.bind("next", step_no_advertise);
    } else {
      position.bind("next", step_advertises);
    }
  }


  /* playging advertises */
  var position_ids = [];
  $(".advertise").each(function() {
    if ( /^adv-[0-9]+$/.test(this.id) ) {
      var position_id = this.id.split("adv-")[1];
      position_ids.push(position_id);
    }
  });
  if ( window.ad_list_url ) {
    positions_json_callback_url = window.ad_list_url + "?jsoncallback=?";
  }
  $.getJSON(positions_json_callback_url, {
      positions: position_ids.join(",")
    }, function(position_list) {
      $.each(position_list, function() {
        var position_json = this;
        var position = $("#adv-" + position_json.position_id);
        add_styles_to_position(position, position_json);
        add_advertise_to_position(position, position_json);
        play_advertise(position);
      });
    }
  );
  setInterval(function() {
    $(".advertising").trigger("next");
  }, 3000);
});

