function adsBySeesaa () {
  this.onload_prehandler = window.onload;
  this.ad_links  = new Array();
  this.ad_data   = new Array();
  this.k_id_list = new Array();
  this.ad_layers = new Array();
  this.ad_data_load_finished = 0;
  this.script_path = 'http://match.seesaa.jp';
  this.hostsite_id = 11;
  this.site_id     = '';
}

adsBySeesaa.prototype.getAdLinks = function () {
  var i;
  var links = document.links;
  var links_len = links.length;
  var k_id_hash = new Array();
  for ( i = 0 ; i < links_len ; i++ ) {
    var link = links[i];
    if ( link.href.match(/^http:\/\/www\.seesaa\.jp\/afr\.pl\?affiliate_id=(\d+)&keyword=(.+)$/) ) {
          this.ad_links.push(link);
      this.ad_a_id = RegExp.$1;
      link.ad_k_id = RegExp.$2;
      link.ad_l_id = this.ad_links.length;
      if ( ! k_id_hash[link.ad_k_id] ) {
	k_id_hash[link.ad_k_id] = 1;
	this.k_id_list.push(link.ad_k_id);
      }
    }
  }
  return this.ad_links.length;
}

adsBySeesaa.prototype.createPopupAd = function(name, parent_element) {
  var lh = createLayer(name);

  lh.bgColor('transparent');
  //lh.bgColor('#000');
  lh.parentElement = parent_element;
  
  if ( lh.srcElement.style ) {
    lh.srcElement.style.color      = '#fff';
    lh.srcElement.style.fontFamily = '"MS UI Gothic", "Osaka", "sans-serif"';
    lh.srcElement.style.fontSize   = '11px';
    lh.srcElement.style.padding    = '1px';
    lh.srcElement.style.textAlign  = 'left';
    //    lh.srcElement.style.width      = '500px';
  }

  lh.showByEvent = function (e) {
    var pos = YAHOO.util.Dom.getXY(this.parentElement);
    var fix = YAHOO.util.Event.isIE ? 2 : 0;
    this.moveTo(pos[0] - fix, pos[1] - 95 - fix);
    this.show();
  }

  lh.nowLoading = function(e) {
    var loading_img = ads_by_seesaa.script_path + '/img/cmn/loading.gif';
    var ads = [ { disp_html:'<div style="text-align:center;"><img src="' + loading_img + '" border="0"/></div>', width:50, height:0 } ];
    this.showAds(ads);
  }

  lh.showAds = function(ads){
    var ad = ads[0];
    var color_sets = new Array([ 'purple', 'ab00ff' ],
			       //[ 'orange', 'ff9900' ],
			       //[ 'gray'  , '999999' ],
			       [ 'blue'  , '0000ff' ],
			       [ 'black' , '000000' ],
			       [ 'red'   , 'e50019' ],
			       [ 'green' , '05d800' ]);
    
    this.border_color = this.border_color || color_sets[Math.floor(Math.random() * color_sets.length)];
    var border_path  = ads_by_seesaa.script_path + '/img/cmn/border/' + this.border_color[0] + '/';

    var fix_top = 10;
    var fix_main = YAHOO.util.Event.isIE == 6 ? 22 : 20;  // ie6 22, other 20;

    var ad_html = ad.disp_html.replace('__TITLE_COLOR__', this.border_color[1]);
    ad_html = ad_html.replace('__SITEURL_COLOR__', this.border_color[1]);

    var html = '<div style="float:left;_margin-top:-1px;"><img src="' + border_path + 'top_left.gif' + '" /></div>'
    + '<div style="float:left;_margin-top:-1px;"><img src="' + border_path + 'top.gif" width="' + (ad.width + fix_top) + '" height="7" /></div>'
    + '<div style="float:left;_margin-top:-1px;"><img src="' + border_path + 'top_right.gif" /></div>'
    + '<div style="clear:left;"></div>'
    + '<div style="padding-top:1px;border-right:solid 1px #' + this.border_color[1] + ';border-left:solid 1px #' + this.border_color[1] + ';float:left;width:' + (ad.width + fix_main) + 'px;background:#fff;_margin-top:-1px;"><a href="' + ad.clickurl + '" target="_blank" onmouseover="window.status=\'' + ad.siteurl + '\';return true;" onmouseout="window.status=\'\'; return true;">' + ad_html + '</a></div>'
    + '<div style="clear:left;"></div>'
    + '<div style="border-right:solid 1px #' + this.border_color[1] + ';border-left:solid 1px #' + this.border_color[1] + ';float:left;width:' + (ad.width + fix_main) + 'px;background:#fff;color:#8e8e8e;text-align:right;_margin-top:-1px;"><a href="http://seesaa.jp" target="_blank" style="color:#8e8e8e;">ads by Seesaa</a>&nbsp;&nbsp;&nbsp;</div>'
    + '<div style="clear:left;"></div>'
    + '<div style="float:left;_margin-top:-1px;"><img src="' + border_path + 'bottom_left.gif" /></div>'
    + '<div style="float:left;_margin-top:-1px;"><img src="' + border_path + 'yaji.gif" /></div>'
    + '<div style="float:left;_margin-top:-1px;"><img src="' + border_path + 'bottom.gif" height="24" width="' + ad.width + '" /></div>'
    + '<div style="float:left;_margin-top:-1px;"><img src="' + border_path + 'bottom_right.gif" /></div>'
    + '<div style="clear:left;"></div>'
    + '<div style="float:left;"><img src="' + ads_by_seesaa.script_path + '/img/cmn/spacer.gif" width ="' + ad.width + '"height="10"></div>'
    + '<div style="clear:left;"></div>';
    
    this.srcElement.style.width = ad.width + fix_main + fix_top;
    this.innerHTML(html);
  }
  return lh;
}

