/*@cc_on _d=document;eval('var document=_d')@*/

var ads_node = new Array(); //Node
var ads_hs_node = new Array(); //Node

/*
 * 広告を表示します 
 * <adv:ov 
 *   count="3" 
 *   type="2" 
 *   area="right" 
 *   rowcss="interestmatch" 
 *   titleclass="ov_title" 
 *   descclass="ov_desc"
 *   hostclass="host" 
 *   isfooter="1" 
 *   />
 * do_adv_parsing を呼んだ後読んでください。
 *
*/
function adv_write(){
  
  //////// ---------IM / SS ------ ////////

  if(typeof(zSr) == "undefined"){
    return ;
  }
  zSr.splice(0,6);
  
  
  //click パラメータ
  var paths = location.pathname.split("/");
  var path  = (paths.length > 1 ? paths[1] : "top");
  if (location.pathname.indexOf("search") == 0){
    path = "search";
  }
  var oreg  = new RegExp("^[a-z]+$");
  if (!path.match(oreg)){
    path = (paths.length > 2 ? paths[2] : "top");
  }
  if (path.indexOf('%') != -1){
    path = "local";
  }
  
  
  for (var i = ads_node.length -1 ; i >= 0 ; i-- ) {
    var html = '';
    
    var type        = ads_node[i].attr('type');         //表示タイプ
    var title_class = ads_node[i].attr('titleclass');   //タイトルクラス名
    var all_class   = ads_node[i].attr('rowcss');       //行ごとのクラス名
    var desc_class  = ads_node[i].attr('descclass');    //詳細のクラス名
    var host_class  = ads_node[i].attr('hostclass');    //ホストのクラス名
    var area        = ads_node[i].attr('area');         //Clickパラメタ
    var isfooter    = ads_node[i].attr('isfooter');     //インタレストマッチのフッタの有無

    var hit = 0;
    for (var k = ads_node.length -1 ; k >= 0 ; k-- ){
      if (area == ads_node[k].attr('area')){
        hit++;
      }
    }
    if(hit > 1){
      hit = 0;
      for (var k = ads_node.length -1 ; k >= i ; k-- ){
        if (area == ads_node[k].attr('area')){
          hit++;
        }
      }
      area = area + "-" + hit;
    }

    for (var count = ads_node[i].attr('count') -1 ; count >= 0 ; count--){
      var descr = zSr.shift();
      var unused1 = zSr.shift();
      var clickURL = zSr.shift();
      var title = zSr.shift();
      var sitehost = zSr.shift();
      var unused2 = zSr.shift();
      if (title == undefined){
        break;
      }
      html += "<div class ='" + all_class + "'>";
      html += '<a class="ad" href="' + clickURL + '"';
      html += 'onclick="ov_click_count(location.href,\'' ;
      html += path + area +  '\',\'' + title + '\');" >';
      
      //1行
      if (type == 1) {
        html +=  "<span class ='" + title_class + "'>" + title + '</span>';
        html +=  "<span class ='" + desc_class + "'>" + descr + '</span>';
      }
      //2行(詳細横にホスト名)
      if (type == 2) {
        html +=  "<span class ='" + title_class + "'>" + title + '</span>';
        html +=  "<span class ='" + desc_class + "'>" + descr ;
        html +=  "<span class ='" + host_class + "'>" + sitehost + '</span>'+ '</span>';
      }
      //2行(タイトル横にホスト名)
      if (type == 3) {
        html += "<span class ='" + title_class + "'>" + title + '</span>';
        html += "<span class ='" + host_class + "'>" + sitehost + '</span>';
        html += "<span class ='" + desc_class + "'>" + descr + '</span>';
      }
      
      html += '</a></div>';
      
    }
    
    //フッタがいるなら
    if (isfooter == 1) {
      html +=  '<div align="right" class="interest_footer"><a href="http://ov.yahoo.co.jp/service/int/index.html?o=IM0028"  target="_blank">インタレストマッチ</a></div>';
    }
    
    //親オブジェクトに広告書き込み
    if(navigator.userAgent.indexOf("Opera") != -1){
      var div_id = ads_node[i][0].parentNode.id;
      var element = document.getElementById(div_id); 
      var newElement = document.createElement("div"); 
      newElement.innerHTML = html; 
      element.insertBefore(newElement,element.firstChild);
     
    } else{
      ads_node[i][0].parentNode.innerHTML = html ;
    }
    if (navigator.userAgent.indexOf("MSIE")!=-1){
      //ads_node[i][0].parentNode.innerHTML += ""; //for IE
    }
    if (navigator.userAgent.indexOf("MSIE")!=-1){
      //ads_node[i][0].parentNode.innerHTML += ""; //for IE
    }
    //<adv:ov> タグ削除
    //ads_node[i].remove();
  }
  
  //////// ---------HotSpot ------ ////////

  var ctxtids = Array("Adobe","Adobe Photoshop Web","Ajax 開発","ASP.NET",
                      "ASP.NET Ajax","Google Web Accelerator","JSP 入門",
                      "Linux","Net Framework","Net 開発","PHP システム開発",
                      "PHP 開発","PHP 開発ツール","Server 構築","Web サーバ",
                      "Webサーバー","Web サイト構築","Web リサーチ","Web 開発",
                      "Web 調査","Webアプリケーション","Webアプリケーション開発",
                      "Windows Server","アプリケーション 開発","インターネット モバイル",
                      "インターネットビジネス","インターネットマンション","インターネット英会話",
                      "インターネット広告","インターネット接続","インフラ 構築",
                      "サーバ 構築","サーバー 統合","サーバー 保守","サーバー構築",
                      "サーバ統合","サイドビジネス","システム","システム開発","システム統合",
                      "ネットゲーム","ネットワークエンジニア","ネットワークエンジニア 年収",
                      "ネット広告料金","パソコン で 稼ぐ","パソコンアウトレット","ファイルサーバ",
                      "ファイルサーバー","フリーエンジニア","フリーソフト","フリーデータベース",
                      "フリープログラマ","プログラミング言語C","プログラミング入門","メール レンタル サーバー",
                      "ユニットテスト","レンタル サーバー SSL","案件情報","開発ソフト",
                      "業務システム","携帯 サイト 製作","携帯メール配信","光インターネット",
                      "在宅 ビジネス","在宅 稼ぐ","在宅 仕事","在宅アルバイト","転職 ネットワークエンジニア",
                      "副業 SOHO","副業 パソコン","保守システム");
  
  for (var i = ads_hs_node.length -1 ; i >= 0 ; i-- ) {
    var html        = '';
    var ref    = path + 'keyword';    
    var type        = ads_hs_node[i].attr('type');         //表示タイプ
    var all_class   = ads_hs_node[i].attr('rowcss');       //行ごとのクラス名
    
    for (var count = ads_hs_node[i].attr('count') -1 ; count >= 0 ; count--){
      var randindex  = Math.floor( Math.random() * ctxtids.length );
      var s           = ctxtids[randindex];
      ctxtids.splice(randindex,1);
      
      //2行
      if (type == 2 && (ads_hs_node[i].attr('count') / 2) ==  (count + 1)  ){
        html += '</div>';
        html += '<div class="hs_2col">';
      }
      if (type == 2 && count == ads_hs_node[i].attr('count') -1 ){
        html += '<div class="hs_2col">';
      }
      
      html +=
        '<div class="' + all_class + '"><a href="http://search.codezine.jp/sponsor/?Keywords=' + 
        encodeURI(s) + '&ref=' + ref + '" ' + 
        'onclick="ov_click_count(location.href,\'' + ref + '\',\'' + s + '\')"' + 
        'target="_blank">' + s + '</a></div>';
      
    }
    //親オブジェクトに広告書き込み
    if(navigator.userAgent.indexOf("Opera") != -1){
      var div_id = ads_hs_node[i][0].parentNode.id;
      var element = document.getElementById(div_id); 
      var newElement = document.createElement("div"); 
      newElement.innerHTML = html; 
      element.insertBefore(newElement,element.firstChild);
    } else {
      ads_hs_node[i][0].parentNode.innerHTML = html ;
    }
    if (navigator.userAgent.indexOf("MSIE")!=-1){
      //ads_hs_node[i][0].parentNode.innerHTML += ""; //for IE
    }
    //<adv:hs> タグ削除
    //ads_hs_node[i].remove();
    
  }
  
  
}
/*
 *  広告初期処理
 *  Footer で呼んでください。
 *  
 *
 *
*/
function do_adv_parsing( element ) {

  //////// ---------IM / SS ------ ////////
  
  //-- Keyword があれば SS になる ---
  //1ページにIMとSSが一緒になることはない
  var keyword = '';
  if(location.pathname.indexOf("/search") != -1){
    keyword = get_param_keyword(location.search);
  }
  if(location.pathname.indexOf("/article/tag/") != -1 
    && location.pathname != "/article/tag/"){
    var metas = document.getElementsByTagName("meta");
    for(var i = 0 ; i < metas.length ; i++ ){
      if (metas[i].name == "keywords"){
        keyword = metas[i].content;
      }
    }
  }

  var includes = adv_get_subelements_by_name(element, 'adv:ov'); 
  var includes_total = includes.length+1;
  var includes_ad_count = 0;

  //広告数取得およびNode取得
  for (var i = includes.length -1 ; i >= 0 ; i-- ) {
    var include = $(includes[i]);
    var count = include.attr('count');
    includes_ad_count = eval(includes_ad_count) + eval(count);
    ads_node.push(include);
  }
  
  var paths = location.pathname.split("/");
  var path  = (paths.length > 2 ? paths[1] : "top");
  var oreg  = new RegExp("^[a-z]+$");
  if (!path.match(oreg)){
    path = (paths.length > 3 ? paths[2] : "top");
  }
  if (path.indexOf('%') != -1){
    path = "local";
  }
  
  path = encodeURIComponent(path);
  var config  = "23228933181";
  var source  = "ecnavi_jp_codezine_im";
  var ctxtUrl = encodeURIComponent(location.protocol + "//" + location.host + location.pathname);
  
  var ctxtids = Array("boo0102","boo0105","sof0100","sof0200","ele0200",
                 "ele0300","spo0200","hea0101","hea0102","hea0204",
                 "hea0400","ret0203","ret0204","car0200","edu0400",
                 "edu0500","ent0300","fin0100","fin0200","ins0300",
                 "tra0000","com0100","com0200","com0300","com0400",
                 "com0500","com0600","com0800","com0900","com1001",
                 "com1003","rea0103","rea0301","pro1100","pro1200",
                 "spo0200","hea0204","hea0400","ret0203","ret0204",
                 "car0200","edu0400","ent0300","fin0100","fin0200",
                 "ins0300","com0400","com0900","com1001","pro0105",
                 "pro1100","pro1200","aut0102","app0401","ele0302",
                 "spo0500","fin0101","fin0102","fin0201","fin0202",
                 "fin0203","fin0204","fin0205","fin0206","pro0902");
  
  var ctxtid  = ctxtids[Math.floor( Math.random() * ctxtids.length )];
  var type    = "im_cz_" + path + "_" + ctxtid;
  var gen     = "M";
  var age     = "35";

  // keyrowd がないので IM
  if (keyword == ''){
    document.write('<s' + 'cript type="text/javascript" language="javascript" src="http://im.ecnavi.ov.yahoo.co.jp/js_flat/?source=' 
                    + source + '&type=' + type + '&ctxtId=' + ctxtid + '&maxCount=' 
                    + includes_ad_count + '&gen=' + gen + '&age=' + age + '&outputCharEnc=utf8&ctxtUrl=' + ctxtUrl + '"></s' + 'cript>');
  // keyrowd があるので SS
  } else {
      document.write('<s' + 'cript type="text/javascript" language="javascript" src="/search/xml_overture?keywords=' 
                      + encodeURI(keyword) + '&maxcount=' + includes_ad_count + '&ua=' 
                      + escape(escape(navigator.userAgent)) + '"></s' + 'cript>');
  }


  //////// ---------HotSpot ------ ////////
  includes = adv_get_subelements_by_name(element, 'adv:hs'); 
  includes_total = includes.length+1;
  includes_ad_count = 0;

  //広告数取得およびNode取得
  for (var i = includes.length -1 ; i >= 0 ; i-- ) {
    var include = $(includes[i]);
    var count = include.attr('count');
    includes_ad_count = eval(includes_ad_count) + eval(count);
    ads_hs_node.push(include);
  }


}

