﻿
function addDOMLoadEvent(f){if(!window.__ADLE){var n=function(){if(arguments.callee.d)return;arguments.callee.d=true;if(window.__ADLET){clearInterval(window.__ADLET);window.__ADLET=null}for(var i=0;i<window.__ADLE.length;i++){window.__ADLE[i]()}window.__ADLE=null};if(document.addEventListener)document.addEventListener("DOMContentLoaded",n,false);/*@cc_on @*//*@if (@_win32)document.write("<scr"+"ipt id=__ie_onload defer src=//0><\/scr"+"ipt>");var s=document.getElementById("__ie_onload");s.onreadystatechange=function(){if(this.readyState=="complete")n()};/*@end @*/if(/WebKit/i.test(navigator.userAgent)){window.__ADLET=setInterval(function(){if(/loaded|complete/.test(document.readyState)){n()}},10)}window.onload=n;window.__ADLE=[]}window.__ADLE.push(f)}

function addLoadEvent(func){
  var oldonload = window.onload;
  
  if(typeof window.onload != 'function'){
    window.onload = func;
  }else{
    window.onload = function(){
      oldonload();
      func();
    }
  }
}


var data1 = new Date();
var topFlg = true;
var page = "top";
var selectedThumb = "";
var selectedThumbTitle = "";


var main_nav;

function navigation()
{ 
  var contents = $('contents');

  //navigationの基本スクロール設定
  main_nav = new Fx.Scroll($('contents_wrapper'), {
    transition: Fx.Transitions.quadInOut,
    duration: 1500,
    paddingLeft: 0,
    paddingTop: 0,
    onComplete: scrollEnd
  });
  
  main_nav.clearTimer().toElement($('top'));

  //navigationにクリックイベントを設定
  var main_nav_lks = new Array();
  var linkuri = $$('a');
  var content_div =  $$('.content');

  for(m = 0; m < linkuri.length; m++){
    if(linkuri[m].getAttribute('rel') == 'main_menu'){
      main_nav_lks.push(linkuri[m]);
    }
  }

  for(i = 0; i < main_nav_lks.length; i++){
    main_nav_lks[i].onclick = function(){
      var target_page = $(this.className);
      
      if(this.getAttribute('rel') == 'main_menu'){
      
        page = this.className;
        
        if(topFlg && page != "top"){
          topFlg = false;
          thisMovie("externalTop").hideTop(this.className);
        }else{
          main_nav.clearTimer().toElement($(target_page));
        }
        
      }
      
      return false;
    }
  } 
  
  //縦スクロールの設定
  resetHello('hello_wrapper', 'hello2', 'hello');
  initVerticalScroll('hello');
  resetFood('food_wrapper', 'food2', 'food');
  initVerticalScroll('food');
  resetImages('lunch_wrapper', 'lunch2', 'lunch');
  initVerticalScroll('lunch');
  
  resetImages('sweets_wrapper', 'sweets2', 'sweets');
  initVerticalScroll('sweets');
  initVerticalScroll('drink');
  initVerticalScroll('products');
  resetImages('dog_wrapper', 'dog2', 'dog');
  initVerticalScroll('dog');
  initVerticalScroll('diary');
  
}

/**
 * 各コンテンツ毎の設定
 */
function initVerticalScroll(menu_name){
  var wrapper = document.getElementById(menu_name + '_wrapper');
  var img= $$('#' + menu_name + ' img');
  for(m = 0; m < img.length; m++){
    img[m].style.top = m * 500 + 'px';
  }
  
  var nav = $(menu_name + '_nav');
   nav.style.visibility = 'visible';
   nav.style.position = 'absolute';
   nav.style.top = 500 + 'px';
   nav.style.left = 0;
   nav.style.margin = 0;
   
  var scroller = new Fx.Scroll($(menu_name + '_wrapper'), {
		transition: Fx.Transitions.quadInOut,
		duration: 500,
		paddingLeft: 0,
		paddingTop: 0
	});
  scroller.clearTimer().toElement($( menu_name + '1'));



  var current = 0;
  
  var nav2_lks = $$('#' + menu_name + '_nav a');
  nav2_lks[0].setAttribute('id','selectat');
  
  //var len = $$('#' + menu_name + "_wrapper div").length - 1;
  var up = document.getElementById(menu_name + '_up');
  var down= document.getElementById(menu_name + '_down');

  if(nav2_lks.length == 1){
    up.style.visibility  = 'hidden';
    down.style.visibility  = 'hidden';
  }

  up.onclick = function(){
    if(current == 1){this.style.visibility = 'hidden';}
    if(current > 0){ current--; }
    
    
    var selectat = $('selectat');
    if(selectat) {selectat.removeAttribute('id');}
    nav2_lks[current].setAttribute('id','selectat');
    
    down.style.visibility  = 'visible';
    
    scroller.clearTimer().toElement($(menu_name + (current + 1)));
    
    return false;
  }
  
  down.onclick = function(){
    //if(current == len - 1){this.style.visibility = 'hidden';}
    //if(current < len){ current++; }
    if(current == nav2_lks.length - 2){this.style.visibility = 'hidden';}
    if(current < nav2_lks.length - 1){ current++; }
    
    var selectat = $('selectat');
    if(selectat) {selectat.removeAttribute('id');}
    nav2_lks[current].setAttribute('id','selectat');
    
    up.style.visibility  = 'visible';
    
    scroller.clearTimer().toElement($(menu_name + (current + 1)));

    return false;
  }
}

