jQuery(function( $ ){
  /**
   * Most jQuery.localScroll's settings, actually belong to jQuery.ScrollTo, check it's demo for an example of each option.
   * @see http://flesler.demos.com/jquery/scrollTo/
   * You can use EVERY single setting of jQuery.ScrollTo, in the settings hash you send to jQuery.LocalScroll.
   */
  
  // The default axis is 'y', but in this demo, I want to scroll both
  // You can modify any default like this
  $.localScroll.defaults.axis = 'xy';
  
  // Scroll initially if there's a hash (#something) in the url 
  $.localScroll.hash({
    target: '#content', // Could be a selector or a jQuery object too.
    queue:true,
    duration:1500
  });
  
  /**
   * NOTE: I use $.localScroll instead of $('#navigation').localScroll() so I
   * also affect the >> and << links. I want every link in the page to scroll.
   */
  $.localScroll({
    target: '#content', // could be a selector or a jQuery object too.
    queue:true,
    duration:1000,
    hash:false,
    onBefore:function( e, anchor, $target ){
      // The 'this' is the settings object, can be modified
    },
    onAfter:function( anchor, settings ){
      // The 'this' contains the scrolled element (#content)
    }
  });

  $('#main a').hover(
    function() {$(this).addClass('hover')},
    function() {$(this).removeClass('hover')}
  );

  $('#navigation a.cat').click(function() {
    clear_hover();
    reset_buttons();
    var id = $(this).children('img').attr('id');
    name = id.replace('navi-','');
    $(this).children('img').attr('src','img/navi/'+name+'_on.png');
    $('#'+id).hover(function(){},function(){this.src='img/navi/'+name+'_on.png'});
    // $('#navigation a').css('color', '');
    // $(this).css('color', '#eee043');
  });

  // main category
  $('.cat').click(function() {
    if ($(this).next().css('display') != 'block') {
      $('#navigation a').next().slideUp('fast');
      $(this).next().slideDown('fast');
    }
  });

  
  $('.next, .prev, .backtohome').click(function() {
    var id = $(this).attr('href');
    // $('#navigation a').css('color', '');
    // $('#navigation a[href='+id+']').css('color', '#eee043');
  });
  
  $('#topButton').click(function() {
    if ($('#head-content-box').css('display') == 'none') {
      $('#head-content-box').slideDown(125);
      $('#topButton a').text('Close Tools');
      $('#topButton span').text('Close Tools');
      return false;
    } else {
      $('#head-content-box').slideUp(125);
      $('#topButton a').text('Open Tools');
      $('#topButton span').text('Open Tools');
      return false;
    }
  });

  $("#topnav li").prepend("<span></span>");

  $("#topnav li").each(function() {
    var linkText = $(this).find("a").html();
    $(this).find("span").show().html(linkText);
  }); 

  $("#topnav li").hover(function() {
    $(this).find("span").stop().animate({
      marginTop: "-20"
    }, 125);
  } , function() {
    $(this).find("span").stop().animate({
      marginTop: "0"
    }, 125);
  });
  
  $('#tab-inq').click(function(){
    $('#navigation a').css('color', '');
    $('#navigation a[href=#section5]').css('color', '#eee043');
  });
  
  $('.fadein div:gt(0)').hide();
  setInterval(function(){$('.fadein div:first-child').fadeOut().next('div').fadeIn().end().appendTo('.fadein');}, 5000);
  
  // var formInputColor = "#333"
  // var formBlurColor = '#999';
  // $('#form-name').focus(function(){
  //   if ($(this).val() == '氏名') $(this).val('').css('color',formInputColor);
  // }).blur(function(){
  //   if ($(this).val() == '') $(this).val('氏名').css('color',formBlurColor);
  // });
  // $('#form-furi').focus(function(){
  //   if ($(this).val() == 'ふりがな') $(this).val('').css('color',formInputColor);
  // }).blur(function(){
  //   if ($(this).val() == '') $(this).val('ふりがな').css('color',formBlurColor);
  // });
  // $('#form-shamei').focus(function(){
  //   if ($(this).val() == '社名') $(this).val('').css('color',formInputColor);
  // }).blur(function(){
  //   if ($(this).val() == '') $(this).val('社名').css('color',formBlurColor);
  // });
  // $('#form-tel').focus(function(){
  //   if ($(this).val() == '電話番号') $(this).val('').css('color',formInputColor);
  // }).blur(function(){
  //   if ($(this).val() == '') $(this).val('電話番号').css('color',formBlurColor);
  // });
  // $('#form-mail').focus(function(){
  //   if ($(this).val() == 'メールアドレス') $(this).val('').css('color',formInputColor);
  // }).blur(function(){
  //   if ($(this).val() == '') $(this).val('メールアドレス').css('color',formBlurColor);
  // });
  // $('#form-naiyou').focus(function(){
  //   if ($(this).val() == 'お問い合わせ内容') $(this).val('').css('color',formInputColor);
  // }).blur(function(){
  //   if ($(this).val() == '') $(this).val('お問い合わせ内容').css('color',formBlurColor);
  // });
  
  changeNavi('home');

  reset_buttons();
  
  $('.page-next').hover(
    function(){this.src='img/next_on.png'},
    function(){this.src='img/next.png'}
  );
  $('.page-prev').hover(
    function(){this.src='img/prev_on.png'},
    function(){this.src='img/prev.png'}
  );

  var mailaddress = 'info'+'@'+'tao-inc.jp';
  $('.mailto').text(mailaddress);

  $('#mobile-map-send').click(function(){
    if ($('#mobile-address').val() == '') {
      alert('メールアドレスを入力してください。');
      return false;
    }
    if($('#mobile-address').val().match("^[0-9A-Za-z._\-]+@[0-9A-Za-z.\-]+$") == null) {
      alert('メールアドレスを正しく入力してください。');
      return false;
    }

    $.ajax({
      url: "mobile_map_send.php",
      type: "post",
      data: ({address: $('#mobile-address').val()}),
      success: function(request) {
        $('#mobile-map').html(request);
      }
    });
  });

});

function reset_buttons() {
  $('#navi-home').hover(
    function(){this.src='img/navi/home_on.png'},
    function(){this.src='img/navi/home.png'}
  );
  $('#navi-work').hover(
    function(){this.src='img/navi/work_on.png'},
    function(){this.src='img/navi/work.png'}
  );
  $('#navi-gallery').hover(
    function(){this.src='img/navi/gallery_on.png'},
    function(){this.src='img/navi/gallery.png'}
  );
  $('#navi-company').hover(
    function(){this.src='img/navi/company_on.png'},
    function(){this.src='img/navi/company.png'}
  );
  $('#navi-access').hover(
    function(){this.src='img/navi/access_on.png'},
    function(){this.src='img/navi/access.png'}
  );
  $('#navi-contact').hover(
    function(){this.src='img/navi/contact_on.png'},
    function(){this.src='img/navi/contact.png'}
  );
}

function clear_hover() {
  $('#navi-home').attr('src','img/navi/home.png');
  $('#navi-work').attr('src','img/navi/work.png');
  $('#navi-gallery').attr('src','img/navi/gallery.png');
  $('#navi-company').attr('src','img/navi/company.png');
  $('#navi-access').attr('src','img/navi/access.png');
  $('#navi-contact').attr('src','img/navi/contact.png');
}

