$.extend($.expr[':'], {
  external: function(a,i,m) {
    if (!a.href) {
      return false;
    }
    return a.hostname && a.hostname !== window.location.hostname;
  }
});
$.fn.extend({
  equalHeights: function(children) {
    $(this).each(function() {
      var tallest = 0;
      $(this).find(children).each(function() {
        var thisHeight = $(this).height();
        if (thisHeight > tallest) {
          tallest = thisHeight;
        }
      }).height(tallest);
    });
  },
  openWindow: function() {
    $(this).click(function() {
      window.open(this.href);
      return false;
    });
  },
	setCurrent: function(url) {
		$(this).find('a').each(function() {
		  if ($(this).attr('href') == url) {
				$(this).replaceWith('<em class="current">' + $(this).html() + '</em>');
			}
		});
		$('#local-nav ul.nav').findCurrent();
	},
	setCurrentParent: function(url) {
		$(this).find('a').each(function() {
		  if ($(this).attr('href') == url) {
				$(this).wrap('<em class="current-parent"></em>');
			}
		});
		$('#local-nav ul.nav').findCurrent();
	},
	findCurrent: function() {
		$(this)
			.find('li:has(li:has(em.current)) > a, li:has(em.parent) > a, li:has(em.current-parent) > a').wrap('<em class="parent"></em>').end()
			.find('a + ul').addClass('close').end()
			.find('em.current + ul, ul:has(em.current), ul:has(em.current-parent)').removeClass('close').addClass('open').end()
			.find('li:has(ul.open)').addClass('has-ul-open').end()
			.find('li:has(em.current-parent)').addClass('has-current-parent').end()
			.find('li:has(span.no-link)').addClass('has-no-link');
	}
});

