﻿var pageIndexForPremium = 1;
var pageIndexForNewArrival = 1;
var pageIndexForEvent = 1;
var pageIndexForPopular = 1;

function loadPremiumProducts(arg)
{
	if (!arg)
	{
		arg = "&v=0";
	}
	sendRequest(on_loadedPremiumXML, arg, "GET", "/Market/GetPremiumListXml.aspx", true, true);
}

function loadNewArrivalProducts(arg)
{
	if (!arg)
	{
		arg = "&v=0";
	}
	sendRequest(on_loadedNewArrivalXML, arg, "GET", "/Market/GetNewArrivalListXml.aspx", true, true);
}

function loadEventProducts(arg)
{
	if (!arg)
	{
		arg = "&v=0";
	}
	sendRequest(on_loadedEventXML, arg, "GET", "/Market/GetEventProductListXml.aspx", true, true);
}

function loadPopularProducts(arg)
{
	if (!arg)
	{
		arg = "&v=0";
	}
	sendRequest(on_loadedPopularXML, arg, "GET", "/Market/GetPopularProductListXML.aspx", true, true);
}

function on_loadedPremiumXML(oj)
{
	var areaPremium = document.getElementsByName("MK_PreITEM")[0];
	var areaRolling = document.getElementsByName("PreRollingArea")[0];
	var btnLeft = document.getElementsByName("preLeftBtn")[0];
	var btnRight = document.getElementsByName("preRightBtn")[0];
	
	var xdoc = oj.responseXML;
	if (!xdoc)
	{
		areaRolling.innerHTML = "";
	}
	else
	{
		var item = xdoc.getElementsByTagName("item");
		var productId = xdoc.getElementsByTagName("productid");
		var thumbImageUrl = xdoc.getElementsByTagName("thumbimageurl");
		var salesStatus = xdoc.getElementsByTagName("salesstatus");
		var giftEvent = xdoc.getElementsByTagName("giftevent");
		var isFocus = xdoc.getElementsByTagName("isfocus");
		var productTitle = xdoc.getElementsByTagName("producttitle");
		var unitPrice = xdoc.getElementsByTagName("unitprice");
		var specialPrice = xdoc.getElementsByTagName("specialprice");
		var discountPrice = xdoc.getElementsByTagName("discountprice");

		var productCount = item.length;
		var startIndex = (pageIndexForPremium - 1) * 5;
		var endIndex = startIndex + 4;
		var nextPageIndex = pageIndexForPremium + 1;
		if (productCount-1 <= endIndex)
		{
			endIndex = productCount - 1;
			nextPageIndex = pageIndexForPremium;
		}
		var prevPageIndex = 1;
		if (pageIndexForPremium > 1)
		{
			prevPageIndex = pageIndexForPremium - 1;
		}
		
		btnLeft.onclick = function()
		{
			pageIndexForPremium = prevPageIndex;
			loadPremiumProducts();
		}
		
		btnRight.onclick = function()
		{
			pageIndexForPremium = nextPageIndex;
			loadPremiumProducts();
		}
		
		//areaRolling.innerHTML = "";
		var htmlRolling = "";
		
		for (var i = startIndex; i <= endIndex; i++)
		{
			htmlRolling += createProductCell(productId[i].firstChild.nodeValue, thumbImageUrl[i].firstChild.nodeValue, productTitle[i].firstChild.nodeValue, giftEvent[i].firstChild.nodeValue, isFocus[i].firstChild.nodeValue, salesStatus[i].firstChild.nodeValue, unitPrice[i].firstChild.nodeValue, specialPrice[i].firstChild.nodeValue, discountPrice[i].firstChild.nodeValue);
		}
		
		var vacantIndex = startIndex + 4 - endIndex;
		
		for (var i = 0; i < vacantIndex; i++)
		{
			htmlRolling += createVacantCell();
		}
		
		areaRolling.innerHTML = htmlRolling;
	}
}