adsBySeesaa.prototype.setEventOfAdLinks = function () {
  var i, count_links = this.ad_links.length;

  var mouse_over = function (e) {
    if ( ! this.layer ) {
      
      var ad_data = ads_by_seesaa.ad_data[this.ad_k_id];
      if ( ! ad_data || ad_data == 0) { return false; }
      var layer_name = 'ads_by_seesaa_layer_' + this.ad_l_id;

      this.layer = ads_by_seesaa.createPopupAd(layer_name, this);

      var mouse_over = function (e){
	var lh = new layerHandle(this.id);
	lh.show();
	if(lh.srcElement.hideTimer){
	  clearTimeout(lh.srcElement.hideTimer);
	  lh.srcElement.hideTimer = null;
	}
      }
      var mouse_move = function (e) {
	new layerHandle(this.id).show();
      }
      var mouse_out = function (e) {
	var timer = setTimeout('new layerHandle("' + this.id + '").hide()', 300);
	layerHandle(this.id).srcElement.hideTimer = timer;
      }
      YAHOO.util.Event.addListener(this.layer.srcElement, 'mouseover', mouse_over, true);
      YAHOO.util.Event.addListener(this.layer.srcElement, 'mousemove', mouse_move, true);
      YAHOO.util.Event.addListener(this.layer.srcElement, 'mouseout',  mouse_out,  true);

      this.removeAttribute('href');
      var link_click = function(e){ return false }
      YAHOO.util.Event.addListener(this, 'click', link_click, true);

      ads_by_seesaa.ad_layers.push(this.layer);
    }
    if(! this.layer.finishLoading){
      this.layer.nowLoading();
    
      var handleSuccess = function(o){
	var data = eval("("+ o.responseText +")");
	var ele   = o.argument.ele;
	var layer = ele.layer;
	if(data.ads.length){
	  layer.showAds(data.ads);
	}
	else {
	  layer.hide();
	  YAHOO.util.Event.removeListener(ele, 'mouseover');
	}
	layer.finishLoading  = true;
      }
      
      var handleFailure = function(o){
	var ele   = o.argument.ele;
	var layer = ele.layer;
	layer.hide();
	YAHOO.util.Event.removeListener(ele, 'mouseover');
	layer.finishLoading  = true;
      }

      var callback = { success:handleSuccess,
		       failure:handleFailure,
		       argument:{ ele:this } };

      var sUrl = '/pages/user/floating_ad.pl?hid=' + ads_by_seesaa.hostsite_id + '&sid=' + ads_by_seesaa.site_id + '&k=' + this.ad_k_id + '&referer=' + encodeURIComponent(document.location.href) + '&__time=' + (new Date()).getTime();
      var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback);
    }
    this.layer.showByEvent(e);
  }

  for ( i = 0 ; i < count_links ; i++ ) {
    YAHOO.util.Event.addListener(this.ad_links[i], 'mouseover', mouse_over, true);
  }
}

