var crwa_targetClass="coneco_widget_adv_area";
var crwa_apikey="b9b8b855c554442c60514b07eadab62b";
var crwa_url_top = "http://api.coneco.net/";
//var crwa_url_top = "http://apollo10:3000/";
var crwa_cateApiUrl = crwa_url_top + "cws/v1/SearchCategories_json";
var crwa_img_dir = "http://www.coneco.net/widget/image/";
var crwa_category_anchor = "crwa_category_anchor";
var crwa_items_list = "crwa_items_list";
var crwa_move_next = "crwa_move_next";
var crwa_move_prev = "crwa_move_prev";
var crwa_move_first = "crwa_move_first";
var crwa_move_last = "crwa_move_last";
var crwa_current_page = "crwa_current_page";
var crwa_max_page = "crwa_max_page";
var crwa_WidgetObject = new Object;
var crwa_disabled = "crwa_disabled";

function crwa_requestJsonpAPI(url,crwa,id,callback,boxId,cond){

	if(document.getElementById(id)){
		crwa.showLoading();
		eval("crwa."+callback+"(crwa."+callback+"Json['j"+cond+"'])");
		return;
	}

	url += "&callback=crwa_WidgetObject['"+boxId+"']."+callback;
//prompt("",url);
//console.log(url);
	var objScript = document.createElement("script");
	objScript.setAttribute("type", "text/javascript");
	objScript.setAttribute("charset", "utf-8");
	objScript.setAttribute("src", url);
	objScript.setAttribute("id", id+"_"+boxId);
	objScript.setAttribute("alt", "json-in-script");
	document.getElementsByTagName("head").item(0).appendChild(objScript);
}

function crwa_ComRequest(hash,boxId){
	this.apiUrl = crwa_url_top + "cws/v1/SearchProducts_json";
	this.priceListUrl = "http://www.coneco.net/PriceList.asp";
	this.topUrl = "http://www.coneco.net/";
	this.clubImgDir = "http://www.coneco.net/images/club/";
	this.imgDir = crwa_img_dir;
	this.apiKey = crwa_apikey;
	this.partsWidth = 300;
	this.cate8RankUrl = "http://www.coneco.net/masterranking/";
	this.widgetUrl = "http://www.coneco.net/widget/";

	this.catid = hash["category_id"];
	this.boxId = boxId;
	this.count = 10;
	this.specMaxLen = 5;
	this.nameMaxLen = 0;
	this.flex = hash["flex"] || 0;
	this.focus = 0;
	this.descMaxLen = hash["description_length"] || 50;
	this.loadingImg = hash["loading_img_url"] || this.imgDir + "loading.gif";
	this.tmplSep = '';
	this.selecter = '';
	this.loaded = false;
	this.sort = hash["sort"] || "ranking";
	this.maker = hash["maker"] || "";
	this.brand = hash["brand"] || "";
	this.comId = hash["com_id"] || "";
	this.onlyCategory = (hash["only_category"] == "1");
	this.showAfterFunction = hash["show_after_function"] || false;
	this.showBeforeFunction = hash["show_before_function"] || false;
	this.loadBeforeFunction = hash["load_before_function"] || false;
	if(this.sort){
		this.sort = this.sort.replace("new","comId");
	}
	this.keyword = hash["keyword"] || "";
	this.note_keyword = hash["note"] || "";
	this.imageFlg = hash["image"] || "0";
	this.orFlg = hash["or_search"] || hash["orFlag"] || "0";

	this.showCategoryListJson = new Object;
	this.showItemsJson = new Object;
	this.showItemsHtml = new Object;
	this.ConditionPageCount = new Object;
	this.matchClass = new Object;
	this.matchId = new Object;
	this.CurrentPage = 1;

	if(String(hash["count"]).replace(/\d/g,"")=="" && hash["count"] > 0){
		this.count = hash["count"];
		if(this.count > 20){
			this.count = 20;
		}
	}

	this.tmplLoading = "<div class='crwa_loading'><img src='"+this.loadingImg+"'></div>";

	var tmplItem = "<div class='crwa_item_body' style='margin:0;width:100%;'>";
	tmplItem += "	<p class='crwa_item_title' style='margin:4px 0px 0px ;font-size:12px;'>";
	tmplItem += "	<span title='全商品中 %%rank%%位'>%%category_rank%%</span>&nbsp;<a href='%%url%%' target='_blank' title='%%com_name%%'><span style='color:blue;'>%%com_name%%</span></a><span style='font-size:11px;'> / %%maker%%</span>";
	tmplItem += "	</p>\n";
	tmplItem += "	<div style='margin:4px 4px 0;'>";
	tmplItem += "		<a href='%%url%%' target='_blank' title='%%com_name%%'><img src='%%s_img_url%%' style='margin:0 0.5em 0 2px;%%img_style%%' align='left' border='0'></a>";
	tmplItem += "		<p style='margin:0;'><a href='%%url%%' target='_blank' style='text-decoration:none;color:#F00;'><span style='font-size:10px;'>最安:</span><span style='font-size:10px;'>&yen;%%lowest_price%%</span></a></p>";
	tmplItem += "		<p style='margin:0;font-size:10px;color:#444;'>%%description%%</p>";
	tmplItem += "	</div>";
	tmplItem += "	<p style='clear:left;margin:1px 0 0px 4px;font-size:9px;'>%%url%%</p>";
	tmplItem += "	<p style='clear:left;margin:1px 0 0px 4px;font-size:12px;'><a href='%%review_url%%' target='_blank' style='%%rating_style%%'>評価:<img src='" + this.clubImgDir + "star_s%%rating%%.gif' alt='満足度：%%rating%%' border='0' height='12' />(%%review_count%%件)</a></p>";
	tmplItem += "</div>\n";

	var innerTmplArea = document.getElementById(this.boxId);
//console.log(innerTmplArea.innerHTML);

	var innerTmplItem = this.getByClass(crwa_items_list,innerTmplArea);
//console.log(innerTmplItem.innerHTML);

	if(innerTmplItem){
//		this.tmplItem = innerTmplItem.innerHTML.replace(/%%box_id%%/,this.boxId);
		this.tmplItem = innerTmplItem.innerHTML;
		innerTmplItem.innerHTML = "";
	} else {
		this.tmplItem = tmplItem;
	}
//alert(this.tmplItem);
	this.copyright = "";

	var header = '<div class="crwa_header">';
	header += '</div>';
	this.header = header;

	var tmpl = "";

	tmpl += '<div style="width:' + this.partsWidth + 'px; padding-top:4px; text-align:center;">';
	tmpl += '%%category_selecter%%';
	tmpl += '</div>\n';

	tmpl += '%%error%%\n';

	tmpl += '<div style="width:' + this.partsWidth + 'px; padding-top:4px; text-align:center;overflow-x:hidden">\n';
	tmpl += '<div style="width:90%;text-align:left;margin-left:auto;margin-right:auto;padding:3px 0;" id="'+this.boxId+'_items">\n';
	tmpl += '<div id="'+this.boxId+'_list" class="'+crwa_items_list+'">\n';
	tmpl += '</div>\n';
	tmpl += '</div>\n';
	tmpl += '</div>\n';


	this.tmplAll = this.header;
//	this.tmplAll += (innerTmplArea.innerHTML.replace(/\s/g,"").length>0)?innerTmplArea.innerHTML.replace(/%%box_id%%/g,this.boxId).replace(/%%category_selecter%%/,""):tmpl;
	this.tmplAll += (innerTmplArea.innerHTML.replace(/\s/g,"").length>0)?innerTmplArea.innerHTML:tmpl;
	this.tmplAll += this.copyright;

	var err = "";
	err += '<div class="error">';
	err += '表示する情報が見つかりません。';
	err += '</div>\n';

	this.tmplErr = err;
}

