﻿var pageIndexForPremium = 1;

function loadPremiumProducts(arg)
{
	if (!arg)
	{
		arg = "&v=0";
	}
	sendRequest(on_loadedPremiumXML, arg, "GET", "/Market/GetPremiumListXml.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 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=\"" + 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,6) + "<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;
}

addLoadEvent(loadPremiumProducts);