function adv_get_subelements_by_name(element,elementname) {
  var found = new Array();
  elementname = elementname.toLowerCase();
  if (element.nodeType == 9 || element.nodeType == 1) {
    var children = element.childNodes;
    for (var i = 0; i < children.length ; i++ ) {
      var elem = children[i];
      if (elem.nodeType == 1) {
        var tagname = elem.tagName.toLowerCase();
        if (tagname == elementname) {
          found.push(element.childNodes[i]);
        }
        if ( elem.childNodes.length > 0) {
          var res = adv_get_subelements_by_name(elem,elementname);
          found = found.concat(res);
        }
        
      }
    }
  }
  return found;
}

function get_param_keyword(str){
  var dec = decodeURIComponent;
  var par = new Array, itm;
  if(typeof(str) == 'undefined') return par;
  if(str.indexOf('?', 0) > -1) str = str.split('?')[1];
  str = str.split('&');
  for(var i = 0; str.length > i; i++){
    itm = str[i].split("=");
    if(itm[0] == 'q'){
      return typeof(itm[1]) == 'undefined' ? true : dec(itm[1]);
    }
  }
  
}

// create flash control
function CreateFlash(flashid, srcpath, width, height, cssclass) {
  var proto = location.protocol;
  var flash_tag = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="'+proto+'//fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="'+width+'" height="'+height+'" id="'+flashid+'" align="middle">'+
  '<param name="allowScriptAccess" value="sameDomain" />' +
  '<param name="movie" value="'+srcpath+'" />' +
  '<param name="quality" value="high" />' +
  '<param name="bgcolor" value="#ffffff" />' +
  '<embed src="'+srcpath+'" quality="high" bgcolor="#ffffff" width="'+width+'" height="'+height+'" name="'+flashid+'" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="'+proto+'//www.macromedia.com/go/getflashplayer" />'+
  '</object>';
  
  document.write('<div class="'+cssclass+'">' + flash_tag + '</div>');
}