crwa_ComRequest.prototype.resetMatchObject = function(){
	this.matchClass = new Object;
	this.matchId = new Object;
}
crwa_ComRequest.prototype.showLoading = function(){
	this.itemsDiv = this.getByClass(crwa_items_list);
//console.log(this.itemsDiv);
	if(this.itemsDiv){
//alert(this.itemsDiv.nodeType);
//alert(this.tmplLoading);
		var strHtml = this.tmplLoading;
		this.itemsDiv.innerHTML = strHtml;
//console.log(this.itemsDiv.innerHTML);
	}
	if(this.selecter){
		this.selecter.disabled = true;
	}
}

crwa_ComRequest.prototype.getNewId = function(){
	return "crwa_rndid"+Math.floor(Math.random() * 99999);
}
crwa_ComRequest.prototype.getById = function(id,parNode){
	var node = parNode || document.getElementById(this.boxId);
	if(!node.id || String(node.id) == "undefined"){
		node.id = this.getNewId();
	}
	if(this.matchId[id+"_in_#"+node.id]){
		return this.matchId["#"+id+"_in_#"+node.id];
	}
	for(var n=node.firstChild;n;n=n.nextSibling){
		if(n.nodeType == 1){
			if(n.id == id){
				this.matchId["#"+id+"_in_#"+node.id] = n;
				return n;
			}
			if(n.childNodes.length>0){
				var cn = this.getById(id,n);
				if(cn){
					this.matchId["#"+id+"_in_#"+n.id] = cn;
					return cn;
				}
			}
		}
	}
}
crwa_ComRequest.prototype.getByClass = function(cls,parNode){
	var node = parNode || document.getElementById(this.boxId);
	if(!node.id || String(node.id) == "undefined"){
		node.id = this.getNewId();
	}
	if(this.matchClass[cls+"_in_#"+node.id]){
		return this.matchClass[cls+"_in_#"+node.id];
	}
	for(var n=node.firstChild;n;n=n.nextSibling){
		if(n.nodeType == 1){
			if(String(" "+n.className+" ").indexOf(" "+cls+" ") >= 0){
				this.matchClass[cls+"_in_#"+node.id] = n;
					if(!n.id || String(n.id) == "undefined"){
						n.id = this.getNewId();
					}
					return n;
			}
			if(n.childNodes.length>0){
				var cn = this.getByClass(cls,n);
				if(cn){
					if(!n.id || String(n.id) == "undefined"){
						n.id = "crwa_rndid"+Math.floor(Math.random() * 99999);
					}
					this.matchClass[cls+"_in_#"+n.id] = cn;
//console.log(cls+"_in_#"+n.id+"="+this.matchClass[cls+"_in_#"+n.id]);
					if(!cn.id || String(cn.id) == "undefined"){
						cn.id = this.getNewId();
					}
					return cn;
				}
			}
		}
	}
}


