/**
 * 共通プラグイン
 * @author sano
 */
(function($){

	/**
	 * 一覧表示テーブルに関する処理をまとめて実行
	 *
	 * @author sano
	 */
	$.fn.indexTable = function() {
		$(this).find("tbody tr").hover(function(){
				$(this).addClass("hover");
			}, function(){
				$(this).removeClass("hover");
			});

		$(this).find("tbody tr:odd").addClass("odd");
	};


	/**
	 * 指定されたエレメントに時計を表示する
	 */
	$.fn.showWatch = function(options) {
		var elements = this;
		var op = $.extend({}, options);

		elements.each(function(){
			var element = $(this);
			var zeroPadding = function(s, len) {
				if(!(s instanceof String)) {
					s = s.toString();
				}
				if(s.length < len) {
					s = ("0" * len) + s;
					return s.substr(s.length - len);
				}
				return s;
			}
			var showWatch = function() {
				var now = new Date();
				var year = now.getFullYear();
				var month = zeroPadding(now.getMonth() + 1, 2) ;
//				var day = zeroPadding(now.getDay(), 2);
				var day = zeroPadding(now.getDate(), 2);
				var hour = zeroPadding(now.getHours(), 2);
				var min = zeroPadding(now.getMinutes(), 2);
				var sec = zeroPadding(now.getSeconds(), 2);
				element.html(year + "/" + month + "/" + day + " " + hour + ":" + min + ":" + sec);
				setTimeout(showWatch, 1000);
			};
			showWatch();
		});
	};

	/** CakePHPのflashMessageをtwitterのメッセージ風に表示する. */
	$.fn.flashMessage = function(options){
		var elements = this;
		var op = $.extend({sec:30}, options);
		elements.each(function(){
			var element = $(this);
			var close = function(){element.slideUp();};
			element.slideDown();
			//element.click(close);
			//setTimeout(close, op.sec * 1000);
		});
		return this;
	};
	/** preload images */
	$.fn.preloadImages=function(){var images=this;images.each(function(){$("<img/>").attr("src",$(this));});return this;};
})(jQuery);