function on_loadedNewArrivalXML(oj)
{
	var areaNewArrival = document.getElementsByName("MK_NewITEM")[0];
	var areaRolling = document.getElementsByName("newRollingArea")[0];
	var btnLeft = document.getElementsByName("newLeftBtn")[0];
	var btnRight = document.getElementsByName("newRightBtn")[0];
	
	var xdoc = oj.responseXML;
	if (!xdoc)
	{
		areaRolling.innerHTML = "";
	}
	else
	{
		var item = xdoc.getElementsByTagName("item");
		var productId = xdoc.getElementsByTagName("productid");
		var thumbImageUrl = xdoc.getElementsByTagName("thumbimageurl");
		var salesStatus = xdoc.getElementsByTagName("salesstatus");
		var giftEvent = xdoc.getElementsByTagName("giftevent");
		var isFocus = xdoc.getElementsByTagName("isfocus");
		var productTitle = xdoc.getElementsByTagName("producttitle");
		var unitPrice = xdoc.getElementsByTagName("unitprice");
		var specialPrice = xdoc.getElementsByTagName("specialprice");
		var discountPrice = xdoc.getElementsByTagName("discountprice");

		var productCount = item.length;
		var startIndex = (pageIndexForNewArrival - 1) * 5;
		var endIndex = startIndex + 4;
		var nextPageIndex = pageIndexForNewArrival + 1;
		if (productCount-1 <= endIndex)
		{
			endIndex = productCount - 1;
			nextPageIndex = pageIndexForNewArrival;
		}
		var prevPageIndex = 1;
		if (pageIndexForPremium > 1)
		{
			prevPageIndex = pageIndexForNewArrival - 1;
		}
		
		btnLeft.onclick = function()
		{
			pageIndexForNewArrival = prevPageIndex;
			loadNewArrivalProducts();
		}
		
		btnRight.onclick = function()
		{
			pageIndexForNewArrival = nextPageIndex;
			loadNewArrivalProducts();
		}
		
		//areaRolling.innerHTML = "";
		var htmlRolling = "";
		
		for (var i = startIndex; i <= endIndex; i++)
		{
			htmlRolling += createProductCell(productId[i].firstChild.nodeValue, thumbImageUrl[i].firstChild.nodeValue, productTitle[i].firstChild.nodeValue, giftEvent[i].firstChild.nodeValue, isFocus[i].firstChild.nodeValue, salesStatus[i].firstChild.nodeValue, unitPrice[i].firstChild.nodeValue, specialPrice[i].firstChild.nodeValue, discountPrice[i].firstChild.nodeValue);
		}
		
		var vacantIndex = startIndex + 4 - endIndex;
		
		for (var i = 0; i < vacantIndex; i++)
		{
			htmlRolling += createVacantCell();
		}
		
		areaRolling.innerHTML = htmlRolling;
	}
}