$(function(){

  $('body')
    .addClass('js-enabled')
    .find(':first-child').addClass('first-child').end()
    .find(':last-child').addClass('last-child');

  var browserName;
  var browserVer;
  if ($.browser.msie) {
    browserName = 'msie';
    switch ($.browser.version) {
      case '6.0': browserVer = 'v6'; break;
      case '7.0': browserVer = 'v7'; break;
      case '8.0': browserVer = 'v8'; break;
    }
    $('body').addClass(browserVer);
  } else if ($.browser.mozilla) {
    browserName = 'mozilla';
  } else if ($.browser.safari) {
    browserName = 'safari';
  } else if ($.browser.opera) {
    browserName = 'opera';
  } else {
    browserName = 'unknown';
  }
  $('body').addClass(browserName);

  // link
  $('a:external:not([href*="nikkiso.co.jp/"]):not([href*="nikkiso.com/"])').addClass('external');
  $('a[href$=".pdf"]').addClass('pdf').openWindow();
  $('a[href$=".xls"]').addClass('xls').openWindow();
  $('a.external').openWindow();

  // open-window
  $('a.open-window').click(function() {
    var targetWinName = 'winB';
    if (window.name == 'winB') {
      targetWinName = 'winA';
    } else {
      window.name = 'winA';
    }
    window.open(this.href, targetWinName).focus();
    return false;
  });

  // font-size
  $('#fontsize a').click(function() {
    switchStyle($(this).attr('title'));
    return false;
  });
  var c = $.cookie('style');
  if (c) switchStyle(c);
  function switchStyle(styleName) {
    $('link[rel*="stylesheet"][title]').each(function() {
      this.disabled = true;
      if ($(this).attr('title') == styleName) this.disabled = false;
    });
    $.cookie('style', styleName, {
      expires: 30,
      path: '/'/*,
      domain: 'nikkiso.co.jp'*/
    });
  }

  // rollover
  $('li.current img.btn').each(function() {
    this.normalSrc = $(this).attr('src');
    this.hoverSrc = this.normalSrc.replace(/(\.gif|\.jpg|\.png)/, '-h$1');
    this.hoverImg = new Image;
    this.hoverImg.src = this.hoverSrc;
    $(this).attr('src',this.hoverSrc);
    $(this).removeClass('btn');
  });
  $('img.btn, input.btn').each(function() {
    this.normalSrc = $(this).attr('src');
    this.hoverSrc = this.normalSrc.replace(/(\.gif|\.jpg|\.png)/, '-h$1');
    this.hoverImg = new Image;
    this.hoverImg.src = this.hoverSrc;
  }).hover(function() {
    $(this).attr('src', this.hoverSrc);
  }, function() {
    $(this).attr('src', this.normalSrc);
  });

  // ui-tabs
  $('div.ui-tabs > ul.nav').tabs();
  $('div.ui-tabs-l > ul.nav').tabs();
  $('ul.ui-tabs-nav a').addClass('ui-tabs-nav-a');
  $('ul.ui-tabs-nav').equalHeights('a.ui-tabs-nav-a');

  // scroll
  if (!$.browser.opera) {
    $('*[href="#"]:not(a.print-this-page):not(a.switcher)').click(function() {
      $('html,body').animate({scrollTop: 0}, 800, 'easeOutExpo');
      return false;
    });
    $('*[href^="#"]:not(*[href="#"]):not(.ui-tabs-nav-a)').click(function() {
      var targetOffset = $(this.hash).offset().top;
      var documentHeight = $(document).height();
      var windowHeight = $(window).height();
      if ((documentHeight - targetOffset) < windowHeight) {
        targetOffset = (documentHeight - windowHeight);
      }
      $('html, body').animate({scrollTop: targetOffset}, 800, 'easeOutExpo');
    });
  }

  // .pager
  $('div.pager ul li:empty').addClass('empty');
  $('div.pager ul li').hover(function() {
    $(this).addClass('hover');
  }, function() {
    $(this).removeClass('hover');
  }).click(function() {
    window.location = $(this).find('a').attr('href');
  });

  // ul.links
  $('ul.links li:not(:has(a))').addClass('not-has-a');

  // .columns
  $('div.columns.two:has(div.column:only-child)').addClass('one-half');
  $('div.columns.three').each(function() {
    var n = $(this).find('div.column').length;
    switch (n) {
      case 1: $(this).addClass('one-third'); break;
      case 2: $(this).addClass('two-thirds'); break;
    }
  });
  $('div.columns:not(:has(div.column-body))').addClass('not-has-body');
  $('.column-header:not(:has(a))').addClass('not-has-a');
  $('.column-header a.pdf').wrapInner('<span class="download"></span>');
  $('div.columns').equalHeights('.column-header a');
  $('div.column-body li:not(:has(a))').addClass('not-has-a');

  // #aside ul.nav
  $('#news2011 #ln-past #ln-2011 > a, #news2010 #ln-past #ln-2010 > a, #news2009 #ln-past #ln-2009 > a, #news2008 #ln-past #ln-2008 > a, #news2007 #ln-past #ln-2007 > a, #news2006 #ln-past #ln-2006 > a, #news2005 #ln-past #ln-2005 > a, #news2004 #ln-past #ln-2004 > a, #news2003 #ln-past #ln-2003 > a, #news2002 #ln-past #ln-2002 > a, #news2001 #ln-past #ln-2001 > a, #news2000 #ln-past #ln-2000 > a, #news1999 #ln-past #ln-1999 > a').each(function() {
    $(this).wrap('<em class="current-parent"></em>');
  });
	$('#products-particle-technical #ln-technical > a, #canned #ln-canned > a').each(function() {
    $(this).wrap('<em class="parent"></em>');
	});
  $('#aside ul.nav').setCurrent(window.location.pathname);
	
  // table
  $('th:empty, td:empty').addClass('empty');
  $('table:has(.empty)').addClass('has-empty');
  $('table:has(caption)').addClass('has-caption');
	$('table.downloads')
		.find('tr:only-child').addClass('only-child').end()
		.find('tr:nth-child(odd)').addClass('odd').end()
		.find('tr:nth-child(even)').addClass('even');

	// Facebox
	$('a.subwin').facebox();

  // .toc
  $('.msie.v6, .msie.v7').find('div.toc a').prepend('<span>&nbsp;</span>');

  // pngfix
  $('.msie.v6 img[src$=".png"]').pngfix();

});
