/*
========================================
Various jQuery Plugins
this script requires jQuery 1.2.x
========================================
*/

/*--------------------------------------
Image Rollover Plugin
--------------------------------------*/

$(function(){
	$('img.rollover, .rollGroup img, input.rollover')
	.not('[@src*="_o."]')
	.mouseover(function(){
		$(this).attr('src',$(this).attr('src').replace(/^(.+)(\.[a-z]+)$/, '$1_o$2'))
	})
	.mouseout(function(){
		$(this).attr('src',$(this).attr('src').replace(/^(.+)_o(\.[a-z]+)$/, '$1$2'));
	})
	.each(function(){
		$('<img>').attr('src',$(this).attr('src').replace(/^(.+)(\.[a-z]+)$/, '$1_o$2'))
	})
	$('img.current')
	.mouseover(function(){
		$(this).attr('src',$(this).attr('src').replace(/^(.+)_c(\.[a-z]+)$/, '$1_o$2'))
	})
	.mouseout(function(){
		$(this).attr('src',$(this).attr('src').replace(/^(.+)_o(\.[a-z]+)$/, '$1_c$2'));
	})
	.each(function(){
		$('<img>').attr('src',$(this).attr('src').replace(/^(.+)_c(\.[a-z]+)$/, '$1_o$2'))
	})
})

/*--------------------------------------
Fading Plugin
--------------------------------------*/

$(function(){
	$('.fading')
	.mouseover(function(){
		$(this).stop().fadeTo('fast', 0.6);
	})
	.mouseout(function(){
		$(this).stop().fadeTo('normal', 1);
	});
});

/*--------------------------------------
Page Scroller Plugin
--------------------------------------*/

(function($){
	$(function(){
		$('.scroll a[href^=#]').initScroll();
	});
	$.fn.initScroll = function(){
		return (this.each(function(){
			$(this).click(function(){
				var target = $('#' + this.getAttribute('href').split('#')[1]);
				if (target){
					$.scroller.start.init({
						endY:target.offset().top
					});
					return false;
				}
			});
		}));
	};
	$.scroller = {
		start: (function(){
			var params;
			var timerId;
			var stepCount = 0;
			var startY = 0;
			var endY = 0;
			var lastY = 0;
			function move(){
				if (stepCount == params.step){
					window.scrollTo(getCurrentX(), params.endY);
					stepCount = 0;
				}
				else if (lastY == getCurrentY()){
					stepCount++;
					window.scrollTo(getCurrentX(), getEasingY());
					lastY = getEasingY();
					timerId = setTimeout(move, Math.floor(1000/params.fps));
				}
			};
			var getCurrentY = function(){
				return (document.body.scrollTop || document.documentElement.scrollTop);
			}
			var getCurrentX = function(){
				return (document.body.scrollLeft || document.documentElement.scrollLeft);
			}
			var getEasingY = function(){
				return (Math.floor(getEasing(params.startY, params.endY, stepCount, params.step, params.easing)));
			}
			var getEasing = function(start, end, stepCount, step, easing){
				var s = stepCount / step;
				return ((end - start) * (s + easing / (100 * Math.PI) * Math.sin(Math.PI * s)) + start);
			}
			return {
				init: function(options){
					params = $.extend({
						easing:100,
						step:30,
						fps:80
					}, options);
					stepCount = 0;
					lastY = params.startY = getCurrentY();
					timerId = setTimeout(move, Math.floor(1000/params.fps));
				}
			};
		})()
	};
})(jQuery);

/*--------------------------------------
Stripe Row Plugin
--------------------------------------*/

$(function(){
	$('ul li:nth-child(odd), table tr:nth-child(odd)').addClass('odd'); // 奇数行
	$('ul li:nth-child(even), table tr:nth-child(even)').addClass('even'); // 偶数行
});

/*--------------------------------------
Simple Tabs Plugin
--------------------------------------*/

$(function(){
	$('.tabNav').each(function(){
		var tabs = $(this).find('a[href^=#]');
		var contents;
		tabs.each(function(){
			var selecter = $(this).attr('href');
			if (contents) {
				contents = contents.add(selecter);
			} else {
				contents = $(selecter);
			};
			$(this).click(function(){
				tabs.removeClass('active');
				$(this).addClass('active');
				$(selecter).fadeIn();
				contents.hide();
				$(selecter).show();
				return false;
			});
		});
		tabs.eq(0).trigger('click'); // eq(index)番目のタブを現行選択とする
	});
});

/*--------------------------------------
Input Dummy Value Plugin
--------------------------------------*/

$(function(){
	$('#search').css('color', '#999')
	.focus(function(){
		if($(this).val() == $(this).attr('defaultValue'))
			$(this).css('color', '#000').val('');
	})
	.blur(function(){
		if(jQuery.trim($(this).val()) == ''){
			$(this).css('color', '#999').val($(this).attr('defaultValue'));
		}
	});
});

/*--------------------------------------
Focus Form Plugin
--------------------------------------*/

$(function(){
	$('input[type="text"], input[type="password"], textarea, select').addClass('idle')
	.focus(function(){
		$(this).addClass('focus');
	})
	.blur(function(){
		$(this).removeClass('focus');
	});
});

/*--------------------------------------
External Link Plugin
--------------------------------------*/

$(function(){
	$('.external').click(function(){
		window.open(this.href, '_blank');
		return false;
	});
});

/*--------------------------------------
Pop Up Window Plugin
--------------------------------------*/

$(function(){
	$('.popupWin').click(function(){
		w=window.open(this.href, 'popupWin', 'top=0, left=0, width=640, height=480, scrollbars=1, status=1');
		w.focus();
		return false;
	});
});
