﻿/// <reference path="jquery-1.3.2-vsdoc.js">
/// <reference path="Global.js">

/*
작성자 이재원 
작성일 2010-03-18

api
----------------------------------------------------------
ShoppingBag.Floate();	//쇼핑백 화면에 띄운기.
ShoppingBag.Show();		//쇼핑백 오픈.
ShoppingBag.Hide();		//쇼핑백 닫기.

ShoppingBag.TodayGoods.Show();	//오늘본 상품 오픈
ShoppingBag.TodayGoods.Hide();  //오늘본 상품 닫기

ShoppingBag.InterestGoods.Show();	//관심상품 오픈
ShoppingBag.InterestGoods.Hide();	//관심상품 닫기
ShoppingBag.InterestGoods.Add(GoodsCd, GoodsClsCd, LinkUrl);	//관심상품을 추가(동시에 오픈)

ShoppingBag.MyPage.Show();	//MyPage 오픈
ShoppingBag.MyPage.Hide();	//MyPage 닫기
----------------------------------------------------------
*/

var ShoppingBag = {
	Container: null,
	Menu: null,
	Body: null,
	btnShow: null,
	btnHide: null,
	isShow: false,
	isMouseover: false,
	isIe6: false,

	Load: function() {

		if (
			$.browser.msie 
			&& $.browser.version == "6.0"
			&& navigator.appVersion.indexOf("MSIE 6.0") >= 0
		) 
		{
			this.isIe6 = true;
		}



		/*객체 바인딩*/
		this.Container = $("#shoppingBag");
		this.Menu = $("#shoppingBagMenu");
		this.Body = $("#shoppingBagBody");
		this.btnShow = $("#btnBagShow");
		this.btnHide = $("#btnBagHide");

		this.TodayGoods.MenuOn = $("#menuTodayGoods_on");
		this.TodayGoods.MenuOff = $("#menuTodayGoods_off");
		this.TodayGoods.Body = $("#bodyTodayGoods");
		this.TodayGoods.List = $("#bodyTodayGoodsList");
		this.TodayGoods.None = $("#bodyTodayGoodsNone");


		this.InterestGoods.MenuOn = $("#menuInterestGoods_on");
		this.InterestGoods.MenuOff = $("#menuInterestGoods_off");
		this.InterestGoods.Body = $("#bodyInterestGoods");
		this.InterestGoods.Login = $("#bodyInterestGoodsLogin");
		this.InterestGoods.List = $("#bodyInterestGoodsList");
		this.InterestGoods.None = $("#bodyInterestGoodsNone");


		this.MyPage.MenuOn = $("#menuMyPage_on");
		this.MyPage.MenuOff = $("#menuMyPage_off");
		this.MyPage.Body = $("#bodyMyPage");
		this.MyPage.List = $("#bodyMyPageList");
		this.MyPage.Login = $("#bodyMyPageLogin");


		//$("#shoppingBagMenu ul").append("<li><input type=text id='debug'/></li>");
		//$("#debug").val(stringformat("{0}/{1}/{2}", $.browser.msie, $.browser.version, navigator.appVersion));


		/* 브라우져 셋팅 포지션 픽스 */
		if (this.isIe6) {

			this.Container.css("position", "absolute");

			//setInterval(function() { ShoppingBag.PoxFix();}, 10);

			$(window).scroll(function() {
				ShoppingBag.PoxFix();
			});

		}
		else {
			this.Container.css("position", "fixed");
		}


		/* 리싸이즈시 */
		$(window).resize(function() {
			ShoppingBag.PoxFix();
		});


		/* 이벤트 연결 */
		$(this.TodayGoods.MenuOff).css("cursor", "pointer").click(function() {
			ShoppingBag.TodayGoods.Show();
		});
		$(this.InterestGoods.MenuOff).css("cursor", "pointer").click(function() {
			ShoppingBag.InterestGoods.Show();
		});
		$(this.MyPage.MenuOff).css("cursor", "pointer").click(function() {
			ShoppingBag.MyPage.Show();
		});

		$(ShoppingBag.btnShow).css("cursor", "pointer").click(function() {

			if (
				!ShoppingBag.TodayGoods.isView
				&& !ShoppingBag.InterestGoods.isView
				&& !ShoppingBag.MyPage.isView
			) {
				ShoppingBag.TodayGoods.Show();
			}
			else
				ShoppingBag.Show();

		});
		$(ShoppingBag.btnHide).css("cursor", "pointer").click(function() {
			ShoppingBag.Hide();
		});


		$(ShoppingBag.Container)
	    			.mouseover(function() {
	    				ShoppingBag.isMouseover = true;
	    			})
    				.mouseleave(function() {
    					ShoppingBag.isMouseover = false;
    				});

		$(document).click(function() {
			if (!ShoppingBag.isMouseover && ShoppingBag.isShow)
				ShoppingBag.Hide();
		});
	},


	Init: function() {
		/*상태 초기화*/
		ShoppingBag.TodayGoods.Hide();
		ShoppingBag.InterestGoods.Hide();
		ShoppingBag.MyPage.Hide();
	},


	Floate: function() {
		this.Load();
		this.Draw();
	},


	BtnOnShow: function() {
		$(ShoppingBag.btnHide).hide();
		$(ShoppingBag.btnShow).show();
	},


	BtnOffShow: function() {
		$(ShoppingBag.btnHide).show();
		$(ShoppingBag.btnShow).hide();
	},


	Draw: function() {
		this.Hide();
	},


	ClientHeight: function() {

		var h;
		if (ShoppingBag.isIe6) {
			h = document.documentElement.clientHeight + ((document.documentElement.scrollTop > document.body.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop);

			if (h > document.body.clientHeight)
				return document.body.clientHeight;
		}
		else
			h = document.documentElement.clientHeight;

		//$("#debug").val(stringformat("{0}/{1}", h, document.body.clientHeight));

		return h;
	},

	PoxFix: function() {
		if (ShoppingBag.isShow)
			ShoppingBag.Show(false);
		else
			ShoppingBag.Hide();
	},

	Show: function(isAni) {

		$(this.Body).show();

		if (isAni == null) isAni = true;

		var posy = this.ClientHeight() - $(this.Container).outerHeight();

		if (isAni) {
			this.Container.animate(
				{ top: posy + 'px' }
				, 500
				, function() {
					ShoppingBag.isShow = true;
					ShoppingBag.BtnOffShow();
				}
			);
		}
		else {

			this.Container.css("top", posy + "px");
			this.isShow = true;
			this.BtnOffShow();
		}


	},

	Hide: function() {

		this.Init();
		this.isShow = false;
		this.BtnOnShow();
		var top = this.ClientHeight() - $(this.Menu).outerHeight();
		this.Container.css("top", top + "px");

		$(this.Body).hide();
	},

	ShowItem: function(item) {
		ShoppingBag.Init();
		item.isView = true;
		item.MenuOn.show();
		item.MenuOff.hide();
		item.Body.show();
		if (!ShoppingBag.isShow) ShoppingBag.Show();
	},

	HideItem: function(item) {
		item.isView = false;
		item.MenuOn.hide();
		item.MenuOff.show();
		item.Body.hide();
	},


	TodayGoods: {
		isView: false,
		MenuOn: null,
		MenuOff: null,
		Body: null,
		List: null,
		None: null,
		DataBind: function() {

			$.ajax({
				url: "/ClientWebService/wsShoppingBag.asmx/GetTodayGoods"
					, data: "{}"
					, success: function(data) {

						var result;
						eval("result = " + data.d);

						var totalitemcount = result.totCnt;

						/*리스트에 아무것도 없는 경우*/
						if (totalitemcount == 0) {
							ShoppingBag.TodayGoods.NoneView();
						}
						else {

							var list = result.list;


							var formatstr = "<li>"
											+ "<div class='posWrap2'>"
											+ "	<dl class='contBox'>"
											+ "		<dt>{3}</dt>"
											+ "		<dd class='img'><img src='{6}' alt='' width='110' height='85' /></dd>"
											+ "		<dd class='txt'>"
											+ "			<P>{4}</P>"
											+ "		</dd>"
											+ "		<dd><span class='icoPM'>{5}원~</span></dd>"
											+ "	</dl>"
											+ "	<div class='overArea' style='display:none;'>"
											+ "		<div class='overbg'>"
											+ "		</div>"
											+ "		<div class='overbtn1'>"
											+ "			<span class='bbtn' style='float:left;'><a href='javascript:ShoppingBag.TodayGoods.Move(\"{0}\",\"{1}\",\"{2}\")'><img src='../images/btn/btn_masterBtn_01.gif' alt='' style='margin-right:3px;' /></a></span>"
											+ "			<span class='sbtn' style='width:51px; float:left;'><a href='{2}'><img src='../images/btn/btn_masterBtn_02.gif' style='margin-bottom:1px;' alt='' /></a><br /> <a href='javascript:ShoppingBag.TodayGoods.Delete(\"{0}\")'><img src='../images/btn/btn_masterBtn_03.gif' alt='' /></a></span>"
											+ "		</div>"
											+ "	</div>"
											+ "</div>"
											+ "</li>";


							var temp = "";
							$.each(list, function(idx, obj) {
								temp += stringformat(formatstr, obj.GoodsCd, obj.GoodsClsCd, obj.LinkUrl, obj.GoodsClsCdName, obj.GoodsName, formatCurrency(obj.GoodsFare), obj.MainImg);
							});

							$(ShoppingBag.TodayGoods.List).find("ul").html(temp);

							$(ShoppingBag.TodayGoods.List).find("ul li").mouseover(function() {
								$(this).find(".overArea").show();
							}).mouseleave(function() {
								$(this).find(".overArea").hide();
							});


							ShoppingBag.TodayGoods.ListView();
						}


					}
					, error: function(xhr, status, error) {
						alert(xhr + '\n' + status + '\n' + error);
					}
			});


		},
		Show: function(isDataBind) {

			if (isDataBind == null) isDataBind = true;
			if (isDataBind) this.DataBind();

			ShoppingBag.ShowItem(this);

		},
		Hide: function() {
			ShoppingBag.HideItem(this);
			$(this.None).hide();
			$(this.List).hide();
		},
		Delete: function(GoodsCd) {

			$.ajax({
				url: "/ClientWebService/wsShoppingBag.asmx/DeleteTodayGoods"
					, data: stringformat("{'GoodsCd':'{0}'}", GoodsCd)
					, success: function(data) {

						var result = data.d;

						if (result == "Y") {
							ShoppingBag.TodayGoods.DataBind();
						}
					}
					, error: function(xhr, status, error) {
						alert(xhr + '\n' + status + '\n' + error);
					}
			});

		},
		Move: function(GoodsCd, GoodsClsCd, LinkUrl) {

			//ShoppingBag.InterestGoods.Add(GoodsCd, GoodsClsCd, LinkUrl);
			//this.Delete(GoodsCd);

			$.ajax({
				url: "/ClientWebService/wsShoppingBag.asmx/AddInterestGoods"
					, data: stringformat("{'GoodsCd':'{0}','GoodsClsCd':'{1}','LinkUrl':'{2}'}", GoodsCd, GoodsClsCd, LinkUrl)
					, success: function(data) {

						var result = data.d;

						/*로그인 하지 않은 경우*/
						if (result == "N") {
							ShoppingBag.InterestGoods.Show(false);
							ShoppingBag.InterestGoods.LoginView();
						}
						else {
							ShoppingBag.TodayGoods.Delete(GoodsCd);
							//ShoppingBag.InterestGoods.Show();
						}
					}
					, error: function(xhr, status, error) {
						alert(xhr + '\n' + status + '\n' + error);
					}
			});

		},
		NoneView: function() {
			$(this.List).hide();
			$(this.None).show();
		},
		ListView: function() {
			$(this.None).hide();
			$(this.List).show();
		}
	},


	InterestGoods: {
		isView: false,
		MenuOn: null,
		MenuOff: null,
		Body: null,
		Login: null,
		List: null,
		None: null,
		DataBind: function() {

			$.ajax({
				url: "/ClientWebService/wsShoppingBag.asmx/GetInterestGoods"
				, data: "{}"
				, success: function(data) {

					var result;
					eval("result = " + data.d);

					if (result.res == "N") {
						/*로그인 하지 않은경우*/
						ShoppingBag.InterestGoods.LoginView();
					}
					else {

						var totalitemcount = result.data.totCnt;

						/*리스트에 아무것도 없는 경우*/
						if (totalitemcount == 0) {
							ShoppingBag.InterestGoods.NoneView();
						}
						else {

							var list = result.data.list;

							var formatstr = "<li>"
											+ "	<div class='posWrap2'>"
											+ "		<dl class='contBox'>"
											+ "			<dt>{3}</dt>"
											+ "			<dd class='img'><img src='{6}' alt='' width='110' height='85' /></dd>"
											+ "			<dd class='txt'>"
											+ "				<P>{4}</P>"
											+ "			</dd>"
											+ "			<dd><span class='icoPM'>{5}원~</span></dd>"
											+ "		</dl>"
											+ "		<div class='overArea' style='display:none;'>"
											+ "			<div class='overbg'></div>"
											+ "			<div class='overbtn2'>"
											+ "				<span class='sbtn1'><a href='{2}'><img src='../images/btn/btn_masterBtn_02.gif' style='margin-bottom:1px;' alt='' /></a><br /> <a href='javascript:ShoppingBag.InterestGoods.Delete(\"{0}\")'><img src='../images/btn/btn_masterBtn_03.gif' alt='' /></a></span>"
											+ "			</div>"
											+ "		</div>"
											+ "	</div>"
											+ "</li>";


							var temp = "";
							$.each(list, function(idx, obj) {
								temp += stringformat(formatstr, obj.GoodsCd, obj.InterestClsCd, obj.LinkUrl, obj.InterestClsCdName, obj.GoodsName, formatCurrency(obj.GoodsFare), obj.MainImg);
							});


							$(ShoppingBag.InterestGoods.List).find("ul").html(temp);

							$(ShoppingBag.InterestGoods.List).find("ul li").mouseover(function() {
								$(this).find(".overArea").show();
							}).mouseleave(function() {
								$(this).find(".overArea").hide();
							});


							ShoppingBag.InterestGoods.ListView();
						}
					}

				}
				, error: function(xhr, status, error) {
					alert(xhr + '\n' + status + '\n' + error);
				}
			});


		},
		Show: function(isDataBind) {

			if (isDataBind == null) isDataBind = true;
			if (isDataBind) this.DataBind();

			ShoppingBag.ShowItem(this);

		},
		Hide: function() {
			ShoppingBag.HideItem(this);
			$(this.None).hide();
			$(this.Login).hide();
			$(this.List).hide();
		},
		Add: function(GoodsCd, GoodsClsCd, LinkUrl) {

			$.ajax({
				url: "/ClientWebService/wsShoppingBag.asmx/AddInterestGoods"
				, data: stringformat("{'GoodsCd':'{0}','GoodsClsCd':'{1}','LinkUrl':'{2}'}", GoodsCd, GoodsClsCd, LinkUrl)
				, success: function(data) {

					var result = data.d;

					/*로그인 하지 않은 경우*/
					if (result == "N") {
						ShoppingBag.InterestGoods.LoginView();
					}
					else {
						ShoppingBag.InterestGoods.DataBind();
					}
				}
				, error: function(xhr, status, error) {
					alert(xhr + '\n' + status + '\n' + error);
				}
			});

			this.Show(false);
		},
		Delete: function(GoodsCd) {

			$.ajax({
				url: "/ClientWebService/wsShoppingBag.asmx/DeleteInterestGoods"
					, data: stringformat("{'GoodsCd':'{0}'}", GoodsCd)
					, success: function(data) {

						var result = data.d;

						/*로그인 하지 않은 경우*/
						if (result == "N") {
							ShoppingBag.InterestGoods.LoginView();
						}
						else {
							ShoppingBag.InterestGoods.DataBind();
						}
					}
					, error: function(xhr, status, error) {
						alert(xhr + '\n' + status + '\n' + error);
					}
			});

		},
		LoginView: function() {
			$(this.List).hide();
			$(this.None).hide();
			$(this.Login).show();
		},
		NoneView: function() {
			$(this.Login).hide();
			$(this.List).hide();
			$(this.None).show();
		},
		ListView: function() {
			$(this.Login).hide();
			$(this.None).hide();
			$(this.List).show();
		}
	},


	MyPage: {
		isView: false,
		MenuOn: null,
		MenuOff: null,
		Body: null,
		DataBind: function() {

			$.ajax({
				url: "/ClientWebService/wsShoppingBag.asmx/GetMyPage"
				, data: "{}"
				, success: function(data) {


					var result;
					eval("result = " + data.d);

					if (result.res == "N") {
						/*로그인 하지 않은경우*/
						ShoppingBag.MyPage.LoginView();
					}
					else {

						var list = result.data1.list;

						if (list.length == 0) {
							alert("error");
							return;
						}

						var dr = list[0];

						$(ShoppingBag.MyPage.List).find("#sbcouponCnt").text(dr.couponCnt);
						$(ShoppingBag.MyPage.List).find("#sbEmail").text(dr.Email);
						$(ShoppingBag.MyPage.List).find("#sbEngName").text(dr.MembName + ' (' + dr.EngName + ')');
						$(ShoppingBag.MyPage.List).find("#sbHpTel").text(dr.HpTel);
						$(ShoppingBag.MyPage.List).find("#sbMembId").text(dr.MembId);
						$(ShoppingBag.MyPage.List).find("#sbMembName").text(dr.MembName);
						$(ShoppingBag.MyPage.List).find("#sbmemoCnt").text(dr.memoCnt);
						$(ShoppingBag.MyPage.List).find("#sbqaCnt").text(dr.qaCnt);

						if (dr.PsrtEndDate == "")
							$(ShoppingBag.MyPage.List).find("#sbPsrtEndDate").text("정보가 없습니다.");
						else
							$(ShoppingBag.MyPage.List).find("#sbPsrtEndDate").text(dr.PsrtEndDate);

						$(ShoppingBag.MyPage.List).find("#sbRmdrTourMoney").text(formatCurrency(dr.RmdrTourMoney));
						$(ShoppingBag.MyPage.List).find("#sbrsvCnt").text(dr.rsvCnt);


						list = result.data2.list;

						var formatstr = "<li><a>{0} – <strong>{1}</strong></a></li>"

						var temp = "";
						$.each(list, function(idx, obj) {
							temp += stringformat(formatstr, obj.ReserveClsCdName, obj.GoodsName);
						});

						$(ShoppingBag.MyPage.List).find("#sbRsvList").html(temp);

						ShoppingBag.MyPage.ListView();
					}

				}
				, error: function(xhr, status, error) {
					alert(xhr + '\n' + status + '\n' + error);
				}
			});


		},
		Show: function(isDataBind) {

			if (isDataBind == null) isDataBind = true;
			if (isDataBind) this.DataBind();

			ShoppingBag.ShowItem(this);

		},
		Hide: function() {
			ShoppingBag.HideItem(this);
			$(this.List).hide();
			$(this.Login).hide();
		},
		LoginView: function() {
			$(this.List).hide();
			$(this.Login).show();
		},
		ListView: function() {
			$(this.Login).hide();
			$(this.List).show();
		}
	}
};

$(document).ready(function() {
	ShoppingBag.Floate();
});