jQuery.noConflict();

var buyerBtnActionFnc = 
	(function($){
			var buyerBtnActionFnc = {
				/**
				 * ÄíÅ° Ã¼Å©
				 * 
				 * @return 
				 */
				getCookie : function(){
					return getCookie('KTFE%5FUserID');
				},
				
				/**
				 * Á¤È¸¿ø ±¸ÀÔ´Ü¸¸ °¡´É ¿©ºÎ Ã¼Å©
				 * 
				 * @param serviceNo
				 * @param pageType
				 * @return boolean
				 */
				getAppBuyCheakAjax : function(serviceNo, pageType, alreadyChk, buyUrl, ageGrade){		
					//alert('Á¤È¸¿ø ±¸ÀÔ´Ü¸¸ °¡´É ¿©ºÎ Ã¼Å©  : '+ serviceNo + '@@' + pageType + '@@' +alreadyChk + '@@' +buyUrl);
					var rtnVal = false;
					var time = new Date().getTime();
					if(pageType=="" || pageType=="sale" ||
						pageType=="coupon" || pageType == "set" || pageType == "main"){
						$.ajax({
							url : 'appBuyCheakAjax.do',
							type: 'get',
							dataType : 'text',
							data: {
								'serviceNo' : serviceNo,
								'time' : time,
								'grade' : ageGrade
							},
							success : function(msg){
								//alert(msg);
								switch(msg){
									case "S000" : rtnVal=true; break; 

									case "ACTOR.CD02"  	: alert("ÁØÈ¸¿øÀº ±¸¸ÅÇÏ½Ç ¼ö ¾ø½À´Ï´Ù."); 
												rtnVal=false; break;

									case "MSG.NOT_ADULT_AUTH"  	:										
												var alertMsg = "";
		
												if(ageGrade=="01")
													alertMsg = "12¼¼ÀÌ»ó ÀÌ¿ë °¡´ÉÇÑ AppÀÔ´Ï´Ù.";
												else if(ageGrade=="02")
													alertMsg = "15¼¼ÀÌ»ó ÀÌ¿ë °¡´ÉÇÑ AppÀÔ´Ï´Ù.";
												else if(ageGrade=="03")
													alertMsg = "19¼¼ÀÌ»ó ÀÌ¿ë °¡´ÉÇÑ AppÀÔ´Ï´Ù.";
												else
													alertMsg = "19¼¼ÀÌ»ó ÀÌ¿ë °¡´ÉÇÑ AppÀÔ´Ï´Ù.";
												alert(alertMsg);
												rtnVal=false; break;
									
									case "C"  	: alert(""); 
												rtnVal=false; break;
									default : 
										alert("±¸¸Å °¡´ÉÇÑ ´Ü¸»ÀÌ ¾Æ´Õ´Ï´Ù."); rtnVal=false;
								}
								
								if(rtnVal==true){
									buyerBtnActionFnc.actionPayment(alreadyChk, buyUrl);
								}									
							},
							error : function(){
								alert('±¸¸Å°¡´É¿©ºÎ¸¦ È®ÀÎÁß ¹®Á¦°¡ ¹ß»ýÇÏ¿´½À´Ï´Ù.\n°ü¸®ÀÚ¿¡°Ô ¹®ÀÇÇØ ÁÖ¼¼¿ä.')
							}
						});					
					}
				},
				
				/**
				 * °áÁ¦ ÆäÀÌÁö ÀÌµ¿
				 * 
				 * @param alreadyChk
				 * @param buyUrl
				 * @return
				 */				
				actionPayment : function(alreadyChk, buyUrl){
					if(buyerBtnActionFnc.getCookie()){						
						switch(alreadyChk){
							case "0"  : 
								document.location.href = buyUrl ; break;
							case "-1" : 
								alert("»óÇ°Àº Á¤È¸¿ø¸¸ ±¸¸Å°¡ °¡´ÉÇÕ´Ï´Ù.");
								break;
							default : 
								openWin('510','250', './popup.do?type=confirm&action=buy','win');
								break;
						}
					}else{
						location.href = 
							"http://www.show.co.kr/common/user/cp_sub_login.asp?RETURN_URL="
							+encodeURIComponent(location.href);
					}
				},
				
				/**
				 * ¼±¹° ÇÏ±â 
				 * 
				 * @param appType		: Y(À¯·á) N(¹«·á)
				 * @param serviceNo 	: ¼­ºñ½º³Ñ¹ö
				 * @param pCurrent  	: ´ÜÇ°, ´ÜÇ°ÇÒÀÏ ±¸ºÐ
				 * @param alreadyChk 	: ±¸¸Å¿©ºÎ
				 * @param idx 			: Ä«Å×°í¸® ¾ÆÀÌµð
				 * @param pageType 		: È­¸é¿¡ µû¶ó °áÁ¦ ÆäÀÌÁö¸¦ º¯°æ 
				 * @param pid 	   		: ÇÒÀÎ, ÄíÆù Àº PID·Î Ã³¸® ÇÑ´Ù.
				 * 			"" - ´ÜÇ°, 
				 * 		 "sale"- ÇÒÀÎ
				 *     "coupon"- ÄíÆù
				 *     	  "set"- ¼¼Æ®
				 */
				insertGiftCart : function(appType, serviceNo, pCurrent, alreadyChk, idx, pageType, pid){
					
					if(typeof(appType) == 'undefined')
						appType = "";							
					
					if(typeof(serviceNo) == 'undefined')
						serviceNo = "";					
					
					if(typeof(pCurrent) == 'undefined')
						pCurrent = "";
					
					if(typeof(alreadyChk) == 'undefined')
						alreadyChk = "";
					
					if(typeof(pageType) == 'undefined' )
						pageType = "";

					if(typeof(idx) == 'undefined')
						idx = "";					
					
					if(typeof(pid) == 'undefined')
						pid = "";					
					
					var buyUrl ="";
					
					if(pageType == "") {
						buyUrl = "/appGiftBuy.do?serviceNo=" + serviceNo 
						+ "&pCur=" + pCurrent 
						+ "&idx=" + idx
						+ "&pageType=" + pageType;					
					}else if(pageType	== "sale"){
						buyUrl = "/appGiftSaleBuy.do?pid="+pid+"&pageType="+pageType;
					}else if(pageType	== "coupon"){
						alert('ÄíÆùÀº ¼±¹°ÇÏ±â¸¦ ÁøÇàÇÏ½Ç¼ö ¾ø½À´Ï´Ù.');return;
					}else if(pageType	== "set"){
						buyUrl = "/appGiftSetBuy.do?pid="+pid+"&pageType="+pageType;;
					}
										
					if(appType == "N"){
						alert("¹«·á ¾îÇÃ¸®ÄÉÀÌ¼ÇÀº ¼±¹°À» ÇÏ½Ç¼ö ¾ø½À´Ï´Ù.");
						return;
					}					
					
					//alert(appType + '@@' + serviceNo + '@@' +pageType + '@@' +alreadyChk + '@@' +buyUrl);
					alreadyChk = "0";
					buyerBtnActionFnc.actionPayment(alreadyChk, buyUrl);					
				},				
				
				/**
				 * Âò »óÇ° µî·Ï
				 * 
				 * @param appId : ¼­ºñ½º³Ñ¹ö
				 * @param pCurrent : ´Ü¸» ¾ÆÀÌµð
				 * @return
				 */
				insertMyCart : function(appId, pCurrent, pageType){
										
					if(typeof(appId) == 'undefined')
						appId = "";					
					
					if(typeof(pCurrent) == 'undefined')
						pCurrent = "";
					
					if(typeof(pageType) == 'undefined')
						pageType = "";
										
					
					if(buyerBtnActionFnc.getCookie()){
						$.ajax({
							url : 'appIn.do',
							type: 'get',
							dataType : 'text',
							data: {
								'appId' : appId,
								'pCurrent' : pCurrent,
								'pageType' : pageType
							},
							success : function(msg){
								switch(msg){
									case "S000"  : 									
										openWin('510','250', './popup.do?type=confirm&action=sel','win');
//										var conf = confirm("Âò³»¿ª¿¡ ÀúÀåµÇ¾ú½À´Ï´Ù. Âò³»¿ªÀ¸·Î ÀÌµ¿ÇÏ½Ã°Ú½À´Ï±î?");					
//										if(!conf){
//											return;
//										}
//										location.href = '/myBasket.do';
										break;
									case "MSG.EXISTENT_SELECTION" : 
										openWin('510','250', './popup.do?type=confirm&action=alreadySel','win');
//										var conf = confirm("ÀÌ¹Ì ÂòÇÑ ¾îÇÃÀÔ´Ï´Ù. Âò³»¿ªÀ¸·Î ÀÌµ¿ÇÏ½Ã°Ú½À´Ï±î?");
//										if(!conf){
//											return;
//										}
//										location.href = '/myBasket.do';
										break;
									case "MSG.MAX_SELECTION_CONTENT":
										alert("Âò º¸°üÇÔÀÇ ÃÖ´ë ¼ö·®À» ÃÊ°ú ÇÏ¿´½À´Ï´Ù");
										break;
									case "-3" : //CategoryController.appIn KHUB ¿¹¿Ü¹ß»ý.
										alert("Âò³»¿ª ÀúÀå¿¡ ½ÇÆÐ Çß½À´Ï´Ù.\nÀá½ÃÈÄ ´Ù½Ã ½Ãµµ¹Ù¶ø´Ï´Ù."); break; 
									case "-4" ://CategoryController.appIn InsertFailureException ¿¹¿Ü¹ß»ý.
										alert("Âò³»¿ª ÀúÀå¿¡ ½ÇÆÐ Çß½À´Ï´Ù.\nÀá½ÃÈÄ ´Ù½Ã ½Ãµµ¹Ù¶ø´Ï´Ù."); break; 
									default : 
										alert("Âò³»¿ª ÀúÀå¿¡ ½ÇÆÐ Çß½À´Ï´Ù.\nÀá½ÃÈÄ ´Ù½Ã ½Ãµµ¹Ù¶ø´Ï´Ù."); break;							
								}
							},
							error : function(){alert('¿¡·¯°¡ ¹ß»ýÇß½À´Ï´Ù.')}										
						 });
					}else{
						location.href = 
							"http://www.show.co.kr/common/user/cp_sub_login.asp?RETURN_URL="
							+ encodeURIComponent(location.href);
					}
					
					
				},

				
				/**
				 * »óÇ° °áÁ¦ ÆäÀÌÁö ÀÌµ¿(»óÇ° °áÁ¦ Ã¼Å©)
				 * 
				 * @param appId : ¼­ºñ½º³Ñ¹ö
				 * @param pCurrent : ´ÜÇ°, ´ÜÇ°ÇÒÀÏ ±¸ºÐ
				 * @param alreadyChk : ±¸¸Å¿©ºÎ
				 * @param idx : Ä«Å×°í¸® ¾ÆÀÌµð
				 * @param pageType : È­¸é¿¡ µû¶ó °áÁ¦ ÆäÀÌÁö¸¦ º¯°æ 
				 * @param pid 	   : ÇÒÀÎ, ÄíÆù Àº PID·Î Ã³¸® ÇÑ´Ù.
				 * @param ageGrade : È¸¿ø µî±Þ
				 * 			"" - ´ÜÇ°, 
				 * 		 "sale"- ÇÒÀÎ
				 *     "coupon"- ÄíÆù
				 *     	  "set"- ¼¼Æ®
				 * @return  
				 */
				buyAppCheak : function(appId, pCurrent, alreadyChk, idx, pageType, pid, ageGrade) {

					if(buyerBtnActionFnc.getCookie()){						

					}else{
						location.href = 
							"http://www.show.co.kr/common/user/cp_sub_login.asp?RETURN_URL="
							+encodeURIComponent(location.href);
					}
										
					
					if(typeof(appId) == 'undefined')
						appId = "";					
					
					if(typeof(pCurrent) == 'undefined')
						pCurrent = "";
					
					if(typeof(alreadyChk) == 'undefined')
						alreadyChk = "";
					
					if(typeof(pageType) == 'undefined' )
						pageType = "";

					if(typeof(ageGrade) == 'undefined' )
						ageGrade = "";
					
					
					if(typeof(idx) == 'undefined')
						idx = "";					
					
					if(typeof(pid) == 'undefined')
						pid = "";					
					
					var buyUrl ="";
					
					if(pageType == "") {
						buyUrl = "/appBuy.do?appId=" + appId 
						+ "&pCur=" + pCurrent 
						+ "&idx=" + idx
						+ "&pageType=" + pageType;					
					}else if(pageType	== "sale"){
						buyUrl = "/appSaleBuy.do?pid="+pid+"&pageType="+pageType;
					}else if(pageType	== "coupon"){
						buyUrl = "/appSaleBuy.do?pid="+pid+"&pageType="+pageType;
						//buyUrl = "/appCouponBuy.do?pid"+pid
					}else if(pageType	== "set"){
						buyUrl = "/appSetBuy.do?pid="+pid+"&pageType="+pageType;;
					}else if(pageType	== "main"){
						buyUrl = "/appView.do?appId="+appId+"&pCur=00";
					}
					
					
					buyerBtnActionFnc.getAppBuyCheakAjax(appId, pageType, alreadyChk, buyUrl, ageGrade);

				},
				
				/**
				 * °áÁ¦
				 * 
				 * @param isBuy : ±¸¸Å °¡´É ¿©ºÎ 
				 * @param appId : »óÇ° ¾ÆÀÌµð
				 * @param pCurrent : ´Ü¸» ¾ÆÀÌµð
				 * @param recvpNum : °í°´ ÇÚµåÆù
				 * @param sendpNum : ¹ß¼ÛÀÚ SMS
				 * @param serviceNo : serviceNo
				 * @return  
				 */
				paymentAction : function(isBuy, appId, pCurrent, recvpNum, sendpNum, serviceNo){
					if(isBuy == -1){
						alert("ÁØÈ¸¿øÀº ±¸¸ÅÇÒ ¼ö ¾ø½À´Ï´Ù.");
						location.href = "appView.do?appId=" + appId + "&pCur=" + pCurrent;
					}else if(isBuy > 0){
						alert("ÀÌ¹Ì ±¸¸ÅÇÑ ¾îÇÃ ÀÔ´Ï´Ù.");
						location.href = "appView.do?appId=" + appId + "&pCur=" + pCurrent;
					}else{
						$.ajax({
							url : 'download.do',
							type: 'get',
							dataType : 'text',
							data: {
								'appId' : appId,
								'sendPhoneNum' : sendpNum,
								'rcvPhoneNum' : recvpNum,
								'serviceNo' : serviceNo
							},
							success : function(msg){
								alert(msg);
								location.href = "./appBuyEnd.do?appId=" + appId + "&pCur=" + pCurrent;
							},
							error : function(){alert('¿¡·¯°¡ ¹ß»ýÇß½À´Ï´Ù.')}										
						 });					
					}
				},
				
				/**
				 * SMS ¹ß¼Û
				 * @param appId : »óÇ° ¾ÆÀÌµð
				 * @param serviceNo : ±¸¸Å °¡´É ¿©ºÎ 
				 * @return  
				 */
				sendSMSAction : function(appId, serviceNo){
					if(typeof(appId) == 'undefined'){
						alert("Àß¸øµÈ »óÇ° Á¤º¸ ÀÔ´Ï´Ù.\n°ü¸®ÀÚ¿¡°Ô ¹®ÀÇ ¹Ù¶ø´Ï´Ù.");
						return;
					}
					
					if(typeof(serviceNo) == 'undefined'){
						alert("Àß¸øµÈ »óÇ° Á¤º¸ ÀÔ´Ï´Ù.\n°ü¸®ÀÚ¿¡°Ô ¹®ÀÇ ¹Ù¶ø´Ï´Ù.");
						return;
					}
					
					$.ajax({
						url : 'sendSMS.do',
						type: 'get',
						dataType : 'text',						
						data: {
							'appId' : appId,
							'serviceNo' : serviceNo
						},
						success : function(msg){
							switch (msg) {
							case "S000":
								alert("´Ü¸»±â·Î SMS°¡ Àü¼ÛµÇ¾ú½À´Ï´Ù.\nSMS¸¦ ÅëÇØ¼­ ¾îÇÃ¸®ÄÉÀÌ¼ÇÀ» ´Ù¿î·Îµå ¹ÞÀ¸½Ã±â ¹Ù¶ø´Ï´Ù.");
								break;
							default:
								alert("¹®ÀÚ¹ß¼ÛÁß ¹®Á¦°¡ ¹ß»ýÇÏ¿´½À´Ï´Ù.\n°ü¸®ÀÚ¿¡°Ô ¹®ÀÇ ¹Ù¶ø´Ï´Ù.");
								break;
							}
						},
						error : function(){alert('¿¡·¯°¡ ¹ß»ýÇß½À´Ï´Ù.')}
					});
					
				},
				
				
				/**
				 * main ¹è³Ê Å¬¸¯
				 * 
				 * @param linkUrl 
				 * @param mainType
				 * @param seqNum
				 * @param target
				 * @return  
				 */
				bannerLinkAction : function(linkUrl, mainType, seqNum, target){
					if(target == "_self"){
						location.href = linkUrl;
					}else{
						var param = 'toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=650,height=550';
						window.open(linkUrl, target)
					}
					
				},
				
				/**
				 * »óÇ°¹®ÀÇ ¹öÆ° Å¬¸¯
				 * 
				 * @param service_no
				 * @return  
				 */
				boardAppQNAPop : function(service_no){

					if(buyerBtnActionFnc.getCookie()){
					
						if(service_no == ""){
							alert("Àß¸øµÈ Á¢±Þ ÀÔ´Ï´Ù.\n´Ù½Ã ½ÃµµÇØ ÁÖ¼¼¿ä.");
						}else{
							var linkUrl= "/boardAppQNA.do?method=write_form&service_no="+service_no;
							var param = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=510,height=384';
							window.open(linkUrl, '', param)
						}					
					
					}else{
						alert("·Î±×ÀÎÇÑ »ç¿ëÀÚ¸¸ ÀÛ¼ºÇÒ ¼ö ÀÖ½À´Ï´Ù.");
						location.href = 
							"http://www.show.co.kr/common/user/cp_sub_login.asp?RETURN_URL="
							+encodeURIComponent(location.href);						
					}
					
				},
				
				/**
				 * ¿À·ù½Å°í ¹öÆ° Å¬¸¯
				 * 
				 * @param service_no
				 * @return  
				 */				
				boardAppErrorPop : function(service_no){
					if(service_no == ""){
						alert("¼­ºñ½º¹øÈ£°¡ Á¸Àç ÇÏÁö ¾Ê½À´Ï´Ù.");
					}else{
						var linkUrl= "/boardAppError.do?method=write_form&service_no="+service_no;
						var param = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=510,height=384';
						window.open(linkUrl, '', param)
					}				
				},
				
				testAlert : function(){
					alert('ok');
				}
			}
			return buyerBtnActionFnc;
		})(jQuery);