/* format.js */



function getEnv(){
  var ua = navigator.userAgent.toLowerCase();
  var env = new Object();
  env.ie = (!window.opera && match('msie') != -1)? code(match('msie ')+5): 0;
  env.fx = (match('firefox/') != -1)? code(match('firefox/')+8): 0;
  env.nn = (match('netscape/') != -1)? code(match('netscape/')+9): 0;
  env.op = (match('opera/') != -1)? code(match('opera/')+6): 0;
  env.sa = (match('safari') != -1 && match('version/') != -1)? code(match('version/')+8): 0;
  env.ch = (match('chrome/') != -1)? code(match('chrome/')+7): 0;
  env.win = (match('windows') != -1)? 1: 0;
  env.mac = (match('mac') != -1)? 1: 0;
  env.std = (document.compatMode == 'CSS1Compat')? 1: 0;
  return env;
  function match(str){
    return Number(ua.indexOf(str));
  }
  function code(num){
    return Number(ua.charAt(num));
  }
}

function setSlider(){
  var ua = navigator.userAgent.toLowerCase();
  if(!ua.match(/(MSIE|Chrome|Firefox|Gecko|Opera|Safari)/i) || ua.match(/(Nitro|iPod|iPhone|525\.20|PDA)/i)) return false;
  if(!document.getElementById('index')) return false;
  var body = document.getElementById('body');
  if(!body) return false;
  var html = '';
  html += '<object id="slider" data="slider/slider.swf" type="application/x-shockwave-flash" width="900" height="320">';
  html += '<param name="movie" value="slider/slider.swf" />';
  html += '<param name="wmode" value="transparent" />';
  html += '<param name="menu" value="false" />';
  html += '</object>';
  body.innerHTML += html;
}

function swapBodyImage(){
  if(env.fx >= 3 || env.op >= 9 || env.ie >= 8){
    document.getElementById('html').style.background = '#80b0ff url(../img/format/body.gif) repeat-y center top';
    document.body.style.background = '#80b0ff url(../img/format/body.gif) repeat-y center top';
  }
}

function setOnLoad(func){
  (window.addEventListener)? window.addEventListener('load',func,false):
  (window.attachEvent)? window.attachEvent('onload',func):
  window.onload = func;
}

function setOnResize(func){
  (window.addEventListener)? window.addEventListener('resize',func,false):
  (window.attachEvent)? window.attachEvent('onresize',func):
  window.onresize = func;
}

function setInfoImgView(){
  var mask = document.createElement('div');
  mask.setAttribute('id','imageViewerMask');
  document.body.appendChild(mask);
  setOnResize(setImageViwerMask);
  setImageViwerMask();
  var mask = document.getElementById('imageViewerMask');
  mask.onclick = imageViwerHide;
  var viewer = document.createElement('div');
  viewer.setAttribute('id','imageViewer');
  viewer.setAttribute('title','\u9589\u3058\u308B');
  document.body.appendChild(viewer);
  var viewer = document.getElementById('imageViewer');
  viewer.onclick = imageViwerHide;
  var shadow = document.createElement('div');
  shadow.setAttribute('id','imageViewerShadow');
  viewer.appendChild(shadow);
  var shadow = document.getElementById('imageViewerShadow');
  var image = document.createElement('div');
  image.setAttribute('id','imageViewerImage');
  shadow.appendChild(image);
  document.getElementById('imageViewerImage').innerHTML = '<img id="imageViewerOff" src="/img/format/button-close.png" width="17" height="17" />';
}

function imageViwerHide(){
  setViewLayer('imageViewer',0);
  setViewLayer('imageViewerMask',0);
  var image = document.getElementById('imageViewerImage');
  image.style.backgroundImage = 'url(/img/format/load.gif)';
  image.innerHTML = '';
}

function setImageViwerMask(){
  var mask = document.getElementById('imageViewerMask');
  mask.style.width = (!env.ie)? document.documentElement.scrollWidth + 'px': document.body.scrollWidth + 'px';
  mask.style.height = (!env.ie)? document.documentElement.scrollHeight + 'px': document.body.scrollHeight + 'px';
}

function viewImage(url){
  setViewLayer('imageViewerMask',1);
  setImageViwer(160,120);
  setViewLayer('imageViewer',1);
  var img = new Image();
  img.src = url;
  var timer = setInterval(getStatus,300);
  function getStatus(){
    if(!img.width || !img.height) return false;
    clearInterval(timer);
    var image = document.getElementById('imageViewerImage');
    image.style.backgroundImage = 'url(' + url + ')';
    setImageViwer(img.width,img.height);
    image.innerHTML = '<img id="imageViewerOff" src="/img/format/button-close.png" width="17" height="17" />';
    var viewer = document.getElementById('imageViewer');
    viewer.style.left = ((document.documentElement.scrollLeft || document.body.scrollLeft) + 
                        Math.floor(((document.documentElement.clientWidth || document.body.clientWidth) - img.width) / 2)) + 'px';
    viewer.style.top = ((document.documentElement.scrollTop || document.body.scrollTop) + 
                        Math.floor(((document.documentElement.clientHeight || document.body.clientHeight) - img.height) / 2)) + 'px';
    setViewLayer('imageViewerOff',1)
  }
  return false;
}

function setImageViwer(w,h){
  var image = document.getElementById('imageViewerImage');
  image.style.width = w + 'px';
  image.style.height = h + 'px';
  var viewer = document.getElementById('imageViewer');
  viewer.style.left = ((document.documentElement.scrollLeft || document.body.scrollLeft) + 
                      Math.floor(((document.documentElement.clientWidth || document.body.clientWidth) - w) / 2)) + 'px';
  viewer.style.top = ((document.documentElement.scrollTop || document.body.scrollTop) + 
                     Math.floor(((document.documentElement.clientHeight || document.body.clientHeight) - h) / 2)) + 'px';
}

function setViewLayer(id,mode){
  document.getElementById(id).style.display = (mode)? 'block': 'none';
}

function swapPNG(){
  if(env.mac || env.ie > 6 || env.ie < 5) return;
  var img = document.getElementsByTagName('img');
  for(var i=0;i<img.length;i++){
    var src = img[i].getAttribute('src');
    if(!src.match(/\.png/i)) continue;
    try{
      img[i].setAttribute('src','/img/format/null.gif');
    }catch(e){
      try{
        img[i].src = '/img/format/null.gif';
      }catch(e){}
    }
    img[i].style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=' + src + ',sizingMethod=scale)';
  }
}

function setCountPV(){
  var ref = (document.referrer)? document.referrer: (document.referer)? document.referer: '';
  var host = location.href.split('/')[2];
  if(ref.split('/')[2] == host) ref = '';
  var pv = new Image();
  pv.src = '/tool/pv/?' + ref;
}

function onLoads(){
  setCountPV();
  setSlider();
  swapPNG();
  swapBodyImage();
  setInfoImgView();
}

var env = getEnv();

setOnLoad(onLoads);
