function add_favorite_or_not(){
  if (window.confirm('入稿アプリケーションの起動をお気に入りに登録しますか？')){
    if (window.sidebar){
      window.sidebar.addPanel('地域広告ドットコム 入稿アプリケーション起動','http://www.chi-koku.com/sw/ckk.jnlp','');
      return true;
    }
    window.external.addFavorite('http://www.chi-koku.com/sw/ckk.jnlp' , '地域広告ドットコム 入稿アプリケーション起動');
  }
　return true;
}

function add_fav(url){
  new Ajax.Request(url, {asynchronous:true, evalScripts:true});
  return false;
}

function show_p_code_prompt(callback,param){
  document.forms['p_code_prompt'].decide.param = param;
  document.forms['p_code_prompt'].decide.callback = callback;
  if (! document.forms['p_code_prompt'].decide.defined){
    Event.observe(document.forms['p_code_prompt'].decide,
     'click',
     function(){
      p_code = null;
      if (null != document.forms['p_code_prompt'].pre.value && 0 < document.forms['p_code_prompt'].pre.value.length &&
          null != document.forms['p_code_prompt'].post.value && 0 < document.forms['p_code_prompt'].post.value.length){
        p_code = document.forms['p_code_prompt'].pre.value + "-" + document.forms['p_code_prompt'].post.value;
      }

      $('__dialog').style.display = 'none';
      callback_param = new Array();
      callback_param.push(null == p_code ? "" : p_code);
      for(i = 0 ; i < document.forms['p_code_prompt'].decide.param.length ; i++){
        callback_param.push(document.forms['p_code_prompt'].decide.param[i]);
      }
      document.forms['p_code_prompt'].decide.callback.apply(this,callback_param);
    });
    document.forms['p_code_prompt'].decide.defined = true;
  }
  show_dialog({w:'280px',h:'140px' , x:'100px' , y:'150px',d:true,t:'郵便番号が入力されていません',f:document.forms['p_code_prompt'].pre},$('_p_code_prompt'));
}

function load_cat(p_code  , type){
  elem_id = 'category_' + type
  elem = $(elem_id);
  if (null == p_code || 0 == p_code.length){
    show_p_code_prompt(load_cat,[type]);
  }
  if (null == p_code || 0 == p_code.length){
    return false;
  }

  anchor = null;
  if (1 == type){
    title = 'ショップカテゴリーを選択';
    if (undefined == elem.loaded){
      loadSCat(p_code , elem);
      elem.loaded = true;
    }
    anchor = $('shop_cat_search_anc');
  }else{
    title = '商品カテゴリーを選択'
    if (undefined == elem.loaded){
      loadPCat(p_code , elem);
      elem.loaded = true;
    }
    anchor = $('prod_cat_search_anc');
  }
  show_dialog({w:"360px",h:"350px",x:"100px",y:"150px",d:true,t:title},elem);
  if (undefined == anchor.defined){
    anchor.onclick = function(){
      load_cat(p_code,type);
    };
    anchor.defined = true;
  }

  return false;
}

function chk_pcode(p_code , form){
  form.p_code.value = p_code;
  if (undefined == form.area){
    if (null == form.p_code.value || 'null' == form.p_code.value || 0 == form.p_code.value.length){
      show_p_code_prompt(chk_pcode,[form]);
      return false;
    }else{
      form.submit();
    }
    return true;
  }else{
    if (form.area[0].checked &&
         (null == form.p_code.value || 'null' == form.p_code.value || 0 == form.p_code.value.length)){
      show_p_code_prompt(chk_pcode,[form]);
      return false;
    }else if (form.area[1].checked){
      form.submit();
    }else if (null != form.p_code.value && 'null' != form.p_code.value && 0 != form.p_code.value.length) {
      form.submit();
    }
    return true;
  }
}

