﻿$(document).ready(function() {
	$("a").click(function(e) {
		if ($(this).attr("href") == "#")
			e.preventDefault();
	}).focus(function() {
		$(this).blur();
	});
	
	//시선집중 인기제품
	generateTabs("div.hit-pro-body>p>a", "div.hit-pro-body>ul");
	//리얼 제품평
	generateTabs("div.real-review-body>p>a", "div.real-review-body>ul");
	//신제품
	generateTabs("div.new-pro-body>p>a", "div.new-pro-body>ul");
});

function fireEvent(){
	setTimeout(function(){
		$("]l.pro-list>li>span.pro-img").each(function(i){
			$(this).mouseover(function(){
				$("]l.pro-list>li>span.btn-area:eq(" + i + ")").show();
				$("]l.pro-list>li>span.img-png:eq(" + i + ")").show();
			}).mouseout(function(){
				$("]l.pro-list>li>span.btn-area").hide();
				$("]l.pro-list>li>span.img-png").hide();
			});
		});
		
		$("]l.pro-list>li>span.btn-area").each(function(i){
	  	$(this).mouseover(function(){
	  		$("]l.pro-list>li>span.btn-area:eq(" + i + ")").show();
	  	}).mouseout(function(){
	  		$(this).hide();
	  	});
		});
	}, 500);
}

if (typeof console == "undefined") {
	var console = {
			log: function(args) {
				/*if (typeof setLog == "function")
			return setLog(args);*/
		}
	}
}

function generateTabs(titleElm, contentElm, effectSpeed) {
	/*
	* generateTabs / 번거롭고 귀찮은 탭 스크립팅 한방에..
	- args
	titleElm    : $(selector)   / 필수.탭 제목
	contentElm  : $(selector)   / 필수.탭 내용
	effectSpeed : String or Int / 옵션.이펙트 속도(2000, "slow" ...)
	- author
	saystone@wylielab.co.kr
	- needed
	jQuery 1.3.x or higher
	*/

	if (effectSpeed == null) effectSpeed = "";

	var hasTitleImg = $(titleElm).eq(0).children("img").length == 1 ? true : false;
	//console.log(hasTitleImg);

	$(titleElm).each(function(i) {
		$(this).click(function(e) {
			$(contentElm).hide(effectSpeed);
			$(contentElm).eq(i).show(effectSpeed);

			// 타이틀 이미지가 바뀌어야 할때
			if (hasTitleImg) {
				$(titleElm).each(function(i) {
					var strImg = $(this).children("img").attr("src");
					//console.log(strImg);
					strImg = strImg.replace(/\_on./gi, "_off.");
					$(this).children("img").attr("src", strImg);
				});

				var strImg = $(titleElm).eq(i).children("img").attr("src");
				strImg = strImg.replace(/\_off./gi, "_on.");
				$(titleElm).eq(i).children("img").attr("src", strImg);
			}

			$(titleElm).parent().removeClass("on");
			$(titleElm).eq(i).parent().addClass("on");
		});
	});

	$(contentElm).not(":first").hide();
}


// ajax 컨텐츠 불러오는 탭
function generateTabsWithContent(type) {
	var titleElm, contentElm;

	switch (type) {
		case "bestSeller":
			titleElm = "div.best-seller>p a";
			contentElm = "div.best-seller>ul";
			break;
		case "categorySmall":
			titleElm = "div.side-pro>ul.tab a";
			contentElm = "div.side-pro>ul.side-list";
			break;
		case "categoryBottom":
			titleElm = "div.prolist-block>ul.tab-list a";
			contentElm = "div.prolist-block>ul.pro-list";
			break;

	}

	var hasTitleImg = $(titleElm).eq(0).children("img").length == 1 ? true : false; // A01:타이틀이 이미지 일때
	//console.log(hasTitleImg);

	$(titleElm).each(function(i) {
		$(this).click(function(e) {
			$(titleElm).each(function(i) { // 비활성화 이미지로 교체
				if (hasTitleImg) { // A01:타이틀 이미지가 바뀌어야 할때
					//var strImg = $(this).children("img").attr("src");
					var strImg = $(titleElm + ":eq(" + i + ")>img").attr("src");

					//console.log(strImg);
					strImg = strImg.replace(/\_on./gi, "_off.");

					//$(this).children("img").attr("src", strImg);
					$(titleElm + ":eq(" + i + ")>img").attr("src", strImg);
				}

				//console.log($(contentElm).eq(i).data("loaded"));
				if (type == "bestSeller") {
					if (i != 0) {
						if ($(contentElm).eq(i).data("loaded") == undefined) {
							$(contentElm).eq(i).data("loaded", false);
						}
					}
				} /* else if (type == "categorySmall") {
								if (i != 0) {
									if ($(contentElm).eq(i).data("loaded") == undefined) {
										$(contentElm).eq(i).data("loaded", false);
									}
								}
							}*/
				else if (type == "categoryBottom") {
					if (i != 0) {
						if ($(contentElm).eq(i).data("loaded") == undefined) {
							$(contentElm).eq(i).data("loaded", false);
						}
					}
				}
				if ($(contentElm).eq(i).data("loaded") == undefined) {
					$(contentElm).eq(i).data("loaded", false);
				}

				//console.log($(contentElm).eq(i).data("loaded"));
			});

			if (hasTitleImg) { // A01:타이틀 이미지가 바뀌어야 할때
				// 현재 인덱스 활성 이미지로 교체
				//var strImg = $(titleElm).eq(i).children("img").attr("src");
				var strImg = $(titleElm + ":eq(" + i + ")>img").attr("src");

				strImg = strImg.replace(/\_off./gi, "_on.");

				//$(titleElm).eq(i).children("img").attr("src", strImg);
				$(titleElm + ":eq(" + i + ")>img").attr("src", strImg);
			}

			$(titleElm).parent().removeClass("on"); // 전체 타이틀 텍스트 비활성화 클래스 적용
			$(titleElm).eq(i).parent().addClass("on"); // 현재 타이틀 타이틀 텍스트 활성화 클래스 적용

			//console.log($(contentElm).eq(i).html());
			if ($(contentElm).eq(i).data("loaded") == false) { // 한번만 로드
				getResult(i, type); // 해당하는 컨텐츠 인덱스 노드에 데이터 뿌려둠
			}

			$(contentElm).hide();
			$(contentElm).eq(i).show();

		});
	});

	$(contentElm).not(":first").hide();

	function getResult(i, type) {
		$.post("first_ajax.aspx", { type: type, param: i }, function(result) {
			$(contentElm).eq(i).html(result).data("loaded", true);
		}, "html");
	}

	//run at once
	getResult(0, type);
}
