// ÇÃ·¡½Ã¿¡¼­ °¡°Ý º¯°æ½Ã È£Ãâ
function sendPrice(nLeft, nRight){
	TotalSearch.priceStart = nLeft;
	TotalSearch.priceEnd = nRight;
}

// ÅëÇÕ°Ë»ö
var TotalSearch = {
	isDetailSearch : false,			// »ó¼¼°Ë»ö Ã¢ ¿ÀÇÂ ¿©ºÎ
	order : "ACCURACY",				// Á¤·Ä¼ø
	priceStart : 0,					// °¡°Ý ÃÖ¼Ò°ª
	priceEnd : 0,					// °¡°Ý ÃÖ´ë°ª
	category : "",					// Ä«Å×°í¸®
	reqNo : null,					// Ä«Å×°í¸® ½ÃÄö½º
	resultJson : null,				// °á°ú json
	// Å°¿öµå ¼±ÅÃ
	inKeyword : function(keyword){
		document.getElementById("total_keyword").value = keyword;
		TotalSearch.searchSubmit();
	},
	// ÅèÇÕ°Ë»ö ÀÎ±â °Ë»ö¾î(¸ÞÀÎ)
	keywordView1 : function(){
		if(typeof(keyArray) != undefined && typeof(keyArray) != "undefined"){
			var byteLength = 0;
			var max = (keyArray.length <= 3) ? keyArray.length : 3;
			document.write("<li>");
			for (var i = 0; i < max; i++) {
				byteLength += fnGetByteLen(keyArray[i]);
				if(byteLength > 24){
					break;
				}
				document.write("<a style=\"cursor: pointer;\" id=\"bestKeyWord" + i + "\">" + keyArray[i] + "</a> ");
			}
			document.write("</li>");
		}
	},
	// ÅèÇÕ°Ë»ö ÀÎ±â °Ë»ö¾î
	keywordView2 : function(){
		if(typeof(keyArray) != undefined && typeof(keyArray) != "undefined"){
			for(var i = 0; i < keyArray.length; i++){
				if(i == 0){
					document.writeln("<dd class=\"fir\"><a href=\"javascript:;\" onclick=\"javascript:TotalSearch.inKeyword('" + keyArray[i] + "');\">" + keyArray[i] + "</a></dd>");
				}else{
					document.writeln("<dd><a href=\"javascript:;\" onclick=\"javascript:TotalSearch.inKeyword('" + keyArray[i] + "');\">" + keyArray[i] + "</a></dd>");
				}
			}	
		}
	},
	// °Ë»ö
	searchSubmit : function(){
		var searchKeyword = document.getElementById("total_keyword");
		if(searchKeyword.value.trim().length < 2){
			alert('µÎ±ÛÀÚ ÀÌ»ó¸¸ °Ë»öÀÌ °¡´ÉÇÕ´Ï´Ù.');
			searchKeyword.focus();
			return;
		}
		searchKeyword.value = searchKeyword.value.trim();
		document.getElementById("price_start").value = "0";
		document.getElementById("price_end").value = "0";
		document.getElementById("grade").value = "";
		document.getElementById("order").value = this.order;
		if(this.isDetailSearch){
			document.getElementById("price_start").value = this.priceStart;
			document.getElementById("price_end").value = this.priceEnd;
			var grade = document.getElementsByName("radio_grade");
			for(var i = 0; i < grade.length; i++){
				if(grade[i].checked == true){
					document.getElementById("grade").value = grade[i].value;
				}
			}
		}
		// post ¹æ½Ä¿¡¼­ get ¹æ½ÄÀ¸·Î º¯°æ 
//		document.getElementById("total_search_form").submit();
		var url = "totalSearch.do?";
		url += "category=" + document.getElementById("category").value;
		url += "&grade=" + document.getElementById("grade").value;
		url += "&price_start=" + document.getElementById("price_start").value;
		url += "&price_end=" + document.getElementById("price_end").value;
		url += "&order=" + document.getElementById("order").value;
		if(document.getElementById("inner_search") != null && document.getElementById("inner_search").checked == true){
			url += "&inner_search=" + document.getElementById("inner_search").value;
		}
		url += "&previous_keyword=" + encodeURI(document.getElementById("previous_keyword").value);
		url += "&total_keyword=" + encodeURI(document.getElementById("total_keyword").value);
		location.href = url;
	},
	// »ó¼¼ °Ë»öÃ¢ ¿­±â
	openDetail : function(){
		document.getElementById("open_detail_btn").style.display = "none";
		document.getElementById("close_detail_btn").style.display = "";
		document.getElementById("search_detail").style.display = "";
		this.isDetailSearch = true;
	},
	// »ó¼¼ °Ë»öÃ¢ ´Ý±â
	closeDetail : function(){
		document.getElementById("open_detail_btn").style.display = "";
		document.getElementById("close_detail_btn").style.display = "none";
		document.getElementById("search_detail").style.display = "none";
		this.isDetailSearch = false;
	},
	// ¸¸Á·µµ ¼±ÅÃ
	setGrade : function(grade){
		this.grade = grade;
	},
	// Á¤·Ä¼ø ¼±ÅÃ
	setOrder : function(order){
		this.order = order;
		this.searchSubmit();
	},
	// ´õº¸±â ¿­±â
	openMore : function(category, reqNo){
		this.category = category;
		this.reqNo = reqNo;
		document.getElementById("more_btn_" + reqNo).innerHTML = "<a href=\"javascript:TotalSearch.closeMore('" + category + "', " + reqNo + ");\">ÀÛ°Ôº¸±â<img src='http://appstore.show.co.kr/res/images/buyer/btn/btn_add_on.gif' /></a>";
		document.getElementById("search_list_view_" + reqNo).innerHTML = "<img src=\"/res/images/buyer/loading.gif\" style=\"margin:120 0 120 320\" />";
		this.searchMoreSubmit(1);
	},
	// ´õº¸±â ´Ý±â
	closeMore : function(category, reqNo){
		document.getElementById("more_btn_" + reqNo).innerHTML = "<a href=\"javascript:TotalSearch.openMore('" + category + "', " + reqNo + ");\">´õº¸±â<img src='http://appstore.show.co.kr/res/images/buyer/btn/btn_add_off.gif' /></a>";
		document.getElementById("search_list_view_" + reqNo).innerHTML = "";
		document.getElementById("search_page_view_" + reqNo).innerHTML = "";
		
		var search = this.resultJson.list[0].totalSearch;
		var rowList = search.row[0]['kr.co.show.buyer.bean.dataBean.TotalSearchRowBean'];
		var listView = document.getElementById("search_list_view_" + reqNo);
		var html = "";
		if(rowList.length == null){
			var temp = rowList;
			rowList = new Array(1);
			rowList[0] = temp;
		}
		for(var i = 0; i < rowList.length; i++){
			if(i >= 3){
				break;
			}
			var dl = document.createElement("dl");
			dl.id = "search_item_" + reqNo;
			dl.name = "search_item_" + reqNo;
			dl.className = "typeA";
			
			var appId = (rowList[i].APP__ID != null) ? (rowList[i].APP__ID + "") : "";
			var createId = (rowList[i].CREATE__ID != null) ? rowList[i].CREATE__ID : "";
			var cpName = (rowList[i].CP__NAME != null) ? rowList[i].CP__NAME : "";
			var createHome = (rowList[i].CREATE__HOME != null) ? rowList[i].CREATE__HOME : "";
			var thumbnail = "/upload/app/" + appId.substring(0, 4) + "/" + appId.substring(4, 6) + "/" + appId + "/default_img/" + appId + "_72_72.png";
			var link = "appView.do?appId=" + rowList[i].SERVICE__NO + "&pCur=00";
			
			var html = "";
			html += "<dt><span class=\"ft_bold\"><a href=\"" + link + "\">" + rowList[i].SERVICE__NAME + "</a></span> ";
			for(var j = 1; j <= 5; j++){
				if(j <= rowList[i].AGE__STAR || j == 1){
					html += "<img src=\"http://appstore.show.co.kr/res/images/buyer/icn/icn_sgrade_on.gif\" />";
				}else{
					html += "<img src=\"http://appstore.show.co.kr/res/images/buyer/icn/icn_sgrade_off.gif\" />";
				}
			}
			html += "<span class=\"cate\">" + rowList[i].CAT__NAME.replace(",", "&gt;") + "</span><a href=\"" + link + "\"><img src=\"http://appstore.show.co.kr/res/images/buyer/btn/btn_go.gif\" alt=\"¹Ù·Î°¡±â\" class=\"vat\" /></a></dt>";
			html += "<dd class=\"thumb\"><img src=\"" + thumbnail + "\" width=\"36\" height=\"36\" />";
			html += "<div class=\"box\"><a href=\"" + link + "\"><img src=\"http://appstore.show.co.kr/res/images/buyer/thumb_box04.png\" class=\"png24\" /></a></div>";
			html += "</dd>";
			html += "<dd class=\"name\">" + createId + " / <img src=\"http://appstore.show.co.kr/res/images/buyer/icn/icn_class_normal.gif\" alt=\"ÀÏ¹Ý\" class=\"vam\" /> " + cpName + " / " + createHome + "</dd>";
			html += "<dd class=\"contA\"><a href=\"" + link + "\">" + rowList[i].APP__INFO + "</a></dd>";
			dl.innerHTML = html;
			listView.appendChild(dl);
		}
	},
	// ´õº¸±â ÆäÀÌÁö ÀÌµ¿
	searchMoreGoPage : function(category, page){
		document.getElementById("search_list_view_" + this.reqNo).innerHTML = "<img src=\"/res/images/buyer/loading.gif\" style=\"margin:280 0 280 320\" />";
		this.category = category;
		this.searchMoreSubmit(page);
	},
	// ´õº¸±â
	searchMoreSubmit : function(pg){
		var innerSearch = document.getElementById("inner_search");
		if(innerSearch != null && innerSearch.checked == true){
			innerSearch = "Y";
		}else{
			innerSearch = "N";
		}
		jQuery.ajax({
			type : "post",
			url : "totalSearchMore.ts",
			data : {
				previous_keyword : document.getElementById("previous_keyword").value,
				total_keyword : document.getElementById("total_keyword").value,
				category : CATE_CODE.get(this.category),
				order : document.getElementById("order").value,
				grade : document.getElementById("grade").value,
				page : pg,
				price_start : this.priceStart,
				price_end : this.priceEnd,
				inner_search : innerSearch
			},
			success : function(msg){
				TotalSearch.resultJson = msg.evalJSON();
				var search = TotalSearch.resultJson.list[0].totalSearch;
				var rowList = search.row[0]['kr.co.show.buyer.bean.dataBean.TotalSearchRowBean'];
				var listView = document.getElementById("search_list_view_" + TotalSearch.reqNo);
				
				document.getElementById("total_count_" + TotalSearch.reqNo).innerHTML = search.totalCount;
				document.getElementById("search_list_view_" + TotalSearch.reqNo).innerHTML = "";
				document.getElementById("search_page_view_" + TotalSearch.reqNo).innerHTML = "";
				if(rowList == null){
					return;
				}
				if(rowList.length == null){
					var temp = rowList;
					rowList = new Array();
					rowList[0] = temp;
				}
				
				var html = "";
				for(var i = 0; i < rowList.length; i++){
					var dl = document.createElement("dl");
					dl.id = "search_item_" + TotalSearch.reqNo;
					dl.name = "search_item_" + TotalSearch.reqNo;
					dl.className = "typeA";
					
					var appId = (rowList[i].APP__ID != null) ? (rowList[i].APP__ID + "") : "";
					var createId = (rowList[i].CREATE__ID != null) ? rowList[i].CREATE__ID : "";
					var cpName = (rowList[i].CP__NAME != null) ? rowList[i].CP__NAME : "";
					var createHome = (rowList[i].CREATE__HOME != null) ? rowList[i].CREATE__HOME : "";
					var thumbnail = "/upload/app/" + appId.substring(0, 4) + "/" + appId.substring(4, 6) + "/" + appId + "/default_img/" + appId + "_72_72.png";
					var link = "appView.do?appId=" + rowList[i].SERVICE__NO + "&pCur=00";
					
					var html = "";
					html += "<dt><span class=\"ft_bold\"><a href=\"" + link + "\">" + rowList[i].SERVICE__NAME + "</a></span> ";
					
					for(var j = 1; j <= 5; j++){
						if(j <= rowList[i].AGE__STAR || j == 1){
							html += "<img src=\"http://appstore.show.co.kr/res/images/buyer/icn/icn_sgrade_on.gif\" />";
						}else{
							html += "<img src=\"http://appstore.show.co.kr/res/images/buyer/icn/icn_sgrade_off.gif\" />";
						}
					}
					html += "<span class=\"cate\">" + rowList[i].CAT__NAME.replace(",", "&gt;") + "</span><a href=\"" + link + "\"><img src=\"http://appstore.show.co.kr/res/images/buyer/btn/btn_go.gif\" alt=\"¹Ù·Î°¡±â\" class=\"vat\" /></a></dt>";
					html += "<dd class=\"thumb\"><img src=\"" + thumbnail + "\" width=\"36\" height=\"36\" />";
					html += "<div class=\"box\"><a href=\"" + link + "\"><img src=\"http://appstore.show.co.kr/res/images/buyer/thumb_box04.png\" class=\"png24\" /></a></div>";
					html += "</dd>";
					html += "<dd class=\"name\">" + createId + " / <img src=\"http://appstore.show.co.kr/res/images/buyer/icn/icn_class_normal.gif\" alt=\"ÀÏ¹Ý\" class=\"vam\" /> " + cpName + " / " + createHome + "</dd>";
					html += "<dd class=\"contA\"><a href=\"" + link + "\">" + rowList[i].APP__INFO + "</a></dd>";
					dl.innerHTML = html;
					listView.appendChild(dl);
				}
				var ul = document.createElement("ul");
				ul.className = "buyerPaging";
				var html = "";
				if(search.currentPage == 1){
					html += "<li class=\"btn\"><a><img src=\"http://appstore.show.co.kr/res/images/buyer/btn/btn_first.gif\" alt=\"Ã³À½\" /></a></li> ";
				}else{
					html += "<li class=\"btn\"><a href=\"javascript:TotalSearch.searchMoreGoPage('" + TotalSearch.category + "', 1);;\"><img src=\"http://appstore.show.co.kr/res/images/buyer/btn/btn_first.gif\" alt=\"Ã³À½\" /></a></li> ";
				}
				if(search.prevPage == -1){
					html += "<li class=\"btn\"><a><img src=\"http://appstore.show.co.kr/res/images/buyer/btn/btn_prev.gif\" alt=\"ÀÌÀü\" /></a></li> ";
				}else{
					html += "<li class=\"btn\"><a href=\"javascript:TotalSearch.searchMoreGoPage('" + TotalSearch.category + "', " + search.prevPage + ");;\"><img src=\"http://appstore.show.co.kr/res/images/buyer/btn/btn_prev.gif\" alt=\"ÀÌÀü\" /></a></li> ";
				}
				for(var page = search.startPage; page <= search.endPage; page++){
					if(search.currentPage == page){
						html += "<li><a class=\"current\">" + page + "</a></li> ";
					}else{
						html += "<li><a href=\"javascript:TotalSearch.searchMoreGoPage('" + TotalSearch.category + "', " + page + ");\">" + page + "</a></li> ";
					}
				}
				if(search.nextPage == -1){
					html += "<li class=\"btn\"><a><img src=\"http://appstore.show.co.kr/res/images/buyer/btn/btn_next.gif\" alt=\"´ÙÀ½\" /></a></li> ";
				}else{
					html += "<li class=\"btn\"><a href=\"javascript:TotalSearch.searchMoreGoPage('" + TotalSearch.category + "', " + search.nextPage + ");;\"><img src=\"http://appstore.show.co.kr/res/images/buyer/btn/btn_next.gif\" alt=\"´ÙÀ½\" /></a></li> ";
				}
				if(search.currentPage == search.totalPage){
					html += "<li class=\"btn\"><a><img src=\"http://appstore.show.co.kr/res/images/buyer/btn/btn_last.gif\" alt=\"¸¶Áö¸·\" /></a></li> ";
				}else{
					html += "<li class=\"btn\"><a href=\"javascript:TotalSearch.searchMoreGoPage('" + TotalSearch.category + "', " + search.totalPage + ");;\"><img src=\"http://appstore.show.co.kr/res/images/buyer/btn/btn_last.gif\" alt=\"¸¶Áö¸·\" /></a></li> ";
				}
				ul.innerHTML = html;
				document.getElementById("search_page_view_" + TotalSearch.reqNo).appendChild(ul);
			}
		});
	}
}

