//-------------------------------------------------------------------
// Article Flow
//-------------------------------------------------------------------
function ArticleFlow(_id,_i,_cc,_box){
 try{
  new MWProtoFlow($(_id),{
   startIndex: _i,
   captionColor: _cc,
   useBox: _box
  });
 }catch(e){}
}
//---------------------------------------------------------------------
//   for Book Page Cover
//---------------------------------------------------------------------
function fixedCoverBox() {
 var _top = document.documentElement.scrollTop || document.body.scrollTop || 0;
 var _left = document.documentElement.scrollLeft || document.body.scrollLeft || 0;
 var cb = $('cover-background');
 cb.style.top = _top + 'px';
 cb.style.left = _left + 'px';
}
function closeCoverBox() {
 if ($('cover-background') != undefined) { $('cover-background').style.display = 'none';}
 if ($('cover-container') != undefined) { $('cover-container').style.display = 'none';}
 if (Prototype.Browser.IE) {
  $$('select').each(function(a){a.style.visibility = 'visible';});
  Event.stopObserving(window,'scroll',fixedCoverBox);
 }
}
function createContainer() {
 var b = $$('body')[0];
 var cb = document.createElement('div');
 cb.style.display = 'none';
 cb.id = 'cover-background';

 var cn = document.createElement('div');
 cn.style.display = 'none';
 cn.id = 'cover-container';
 var html = '<div class="cover-container-closer" onclick="closeCoverBox();';
 cn.innerHTML = '<div class="container-closer" onclick="closeCoverBox();"></div><div id="cover-container-content" class="cover-container-content"></div>';
 b.appendChild(cb);
 b.appendChild(cn);
}
function openCoverBox(_target) {
 if ($('cover-background') == undefined) {createContainer();}
 var c = $$('body .' + _target)[0];
 var ccc = $('cover-container-content');
 ccc.innerHTML = c.innerHTML;
 var cb = $('cover-background');
 cb.style.display = 'none';
 var cc = $('cover-container');
 cc.style.display = 'none';
 if (Prototype.Browser.IE) {
  $$('select').each(function(a){a.style.visibility = 'hidden';});
  fixedCoverBox();
  Event.observe(window,'scroll',function(e){fixedCoverBox();},true);
 } else {
  cb.style.position = 'fixed';
  cc.style.position = 'fixed';
 }
 new Effect.Appear(cb,{
  from: 0.1,
  to: 0.7,
  duration: 0.5,
  afterFinishInternal:function(ef){
   new Effect.BlindDown(cc,{
    from: 0.0,
    to: 1.0,
    duration: 1.0
   });
  }
 });
}
//---------------------------------------------------------------------
//   styleswitcher.js
//---------------------------------------------------------------------
function setActiveStyleSheet(title) {
  var a;
  if ( $('altstylel') != undefined ) {
    a = $('altstylel');
    a.disabled = true;
    if(a.getAttribute('title') == title) a.disabled = false;
  }
  if ( $('altstyles') != undefined ) {
    a = $('altstyles');
    a.disabled = true;
    if(a.getAttribute('title') == title) a.disabled = false;
  }
  var expires = '';
  var domain = '; domain=' + location.hostname;
  var date = new Date();
  date.setTime(date.getTime()+(365*24*60*60*1000));
  expires = '; expires=' + date.toGMTString();
  document.cookie = 'jb-style=' + title + expires + domain + '; path=/';
}
function readCookie(name) {
  var nameEQ = name + '=';
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}
function initStyleSwitch(){
  if ( $('altstylel') != undefined ) {
   var cookie = readCookie('jb-style');
   var styletype = cookie ? cookie : 'Small';
   setActiveStyleSheet(styletype);
  }
}
//-------------------------------------------------------------------
// Block Slide
//-------------------------------------------------------------------
function BlockSlide(_id,obj){
 new Effect.toggle($(_id),'slide',{duration:0.3});
 // Case by Case Code ------------------------------------------------------------------
 //if ($(_id).visible()) {
   //obj.innerHTML = obj.innerHTML.replace(/を閉じる/,'を開く');
 //} else {
   //obj.innerHTML = obj.innerHTML.replace(/を開く/,'を閉じる');
 //}
 // ------------------------------------------------------------------------------------
}
//-------------------------------------------------------------------
// Tab Switch
//-------------------------------------------------------------------
var TabSwitchTask = {};
var TabSwitchTime = 300;
var TabCount = {};
function TabSwitch(prefix,event){
  var o = Event.element(event);
  var oid = o.id;
  var n = oid.match(/[0-9]+/i);
  for (var i=1; i<=TabCount[prefix]; i++){
    var t = $(prefix+i+'-box');
    (i==n) ? t.style.display='block' : t.style.display='none';
    // Case by Case Code ------------------------------------------------------------------
    if (prefix == 'latest') {
      var srcprefix = 'http://wedge.ismedia.jp/common/images/v1/top/latesttab';
      if (i==n) {
        $('latest'+i).style.backgroundImage = 'url(' + srcprefix + i +'-b.gif)'
      } else {
        $('latest'+i).style.backgroundImage = 'url(' + srcprefix + i +'-a.gif)'
      }
    } 
    if (prefix == 'sitenavi') {
      var srcprefix = 'http://wedge.ismedia.jp/common/images/v1/top/latesttab';
      if (i==n) {
        $('sitenavi'+i).style.backgroundImage = 'url(' + srcprefix + i +'-b.gif)'
      } else {
        $('sitenavi'+i).style.backgroundImage = 'url(' + srcprefix + i +'-a.gif)'
      }
    } 
    // ------------------------------------------------------------------------------------
  }
}

