function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; 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;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
  
  var sound_cookie = readCookie("sound_off");
  var element_1 = document.getElementById("sound_on"); 
  var element_2 = document.getElementById("sound_off"); 
  var this_url = location.href;
  
  if( sound_cookie == 1 ){
    element_1.style.display = 'none';
    element_2.style.display = 'block'; 
  }
  else{
    playmp3.loadNo("/js/onRollover.mp3", 0);
    playmp3.setVolumeNo(0, 10);
    playmp3.loadNo("/js/onClick.mp3",  1);
    playmp3.setVolumeNo(1, 20);
    
    if( this_url != 'http://ph-hyogo.tambourine.co.jp/' && this_url != 'http://www.ph-hyogo.com/' && this_url != 'http://hs11.wadax.ne.jp/~ph-hyogo-com/' ){
      playmp3.playNo(1);
      
      var tags=document.getElementsByTagName("*");
      for(var i=0;i<tags.length;i++){
        if(tags[i].id=="menu"){
          tags[i].onmouseover=function(){
            playmp3.playNo(0);
          }
        }
      }
    }
    
    element_1.style.display = 'block';
    element_2.style.display = 'none'; 
  }
  
  //トップページFLASH
  
  var flashvars = {
    soundvol: "on"
  };
  if( sound_cookie == 1 ){
    flashvars = {
      soundvol: ""
    };
  };
  var params = {
    menu: "false",
    scale: "noScale",
    allowFullscreen: "true",
    allowScriptAccess: "sameDomain",
    bgcolor: "#FFFFFF"
  };
  var attributes = {
    id:"top7"
  };
  var tm = new Date();
  
  swfobject.embedSWF("top7.swf?" + tm.getDate(), "flash_area2", "950", "243", "8.0.0", "top7.swf", flashvars, params, attributes);
  
  if( this_url.substr(-3, 3) == 'cgi' ){
    //カレンダー用
    setCalendar();
    
    //入力内容チェック
    var node_diary_form = document.getElementById('diary_form');
    if (node_diary_form) {
      node_diary_form.onsubmit = function() {
        return checkDiaryForm(node_diary_form);
      };
    }
    var node_comment_form = document.getElementById('comment_form');
    if (node_comment_form) {
      node_comment_form.onsubmit = function() {
        return checkCommentForm(node_comment_form);
      };
    }
  }
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