crwa_ComRequest.prototype.setKeyword = function(str){
	this.keyword = str;
	return this;
}
crwa_ComRequest.prototype.setBrand = function(str){
	this.brand = str;
	return this;
}
crwa_ComRequest.prototype.setMaker = function(str){
	this.maker = str;
	return this;
}
crwa_ComRequest.prototype.setNote = function(str){
	this.note_keyword = str;
	return this;
}
crwa_ComRequest.prototype.setComId = function(str){
	this.comId = str;
	return this;
}
crwa_ComRequest.prototype.setCategoryId = function(str){
	this.CurrentCategoryId = str;
	return this;
}
crwa_ComRequest.prototype.getKeyword = function(){
	return this.keyword;
}
crwa_ComRequest.prototype.getBrand = function(){
	return this.brand;
}
crwa_ComRequest.prototype.getMaker = function(){
	return this.maker;
}
crwa_ComRequest.prototype.getNote = function(){
	return this.note_keyword;
}
crwa_ComRequest.prototype.getComId = function(){
	return this.comId;
}
crwa_ComRequest.prototype.getCategoryId = function(){
	return this.CurrentCategoryId || this.catid;
}

crwa_ComRequest.prototype.moveNote = function(str){
	this.resetMatchObject();
	this.note_keyword = str;
	this.getItems(this.CurrentCategoryId,1);
}
crwa_ComRequest.prototype.moveCategory = function(catId){
	this.getItems(catId,1);
}
crwa_ComRequest.prototype.moveFirst = function(){
	this.getItems(this.CurrentCategoryId,1);
}
crwa_ComRequest.prototype.moveLast = function(){
//	this.getItems(this.CurrentCategoryId,this.ConditionPageCount["j"+this.boxId+"_"+this.CurrentCategoryId]);
	this.getItems(this.CurrentCategoryId,this.ConditionPageCount[this.getCondKey(this.CurrentCategoryId)]);
}
crwa_ComRequest.prototype.moveNext = function(){
	this.getItems(this.CurrentCategoryId,1+parseInt(this.CurrentPage));
}
crwa_ComRequest.prototype.movePrev = function(){
	if(this.CurrentPage > 1){
		this.getItems(this.CurrentCategoryId,parseInt(this.CurrentPage)-1);
	} else {
//		alert("先頭ページです。");
	}
}
crwa_ComRequest.prototype.getCacheKey = function(incatId,inpg,inkeyword,inbrand,inmaker,innote,incomid){
	var catid = incatId || this.CurrentCategoryId || 0;
	var pg = inpg || 1;
	var comid = incomid || this.comId || "";
	var keyword = inkeyword || this.keyword || "";
	var brand = inbrand || this.brand || "";
	var maker = inmaker || this.maker || "";
	var note = innote || this.note_keyword || "";
	return "j"+this.boxId+"_"+catid+"_"+comid+"_"+keyword+"_"+brand+"_"+maker+"_"+note+"_"+pg;
}
crwa_ComRequest.prototype.getCondKey = function(incatId,inkeyword,inbrand,inmaker,innote,incomid){
	var catid = incatId || this.CurrentCategoryId || 0;
	var comid = incomid || this.comId || "";
	var keyword = inkeyword || this.keyword || "";
	var brand = inbrand || this.brand || "";
	var maker = inmaker || this.maker || "";
	var note = innote || this.note_keyword || "";
	return "cond"+this.boxId+"_"+catid+"_"+comid+"_"+keyword+"_"+brand+"_"+maker+"_"+note;
}
crwa_ComRequest.prototype.getItems = function(catId,pg){
	var page = 1;
	if(pg){
		page = pg;
	}
	if(page < 1){
		page = 1;
	}

	if(this.ConditionPageCount[this.getCondKey(catId)] < page){
//		alert("ページの終端です。");
		return;
	}
//console.log("cat="+catId);
	if(!this.loadBefore(catId)){
		return;
	}
	if(this.onlyCategory){
		return;
	}
	this.showLoading();
	if(this.categoryAnchor){
		this.categoryAnchor.style.display="none";
	}

	var cacheKey = this.getCacheKey(catId,pg);
	if(this.showItemsJson[cacheKey]){
		this.showItems(this.showItemsJson[cacheKey]);
		return;
	}
	var url = this.apiUrl;
	if(String(catId) != "undefined"){
		url+="?categoryId="+catId.replace(/[^0-9]/g,"");
	} else {
		url+="?categoryId=0";
	}
	url += "&apikey="+this.apiKey+"&sort="+this.sort+"&available=1&count="+this.count+"&page="+page;
	if(this.keyword != ""){
		url+="&keyword="+encodeURIComponent(this.keyword);
	}
	if(this.comId != ""){
		url+="&comId="+this.comId.replace(/[^0-9]/g,"");
	}
	if(this.note_keyword != ""){
		url+="&note="+encodeURIComponent(this.note_keyword);
	}
	if(this.maker != ""){
		url+="&maker="+encodeURIComponent(this.maker);
	}
	if(this.brand != ""){
		url+="&brand="+encodeURIComponent(this.brand);
	}
	if(this.imageFlg != ""){
		url+="&imageFlag="+this.imageFlg;
	}
	if(this.orFlg != ""){
		url+="&orFlag="+this.orFlg;
	}
	crwa_requestJsonpAPI(url,this,"crwajs_catid_"+catId,"showItems",this.boxId,catId);
}
crwa_ComRequest.prototype.setEvents = function(){
	var mp = this.getByClass(crwa_move_prev);
	if(mp){
		var self = this;
		mp.onclick = function(){
			self.movePrev()
		}
	}
	var mp = this.getByClass(crwa_move_next);
	if(mp){
		var self = this;
		mp.onclick = function(){
			self.moveNext()
		}
	}
	var mp = this.getByClass(crwa_move_first);
	if(mp){
		var self = this;
		mp.onclick = function(){
			self.moveFirst()
		}
	}
	var mp = this.getByClass(crwa_move_last);
	if(mp){
		var self = this;
		mp.onclick = function(){
			self.moveLast()
		}
	}
}
crwa_ComRequest.prototype.showCategoryList = function(json){
	var box = document.getElementById(this.boxId);
	if(!box){
		return;
	}
	var err = false;
	if(json && json.Header.Count > 0 && json.Current){
		this.tmplAll = this.tmplAll.replace(/%%(crwa_)?category_name%%/g,json.Current.Name);
		this.showCategoryListJson["jc"+json.Current.Id] = json;
	} else if(json && json.Header.Status != "Success"){
		this.tmplAll = this.header + this.tmplErr + this.copyright;
		err = true;
	} else if(json && json.Category.length <= 0){
		this.tmplAll = this.header + this.tmplErr + this.copyright;
		err = true;
	} else if (!json){
	} else {
		this.tmplAll = this.header + this.tmplErr + this.copyright;
		err = true;
	}

	box.innerHTML = this.tmplAll;

	this.setEvents();

	this.resetMatchObject();

	var area = document.getElementById(this.boxId)
	area.style.display = "block";

	this.itemsDiv = this.getByClass(crwa_items_list);

	if(err){
		return;
	}


	var seles = document.getElementById(this.boxId).getElementsByTagName("SELECT");
	if(seles.length == 0){
		this.getItems(json.Current.Id);
		return;
	}

	var sele = false;
	for(var i=0;i<seles.length;i++){
		if(seles[i].className.indexOf("crwa_category_selecter")>=0){
			sele = seles[i];
			break;
		}
	}
	if(!sele){
		return;
	}
	if(!this.loaded){
		this.loaded = true;
	}
	this.selecter = sele;

	this.getItems(json.Current.Id);

	sele.id = "crwa_category_selecter_" + this.boxId;
	var len = json.Category.length;
	sele.style.display = "inline";
	sele.options.length = len+1;
	sele.onchange = function(){
		var seld=this.options[this.selectedIndex];
		var val=seld.value;
		var boxId = this.id.replace("crwa_category_selecter_","");
		if(crwa_WidgetObject[boxId].flex == "1" && val.length<7){
			crwa_WidgetObject[boxId].loadCategory(this.boxId,val);
//			crwa_WidgetObject[boxId].moveCategory(val);
		} else {
//			crwa_WidgetObject[boxId].getItems(val);
			crwa_WidgetObject[boxId].moveCategory(val);
		}
	}

	if (!json){
		return;
	}

	var opt = sele.options[0];
	opt.value = "0";
	opt.innerHTML = "coneco.net 全体";
	var intSt = 1;
	if(json.Current.Id == "0"){
		opt.selected = true;
	} else {
		intSt = 1+parseInt(json.Current.Level);
		sele.options.length = len+intSt;
		if(json.Parent && json.Parent.Level != "0"){
			var opt = sele.options[json.Parent.Level];
			opt.value = json.Parent.Id;
			opt.innerHTML = "└ " + json.Parent.Name + " 全体";
		}
		var opt = sele.options[json.Current.Level];
		opt.value = json.Current.Id;
		opt.innerHTML = this.getSpace(json.Current.Level-1)+"└ " + json.Current.Name + " 全体";
		opt.selected = true;
	}
	for(var i=0;i<len;i++){
		var opt = sele.options[i+intSt];
		opt.value = json.Category[i].Id;
		opt.innerHTML = this.getSpace(json.Current.Level) + (((len-1)==i)?"└":"├")+" "+json.Category[i].Name;
	}

}