function createProductCell(productID, imageUrl, productTitle, isGift, isFocus, salesStatus, unitPrice, specialPrice, discountPrice)
{
	var retHTML = "<div class=\"MK_ITEM\">";
	retHTML += "<ul>";
	
	if (isFocus == "True")
	{
		retHTML += "<li class=\"Row1sp\"  onMouseOver=\"this.className='Row1over'\" onMouseOut=\"this.className='Row1sp'\">";
	}
	else
	{
		retHTML += "<li class=\"Row1\"  onMouseOver=\"this.className='Row1over'\" onMouseOut=\"this.className='Row1'\">";
	}
	
	retHTML += "<div class=\"frame01\">";
	//retHTML += "<a href=\"/Market/ProductView.aspx?id=" + productID + "\"><img src=\"http://upload.serakorea.com/FilesMarket/" + imageUrl + "\" width=\"88\" height=\"68\" alt=\"" + productTitle + "\"/></a>";
	retHTML += "<a href=\"/Market/ProductView.aspx?id=" + productID + "\"><img src=\"" + imageUrl + "\" width=\"88\" height=\"68\" alt=\"" + productTitle + "\"/></a>";
	retHTML += "</div>";
	retHTML += "</li>";
	retHTML += "<li class=\"Row2\">";
	
	switch (eval(salesStatus))
	{
		case 1:
			break;
		case 2:
			retHTML += "<img src=\"" + imageRoot + "market/mk_ico_event.gif\" alt=\"기획\"/>";
			break;
		case 3:
			retHTML += "<img src=\"" + imageRoot + "market/mk_ico_sale.gif\" alt=\"할인\"/>";
			break;
	}
	
	if (isGift == "True")
	{
		if (eval(salesStatus) > 1)
		{
			retHTML += "<img src=\"" + imageRoot + "market/mk_ico_plus.gif\" alt=\"+\"/>";
		}
		retHTML += "<img src=\"" + imageRoot + "market/mk_ico_prod.gif\" alt=\"사은품\"/>";
	}
	
	retHTML += "</li>";
	retHTML += "<li class=\"Row2\">";
	retHTML += "<a href=\"/Market/ProductView.aspx?id=" + productID + "\">" + productTitle.substr(0,10) + "<br />";
	
	switch (eval(salesStatus))
	{
		case 1:
			retHTML += "<em>" + unitPrice + "</em>";
			break;
		case 2:
			retHTML += "<span>" + unitPrice + "</span>→<em>" + specialPrice + "</em>";
			break;
		case 3:
			retHTML += "<span>" + unitPrice + "</span>→<em>" + discountPrice + "</em>";
			break;
	}
	
	retHTML += "<img src=\"" + imageRoot + "market/mk_ico_cash.gif\" alt=\"cash\"></a></li>";
	retHTML += "</ul>";
	retHTML += "</div>";
	
	return retHTML;
}

function createVacantCell()
{
	var retHTML = "<div class=\"MK_ITEM\"></div>";
	return retHTML;
}

function changeBest5Image(productId, imageUrl)
{
	var linkWeeklyBest5 = document.getElementsByName("lkBest5Image")[0];
	var imgWeeklyBest5 = linkWeeklyBest5.getElementsByTagName("img")[0];
	
	linkWeeklyBest5.setAttribute("href", "/Market/ProductView.aspx?id=" + productId);
	imgWeeklyBest5.src = imageUrl;
}