function initTabSwitch(prefix,count){
  TabCount[prefix] = count;
  for (var i=1; i<=count ;i++){
    try{
      Event.observe(prefix+i, 'mouseover', function(e){TabSwitchTask[i] = TabSwitch.later(TabSwitchTime)(prefix,e);},true);
      Event.observe(prefix+i, 'mouseout', function(e){try{TabSwitchTask[i].cancel(e);}catch(err){}},true);
    }catch(e){
    }
  }
}

//-------------------------------------------------------------------
// Box Show
//-------------------------------------------------------------------
var BoxShowTask = {};
var BoxHideTask = {};
var BoxInActiveTask = {};
var BoxShowTime = 300;
var BoxHideTime = 600;
var BoxCount = {};
var BoxActive = {};
function BoxShow(prefix,event){
  var o = Event.element(event);
  var oid = o.id;
  var n = oid.match(/[0-9]+/i);
  for (var i=1; i<=BoxCount[prefix]; i++){
    var m = $(prefix+i);
    var t = $(prefix+i+'-box');
    (i==n) ? t.style.display='block' : t.style.display='none';
    (i==n) ? m.className = m.className.replace(/normal/,'reverse') : m.className = m.className.replace(/reverse/,'normal');
    // Case by Case Code ------------------------------------------------------------------
    // if (p == 'concept-menu') {
    //  if (i==n) {
    // 
    //  } else {
    //
    //  }
    // } 
    // ------------------------------------------------------------------------------------
  }
  BoxActive[prefix] = false;
}

function BoxClose(prefix){
  for (var i=1; i<=BoxCount[prefix]; i++){
    var m = $(prefix+i);
    var t = $(prefix+i+'-box');
    t.style.display='none';
    m.className = m.className.replace(/reverse/,'normal');
  }
}
function BoxInActive(prefix,event){
  if (!(BoxActive[prefix])) {BoxClose(prefix);}
}
function BoxHide(prefix,event){
  if (!(BoxActive[prefix])) {BoxClose(prefix);}
}
function initBoxShow(prefix,count){
  BoxCount[prefix] = count;
  BoxActive[prefix] = false;
  for (var i=1; i<=count ;i++){
    try{
      Event.observe(prefix+i, 'mouseover', function(e){BoxShowTask[i] = BoxShow.later(BoxShowTime)(prefix,e);},true);
      Event.observe(prefix+i, 'mouseover', function(e){try{BoxHideTask[i].cancel(e);}catch(err){}},true);
      Event.observe(prefix+i, 'mouseover', function(e){try{BoxInActiveTask[i].cancel(e);}catch(err){}},true);
      Event.observe(prefix+i, 'mouseout', function(e){BoxHideTask[i] = BoxHide.later(BoxHideTime)(prefix,e);},true);
      Event.observe(prefix+i, 'mouseout', function(e){try{BoxShowTask[i].cancel(e);}catch(err){}},true);
      Event.observe(prefix+i+'-box', 'mouseover', function(e){BoxActive[prefix] = true; try{BoxInActiveTask[i].cancel(e);}catch(err){}},true);
      Event.observe(prefix+i+'-box', 'mouseout', function(e){BoxActive[prefix] = false; BoxInActiveTask[i] = BoxInActive.later(BoxHideTime)(prefix,e);},true);
      var closers = $$('#'+prefix+i+'-box .close');       
      closers.each(function(a){Event.observe(a, 'click', function(e){BoxClose(prefix);},true);});
    }catch(e){
    }
  }
}

//-------------------------------------------------------------------
//    Print Window
//-------------------------------------------------------------------
function printWindow(url,w,h){
  var opt = 'width=' + w + ',height=' + h + 'location=yes,menubar=no,status=no,scrollbars=yes';
  var w = window.open(url,null,opt);
}

//-------------------------------------------------------------------
// Initialize
//-------------------------------------------------------------------
function initEffect(){
  if ($('concept-menu1') != undefined){initBoxShow('concept-menu',4);}
  if ($('latest1') != undefined){initTabSwitch('latest',6);}
  if ($('sitenavi-menu1') != undefined){initBoxShow('sitenavi-menu',6);}
  initStyleSwitch();
}
Event.observe(window,'load',function(e){initEffect();},true);