function changePrintLayout(){
  var all = document.getElementById('all_ad');
  all.style.width = "204mm";
  all.style.height = "280mm";
  all.style.position = "absolute";
  all.style.top = "0";
  all.style.left=  "0";

  var name = document.getElementById('shop-name');
  name.style.width = "204mm";
  name.style.height = "5mm";
  name.style.paddingTop = "0";
  name.style.paddingRight="0";

  var title = document.getElementById('ad_title');
  title.style.width = "204mm";
  title.style.height = "8mm";
  title.font = "normal 100%/100% cursive;"
  title.letterSpacing = "0";
  title.style.backGroundColor = "red";

  var map_container = document.getElementById('map-container');
  map_container.style.height = "45mm" ;
  map_container.style.width = "100mm";
  if (window.sidebar){
    map_container.childNodes[1].style.display = "none";
  }else{
    map_container.childNodes[0].style.display = "none";
  }

  map_container.style.margin = "0 0 0 0";

  var map = document.getElementById('map');
  map.style.height = "45mm";
  map.style.width = "100mm";
  map.style.margin = "0 0 0 0";

  var adv_odds = document.getElementsByClassName("adv-odd");
  for (i = 0 ; i < adv_odds.length ; i++){
    changePropertiesForPrint(adv_odds[i],false);
  }

  var adv_evens = document.getElementsByClassName("adv-even");
  for (i = 0 ; i < adv_evens.length ; i++){
    changePropertiesForPrint(adv_evens[i],true);
  }

  document.getElementById('back_link').style.display = "none";
  document.getElementById('favorite').style.display = "none";
  document.getElementById('category_container').style.display = "none";
  document.getElementById('prod-search').style.display = "none";
  document.getElementById('shop-search').style.display = "none";

  if (document.getElementsByClassName('page_anchor').length > 0){
    document.getElementsByClassName('page_anchor')[0].remove();
  }

  if (window.sidebar){
    document.body.childNodes[1].style.display = "none";
  }else{
    document.body.childNodes[0].style.display = "none";

  }

  removals = document.getElementsByClassName("to_remove_for_print");
  for(i = 0 ; i < removals.length ; i++){
    removals[i].remove();
  }

  document.getElementById("expire_date").remove();
  if (document.getElementById("other_adv")){
    document.getElementById("other_adv").remove();
  }

  addGlassPane();
  window.alert("元の表示用レイアウトに戻るには、ブラウザをリロード[F5ボタン]を押してください。");
}


function changePropertiesForPrint(ad , border){

  ad.style.width = "100mm";
  ad.style.height = "39mm";
  ad.style.margin = "0 0 0 0";
  ad.style.border = "0";
  if (border){
    ad.style.borderLeft = "1px dotted black";
  }

  left = ad.getElementsByClassName("adv_left")[0];
  left.style.width = "25mm";
  left.style.height = "37mm";
  left.style.paddingLeft = "0";
  left.style.zIndex = 1;
  left.style.position = "relative";
  left.style.top = "10";
  left.style.left = "9";

  if (window.sidebar){
    left.childNodes[1].style.width = (left.childNodes[1].naturalWidth / 6) + "mm";
    left.childNodes[1].style.height = (left.childNodes[1].naturalHeight / 6) + "mm";
    left.childNodes[1].style.marginLeft = "0";
  }else{
    left.childNodes[0].style.width = "100%" ;
    left.childNodes[0].style.marginLeft = "0";
  }

  right = ad.getElementsByClassName("adv_right")[0];
  right.style.height = "37mm";
  right.style.position = "relative";
  right.style.top = "0";
  right.style.left = "0mm";
  right.style.zIndex = 2;
  right.getElementsByClassName('name')[0].style.font = "normal 80%/100% sans-serif";

  right.getElementsByClassName('maker')[0].style.font = "normal 80%/100% sans-serif";

  price = right.getElementsByTagName('div')[0];

  price.style.margin = "0 0 0 0";
  price.style.height = "30mm";
  price.style.overflow = "hidden";

  attr = ad.getElementsByClassName("adv_attribute")[0];
  attr.style.font = "normal 70%/100% sans-serif";
  attr.style.width = "40mm";
  attr.style.height = "37mm";
  attr.style.clear = "none";
  attr.style.float = "left";
  attr.style.paddingLeft = "0";
  attr.style.borderTop = "";
  attr.style.overflow = "hidden";
  attr.style.margin = "0 0 0 0";
  attr.style.paddingTop = "0";
  attr.style.position = "relative";
  attr.style.left = "-10mm";
  attr.style.top = "0";
  attr.style.paddingTop = "3mm";
  attr.zIndex = 3;
  pres = attr.getElementsByTagName("pre");
  if (window.sidebar){
    pres[0].textContent = pres[0].textContent.replace("\\n" , "");
  }else{
    attr.style.wordWrap = "break-word";
    attr.style.width = "26mm";
    attr.style.left = "-10mm";
    pres[0].innerText = pres[0].innerText.replace("\\n", "");
  }
  if (pres.length > 1){
    pres[1].remove();
  }
}

var startX = -1;
var startY = -1;
var mode = false;
function dragstart(event){
  mode = !mode;
  if (!mode){
    dragend(event);
    return;
  }

  glass = document.getElementById('glass_pane');
  glass.style.cursor = "url(/images/pen_il.cur),default";
  startX = Event.pointerX(event);
  startY = Event.pointerY(event);
}

function dragging(event){
  if (!mode){
    return;
  }

  var img = document.createElement("img");

  img.setAttribute('src' , '/images/pen.gif');
  img.style.position = 'absolute';
  img.style.left =  Event.pointerX(event) + "px" ;
  img.style.top = Event.pointerY(event) + "px" ;
　img.style.zIndex = 200;
  if (window.sidebar){
    img.style.opacity = "0.30";
  }else{
    img.style.filter = "alpha(opacity=30)"
  }

  var body = document.getElementsByTagName('body')[0];
  body.appendChild(img);
}

function dragend(event){
  startX = -1;
  startY = -1;
  glass = document.getElementById('glass_pane');
  glass.style.cursor = "default";
}

