var AAB = AAB || {};
AAB.TabGift = AAB.TabGift || {};

AAB.TabGift.fnGetElement = function() {
	this.jTab = $("#productTab");
	this.jProducts = $("#productList");
}

AAB.TabGift.fnCreateMenu = function() {
	this.jNew = this.jProducts.find(".wrap").children("div[class*='New']");
	this.jDIY = this.jProducts.find(".wrap").children("div[class*='DIY']");
	this.jSole = this.jProducts.find(".wrap").children("div[class*='Sole']");
	this.jUnique = this.jProducts.find(".wrap").children("div[class*='Unique']");
	var li = "";
	if (this.jNew.size() > 0) {
		li += "<li data-tag='New'>全新特惠</li>";
	}
	if (this.jDIY.size() > 0) {
		li += "<li data-tag='DIY'>超值随心配</li>";
	}
	if (this.jSole.size() > 0) {
		li += "<li data-tag='Sole'>独家套装</li>";
	}
	if (this.jUnique.size() > 0) {
		li += "<li data-tag='Unique'>全球绝版限量</li>";
	}
	if (li.length > 0) {
		$("#sidebar").append("<div id='giftNav'><h2 class='current' data-tag='All'>尊享特惠</h2><ul>" + li + "</ul></div>");
		this.jMenuCurrent = $("#giftNav h2");
	}
}

AAB.TabGift.fnCraeteBtn = function() {
	this.productSuffix = this.jProducts.attr("class");
	this.jSkincare = $((".skincare" + this.productSuffix));
	this.jMakeup = $((".makeup" + this.productSuffix));
	this.jAccessories = $((".accessories" + this.productSuffix));
	if (this.jSkincare.size() > 0) {
		this.jTab.append("<li class='skincare' data-tag='skincare" + this.productSuffix + "'>护&nbsp;&nbsp;&nbsp;&nbsp;肤</li>");
	}
	if (this.jMakeup.size() > 0) {
		this.jTab.append("<li class='makeup' data-tag='makeup" + this.productSuffix + "'>彩&nbsp;&nbsp;&nbsp;&nbsp;妆</li>");
	}
	if (this.jAccessories.size() > 0) {
		this.jTab.append("<li class='accessories' data-tag='accessories" + this.productSuffix + "'>工&nbsp;&nbsp;&nbsp;&nbsp;具</li>");
	}
	this.jTabCurrent = this.jTab.find("li:first");
}

AAB.TabGift.fnResetBtn = function() {
	this.jTabCurrent.removeClass("current");
	this.jTabCurrent = this.jTab.find("li:first");
	this.jTabCurrent.addClass("current");
	this.productSuffix = this.jProducts.attr("class");
	this.jSkincare = $((".skincare" + this.productSuffix));
	this.jMakeup = $((".makeup" + this.productSuffix));
	this.jAccessories = $((".accessories" + this.productSuffix));
	if (this.jSkincare.size() > 0) {
		this.jTab.find(".skincare").attr("data-tag",("skincare" + this.productSuffix)).show();
	} else {
		this.jTab.find(".skincare").hide();
	}
	if (this.jMakeup.size() > 0) {
		this.jTab.find(".makeup").attr("data-tag",("makeup" + this.productSuffix)).show();
	} else {
		this.jTab.find(".makeup").hide();
	}
	if (this.jAccessories.size() > 0) {
		this.jTab.find(".accessories").attr("data-tag",("accessories" + this.productSuffix)).show();
	} else {
		this.jTab.find(".accessories").hide();
	}
}

AAB.TabGift.bindBtn = function() {
	this.jTab.click(function(e) {
		if (e.target != this && e.target.className.indexOf("current") === -1) {
			var tag = e.target.getAttribute("data-tag");
			AAB.TabGift.jTabCurrent.removeClass("current");
			AAB.TabGift.jTabCurrent = $(e.target);
			AAB.TabGift.jTabCurrent.addClass("current");
			if (AAB.TabGift.jTabCurrent.attr("class").indexOf("all") != -1) {
				var tag = AAB.TabGift.jMenuCurrent.attr("data-tag");
				if (tag == "All") {
					AAB.TabGift.jProducts.find(".product").show();
				} else {
					AAB.TabGift.jProducts.find(".product").each(function() {
						if (this.className.indexOf(tag) != -1) {
							$(this).show();
						} else {
							$(this).hide();
						}
					});
				}
			} else {
				AAB.TabGift.jProducts.find(".product").each(function() {
					if (this.className.indexOf(tag) != -1) {
						$(this).show();
					} else {
						$(this).hide();
					}
				});
			}
		}
	});
	$("#giftNav").click(function(e) {
		if ((e.target.nodeName == "LI" || e.target.nodeName == "H2") && e.target.className.indexOf("current") === -1) {
			var tag = e.target.getAttribute("data-tag");
			AAB.TabGift.jMenuCurrent.removeClass("current");
			AAB.TabGift.jMenuCurrent = $(e.target);
			AAB.TabGift.jMenuCurrent.addClass("current");
			AAB.TabGift.jProducts[0].className = tag;
			if (tag.indexOf("All") != -1) {
				AAB.TabGift.jProducts.find(".product").show();
			} else {
				AAB.TabGift.jProducts.find(".product").each(function() {
					if (this.className.indexOf(tag) != -1) {
						$(this).show();
					} else {
						$(this).hide();
					}
				});
			}
			AAB.TabGift.fnResetBtn();
		}
	});
	$("#giftNav h2,#giftNav li").hover(function(){
		$(this).addClass("hover");
	}, function() {
		$(this).removeClass("hover");
	});

}

AAB.TabGift.ini = function() {
	this.fnGetElement();
	this.fnCreateMenu();
	this.fnCraeteBtn();
	this.bindBtn();
}

function fnChangeProductLinks() {
	var data = [];
	data.push(["10422","/shuuemura/_zh/_cn/Xmasx/Xmasx_collection/10Xmas_portable_brush_set.aspx"]);
	data.push(["10421","/shuuemura/_zh/_cn/Xmasx/Xmasx_collection/Christmas_advanced_formula.aspx"]);
	data.push(["10430","/shuuemura/_zh/_cn/skincare/cleansing-oils/cleansing_oil_100ml.aspx"]);
	data.push(["10431","/shuuemura/_zh/_cn/skincare/serum/skin_carekit_red.aspx"]);
	data.push(["CSGS06","/shuuemura/_zh/_cn/skincare/cleansing-oils/face_architect_essential_kit.aspx"]);
	for (var i=0; i<data.length; i++) {
		$("#productList .productImg img[src$='" + data[i][0] + "_m.jpg']").parent().parent().find("a").each(function() {
			this.href = data[i][1];
		});
	}
}

$(function() {
	AAB.TabGift.ini();
});
