/* ================================================
  Author     ： u8.f
  Since      ： 2010-04-20
  Modified   ： 2010-07-21
  Script Info： ウェブサイト用基本処理
                jQuery 1.4.2 or higher
================================================ */

(function($) {
	
	/* ============================================
	   コメントアウトで機能オフ
	============================================ */
	
	$(function() {
		$.config.addFirstChild({
			selector: '#sub ul.basicList'
		});
		//$.config.addLastChild();
		$.config.addHoverClass({
			selector: '#sub div.aboutList li'
		});
		$.config.rollover();
		$.config.selfLink();
		$.config.externalLink({
			notWindow: 'a[href^="http://www.ymzn.co.jp/"]'
		});
		$.config.scroll();
		
		$('#sub div.aboutList li').each(function() {
			$(this).wrapInner('<a href="' + $(this).find('a').attr('href') + '"></a>');
		});
	});
	
	/* ========================================= */
	
	$.config = {
		addFirstChild: function(options) {
			var c = $.extend({
				selector: '',
				addClass: 'firstChild'
			}, options);
			
			$(c.selector).each(function() {
				$(':first-child', this).addClass(c.addClass);
			});
		},
		addLastChild: function(options) {
			var c = $.extend({
				selector: '',
				addClass: 'lastChild'
			}, options);
			
			$(c.selector).each(function() {
				$(':last-child', this).addClass(c.addClass);
			});
		},
		addHoverClass: function(options) {
			var c = $.extend({
				selector: '',
				addClass: 'hover'
			}, options);
			
			$(c.selector).each(function() {
				$(this).hover(function() {
					$(this).addClass(c.addClass);
				}, function(){
					$(this).removeClass(c.addClass);
				});
			});
		},
		rollover: function(options) {
			var c = $.extend({
				hoverSelector: '#globalNav li',
				hoverfix: '_over',
				alphaSrc: 'http://www.ymzn.co.jp/img/common/alpha.gif'
			}, options);
			
			var rolloverImg = $(c.hoverSelector).find('img');
			
			rolloverImg.each(function() {
				this.originalSrc = $(this).attr('src');
				this.rolloverSrc = this.originalSrc.replace(/^(.+)(\.[a-z]+)$/, '$1' + c.hoverfix + '$2');
				$('<img>').attr('src', this.rolloverSrc);
			});
			
			if ($.browser.msie && $.browser.version < 7 && rolloverImg.attr('src').indexOf('png') != -1) {
				rolloverImg.each(function() {
					var imgWidth = $(this).parent().width();
					var imgHeight = $(this).parent().height();
					
					$(this)
						.data('pngSrc', this.originalSrc)
						.attr('src', c.alphaSrc)
						.css({
							width: imgWidth + 'px',
							height: imgHeight + 'px',
							filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + $(this).data('pngSrc') + '",sizingMethod="image")'
						});
				}).mouseover(function() {
					$(this).css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + $(this).data('pngSrc').replace(/^(.+)(\.[a-z]+)$/, '$1' + c.hoverfix + '$2') + '",sizingMethod="image")');
				}).mouseout(function() {
					$(this).css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + $(this).data('pngSrc') + '",sizingMethod="image")');
				});
			}
			else {
				rolloverImg.mouseover(function() {
					$(this).attr('src', this.rolloverSrc);
				}).mouseout(function() {
					$(this).attr('src', this.originalSrc);
				});
			}
		},
		selfLink: function(options) {
			var c = $.extend({
				selfSelector: '#globalNav li',
				addClass: 'current',
				selffix: '_over',
				depth: '2'
			}, options);
			
			$(c.selfSelector).each(function() {
				var anchor = $(this).find('a');
				var myURL = anchor.attr('href');
				var currentURL = location.href;
				
				var selfImg = $(this).find('img');
				this.currentImg = selfImg.attr('src').replace(/^(.+)(\.[a-z]+)$/, '$1' + c.selffix + '$2');
				
				if (myURL == '/') {
					if (location.pathname.split('/').length <= c.depth) {
						anchor.addClass(c.addClass);
						if (selfImg) selfImg.attr('src', this.currentImg).unbind();
					}
				}
				else if (currentURL.indexOf(myURL) != -1) {
					anchor.addClass(c.addClass);
					if (selfImg) selfImg.attr('src', this.currentImg).unbind();
				}
			});
		},
		externalLink: function(options) {
			var c = $.extend({
				addClass: 'externalLink',
				notWindow: ''
			}, options);
			
			$('a[href*="http"], a[href$=".pdf"]').not(c.notWindow).each(function() {
				$(this).addClass(c.addClass).click(function() {
					window.open(this.href, '_blank');
					return false;
				});
			});
		},
		scroll: function(options) {
			var c = $.extend({
				easing: $.easing['easeOutQuint'] ? 'easeOutQuint' : 'swing',
				duration: 800
			}, options);
			
			var boxModel = $.support.boxModel ? navigator.appName.match(/Opera/) ? 'html' : 'html, body' : 'body';
			
			$('a[href*="#"]').each(function() {
				if (this.hash && $(this.hash).length > 0) {
					$(this).click(function(e) {
						var offset = $(this.hash).offset();
						
						$(boxModel).animate({
							scrollTop: offset.top,
							scrollLeft: offset.left
						}, c.duration, c.easing, false);
						
						e.preventDefault();
						e.stopPropagation();
					});
				}
			});
			
			$(document).click(function(e) {
				$(boxModel).stop();
			});
		}
	};
})(jQuery);