function addGlassPane(){
  glass = document.createElement('div');
  glass.setAttribute('id' , 'glass_pane');
  glass.style.width = "204mm";
  glass.style.height = "280mm";
  glass.style.backgroundColor = "white";
  glass.style.opacity = "0.00";
  glass.style.position = "absolute";
  glass.style.top = "0px";
  glass.style.left = "0px";
  glass.style.zIndex = 300;
  if (! window.sidebar){
    glass.style.filter = "alpha(opacity=0)"
  }
　Event.observe(glass , "mousedown" ,dragstart);
　Event.observe(glass , "mousemove" , dragging);

  document.getElementsByTagName('body')[0].appendChild(glass);
}

index_timer_hash = [];

function do_effect_on_index(){
  document.forms[0].elements[0].focus();
  org = $('req-adv-container').innerHTML;
  this.org = org;

  index_timer_hash.push(setTimeout(function(){
     dl = $('req-adv-container').getElementsByTagName('dl')[0];
     rec = dl.getElementsByTagName('dd');
     param = []
     for(i = 0 ; i < rec.length ; i=i+2){
        address = rec[i] ;
        shop_name = rec[i+1];
        hash = [];
        hash['address'] = address.innerHTML;
        hash['name']  = shop_name.innerHTML;
        param.push(hash);
     }
     play_req_adv_slide_show(0 , param);
  },1000)) ;
}

function play_req_adv_slide_show(index, hash){

   if (hash.length <= index){
     clear_timer();
     $('req-adv-container').innerHTML = this.org;
     ($('req-adv-container').getElementsByTagName('img')[0]).style.display = 'none';
     Effect.Appear(($('req-adv-container').getElementsByTagName('img')[0]),{duration:0.3});

     index_timer_hash.push(setTimeout(function(){
        play_req_adv_slide_show(0 , hash);
     },1800));
   }

   buff = document.createElement('p');

   container = document.createElement('div');
   container.className = 'req_adv_info';
   container.style.display = 'none';
   container.setAttribute('id', 'req_adv_info');
   buff.appendChild(container);

   address = document.createElement('p');
   address.className = 'req_adv_address';
   address.innerHTML = hash[index]['address'];
   container.appendChild(address);

   shop_name = document.createElement('p');
   shop_name.className = 'req_adv_name';
   shop_name.innerHTML = hash[index]['name'];
   container.appendChild(shop_name);

   $('req-adv-container').innerHTML = '';
   $('req-adv-container').appendChild(container);
   Effect.Appear($('req_adv_info'),{duration:0.5});

   index_timer_hash.push(setTimeout(function(){
     appearEnd(index + 1 , hash);
   }, 2000));
}

function appearEnd(nextIndex , hash){
  Effect.Fade($('req_adv_info'),{duration:0.5});

  index_timer_hash.push(setTimeout(function(){
    play_req_adv_slide_show(nextIndex, hash);
  },1000));
}

function unload_on_index(){
  clear_timer();
}

function clear_timer(){
  for(;;){
    clearTimeout(index_timer_hash.shift());
     if (0 == index_timer_hash.length){
      break;
    }
  }
}

function show_dialog(param,elem){
  dialog = null;
  if (null != $('__dialog')){
    $('__dialog').last_node.style.display = 'none';
    document.body.appendChild($('__dialog').last_node);
    document.body.removeChild($('__dialog'));
  }
  dialog = document.createElement('div');
  dialog.style.position = 'absolute';
  dialog.style.top = "" + param['y'] ;
  dialog.style.left = "" + param['x'] ;
  dialog.style.width = "" + param['w'] ;
  dialog.style.height = "" + param['h'] ;
  dialog.style.display = 'none';
  document.body.appendChild(dialog);
  dialog.setAttribute('id','__dialog');
  dialog.className = 'dialog';

  toolbar = document.createElement('div');
  toolbar.className = 'dialog_tool_bar';
  toolbar.style.width = "" + param['w'];
  toolbar.style.cursor = 'move';
  if (param['t']){
    title = document.createElement('span');
    title.innerHTML = param['t'];
    toolbar.appendChild(title);
  }

  dialog.last_tool_bar = toolbar;

  close_button = document.createElement('a');
  close_button.href = "#";
  Event.observe(close_button,'click',function(){$('__dialog').style.display = 'none';return false ;});
  close_button.innerHTML = '&nbsp;';
  toolbar.appendChild(close_button);

  dialog.appendChild(toolbar);

  dialog.last_node = elem;
  dialog.appendChild(elem);

  Effect.Appear($('__dialog'),{duration:0.9});
  if (eval(param['d'])){
    new Draggable($('__dialog'),{handle:"dialog_tool_bar"});
  }
  elem.style.display = 'block';
  if (param['f']){
    param['f'].focus();
  }
}

function to_html(elem){
  buff = document.createElement('b');
  buff.appendChild(elem);
  return buff.innerHTML;
}