/*
  インタレストマッチ対応版

  loadScript にあるエンコード指定を調整すること。
 */
var _OvertureIM = function() {
  var SourceID_ = 'gmo_jp_kabegami_shashinbu_im'; //'gmo_jp_kabe_cat_ctxt';
  var imCount_  = 5;

  var timing_ = 1*1000;
  var retry_  = 10;
  var indexLists_ = {};
  var fDraw_ = false;

  function  getOvertureString( no ) {
    var i = no * 6;
  	if( i>=6 && i+6 <= zSr.length ) {
      return  { descr: zSr[ i+0 ], unused1: zSr[ i+1 ], clickURL: zSr[ i+2 ], title: zSr[ i+3 ], sitehost: zSr[ i+4 ], unused2: zSr[ i+5 ] };
    }
    return  null;
  }

  /**
   *  個別に表示を行う
   */
  function showOverture( vals, drawFunc ) {
    var divId       = vals.id;
    var endIndex    = vals.end;
    var no;

    if( typeof( divId )=='string' ) {
      divId = document.getElementById( divId );
    }

  	var strOutputSource='';
    for( no=vals.start; no<=endIndex; ++no ) {
  		var ov  = getOvertureString( no );
      if( ov!=null )  {
        strOutputSource += drawFunc( ov );
      }
  	}
  	divId.innerHTML=strOutputSource;
  }
  /**
   *  全てを表示する
   */
  function showALL() {
    for( var idx in indexLists_ ) {
      var list = indexLists_[ idx ];
      if( list.index && list.drawFunc ) {
        var index = list.index;
        var drawFunc = list.drawFunc;

        var cnt = 0;
        var size = index.length;
        while( cnt<size ) {
          showOverture( index[ cnt ], drawFunc );
          ++cnt;
        }
      }
    }
    fDraw_ = true;
  }

  function retryLoadEnd() {
    --retry_;
    if( retry_>0 )  {
      setTimeout( '_OvertureIM.LoadEnd();', timing_ );
    }
  }

  /**
   *  Overture読み込み
   *
   *  IE6ではスコープの問題があるらしく、thisあたりを同一にしていないと<script>が変なところで読み込まれるっぽい。
   *  @params category_id ページ(カテゴリID)
   *  @params keyword キーワード文字列。
   */
  function loadScript( category_id, keyword ) {
    /*
      設定部
        SourceID        指定されたSource ID
        type            識別ID
        ctxtKeywords    キーワード文字列。複数指定時は','区切り。
        ctxtId          ページカテゴリID
        keywordCharEnc  キーワードの文字エンコード。（基本utf8）
        outputCharEnc   出力文字エンコード。'shiftjis','euc-jp','utf8'のいずれか。
    */
    var type          = null;                           //'im_shashin-bu';
    var ctxtKeywords  = keyword;
    var ctxtId        = category_id;

    var keywordCharEnc  = 'utf8';
    var outputCharEnc   = 'utf8';

    var url = 'http://im.gmo.ov.yahoo.co.jp/js_flat/?source=' + SourceID_;
    if( type!=null ) {
      url += '&type=' + type;
    }
    if( ctxtKeywords!=null ) {
      url += '&ctxtKeywords=' + encodeURIComponent( ctxtKeywords );
    }
    if( ctxtId!=null ) {
      url += '&ctxtId=' + ctxtId;
    }
    if( document.URL!=null ) {
      url += '&ctxtUrl=' + encodeURIComponent( document.URL );
    }
    url += '&keywordCharEnc=' + keywordCharEnc + '&outputCharEnc=' + outputCharEnc + '&maxCount=' + imCount_;

    var isMSIE  = false;
    /*@cc_on
      isMSIE = true;
    @*/
    var scr = document.createElement( 'script' );
    scr.type = 'text/javascript';
    scr.language = 'javascript';
    scr.src = url;
    if( isMSIE ) {
      scr.onreadystatechange = function() {
        if( scr.readyState=='complete' || scr.readyState=='loaded' ) {
          _OvertureIM.LoadEnd();
        }
      };
    }
    else {
      scr.onload = _OvertureIM.LoadEnd;
    }
    document.body.appendChild( scr );
    //document.write( '<scr' + 'ipt language="javascript" type="text/javascript" src="' + url + '"></scr' + 'ipt>' );
    //document.write( '<scr' + 'ipt language="javascript" type="text/javascript">_OvertureIM.LoadEnd();</scr' + 'ipt>' );
  }

  //==============================================================================

  /**
   *  描画登録、ないしは描画
   */
  function pushWriteList( type, drawFunc, divId, startIndex, endIndex ) {
      if( startIndex>0 && startIndex<endIndex ) { //0番はない
        var obj = { id:divId, start:startIndex, end:endIndex };
        if( fDraw_ ) {  //描画済みの場合、描画
          showOverture( obj, drawFunc );
        }
        else {          //未描画の場合、リストに保存
          if( indexLists_[ type ]==null ) {
            indexLists_[ type ] = { index: [], drawFunc: drawFunc };
          }
          indexLists_[ type ].index.push( obj );
        }
      }
  }

  /**
   *  HTML化関数
   */
  function getDrawFunc_index( div_class ) { //1,2共用
    return function( ov ) {
      var html  = '<div class="' + div_class + '">'
                + '<a href="' + ov.clickURL + '" target="_blank">'
                + '<span class="ov_im_title">' + ov.title + '</span><br>'
                + '<span class="ov_im_url">' + ov.sitehost + '</span><br>'
                + '<span class="ov_im_explain">' + ov.descr + '</span></a>'
                + '</div>';
      return html;
    }
  }
  function drawFunc_index3( ov ) {
    var html  = '<div class="ov_im_left">'
              + '<a href="' + ov.clickURL + '" target="_blank">'
              + '<span class="ov_im_title">' + ov.title + '</span>&nbsp;'
              + '<span class="ov_im_url">' + ov.sitehost + '</span><br>'
              + '<span class="ov_im_explain">' + ov.descr + '</span></a>'
              + '</div>';
    return html;
  }

  //==============================================================================

  return {
    LoadEnd: function() {
      if( typeof( zSr )=='undefined' ) {  //リトライ
        //alert( 'no show' );
        retryLoadEnd();
      }
      else {                              //広告表示
        //alert( 'show' );
        showALL();
      }
    },
    /* */
    SetWriteList1: function( divId, startIndex, endIndex )  {
      pushWriteList( 'w1', getDrawFunc_index( 'ov_im_right' ), divId, startIndex, endIndex );
    },
    SetWriteList2: function( divId, startIndex, endIndex )  {
      pushWriteList( 'w2', getDrawFunc_index( 'ov_im' ), divId, startIndex, endIndex );
    },
    SetWriteList3: function( divId, startIndex, endIndex )  {
      pushWriteList( 'w3', drawFunc_index3, divId, startIndex, endIndex );
    },
    /* */
    SetMaxCount: function( cnt ) {
      imCounr_ = cnt;
    },
    /* */
    LoadListings: loadScript
  }
}();

//--------------------------------------------------------------------------------

//Overture(縦)
var WriteListingsWithIndex = _OvertureIM.SetWriteList1;
//Overture(横)
var WriteListingsWithIndex2 = _OvertureIM.SetWriteList2;
//Overture(横520px)
var WriteListingsWithIndex3 = _OvertureIM.SetWriteList3;

var LoadListings = _OvertureIM.LoadListings;