function on_loadedEventXML(oj)
{
	var areaEvent = document.getElementsByName("MK_BrandB")[0];
	var areaRolling = areaEvent.getElementsByTagName("dl")[0];
	var btnLeft = document.getElementsByName("eventLeftBtn")[0];
	var btnRight = document.getElementsByName("eventRightBtn")[0];
	
	var xdoc = oj.responseXML;
	if (!xdoc)
	{
		areaRolling.innerHTML = "";
	}
	else
	{
		var item = xdoc.getElementsByTagName("item");
		var productId = xdoc.getElementsByTagName("productid");
		var thumbImageUrl = xdoc.getElementsByTagName("thumbimageurl");
		var salesStatus = xdoc.getElementsByTagName("salesstatus");
		var giftEvent = xdoc.getElementsByTagName("giftevent");
		var productTitle = xdoc.getElementsByTagName("producttitle");
		var unitPrice = xdoc.getElementsByTagName("unitprice");
		var specialPrice = xdoc.getElementsByTagName("specialprice");
		var discountPrice = xdoc.getElementsByTagName("discountprice");

		var productCount = item.length;
		var nextPageIndex = pageIndexForEvent + 1;
		if (productCount < nextPageIndex)
		{
			nextPageIndex = pageIndexForEvent;
		}
		var prevPageIndex = 1;
		if (pageIndexForEvent > 1)
		{
			prevPageIndex = pageIndexForEvent - 1;
		}
		
		btnLeft.onclick = function()
		{
			pageIndexForEvent = prevPageIndex;
			loadEventProducts();
		}
		
		btnRight.onclick = function()
		{
			pageIndexForEvent = nextPageIndex;
			loadEventProducts();
		}
		
		var indexNow = pageIndexForEvent - 1;
		var htmlRolling = "<dt><a href=\"/Market/ProductView.aspx?id=" + productId[indexNow].firstChild.nodeValue + "\"><img src=\"" + thumbImageUrl[indexNow].firstChild.nodeValue + "\" alt=\"" + productTitle[indexNow].firstChild.nodeValue + "\"></a></dt>";
		htmlRolling += "<dd>";
		
		switch (eval(salesStatus[indexNow].firstChild.nodeValue))
		{
			case 1:
				break;
			case 2:
				htmlRolling += "<img src=\"" + imageRoot + "market/mk_ico_event.gif\" alt=\"기획\"/>";
				break;
			case 3:
				htmlRolling += "<img src=\"" + imageRoot + "market/mk_ico_sale.gif\" alt=\"할인\"/>";
				break;
		}
		
		if (giftEvent[indexNow].firstChild.nodeValue == "True")
		{
			if (eval(salesStatus[indexNow].firstChild.nodeValue) > 1)
			{
				htmlRolling += "<img src=\"" + imageRoot + "market/mk_ico_plus.gif\" alt=\"+\"/>";
			}
			htmlRolling += "<img src=\"" + imageRoot + "market/mk_ico_prod.gif\" alt=\"사은품\"/>";
		}
		htmlRolling += "</dd>";
		htmlRolling += "<dd class=\"POS_U\"><a href=\"/Market/ProductView.aspx?id=" + productId[indexNow].firstChild.nodeValue + "\">" + productTitle[indexNow].firstChild.nodeValue.substr(0,6) + "<br />";
		switch (eval(salesStatus[indexNow].firstChild.nodeValue))
		{
			case 1:
				htmlRolling += "<em>" + unitPrice[indexNow].firstChild.nodeValue + "</em>";
				break;
			case 2:
				htmlRolling += "<span>" + unitPrice[indexNow].firstChild.nodeValue + "</span>→<em>" + specialPrice[indexNow].firstChild.nodeValue + "</em>";
				break;
			case 3:
				htmlRolling += "<span>" + unitPrice[indexNow].firstChild.nodeValue + "</span>→<em>" + discountPrice[indexNow].firstChild.nodeValue + "</em>";
				break;
		}
		htmlRolling += "<img src=\"" + imageRoot + "market/mk_ico_cash.gif\" alt=\"cash\"></a></dd>";
		
		areaRolling.innerHTML = htmlRolling;
	}
}

