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

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

(function($){
	$(function(){
		$.rollover.init();
	});
	$.rollover = {
		init: function(){
			$('img.rollover, .rollGroup img, input.rollover')
				.not('[@src*="_o."]')
				.mouseover(function(){
					this.src = this.src.replace(/^(.+)(\.[a-z]+)$/, '$1_o$2');
				})
				.mouseout(function(){
					this.src = this.src.replace(/^(.+)_o(\.[a-z]+)$/, '$1$2');
				})
				.each(this.preload);
			$('img.current')
				.mouseover(function(){
					this.src = this.src.replace(/^(.+)_c(\.[a-z]+)$/, '$1_o$2');
				})
				.mouseout(function(){
					this.src = this.src.replace(/^(.+)_o(\.[a-z]+)$/, '$1_c$2');
				})
				.each(this.preload);
		},
		preload: function(){
			this.preloaded = new Image;
			this.preloaded.src = this.src.replace(/^(.+)(\.[a-z]+)$/, '$1_o$2');
		}
	};
})(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'); // 偶数行
});

/*--------------------------------------
Tab Navigation 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');
				contents.hide();
				$(selecter).show();
				return false;
			});
		});
		tabs.eq(0).trigger('click'); // eq(index)番目のタブを現行選択とする
	});
});

/*--------------------------------------
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);

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

$(function(){
	jQuery.fn.inputDummyValue = function(config){
		config = jQuery.extend({
			value: "",
			color: ""
		},config);
		var target = this;
		var setDummy = function() {
			target.css('color',config.color);
			target.val(config.value);
		};
		$(target)
			.focus(function() {
				if($(this).val() !== config.value) return;
				$(this).val('');
				$(this).css('color','');
			})
			.blur(function() {
				if($(this).val()) return;
				setDummy();
			});
		setDummy();
		return this;
	};
	$(function(){
		var dummyValue = {
			value: "KEYWORD(S)",
			color: "#999"
		};
		$('input#s').inputDummyValue(dummyValue);
	});
});

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

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