adsBySeesaa.prototype.scriptDynamicLoad = function () {
  var i, load_script = new Array('/js/yui/yahoo-dom-event-connection.js');

  for ( i = 0 ; i < load_script.length; i++ )
  load_script[i] = this.script_path + load_script[i];

  var ad_data_url = '';
  var u_len = 0;
  for ( i = 0 ; i < this.k_id_list.length ; i++ ) {
    if ( u_len > 1024 ) {
      load_script.push(ad_data_url);
      ad_data_url = this.script_path + '/floating_start.pl?aid=' + this.ad_a_id + '&hid=' + this.hostsite_id + '&url=' + encodeURIComponent(document.location.href) + '&__time=' + (new Date()).getTime();
    }
    else if ( i == 0 ) {
      ad_data_url = this.script_path + '/floating_start.pl?aid=' + this.ad_a_id + '&hid=' + this.hostsite_id + '&url=' + encodeURIComponent(document.location.href) + '&__time=' + (new Date()).getTime();
    }
    ad_data_url += '&k=' + this.k_id_list[i];
    u_len = ad_data_url.length;
  }
  load_script.push(ad_data_url);

  for ( i = 0 ; i < load_script.length ; i++ ) {
    var dynamic_load_script = document.createElement('script');
    dynamic_load_script.src = load_script[i];
    dynamic_load_script.charset = 'shift_jis';
    document.body.appendChild(dynamic_load_script);
  }

  this.checkTimer = null;
  this.checkTimeoutCount = 0;
  this.loadCheck = function() {
    this.checkTimeoutCount++;
    if((typeof YAHOO !== "undefined" && this.ad_data_load_finished == 1) || this.checkTimeoutCount > 20){
      clearInterval(this.checkTimer);
      this.checkTimer = null;
      this.setEventOfAdLinks();
    }
  };
  this.startLoadCheck = function (){
    if(!this.checkTimer) {
      this.checkTimer = setInterval("ads_by_seesaa.loadCheck();", 500);
    }
  }
  this.startLoadCheck();
}

adsBySeesaa.prototype.onLoad = function () {
  if ( this.onload_prehandler )
  this.onload_prehandler();

  if (this.getAdLinks() <= 0 ) return false;
  this.scriptDynamicLoad();
}

ads_by_seesaa = new adsBySeesaa();
window.onload = function () { ads_by_seesaa.onLoad(); }

function layerHandle (layer) {
  if ( typeof(layer) == 'undefined' ) {
    return this;
  }
  else if ( typeof(layer) == 'string' ) {
    if ( document.getElementById ) {
      layer = document.getElementById(layer);
    }
    else if ( document.all ) {
      layer = document.all(layer);
    }
    else if ( document.layers ) {
      layer = document.layers[layer];
    }
  }
  this.srcElement = layer;
  return this;
}