crwa_ComRequest.prototype.getSpace = function(n){
	var ret = '';
	for(var i=0;i<n;i++){
		ret+='　';
	}
	return ret;
}
crwa_ComRequest.prototype.showItems = function(json){
	if(!json){
		
	}
	document.getElementById(this.boxId).style.display = "block";


	if(!this.showBefore(json)){
		return;
	}

	this.itemsDiv = this.itemsDiv || document.getElementById(this.boxId+'_list') || this.getByClass(crwa_items_list);
	var listBox = this.itemsDiv;
	var strReadMoreUrl = "";
//x=json;
	if(json.Header && json.Header.Condition.Category.Id.length >= 7){
		strReadMoreUrl = this.cate8RankUrl + ((json.Header.Condition.Category.Id.length==7)?'0':'')+json.Header.Condition.Category.Id + ".html";
	}

	this.CurrentCategoryId = json.Header.Condition.Category.Id;
	this.CurrentPage = json.Header.Page.Current;

	this.ConditionPageCount[this.getCondKey(this.CurrentCategoryId)] = json.Header.Page.PageCount;

	this.showItemsJson[this.getCacheKey(this.CurrentCategoryId,this.CurrentPage)] = json;

	var cacheKey = this.getCacheKey(this.CurrentCategoryId,this.CurrentPage);
	if(this.showItemsHtml[cacheKey]){
		listBox.innerHTML = this.showItemsHtml[cacheKey];
	} else {

		tmplItem = this.tmplItem;
//alert(tmplItem);
		var arrItems = new Array();
		var len = json.Item.length;
		for(var i=0;i<len;i++){
			arrItems.push(this.showItem(json.Item[i],tmplItem,i));
		}
		this.showItemsHtml[cacheKey] = arrItems.join(this.tmplSep);
		listBox.innerHTML = this.showItemsHtml[cacheKey];
	}

	var ancs = document.getElementById(this.boxId).getElementsByTagName("A");
	var anclen = ancs.length;
	for(var i=0;i<anclen;i++){
		if(ancs[i].className.indexOf(crwa_category_anchor)>=0){
			this.categoryAnchor = ancs[i];
			ancs[i].href = json.Header.Condition.Category.Url;
		}
	}
	if(this.categoryAnchor){
		this.categoryAnchor.style.display="";
	}
	if(this.selecter){
		this.selecter.disabled = false;
		if(this.focus){
			this.selecter.focus();
		} else {
			this.focus = 1;
		}
	}
	
//console.log("show="+listBox.innerHTML);
//console.log(listBox);
	this.showAfter(json);
}
crwa_ComRequest.prototype.loadBefore = function(catId){
	var mp = this.getByClass(crwa_move_prev);
	if(mp){
		mp.style.visibility = "hidden";
	}
	var mp = this.getByClass(crwa_move_next);
	if(mp){
		mp.style.visibility = "hidden";
	}
	var mp = this.getByClass(crwa_move_first);
	if(mp){
		mp.style.visibility = "hidden";
	}
	var mp = this.getByClass(crwa_move_last);
	if(mp){
		mp.style.visibility = "hidden";
	}
	var mp = this.getByClass(crwa_current_page);
	if(mp){
		mp.style.visibility = "hidden";
	}
	var mp = this.getByClass(crwa_max_page);
	if(mp){
		mp.style.visibility = "hidden";
	}
	if(this.loadBeforeFunction){
		return eval(this.loadBeforeFunction)(catId);
	}
	return true;
}
crwa_ComRequest.prototype.showBefore = function(json){
	if(this.showBeforeFunction){
		return eval(this.showBeforeFunction)(json);
	}
	return true;
}
crwa_ComRequest.prototype.showAfter = function(json){
	var pgcur = json.Header.Page.Current;
	var pgcnt = json.Header.Page.PageCount;
	var mp = this.getByClass(crwa_move_prev);
	if(mp){
		mp.style.visibility = "";
		mp.className = mp.className.replace(crwa_disabled,"");
	}
	var mn = this.getByClass(crwa_move_next);
	if(mn){
		mn.style.visibility = "";
		mn.className = mn.className.replace(crwa_disabled,"");
	}
	var mf = this.getByClass(crwa_move_first);
	if(mf){
		mf.style.visibility = "";
		mf.className = mf.className.replace(crwa_disabled,"");
	}
	var ml = this.getByClass(crwa_move_last);
	if(ml){
		ml.style.visibility = "";
		ml.className = ml.className.replace(crwa_disabled,"");
	}
	var mc = this.getByClass(crwa_current_page);
	if(mc){
		mc.style.visibility = "";
		mc.innerHTML = pgcur;
	}
	var mm = this.getByClass(crwa_max_page);
	if(mm){
		mm.style.visibility = "";
		mm.innerHTML = pgcnt;
	}
	if(parseInt(pgcur) >= parseInt(pgcnt)){
		if(mn){
			mn.className += " "+crwa_disabled;
		}
		if(ml){
			ml.className += " "+crwa_disabled;
		}
	}
	if(parseInt(pgcur) <= 1){
		if(mp){
			mp.className += " "+crwa_disabled;
		}
		if(mf){
			mf.className += " "+crwa_disabled;
		}
	}

	if(this.showAfterFunction){
		return eval(this.showAfterFunction)(json);
	}
	return true;
}
crwa_ComRequest.prototype.showItem = function(item,tmplItem,idx){
	var strItem = tmplItem;
	if(item.Category){
		strItem = strItem.replace(/%%(crwa_)?category_name%%/g,item.Category.Name);
		strItem = strItem.replace(/%%(crwa_)?category_id%%/g,item.Category.Id);
	}
	strItem = strItem.replace(/%%(crwa_)?com_id%%/,item.ComId);
	var strName = item.Name;
	if(this.nameMaxLen > 0 && strName.length>this.nameMaxLen){
		strName = strName.substr(0,this.nameMaxLen)+"...";
	}
	var strImgUrl = item.ImageUrl;
	var strImgStyle = "crwa_noimage crwa_no_image";
	if(strImgUrl.indexOf("noimg") == -1){
		strImgStyle = "";
	}
	strItem = strItem.replace(/%%(crwa_)?s_img_url%%/,item.SmallImageUrl);
	strItem = strItem.replace(/%%(crwa_)?no_image%%/,strImgStyle);
	var strPrice = String(item.LowestPrice);
	var strAVGPrice = String(item.AveragePrice);
	if(item.LowestPrice > 0){
		while(strPrice != (strPrice = strPrice.replace(/^(-?\d+)(\d{3})/, "$1,$2")));
	}
	if(item.AveragePrice > 0){
		while(strAVGPrice != (strAVGPrice = strAVGPrice.replace(/^(-?\d+)(\d{3})/, "$1,$2")));
	}
	var strRevUrl = item.Review.ReviewListUrl || "http://www.coneco.net/Reviewlist.asp?COM_ID=" + item.ComId;
	if(parseInt(item.Review.ReviewCount) > 0 && String(item.Review.OverallRating) != "undefined"){
		strItem = strItem.replace(/%%(crwa_)?rating%%/g,item.Review.OverallRating);
		strItem = strItem.replace(/(title\=)?(['"])?%%(crwa_)?(s_)?rating_img%%['"]?/g,this.clubImgDir + "star_s" + item.Review.OverallRating + ".gif");
		strItem = strItem.replace(/(title\=)?(['"])?%%(crwa_)?l_rating_img%%['"]?/g,this.clubImgDir + "star" + item.Review.OverallRating + ".gif");
		strItem = strItem.replace(/(title\=)?(['"])?%%(crwa_)?rating_img_l%%['"]?/g,this.clubImgDir + "star" + item.Review.OverallRating + ".gif");
		strItem = strItem.replace(/(title\=)?(['"])?%%(crwa_)?(s_)?rating_(img_)?attr%%['"]?/g," src='"+this.clubImgDir + "star_s" + item.Review.OverallRating + ".gif'");
		strItem = strItem.replace(/(title\=)?(['"])?%%(crwa_)?l_rating_(img_)?attr%%['"]?/g," src='"+this.clubImgDir + "star" + item.Review.OverallRating + ".gif'");
		strItem = strItem.replace(/(title\=)?(['"])?%%(crwa_)?rating_(img_)?l_attr%%['"]?/g," src='"+this.clubImgDir + "star" + item.Review.OverallRating + ".gif'");
//		strItem = strItem.replace(/%%(crwa_)?review_url%%/g,item.Review.ReviewListUrl);
		strItem = strItem.replace(/%%(crwa_)?review_count%%/g,item.Review.ReviewCount);
	} else {
		strItem = strItem.replace(/%%(crwa_)?no_rating%%/g," crwa_norating crwa_no_rating ");
		strItem = strItem.replace(/%%(crwa_)?rating%%/g,"");
		strItem = strItem.replace(/(title\=)?(['"])?%%(crwa_)?(s_)?rating_img%%['"]?/g,this.clubImgDir + "star_s.gif");
		strItem = strItem.replace(/(title\=)?(['"])?%%(crwa_)?l_rating_img%%['"]?/g,this.clubImgDir + "star0.gif");
		strItem = strItem.replace(/(title\=)?(['"])?%%(crwa_)?(s_)?rating_(img_)?attr%%['"]?/g," src='"+this.clubImgDir + "star_s.gif'");
		strItem = strItem.replace(/(title\=)?(['"])?%%(crwa_)?l_rating_(img_)?attr%%['"]?/g," src='"+this.clubImgDir + "star0.gif'");
	}
	strItem = strItem.replace(/%%(crwa_)?review_url%%/g,strRevUrl);

	var strDesc = item.Description;
	if(this.descMaxLen > 0 && strDesc.length>this.descMaxLen){
		strDesc = item.Description.substr(0,this.descMaxLen) + "...";
	}
	var strLatestReview = "";
	var strLatestReviewTitle = "";
	var strLatestReviewUrl = "";
	var strLatestReviewCreated = "";
	var strLatestReviewPros = "";
	var strLatestReviewCons = "";
	var strLatestReviewRating = "";
	var strLatestReviewProsList = "";
	var strLatestReviewConsList = "";
	if(item.Review.ReviewCount>0 && item.UserReview.length>0){
		strLatestReviewTitle = item.UserReview[0].Summary;
		strLatestReview = item.UserReview[0].Description;
		strLatestReviewUrl = item.UserReview[0].Url;
		strLatestReviewCreated = item.UserReview[0].Created;
		strLatestReviewRating = item.UserReview[0].OverallRating;
		strLatestReviewPros = item.UserReview[0].Pros;
		strLatestReviewCons = item.UserReview[0].Cons;
		strLatestReview = strLatestReview.substr(0,this.descMaxLen) + "...";
		var len = item.UserReview.length;
		var arrPros = new Array;
		var arrCons = new Array;
		for(var i=0;i<len;i++){
			arrPros.push(item.UserReview[i].Pros);
			arrCons.push(item.UserReview[i].Cons);
		}
		if(arrPros.length>0){
			strLatestReviewProsList = "<ul class='crwa_review_pros'><li>" + arrPros.join("</li><li>") + "</li></ul>";
		}
		if(arrCons.length>0){
			strLatestReviewConsList = "<ul class='crwa_review_cons'><li>" + arrCons.join("</li><li>") + "</li></ul>";
		}
	}

	if(idx<3 && this.CurrentPage==1){
		strItem = strItem.replace(/%%(crwa_)?category_rank%%/g,(idx+1));
		strItem = strItem.replace(/%%(crwa_)?category_rank_img%%/g,"<img src='"+this.imgDir+"prize"+(idx+1)+".gif' style='vertical-align:middle' height='18' width='18' />");
	} else {
		strItem = strItem.replace(/%%(crwa_)?category_rank%%/g,(idx+1+this.count*(this.CurrentPage-1)));
		strItem = strItem.replace(/%%(crwa_)?category_rank_img%%/g,(idx+1+this.count*(this.CurrentPage-1))+".");
	}
	if(item.ReleaseDate){
		strItem = strItem.replace(/%%(crwa_)?release_date_yyyymm%%/g,item.ReleaseDate.substring(0,7));
		strItem = strItem.replace(/%%(crwa_)?release_date%%/g,item.ReleaseDate);
	}
	strItem = strItem.replace(/%%(crwa_)?rank%%/g,item.Ranking);
	strItem = strItem.replace(/%%(crwa_)?url%%/g,item.Url);
	strItem = strItem.replace(/(title\=)?(['"])?%%(crwa_)?s_img_url%%['"]?/g,item.SmallImageUrl);
	strItem = strItem.replace(/(title\=)?(['"])?%%(crwa_)?img_url%%['"]?/g,item.ImageUrl);
	strItem = strItem.replace(/(title\=)?(['"])?%%(crwa_)?s_img_attr%%['"]?/g," src='"+item.SmallImageUrl+"'");
	strItem = strItem.replace(/(title\=)?(['"])?%%(crwa_)?img_attr%%['"]?/g," src='"+item.ImageUrl+"'");
	strItem = strItem.replace(/=%%(crwa_)?com_name%%/g,"='"+item.Name.replace(/ /g,"&nbsp;")+"'");
	strItem = strItem.replace(/%%(crwa_)?com_name%%/g,item.Name);
//	strItem = strItem.replace(/%%(crwa_)?com_name_sp%%/g,item.Name.replace(/ /g,"&nbsp;"));
	strItem = strItem.replace(/=%%(crwa_)?description%%/g,"='"+strDesc.replace(/ /g,"&nbsp;")+"'");
	strItem = strItem.replace(/%%(crwa_)?description%%/g,strDesc);
	strItem = strItem.replace(/%%(crwa_)?description_full%%/g,item.Description.replace(/ /g,"&nbsp;"));
	strItem = strItem.replace(/%%(crwa_)?latest_review%%/g,strLatestReview);
	strItem = strItem.replace(/%%(crwa_)?latest_review_title%%/g,strLatestReviewTitle);
	strItem = strItem.replace(/%%(crwa_)?latest_review_url%%/g,strLatestReviewUrl);
	strItem = strItem.replace(/%%(crwa_)?latest_review_time%%/g,strLatestReviewCreated);
	strItem = strItem.replace(/%%(crwa_)?latest_review_rating%%/g,strLatestReviewRating);

	strItem = strItem.replace(/%%(crwa_)?rating%%/g,item.Review.OverallRating);
	strItem = strItem.replace(/(title\=)?(['"])?%%(crwa_)?latest_review_rating_img%%['"]?/g,this.clubImgDir + "star_s" + strLatestReviewRating + ".gif");
	strItem = strItem.replace(/(title\=)?(['"])?%%(crwa_)?latest_review_rating_img_l%%['"]?/g,this.clubImgDir + "star" + strLatestReviewRating + ".gif");
	strItem = strItem.replace(/(title\=)?(['"])?%%(crwa_)?latest_review_rating_(img_)?attr%%['"]?/g," src='"+this.clubImgDir + "star_s" + strLatestReviewRating + ".gif'");
	strItem = strItem.replace(/(title\=)?(['"])?%%(crwa_)?latest_review_rating_(img_)?l_attr%%['"]?/g," src='"+this.clubImgDir + "star" + strLatestReviewRating + ".gif'");

	strItem = strItem.replace(/%%(crwa_)?latest_review_pros%%/g,strLatestReviewPros);
	strItem = strItem.replace(/%%(crwa_)?latest_review_cons%%/g,strLatestReviewCons);
	strItem = strItem.replace(/%%(crwa_)?latest_review_pros_all%%/g,strLatestReviewProsList);
	strItem = strItem.replace(/%%(crwa_)?latest_review_cons_all%%/g,strLatestReviewConsList);
	strItem = strItem.replace(/%%(crwa_)?lowest_price%%/g,strPrice);
	strItem = strItem.replace(/%%(crwa_)?avg_price%%/g,strAVGPrice);
	strItem = strItem.replace(/%%(crwa_)?brand%%/g,item.Brand);
	strItem = strItem.replace(/%%(crwa_)?maker_full%%/g,item.Manufacturer);
	var strMaker = item.Manufacturer;
	if(strMaker.indexOf("/") >= 0){
		strMaker = strMaker.split("/")[0];
	}
	strItem = strItem.replace(/%%(crwa_)?maker%%/g,strMaker);

	var speclen = item.Specifications.length;
	for(var i=0;i<speclen;i++){
		var regTitle	= new RegExp("%%(crwa_)?spec_title"+(i+1)+"%%", "g");
		var regSpec		= new RegExp("%%(crwa_)?spec"+(i+1)+"%%", "g");
		var regClass	= new RegExp("%%(crwa_)?spec_class"+(i+1)+"%%", "g");
		strItem = strItem.replace(regTitle	,item.Specifications[i].Title);
		strItem = strItem.replace(regSpec	,item.Specifications[i].Spec);
		strItem = strItem.replace(regClass	,"");
	}
	for(var i=0;i<this.specMaxLen;i++){
		var regClear = new RegExp("%%(crwa_)?spec_class"+(i+1)+"%%", "g");
		strItem = strItem.replace(regClear,"crwa_no_spec");
	}

	strItem = strItem.replace(/%%[a-z0-9_]+%%/g,"");

	return strItem;
}
crwa_ComRequest.prototype.loadCategory = function(areaId,catId){
	var url = crwa_cateApiUrl;
	url += "?categoryId="+catId;
	url += "&apikey="+crwa_apikey;
	var boxId = this.boxId || areaId;
	var jsId = "crwjs_parcatid_"+catId;
	crwa_requestJsonpAPI(url,this,jsId,"showCategoryList",boxId,catId);
}
function conecoRankingWidget_loadArea(area,idx){
	var tit = area.title;
	area.title = "";

	var hash = new Object;
	var arrParams = tit.split(";");
	for(var i=0;i<arrParams.length;i++){
		var arrParam = arrParams[i].split("=");
		if(arrParam.length==2){
			hash[arrParam[0]] = arrParam[1];
		}
	}
	if(String(hash["category_id"]) == "undefined"){
		hash["category_id"] = "0";
	}
	area.title = "";
	if(!area.id || String(area.id) == "undefined"){
		area.id = "crwa_"+hash["category_id"]+"_"+idx;
	}
	crwa_WidgetObject[area.id] = new crwa_ComRequest(hash,area.id);
	if(hash["category_id"].length >= 7){
		crwa_WidgetObject[area.id].setEvents();
		crwa_WidgetObject[area.id].getItems(hash["category_id"]);
	} else {
		crwa_WidgetObject[area.id].loadCategory(area.id,hash["category_id"]);
	}
}

(function(){
	var divs = document.getElementsByTagName("DIV");
	var len = divs.length;
	var idx = 0;
	for(var i=0;i<len;i++){
		if(divs[i] && divs[i].className.indexOf(crwa_targetClass) >= 0){
			conecoRankingWidget_loadArea(divs[i],++idx);
		}
	}
})();