var CATE_CODE = new JsMap();
CATE_CODE.put("ÀüÃ¼", "00");
CATE_CODE.put("°ÔÀÓ", "01");
CATE_CODE.put("¿µÈ­/TV/¹ÂÁ÷", "02");
CATE_CODE.put("À¯Æ¿¸®Æ¼", "03");
CATE_CODE.put("¶óÀÌÇÁ½ºÅ¸ÀÏ", "04");
CATE_CODE.put("ºñÁö´Ï½º/±ÝÀ¶", "05");
CATE_CODE.put("e-ºÏ/±³À°", "06");
CATE_CODE.put("´º½º", "07");
CATE_CODE.put("¼¼Æ®APP", "08");
CATE_CODE.put("ÇÒÀÎAPP", "09");
CATE_CODE.put("ÄíÆù¸ô", "10");
CATE_CODE.put("¼­ºñ½º°¡ÀÌµå", "11");
CATE_CODE.put("ÀÌ¿ë¾È³»", "12");


// treemap
function JsMap(){
	this._array = new Array();
	this.pointer = 0;
	this._getIndexByKey = function(key){
    	for(var i=0; i< this._array.length; i++){
			if(key == this._array[i][0]){
				return i;
			}
		}
		return -1;
	}
	this.put = function(key,value){
		var index = this._getIndexByKey(key)
		if(index == -1){
			var newArray = new Array();//key¿Ívalue¸¦ ´ã´Â ¹è¿­
			newArray[0] = key;
			newArray[1] = value;
			this._array[this._array.length] = newArray;
		}else{
			this._array[index][1] = value;
		}
	}
	this.get = function(key){
		for(var i=0; i < this._array.length; i++){
			if(this._array[i][0] == key)
				return this._array[i][1];
		}
	}
	this.isNext = function(){
		var result;
		if(this._array.length > this.pointer){     
			result =  true;
		}else{
			result = false;
		}
		this.pointer++;
		return result;
	}
	this.size = function(){
		return this._array.length;
	}
	this.nowKey = function(){
		return this._array[this.pointer -1][0];
	}
	this.nowValue = function(){
		return this._array[this.pointer -1][1];
	}
}

// byte ¼ö °è»ê
//String.prototype.getBytes = function() {
//	return encodeURIComponent(this).replace(/%../g, 'x').length;
//};

// byte ¼ö °³»ê
function fnGetByteLen(str) {
	var ibyte = 0;
	for(var i = 0; i < str.length; i++) {
		var tmp = escape(str.charAt(i));
		if (tmp.length == 1) ibyte++;
		else if (tmp.indexOf("%u") != -1) ibyte += 2;
		else if (tmp.indexOf("%") != -1) ibyte += tmp.length/3;
	}
	return ibyte;
}