function on_loadedPopularXML(oj)
{
	var areaPopular = document.getElementsByName("MK_BrandC")[0];
	var areaRolling = areaPopular.getElementsByTagName("dl")[0];
	var btnLeft = document.getElementsByName("popularLeftBtn")[0];
	var btnRight = document.getElementsByName("popularRightBtn")[0];
	
	var xdoc = oj.responseXML;
	if (!xdoc)
	{
		areaRolling.innerHTML = "";
	}
	else
	{
		var item = xdoc.getElementsByTagName("item");
		var productId = xdoc.getElementsByTagName("productid");
		var thumbImageUrl = xdoc.getElementsByTagName("thumbimageurl");
		var salesStatus = xdoc.getElementsByTagName("salesstatus");
		var giftEvent = xdoc.getElementsByTagName("giftevent");
		var productTitle = xdoc.getElementsByTagName("producttitle");
		var unitPrice = xdoc.getElementsByTagName("unitprice");
		var specialPrice = xdoc.getElementsByTagName("specialprice");
		var discountPrice = xdoc.getElementsByTagName("discountprice");
		var aveScore = xdoc.getElementsByTagName("avescore");

		var productCount = item.length;
		var startIndex = (pageIndexForPopular - 1) * 2;
		var endIndex = startIndex + 1;
		var nextPageIndex = pageIndexForPopular + 1;
		if (productCount-1 <= endIndex)
		{
			endIndex = productCount - 1;
			nextPageIndex = pageIndexForPopular;
		}
		var prevPageIndex = 1;
		if (pageIndexForPopular > 1)
		{
			prevPageIndex = pageIndexForPopular - 1;
		}
		
		btnLeft.onclick = function()
		{
			pageIndexForPopular = prevPageIndex;
			loadPopularProducts();
		}
		
		btnRight.onclick = function()
		{
			pageIndexForPopular = nextPageIndex;
			loadPopularProducts();
		}
		
		//areaRolling.innerHTML = "";
		var htmlRolling = "";
		
		for (var i = startIndex; i <= endIndex; i++)
		{
			htmlRolling += "<dt class=\"POS_L\"><a href=\"/Market/ProductView.aspx?id=" + productId[i].firstChild.nodeValue + "\"><img src=\"" + thumbImageUrl[i].firstChild.nodeValue + "\" alt=\"" + productTitle[i].firstChild.nodeValue + "\" width=\"133\" height=\"103\"></a></dt>";
			htmlRolling += "<dd class=\"POS_Rate\"><img src=\"" + imageRoot + "market/mk_ico_rate0" + aveScore[i].firstChild.nodeValue + ".gif\" alt=\"rate0\"></dd>";
			htmlRolling += "<dd class=\"POS_Icon\">";
			switch (eval(salesStatus[i].firstChild.nodeValue))
			{
				case 1:
					break;
				case 2:
					htmlRolling += "<img src=\"" + imageRoot + "market/mk_ico_event.gif\" alt=\"기획\"/>";
					break;
				case 3:
					htmlRolling += "<img src=\"" + imageRoot + "market/mk_ico_sale.gif\" alt=\"할인\"/>";
					break;
			}
			
			if (giftEvent[i].firstChild.nodeValue == "True")
			{
				if (eval(salesStatus[i].firstChild.nodeValue) > 1)
				{
					htmlRolling += "<img src=\"" + imageRoot + "market/mk_ico_plus.gif\" alt=\"+\"/>";
				}
				htmlRolling += "<img src=\"" + imageRoot + "market/mk_ico_prod.gif\" alt=\"사은품\"/>";
			}
			//htmlRolling += "</dd>";
			//htmlRolling += "<dd class=\"POS_U\"><a href=\"/Market/ProductView.aspx?id=" + productId[i].firstChild.nodeValue + "\">" + productTitle[i].firstChild.nodeValue.substr(0,6) + "<br/>";
			htmlRolling += "<a href=\"/Market/ProductView.aspx?id=" + productId[i].firstChild.nodeValue + "\">" + productTitle[i].firstChild.nodeValue.substr(0,10) + "<br/>";
			switch (eval(salesStatus[i].firstChild.nodeValue))
			{
				case 1:
					htmlRolling += "<em>" + unitPrice[i].firstChild.nodeValue + "</em>";
					break;
				case 2:
					htmlRolling += "<span>" + unitPrice[i].firstChild.nodeValue + "</span>→<em>" + specialPrice[i].firstChild.nodeValue + "</em>";
					break;
				case 3:
					htmlRolling += "<span>" + unitPrice[i].firstChild.nodeValue + "</span>→<em>" + discountPrice[i].firstChild.nodeValue + "</em>";
					break;
			}
			htmlRolling += "<img src=\"" + imageRoot + "market/mk_ico_cash.gif\" alt=\"cash\"></a></dd>";
		}
		
		var vacantIndex = startIndex + 1 - endIndex;
		
		for (var i = 0; i < vacantIndex; i++)
		{
			htmlRolling += "<dt class=\"POS_L\"></dt>";
			htmlRolling += "<dd class=\"POS_Rate\"></dd>";
			htmlRolling += "<dd></dd>";
			htmlRolling += "<dd class=\"POS_U\"></dd>";
		}
		
		areaRolling.innerHTML = htmlRolling;
	}
}

addLoadEvent(loadPremiumProducts);
addLoadEvent(loadNewArrivalProducts);
addLoadEvent(loadEventProducts);
addLoadEvent(loadPopularProducts);