layerHandle.prototype.show = function () {
  var e = this.srcElement;
  if ( e.style && e.style.display && e.style.display == 'none' ) {
    e.style.display = 'block';
  }
  else if ( e.visibility && e.visibility == 'hide' ) {
    e.visibility = 'show';
  }
}

layerHandle.prototype.hide = function () {
  var e = this.srcElement;
  if ( e.style && e.style.display && e.style.display == 'block' ) {
    e.style.display = 'none';
  }
  else if ( e.visibility && e.visibility.show ) {
    e.visibility = 'hide';
  }
}

layerHandle.prototype.bgColor = function (color) {
  if ( color ) {
    if ( document.getElementById || document.all ) {
      this.srcElement.style.backgroundColor = color;
    }
    else if ( document.layers ) {
      this.srcElement.bgColor = color;
    }
  }
  if ( document.getElementById || document.all ) {
    return this.srcElement.style.backgroundColor;
  }
  else if ( document.layers ) {
    return this.srcElement.bgColor;
  }
}


layerHandle.prototype.left = function (x) {
  if ( typeof(x) == 'number' ) {
    if ( document.getElementById ) {
      this.srcElement.style.left = x + 'px';
    }
    else if ( document.all ) {
      this.srcElement.style.pixelLeft = x;
    }
    else if ( document.layers ) {
      this.srcElement.left = x;
    }
  }
  if ( document.getElementById ) {
    return parseInt(this.srcElement.style.left);
  }
  else if ( document.all ) {
    return this.srcElement.style.pixelLeft;
  }
  else if ( document.layers ) {
    return this.srcElement.left;
  }
}


layerHandle.prototype.top = function (y) {
  if ( typeof(y) == 'number' ) {
    if ( document.getElementById ) {
      this.srcElement.style.top = y + 'px';
    }
    else if ( document.all ) {
      this.srcElement.style.pixelTop = y;
    }
    else if ( document.layers ) {
      this.srcElement.top = y;
    }
  }
  if ( document.getElementById ) {
    return parseInt(this.srcElement.style.top);
  }
  else if ( document.all ) {
    return this.srcElement.style.pixelTop;
  }
  else if ( document.layers ) {
    return this.srcElement.top;
  }
}


layerHandle.prototype.moveTo = function (x, y) {
  this.left(x);
  this.top(y);
}


layerHandle.prototype.alpha = function (value) {
  var ci = new clientInfo();
  if ( ci.isIE() ) {
    this.srcElement.style.filter = "alpha(opacity=0)";
    this.srcElement.filters.alpha.opacity = value;
  }
  else if ( ci.isGecko() ) {
    this.srcElement.style.MozOpacity = value / 100;
  }
}


layerHandle.prototype.zIndex = function (value) {
  if ( document.layers ) {
    this.srcElement.zIndex = value;
  }
  else {
    this.srcElement.style.zIndex = value;
  }
}


layerHandle.prototype.innerHTML = function (html) {
  if ( document.layers ) {
    with ( this.srcElement.document ) {
      open();
      write(html);
      close();
    }
  }
  else {
    this.srcElement.innerHTML = html;
  }
}


function createLayer (name) {
  if ( document.getElementById ) {
    var layer = document.createElement('div');
    layer.setAttribute('id', name);
    layer.style.display         = 'none';
    layer.style.position        = 'absolute';
    layer.style.left            = '0px';
    layer.style.top             = '0px';
    layer.style.zIndex          = 0;
    document.body.appendChild(layer);
    return new layerHandle(name);
  }
  else if ( document.all ) {
    var str =
      '<div id="' + name + '" style="' +
      'display:none; position:absolute; left:0px; top:0px;"></div>';
    document.body.insertAdjacentHTML('BeforeEnd',str);
    return new layerHandle(name);
  }
  else if ( document.layers ) {
    var layer = new Layer(65535);
    var lh = new layerHandle(layer.name);
    lh.hide();
    lh.moveTo(0, 0);
    return lh;
  }
  return null;
}

