/*
 * Created on 2008. 05. 27
 * 
 * @author In ha
 * @Description 
 *		- 상점 관련 js 모음  
 * @modified on : 
 * @modification author :
 *
 */

	// href의 #를 없애주기 위한 함수  : 아무 처리도 하지 않음 
	function none() {
	}

	// 클릭이벤트 Catch
	function getPosition(div) {
	    var pageX = event.clientX;
	    var pageY = event.clientY; 
	    var scrollTop = document.documentElement.scrollTop;
	    var xLen = 0;
	    
	    if (div == 'L') { // 칭호리스트 아이템
		    xLen = 50;
		    if(pageX > 810) {
		    	xLen = - 350;
		    }
	
			viewItem.style.pixelTop = (pageY + scrollTop) - 80;
			viewItem.style.pixelLeft = pageX + xLen;
	    }
	    else if (div == 'W') { // 착용아이템
		    xLen = 50;
		    if(pageX > 860) {
		    	xLen = - 350;
		    }
	
			viewItem.style.pixelTop = (pageY + scrollTop) - 360;
			viewItem.style.pixelLeft = pageX + xLen;
	    }
	    else { 
		    xLen = 50;
		    if(pageX > 860) {
		    	xLen = - 350;
		    }
	
			viewItem.style.pixelTop = (pageY + scrollTop) - 80;
			viewItem.style.pixelLeft = pageX + xLen;
	    }
	}

	function reloadPage() {
		window.location.reload();
	}

	// pop div로 처리하기 위한 디자인 정보 
	function getHtmlCode (div, itemDiv) {
		var htmlCode = "";
		
		if (div == "buySuccessDiv") {
			if (itemDiv == "E") {
				funcName = "link_team_my";
			}
			else {
				funcName = "link_my_char";
			}
			
			htmlCode = "<table width='316' height='206' border='0' cellpadding='0' cellspacing='0' background='"+_CDN_URL_+"tekken6/store/pop_buy_2_bg.gif'>"
				+ "<tr>"
				+ "<td valign='bottom'>"
				+ "<table width='266' border='0' align='center' cellpadding='0' cellspacing='0'>"
				+ "<tr>"
				+ "<td height='59' valign='top' align='right'><a href='javascript:closePop(\"viewItem\");'><img src='"+_CDN_URL_+"tekken6/store/btn_pop_close.gif' alt='닫기' width='14' height='14' border='0'></a></td>"
				+ "</tr>"
				+ "<tr>"
				+ "<td height='21' valign='top' align='center' class='etc_store_item_name'>아이템 구매가 완료되었습니다!</td>"
				+ "</tr>"
				+ "<tr>"
				+ "<td height='65' valign='top' align='center' class='etc_store_item_info line20'>구입하신 아이템을 캐릭터에 장착하시면 <br/>자유롭게 사용하실 수 있습니다.</td>"
				+ "</tr>"
				+ "<tr>"
				+ "<td height='45' valign='top' align='center'><a href='javascript:"+ funcName +"();'><img src='"+_CDN_URL_+"tekken6/store/btn_wear.gif' alt='장착하기' width='55' height='20' border='0' class='gab6'></a>"
				+ "<a href='javascript:closePop(\"viewItem\");'><img src='"+_CDN_URL_+"tekken6/store/btn_close.gif' alt='닫기' width='42' height='20' border='0'></a></td>"
				+ "</tr>"
				+ "</table>"
				+ "</td>"
				+ "</tr>"
				+ "</table>";
		}
		else if (div == "nameList") {
			htmlCode = "<table width='383' height='323' border='0' cellpadding='0' cellspacing='0' background='"+_CDN_URL_+"tekken6/store/pop_name_1_bg.gif'>"
				+ "<tr>"
				+ "<td valign='top'>"
				+ "<table width='369' height='30' border='0' cellpadding='0' cellspacing='0'>"
				+ "<tr>"
				+ "<td valign='bottom' align='right'><a href='javascript:closePop(\"viewItem\");'><img src='"+_CDN_URL_+"tekken6/store/btn_pop_close.gif' alt='닫기' width='14' height='14' border='0'></a></td>"
				+ "</tr>"
				+ "</table>"
				+ "</td>"
				+ "</tr>"
				+ "</table>";
		}
		
		return htmlCode;
	}
	
	// 아이템 관련 값 ""인지 확인 	
	function checkItemValue(itemDiv, itemId) {
		var	itemImg = $("#img_name_" + itemId).html();
		var	itemName = $("#name_" + itemId).html();
		var	itemPrice = $("#price_" + itemId).html();
		
		if (itemDiv == "C") {
			id = $("input[@type=radio][@checked][@name=itemId_"+itemId+"]").val();
			itemName = $("#itemName_"+id).html();
		}
		// 체크하고자 하는 모든 값이 있을 경우 
		if (itemImg!="" && itemImg!=null && itemName!="" && itemName!=null && itemPrice!="" && itemPrice!=null) {
			return true;
		}
		else {
			closePop("viewItem");
			showMsg("shop_err4", "");
			return false;
		}

		return true;
	} 

	// 구매 팝업 내용 확인 
	function getItemInfo(itemDiv, itemId) {
		var checkLogin = loginConfirm();
		var checkValue = true;
		if (itemDiv!="N" && itemDiv!="A") {
			checkValue = checkItemValue(itemDiv, itemId);
		}	

		if (checkLogin != true || checkValue != true) {
			return;
		}
		
	    $.ajax({
	        type : "POST",
	        url : "/sub_main/shop_br/body/itemInfo_pop.php",
	        data: {
	        	itemDiv:itemDiv 
	        },
	        timeout: 10000,
	        success : function(response) {
	        	if (itemDiv=="N" || itemDiv=="A" || itemDiv=="E") {
	        		itemInfoPop2(response, itemDiv, itemId);
	        	}
	        	else {
	        		itemInfoPop(response, itemDiv, itemId);
	        	}
			},
	        error : function(){
				closePop("viewItem");
	        	showMsg("error", "-v1");
	        }
	    });
	}

	// 착용 구매 팝업 내용 확인 
	function getWearItemInfo(itemId) {
		var checkLogin = loginConfirm();

		if (itemId=="" || itemId==null) {
			closePop("viewItem");
			showMsg("shop_err4", "");
			return;
		}
		if (checkLogin != true) {
			return;
		}
		
	    $.ajax({
	        type : "POST",
	        url : "/sub_main/shop_br/body/wear_item_info.php",
	        data: {
	        	itemId:itemId 
	        },
	        timeout: 10000,
	        success : function(response) {
				var spVal = response.split("|");
				if (spVal[0] == "true") {
					$("#viewItem").html(spVal[1]);
					$("#viewItem").show();
				}			
				else if (spVal[0] == "false") {
					closePop("viewItem");
					showMsg(spVal[1], "");
				}
				else if (spVal[0] == "error") {
					closePop("viewItem");
					showMsg(spVal[0], "-"+spVal[1]);
				}
				else {
					closePop("viewItem");
					showMsg(response, "");
				}
			},
	        error : function(){
				closePop("viewItem");
	        	showMsg("error", "-v4");
	        }
	    });
	}
	
	// 아이템 구매 처리 
	function buyItem(itemDiv, itemId) {
		var checkLogin = loginConfirm();
		if (checkLogin == false) {
			return;
		}
		
		if (itemDiv == "C") {
			itemId = $("input[@type=radio][@checked][@name=itemId_"+itemId+"]").val();
		}
		if (itemDiv == "E") {
			pageUrl = "/sub_main/shop_br/body/buyEmblem_prc.php";
		}
		else {
			pageUrl = "/sub_main/shop_br/body/buyItem_prc.php";			
		}
		
	    $.ajax({
	        type : "POST",
	        url : pageUrl,
	        data: { 
				itemDiv:itemDiv,
				itemId:itemId
	        },
	        timeout: 3000,
	        success : function(response) {
			   buyItemPop(itemDiv, response);
			},
	        error : function(){
				closePop("viewItem");
	        	showMsg("error", "-v2");
	        }
	    });
	}

	//아이템 구매 완료 후 띄워주는 div
	function buySuccessDiv(itemDiv){
		var htmlCode = getHtmlCode("buySuccessDiv", itemDiv);
		$("#viewItem").html(htmlCode);
	}

	function getNameList() {
		var page_url = "/sub_main/shop_br/body/name_info.php";
	    
		if(page_url == false) {
			closePop("viewItem");
			showMsg("confirm_err1", "");
			return;
	    }
		$.ajax({
	        type : "POST",
	        url : page_url,
	        timeout: 5000,
	        success : function(response) {
			   nameListPage(response);
			},
	        error : function(){
				closePop("viewItem");
	        	showMsg("error", "-v1");
	        }
	    });		
	}
	
	function nameListPage(response) {
		var result = response.split("|");

		if (result[0] == "true") {
			$("#viewItem").html(result[1]);
			$("#viewItem").show();		
		}
		else if (result[0] == "false") {
			closePop("viewItem");
			showMsg(result[1], "");
		}
		else if (result[0] == "error") {
			closePop("viewItem");
			showMsg(result[0], "-"+result[1]);
		}
		else {
			closePop("viewItem");
			showMsg(response, "");
		}		
	}
	

	// 아이템 구매처리 후 팝업 
	function buyItemPop(itemDiv, response) {
		var spVal = response.split("|");
		if (spVal[0] == "true") {
			buySuccessDiv(itemDiv);
		} 
		else if (spVal[0] == "false") {
			closePop("viewItem");
			showMsg(spVal[1], '');
		}
		else if (spVal[0] == "error") {
			closePop("viewItem");
			showMsg(spVal[0], "-"+spVal[1]);
		}
		else {
			closePop("viewItem");
			showMsg(response, '');
		}
	}

	// 아이템 구매 정보 팝업	
	function itemInfoPop(response, itemDiv, itemId) {
		var spVal = response.split("|");
		
		if (spVal[0] == "true") {
		 	var	itemImg = $("#img_name_" + itemId).html();
		 	var	itemName = $("#name_" + itemId).html();
		 	var	itemPrice_str = $("#price_" + itemId).html();
		 	var nameVal = "";
		 	var partVal = "";
			var	name = "캐릭터";
			var	part = "부위";	 	
		 	var myMoney = spVal[1];
		 	var myMoney_str = numberFormat(myMoney);
		 	var itemPrice = strToNumber(itemPrice_str);
		 	var leftMoney = myMoney - itemPrice;
		 	var leftMoney_str = numberFormat(leftMoney);
		 	
		 	if (leftMoney < 0) {
				leftMoney_str = "-" + leftMoney_str;
		 	}
		 	
		 	if (itemDiv == "W") {
		 		nameVal = $("#char_name").html();
		 		partVal = $("#partId > option:selected").html();
		 	}
		 	else if (itemDiv == "C") {
		 		nameVal = $("#char_name").html();
				id = $("input[@type=radio][@checked][@name=itemId_"+itemId+"]").val();
//				itemName = $("#itemName_"+id).html() + "(" + $("#setType_"+itemId).html() + "P)";
				itemName = $("#itemName_"+id).html();
				partVal = $("#partName_"+itemId).html() + $("#setType_"+itemId).html();				
			}

			var htmlCode = "<table width=\"316\" height=\"308\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" background=\""+_CDN_URL_+"tekken6/store/pop_buy_3_bg.gif\">"
				+ "<tr>"
				+ "<td valign=\"top\">"
				+ "<table width='100%' height='62' border='0' cellpadding='0' cellspacing='0'>"
				+ "<tr>"
				+ "<td width='275'><a onmousedown='start_drag(\"viewItem\");'><img src='"+_CDN_URL_+"tekken6/main/blank.gif' width='275' height='62'  border='0' style='cursor:hand'></a></td>"
				+ "<td valign='top' style='padding:15px 0 0 0;'><a href='javascript:closePop(\"viewItem\");'><img src='"+_CDN_URL_+"tekken6/store/btn_pop_close.gif' alt='닫기' width='14' height='14' border='0'></a></td>"
				+ "</tr>"
				+ "</table>"
				+ "<table width=\"266\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">"
				+ "<tr>"
				+ "<td height=\"95\" valign=\"top\">"
				+ "<table width=\"100%\" height=\"95\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"
				+ "<tr>"
				+ "<td width=\"101\" valign=\"top\"><img src=\""+ itemImg +"\" width=\"80\" height=\"80\"></td>"
				+ "<td valign=\"top\">"
				+ "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"
				+ "<tr>"
				+ "<td height=\"32\" valign=\"top\" class=\"etc_store_item_name\" style=\"padding:1px 0 0 0;\">"+ itemName +"</td>"
				+ "</tr>"
				+ "<tr>"
				+ "<td height=\"18\" valign=\"top\" class=\"etc_store_item_money\" style=\"padding:0 0 0 12px;\">"+ name +" : "+ nameVal +"</td>"
				+ "</tr>"
				+ "<tr>"
				+ "<td height=\"18\" valign=\"top\" class=\"etc_store_item_money\" style=\"padding:0 0 0 12px;\">"+ part +" : "+ partVal +"</td>"
				+ "</tr>"
				+ "<tr>"
				+ "<td valign=\"top\" class=\"etc_store_item_money\" style=\"padding:0 0 0 12px;\">아이템 가격 : <span class=\"etc_orange\">"+ itemPrice_str +"G</span></td>"
				+ "</tr>"
				+ "</table>"
				+ "</td>"
				+ "</tr>"
				+ "</table>"
				+ "</td>"
				+ "</tr>"
				+ "<tr>"
				+ "<td height=\"80\" valign=\"bottom\">"
				+ "<table width=\"224\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">"
				+ "<tr>"
				+ "<td height=\"14\" valign=\"bottom\" class=\"etc_store_item_info2 vertical3\">내 파이트머니</td>"
				+ "<td align=\"right\" valign=\"bottom\" class=\"store_item_info2\">"+ myMoney_str +"G</td>"
				+ "</tr>"
				+ "<tr>"
				+ "<td height=\"20\" valign=\"bottom\" class=\"etc_store_item_info2 vertical3\">아이템 가격</td>"
				+ "<td height=\"20\" align=\"right\" valign=\"bottom\" class=\"store_item_info2\">"+ itemPrice_str +"G</td>"
				+ "</tr>"
				+ "<tr>"
				+ "<td height=\"25\" valign=\"bottom\" class=\"etc_store_item_name2 vertical3\">남은 파이트머니</td>"
				+ "<td height=\"25\" align=\"right\" valign=\"bottom\" class=\"store_item_info2\"><span class=\"etc_orange\">"+ leftMoney_str +"G</span></td>"
				+ "</tr>"
				+ "</table>"
				+ "</td>"
				+ "</tr>"
				+ "<tr>"
				+ "<td height=\"56\" valign=\"bottom\" align=\"center\"><a href=\"javascript:buyItem('"+ itemDiv + "', '"+ itemId + "');\">"
				+ "<img src=\""+_CDN_URL_+"tekken6/store/btn_buy_2.gif\" alt=\"구매하기\" width=\"55\" height=\"20\" border=\"0\"></a></td>"
				+ "</tr>"
				+ "</table>"
				+ "</td>"
				+ "</tr>"
				+ "</table>";	

			$("#viewItem").html(htmlCode);
			$("#viewItem").show();		
		}
		else if (spVal[0] == "false") {
			closePop("viewItem");
			showMsg(spVal[1], "");
		}
		else if (spVal[0] == "error") {
			closePop("viewItem");
			showMsg(spVal[0], "-"+spVal[1]);
		}
		else {
			closePop("viewItem");
			showMsg(response, "");
		}
	}

	//---------- 아이템 구매 정보 팝업 (칭호/팀엠블렘)	
	function itemInfoPop2(response, itemDiv, itemId) {
		var spVal = response.split("|");
		
		if (spVal[0] == "true") {
		 	var	itemName = $("#name_" + itemId).html();
		 	var	itemPrice_str = $("#price_" + itemId).html();
		 	var myMoney = spVal[1];
		 	var myMoney_str = numberFormat(myMoney);
		 	var itemPrice = strToNumber(itemPrice_str);
		 	var leftMoney = myMoney - itemPrice;
		 	var leftMoney_str = numberFormat(leftMoney);
		 	
		 	if (leftMoney < 0) {
				leftMoney_str = "-" + leftMoney_str;
		 	}
	
		 	//------------- 아이템들에 따라서 정보 구분
		 	if (itemDiv == "A") {
		 		name = "칭호명";
		 		MoneyName = "내 파이트머니"; 
				itemImg = ""+_CDN_URL_+"tekken6br/store/name_gold.jpg";
				teamLevel = $("#teamLevel > option:selected").html();
		 	}
		 	else if (itemDiv == "N") {
		 		name = "칭호플레이트";
		 		MoneyName = "내 파이트머니"; 
		 		itemImg = $("#img_name_" + itemId).html();
				teamLevel = "팀레벨 제한없음";
		 	} 
		 	else {
		 		name = "팀엠블렘";
		 		MoneyName = "팀 파이트머니"; 
		 		itemImg = $("#img_name_" + itemId).html();
				teamLevel = $("#teamLevel > option:selected").html();
				if ($("#itemType").val() == 1) {
					name += "베이스";
				}
				else {
					name += "마크";
				}
		 	}
		 	
			var htmlCode = "<div id=\"pop_wrapper\"><table width=\"316\" height=\"308\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" background=\""+_CDN_URL_+"tekken6/store/pop_buy_3_bg.gif\">"
				+ "<tr><td valign=\"top\"><table width='100%' height='62' border='0' cellpadding='0' cellspacing='0'>"
				+ "<tr><td width='275'><a onmousedown='start_drag(\"viewItem\");'><img src='"+_CDN_URL_+"tekken6br/common/blank.gif' width='275' height='62'  border='0' style='cursor:hand'></a></td>"
				+ "<td valign='top' style='padding:15px 0 0 0;'><a href='javascript:closePop(\"viewItem\");'><img src='"+_CDN_URL_+"tekken6/store/btn_pop_close.gif' alt='닫기' width='14' height='14' border='0'></a></td></tr></table>"
				+ "<table width=\"266\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">"
				+ "<tr><td height=\"95\" valign=\"top\">"
				+ "<table width=\"100%\" height=\"95\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"
				+ "<tr><td width=\"101\" valign=\"top\"><img src=\"" + itemImg +"\" width=\"80\" height=\"80\"></td>"
				+ "<td valign=\"top\"><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"
				+ "<tr><td height=\"32\" valign=\"top\" class=\"store_item_name\" style=\"padding:1px 0 0 0;\">"+ itemName +"</td></tr>"
				+ "<tr><td height=\"18\" valign=\"top\" class=\"store_item_detail\" style=\"padding:0 0 0 12px;\">"+ name +"</td></tr>"
				+ "<tr><td height=\"18\" valign=\"top\" class=\"store_item_money\" style=\"padding:0 0 0 12px;\">"+ teamLevel +"</td></tr>"
				+ "<tr><td valign=\"top\" class=\"store_item_money\" style=\"padding:0 0 0 12px;\">아이템 가격 : <span class=\"orange\">"+ itemPrice_str +"G</span></td></tr>"
				+ "</table></td></tr></table></td></tr>"
				+ "<tr><td height=\"80\" valign=\"bottom\"><table width=\"224\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">"
				+ "<tr><td height=\"14\" valign=\"bottom\" class=\"store_item_info2 vertical3\">"+ MoneyName +"</td>"
				+ "<td align=\"right\" valign=\"bottom\" class=\"store_item_info2\">"+ myMoney_str +"G</td></tr>"
				+ "<tr><td height=\"20\" valign=\"bottom\" class=\"store_item_info2 vertical3\">아이템 가격</td>"
				+ "<td height=\"20\" align=\"right\" valign=\"bottom\" class=\"store_item_info2\">"+ itemPrice_str +"G</td></tr>"
				+ "<tr><td height=\"25\" valign=\"bottom\" class=\"store_item_name2 vertical2\">남은 파이트머니</td>"
				+ "<td height=\"25\" align=\"right\" valign=\"bottom\" class=\"store_item_info2\"><span class=\"orange\">"+ leftMoney_str +"G</span></td></tr>"
				+ "</table></td></tr><tr>"
				+ "<td height=\"56\" valign=\"bottom\" align=\"center\"><a href=\"javascript:buyItem('"+ itemDiv + "', '"+ itemId + "');\"><img src=\""+_CDN_URL_+"tekken6br/store/btn_buy_2.gif\" alt=\"구매하기\" width=\"55\" height=\"20\" border=\"0\"></a></td>"
				+ "</tr></table></td></tr></table></div>";	

			$("#viewItem").html(htmlCode);
			$("#viewItem").show();	
		}
		else if (spVal[0] == "false") {
			closePop("viewItem");
			showMsg(spVal[1], "");
		}
		else if (spVal[0] == "error") {
			closePop("viewItem");
			showMsg(spVal[0], "-"+spVal[1]);
		}
		else {
			closePop("viewItem");
			showMsg(response, "");
		}
	}
	