//-----------------------------------------------------------------------------
// impAct AD CODE
function impA_rotate(list) {
  var i = list.length;
  while (--i) {
  var j = Math.floor(Math.random() * (i + 1));
  if (i == j) continue;
  var k = list[i]; list[i] = list[j]; list[j] = k;
  }
  for ( i = 0; i <= list.length; i++){ if ( list[i] == "" ) list.splice(i,1);}
  return list;
}
//-----------------------------------------------------------------------------


// ダブルクリック広告用
  var doubleclick_ord = Math.floor(Math.random()*10000000000000000);
  function ad_dc_txt(site,size,tile){
    var sTile = "";
    if (tile != ""){
      sTile = "key-values;tile=" + tile + ";";
    }
    document.write('<script language="JavaScript" src="' + location.protocol + '//ad.jp.doubleclick.net/adj/' + site + '/;sz=' + size + ';' + sTile + 'ord=' + doubleclick_ord + '?" type="text/javascript" charset="shift_jis"></scr' + 'ipt>');
  }
  function ad_dc_swf(site,size,tile){
    var sTile = "";
    if (tile != ""){
      sTile = "key-values;tile=" + tile + ";";
    }
    document.write('<script language="JavaScript" src="' + location.protocol + '//ad.jp.doubleclick.net/adj/' + site + '/;sz=' + size + ';' + sTile + 'ord=' + doubleclick_ord + '?" type="text/javascript"></scr' + 'ipt>');
  }


