// Need Common Class.
if (!('Common' in this)) document.write('<script type="text/javascript" src="/_common/js/common.js"></script>');

/**
 * Navigator
 */
function Navigator(ids) {

  if (ids['ruby']) {
    this.ruby = new NavigatorRuby(ids['ruby']);
  }
  if (ids['speaker']) {
    //this.speaker = new NavigatorSpeaker(ids['speaker']);
  }

  this.onLoad = function() {
    if (this.ruby) {
      this.ruby.onLoad();
    }
    if (this.speaker) {
      this.speaker.onLoad();
    }
  }
}

function Navigator_initialize(ids) {
  var nav = new Navigator(ids);
  Common.addEvent(window, 'load', function(){ nav.onLoad() });
}
Navigator.initialize = Navigator_initialize;

function Navigator_showMessage() {
	var elm = document.getElementById('furiganaAttention');
	if (elm) {
		return true;
	}
  if (elm = document.getElementById('headerBody')) {
    var msg = document.createElement('div'); 
    msg.id = 'furiganaAttention'; 
    msg.innerHTML = 'ふりがなと読み上げ音声は、システムにより自動的に生成しています。' +
		  '人名、地名、用語などが、正確に発音されない場合がありますので、ご了承ください。';
	  elm.insertBefore(msg, elm.firstChild);
  }
}
Navigator.showMessage = Navigator_showMessage;

/**
 * Navigator Ruby.
 */
function NavigatorRuby(id) {

  this.elementId = id;
  this.cookie    = 'navigator_ruby';

  this.onLoad = function() {
    this.render();
    var _this = this;
    Common.addEvent(document.getElementById(this.elementId), 'click', function(){
      return _this.changeRuby();
    });
  }

  this.changeRuby = function(id) {
    if (Common.getCookie(this.cookie) == 'on') {
      Common.setCookie(this.cookie, 'off');
    } else {
      Common.setCookie(this.cookie, 'on');
    }
    this.redirect();
    return false;
  }

  this.redirect = function(){
		if (Common.getCookie(this.cookie) == 'on') {
		  if (location.pathname.search(/\.html$/i) != -1) {
	      location.href = location.pathname.replace(/\.html/, ".html.r") + location.search;
  	  } else if (location.pathname.search(/\.html\.mp3$/i) != -1) {
        location.href = location.pathname.replace(/\.html\.mp3/, ".html.r") + location.search;
		  } else if (location.pathname.search(/\/$/i) != -1) {
	      location.href = location.pathname + "index.html.r" + location.search;
		  }
		} else {
		  if (location.pathname.search(/\.html\.r/i) != -1) {
	      location.href = location.pathname.replace(/\.html\.r/,".html") + location.search ;
		  }
		}
  }
  
  this.render = function() {
    if (Common.getCookie(this.cookie) == 'on') {
      document.getElementById(this.elementId).className += ' rubyOn';
      //document.getElementsByTagName('body')[0].className += ' rubyOn';
      Navigator.showMessage();
    } else {
      document.getElementById(this.elementId).className += ' rubyOff';
    }
  }
}

/**
 * Navigator Speaker.
 */
function NavigatorSpeaker(id) {

  this.elementId = id;

  this.onLoad = function() {
    var _this = this;
    Common.addEvent(document.getElementById(this.elementId), 'click', function(){
      return _this.speak();
    });
  }

  this.speak = function() {
    location.href = '/_tool/talks' + location.pathname;
    return false;
  }
}