/**
 * imgの再配置
 */
function resetImages(menu_name, childName, newName){
  var obj = document.getElementById(menu_name);
  var nav = document.getElementById(newName + "_nav");
  
  var atag = document.createElement('a');
  atag.setAttribute('href' , '#' + newName + '1');
  nav.appendChild(atag);
  
  var imgArr = $$('#' + childName + ' p');
  obj.removeChild($(childName));

  for(var i = 0; i < imgArr.length; i += 8){
    var div = document.createElement('div');
    var newId = (Math.floor(i / 8) + 2);
    div.setAttribute('id', newName + newId);
    div.className = 'photo';
    
    var imgs = imgArr.slice(i, i + 8);
    for(var j = 0; j < imgs.length; j++){
      var p_atag = imgs[j].getElementsByTagName('a')[0];
      p_atag.onclick = function(){
        var title = this.getElementsByTagName('span')[0].innerHTML;
        title = title.replace(/<.*?>/g, "");
        disp2('pop.html',this.getElementsByTagName('img')[0].src, title);
        return false;
      }
      /*
      imgs[j].onclick = function(){
        disp2('pop.html',this.src, this.getAttribute('alt'));
        
        return false;
      }
      
      var atag2 = document.createElement('a');
      atag2.setAttribute('href', '#');
      atag2.onclick = function(){
        return false;
      }
      
      atag2.appendChild(imgs[j]);
      */
      div.appendChild(imgs[j]);
    }
    obj.appendChild(div);
    
    atag = document.createElement('a');
    atag.setAttribute('href' , '#' + newName + newId);
    nav.appendChild(atag);
  }
  
}

/**
 * Foodの再配置
 */
function resetFood(menu_name, childName, newName){
  var obj = document.getElementById(menu_name);
  var nav = document.getElementById(newName + "_nav");
   
  //foodのテキストのみの部分を取り出して、DOMから削除
  var txtFood = document.getElementById('food_txt');
  //var txtFoodArr = $$('#food_txt div');
  obj.removeChild(txtFood);
  
  //food_navから現在のDOMを取り出して、DOMから削除
  var aFood = $$('#food_nav a');
  for(var i = 0; i < aFood.length;i++){
    nav.removeChild(aFood[i]);
   }
  
  //food_navに１ページ目のAタグを追加
  var atag = document.createElement('a');
  atag.setAttribute('href' , '#' + newName + '1');
  nav.appendChild(atag);
  
  //サムネイルを取り出して、DOMから削除
  var imgArr = $$('#' + childName + ' p');
  obj.removeChild($(childName));

  //サムネイルを再配置する
  for(var i = 0; i < imgArr.length; i += 8){
    var div = document.createElement('div');
    var newId = (Math.floor(i / 8) + 2);
    div.setAttribute('id', newName + newId);
    div.className = 'photo';
    
    var imgs = imgArr.slice(i, i + 8);
    for(var j = 0; j < imgs.length; j++){
      var p_atag = imgs[j].getElementsByTagName('a')[0];
      p_atag.onclick = function(){
        var title = this.getElementsByTagName('span')[0].innerHTML;
        title = title.replace(/<.*?>/g, "");
        disp2('pop.html',this.getElementsByTagName('img')[0].src, title);
        return false;
      }
     
      div.appendChild(imgs[j]);
    }
    obj.appendChild(div);
    
    atag = document.createElement('a');
    atag.setAttribute('href' , '#' + newName + newId);
    nav.appendChild(atag);
  }
  
  //テキストを再配置する
  for(var i = 0; i < aFood.length; i++){
    nav.appendChild(aFood[i]);
  }
  obj.appendChild(txtFood);
}

/**
 * helloの初期化
 */
function resetHello(menu_name, childName, newName){
  var obj = document.getElementById(menu_name);
  var nav = document.getElementById(newName + "_nav");
  
  var atag = document.createElement('a');
  atag.setAttribute('href' , '#' + newName + '1');
  nav.appendChild(atag);
  
  var imgArr = $$('#' + childName + ' img');
  obj.removeChild($(childName));
  for(var i = 0; i < imgArr.length; i++){
    var div = document.createElement('div');
    var newId = i + 2;
    div.setAttribute('id', newName + newId);
    div.appendChild(imgArr[i]);
    
    obj.appendChild(div);
    
    atag = document.createElement('a');
    atag.setAttribute('href' , '#' + newName + newId);
    nav.appendChild(atag);
  }
  
}

/**
 * スクロール完了ハンドラ
 */
function scrollEnd(el){
  if(!topFlg && page == "top"){
    thisMovie("externalTop").showTop();
    topFlg = true;
  }
}

/**
 * swf用スクロール実行関数
 */
function scrollContents(target_page){
  topFlg = false;
	if(main_nav){ main_nav.clearTimer().toElement($(target_page)); }
}

/**
 * ムービーオブジェクトの取得
 */
function thisMovie(movieName) {
  if(navigator.appName.indexOf("Microsoft") != -1){
    return window[movieName]
  }else{
    return document[movieName]
  }
}

/**
 * サブウィンドウの表示
 */
function disp2(url, src, alt){
  selectedThumb = src.substr(0, src.lastIndexOf('/')) + "_l" + src.substr(src.lastIndexOf('/'));
  selectedThumbTitle = alt;

  win = window.open(url, "window_name", "width=327,height=245,scrollbars=no,resizable=yes,toolbar=no,menubar=no,status=no,location=no");
  win.focus();

}


addDOMLoadEvent(navigation);
