﻿(function ($) {
	$.fn.tabMenu = function (options) {
		var o = {  // 缺省设置
			type : "mouseover",  // 事件类型
			currentClass : "current",  // 切换的当前类
			tabListItem : null,  // 导航列表项 (如：tablist中的项)
			targetBoxItem : null,  // 切换目标列表项 (如：detailBox中的项)
			currentIndex : 0,  // 初始化索引
			callback : null  // 回调函数
		}
		for (var p in options) {  // 载入自定义设置
			o[p] = options[p];
		}
		$.each(this, function (idx, element) {
			var ele = $(element),
				tabListItem = typeof o.tabListItem=="function" ? o.tabListItem(ele) : o.tabListItem,
				targetBoxItem = typeof o.targetBoxItem=="function" ? o.targetBoxItem(ele) : o.targetBoxItem;
			if (o.tabListItem.length < o.currentIndex + 1) {
				o.currentIndex = 0;
			}
			targetBoxItem.each(function () {
				var cur = $(this);
				cur.data("position", cur.css("position"));
				cur.data("visibility", cur.css("visibility"));
				cur.data("left", cur.css("left"));
				cur.data("top", cur.css("top"));
			});
			function skipToItem(idx) {  // 跳转函数
				var idx_2 = idx;
				tabListItem.removeClass(o.currentClass);
				targetBoxItem.each(function () {
					var cur = $(this);
					cur.css({
						"position" : "absolute",
						"visibility" : "hidden",
						"left" : "-9999px",
						"top" : "0"
					});
				});
				tabListItem.eq(idx).addClass(o.currentClass);
				if (idx_2+1 > targetBoxItem.length) {
					idx_2 = targetBoxItem.length-1;
				}
				var curItem = targetBoxItem.eq(idx_2);
				curItem.css({
					"position" : curItem.data("position"),
					"visibility" : "visible",
					"left" : curItem.data("left"),
					"top" : curItem.data("top")
				});
				curItem.show();
				if (o.callback != null && typeof o.callback == "function") {
					o.callback(idx);
				}
			}

			skipToItem(o.currentIndex);  // 初始化项目

			tabListItem.each(function (idx, element) {
				var ele = $(element);
				if (element.tagName.toLowerCase() != "a") {
					ele = $(element).find("a").eq(0);
				}
				ele[0].hideFocus = true;
				ele.css("outline", "0 none");
				ele[o.type](function (event) {  // 添加事件
					skipToItem(idx);
					event.preventDefault();
				});
			});
		})
	};
})(jQuery);
$().ready(function () {
	$(".placeholder").each(function () {
		var c = $(this);
		if (c.data("placeholder") == "true") {
			return;
		}
		c.data("color", c.css("color"));
		if (this.value == this.defaultValue) {
			c.css("color", "#CCCCCC");
		}
		c.focus(function () {
			if (this.value == this.defaultValue) {
				this.value = "";
				c.css("color", c.data("color"));
			}
		}).blur(function () {
			if (this.value == "") {
				this.value = this.defaultValue;
				c.css("color", "#CCCCCC");
			}
		});
		c.data("placeholder","true");
	});
});
