var cc_targetId="cc_review";
var cc_targetClass="cc_review";

function cc_requestConecoClubReviewAPI(url,id){
	var objScript = document.createElement("script");
	objScript.setAttribute("type", "text/javascript");
	objScript.setAttribute("charset", "utf-8");
	objScript.setAttribute("src", url);
	objScript.setAttribute("id", id);
	document.getElementsByTagName("head").item(0).appendChild(objScript);
}

function cc_ReviewRequest(hash,boxId){
	this.apiUrl = "http://api.coneco.net/cws/v1/SearchReviews_json";
	this.catUrl = "http://www.coneco.net/ReviewList/cate/";
	this.priceListUrl = "http://www.coneco.net/PriceList.asp";
	this.clubUrl = "http://club.coneco.net/";
	this.imgDir = "http://club.coneco.net/img/blogparts/";
	this.apiKey = "c3b31b86cf0b6b574a9018925ca329a8";
	this.readMoreLabel = "read more";
	this.partsWidth = "160px";

	this.uid = hash["user_id"];
	this.catid = hash["category_id"] || 0;
	this.boxId = boxId;
	this.count = hash["item_count"] || 2;
	if(this.count > 20){
		this.count = 20;
	}
	this.nameMaxLen = 50;
	this.tmplSep = '<hr align="center" width="90%" style="margin:4px auto;" noshade color="#CCCCCC">';
	var tmpl = "";
	tmpl += '<div style="width:' + this.partsWidth + '; height:50px;">';
	tmpl += '<a href="' + this.clubUrl + '" target="_blank"><img src="' + this.imgDir + 'bp-head01.png" border="0" alt="conecoクラブ" style="height:50px;width:' + this.partsWidth + '" /></a>';
	tmpl += '</div>';
	if(String(this.uid) != "undefined"){
		tmpl += '<div style="width:' + this.partsWidth + '; height:19px;%head02_style%">';
		tmpl += '<a href="%read_more_url%" target="_blank"><img src="' + this.imgDir + 'bp-head02.png" border="0" alt="最近投稿したレビュー" style="height:19px;width:' + this.partsWidth + '" /></a>';
		tmpl += '</div>\n';
	} else {
		tmpl += '<div style="width:' + this.partsWidth + '; background:url(' + this.imgDir + 'bp-intro.png) 0 0 no-repeat;background-color:#78BA1D;text-align:center;">';
		tmpl += '<p style="width:94%;margin:0px auto;padding:2px 0">';
		tmpl += '<a href="%read_more_url%" target="_blank" style="display:block;font-size:10px;color:#666600;font-weight:bold;text-decoration:none;">%category_name%<br>最新レビュー</a>';
		tmpl += '</p>';
		tmpl += '</div>\n';
	}
	tmpl += '<div style="width:' + this.partsWidth + '; background-image:url(' + this.imgDir + 'bp-middle.png); padding-top:4px; text-align:center;">\n';
	tmpl += '<div style="width:90%;text-align:left;margin-left:auto;margin-right:auto;">\n';
	tmpl += '%items%\n';
	tmpl += '%read_more%\n';
	tmpl += '</div>\n';
	tmpl += '</div>\n';
	tmpl += '%error%\n';
	tmpl += '<div style="width:' + this.partsWidth + '; height:40px;"><a href="http://apidoc.coneco.net/" target="_blank"><img src="' + this.imgDir + 'bp-foot.png" border="0" alt="coneco.net Web Services" style="width:' + this.partsWidth + ';height:40px;" /></a></div>\n';
	tmpl += '</div>\n';
	
	this.tmplAll = tmpl;

	var err = "";
	err += '<div style="width:' + this.partsWidth + '; height:30px;">';
	err += '<img src="' + this.imgDir + 'error.png" border="0" alt="表示する情報が見つかりません。レビューが存在しないか、ブログパーツの設定に誤りがあります。" style="height:30px;width:' + this.partsWidth + '" />';
	err += '</div>\n';

	this.tmplErr = err;
}

cc_ReviewRequest.prototype.getReview = function(){
	var url = this.apiUrl+"?apikey="+this.apiKey+"&sort=date&count="+this.count;
	if(String(this.uid) != "undefined"){
		url+="&userId="+this.uid;
	}
	if(String(this.catid) != "undefined"){
		url+="&categoryId="+this.catid;
	}
	url += "&callback=cc_objReview["+this.uid+"].showReview";
	cc_requestConecoClubReviewAPI(url,"ccjs_uid_"+this.uid);
//	var oRJSON = new cc_JSONscriptRequest(url);
//	oRJSON.buildScriptTag();
//	oRJSON.addScriptTag();
}
cc_ReviewRequest.prototype.showReview = function(json){
x=json;
	var box = document.getElementById(this.boxId);
	if(!box){
		return;
	}
	if(json.Header.Status != "Success" || json.ItemInfo.length <= 0){
		this.tmplAll = this.tmplAll.replace("%items%","");
		this.tmplAll = this.tmplAll.replace("%head02_style%","display:none;");
		this.tmplAll = this.tmplAll.replace("%read_more%","");
		box.innerHTML = this.tmplAll.replace("%error%",this.tmplErr);
		return;
	}
	this.tmplAll = this.tmplAll.replace("%error%",'');
	var review_url = '';
	if(String(this.uid) != "undefined"){
		review_url = json.ItemInfo[0].Review.AuthorUrl+"review/list.aspx";
	} else {
/*
		var catId = String("0"+json.ItemInfo[0].Item.Category.Id);
		catId = catId.substring(catId.length-8,8);
		review_url = this.catUrl + catId +"/";
*/
		review_url = json.Header.Condition.Category.ReviewListUrl;
	}
	if(parseInt(json.Header.Page.Count)>parseInt(this.count)){
		this.tmplAll = this.tmplAll.replace("%read_more%",this.tmplSep+"<p style='text-align:right;font-size:11px;margin:0;'><a href='"+review_url+"' target='_blank'>その他のレビュー("+json.Header.Page.Count+")</a></p>");
	} else {
		this.tmplAll = this.tmplAll.replace("%read_more%","");
	}
	this.tmplAll = this.tmplAll.replace(/%read_more_url%/g,review_url);
	if(json.Header.Condition.Category){
		this.tmplAll = this.tmplAll.replace(/%category_name%/g,json.Header.Condition.Category.Name.replace("root","coneco.net"));
		this.tmplAll = this.tmplAll.replace(/%category_id%/g,json.ItemInfo[0].Item.Category.Id);
	}

	var tmplReview = "<div class='cc_review-item' style='margin:0;'>";
	tmplReview += "	<p class='review_title' style='margin:4px 0px 2px ;font-size:11px;'>";
	tmplReview += "	<a href='" + this.priceListUrl + "?com_id=%com_id%' target='_blank' title='coneco.net'><img src='%s_img_url%' style='margin:0 0.5em 0 2px;%s_img_style%' align='left' border='0'><span style='color:blue;'>%com_name%</span></a>";
	tmplReview += "	</p>\n";
	tmplReview += "	<p style='clear:left;margin:1px 0 1px 4px;color:#FF0000; font-size:11px;%buy_price_style%'>￥%buy_price%で購入</p>";
	tmplReview += "	<p style='clear:left;margin:1px 0 1px 4px;'><a href='%review_url%' target='_blank'><img src='" + this.clubUrl + "img/star%rating%.gif' alt='満足度：%rating%' border='0' /></a></p>";
	tmplReview += "	<p style='margin:4px 4px 0; font-size:10px;'>";
	tmplReview += "		<a href='%review_url%' target='_blank' style='text-decoration:none;color:#333333;'>%description%...</a><a href='%review_url%' target='_blank'>" + this.readMoreLabel + "</a>";
	tmplReview += "	</p>";
	tmplReview += "</div>\n";

	var arrItems = new Array();
x=json;
	for(var i=0;i<json.ItemInfo.length;i++){
		arrItems.push(this.showReviewItem(json.ItemInfo[i],tmplReview));
	}
	box.innerHTML = this.tmplAll.replace("%items%",arrItems.join(this.tmplSep));
}
//cc_ReviewRequest.prototype.showReviewItem = function(item,tmplItem,tmplRat){
cc_ReviewRequest.prototype.showReviewItem = function(item,tmplItem){
	var strItem = tmplItem;
	strItem = strItem.replace(/%review_url%/g,item.Review.Url);
x=item.Item.Category;
	if(item.Item.Category){
		strItem = strItem.replace(/%category_name%/g,item.Item.Category.Name);
		strItem = strItem.replace(/%category_id%/g,item.Item.Category.Id);
	}
	strItem = strItem.replace("%com_id%",item.Item.ComId);
	var strName = item.Item.Name;
	if(strName.length>this.nameMaxLen){
		strName = strName.substr(0,this.nameMaxLen)+"...";
	}
	strItem = strItem.replace("%com_name%",strName);
	var strImgUrl = item.Item.SmallImageUrl;
	var strImgStyle = "display:none;";
	if(strImgUrl.indexOf("noimg") == -1){
		strImgStyle = "display:inline;";
	}
	strItem = strItem.replace("%s_img_url%",strImgUrl);
	strItem = strItem.replace("%s_img_style%",strImgStyle);
	strItem = strItem.replace("%summary%",item.Review.Summary);
	var strPriceStyle = "display:none;";
	var strPrice = String(item.Review.Purchase.Price);
	if(item.Review.Purchase.Price > 0){
		strPriceStyle = "display:block;";
		while(strPrice != (strPrice = strPrice.replace(/^(-?\d+)(\d{3})/, "$1,$2")));
	}
	strItem = strItem.replace(/%buy_price_style%/g,strPriceStyle);
	strItem = strItem.replace(/%buy_price%/g,strPrice);
	strItem = strItem.replace("%description%",item.Review.Description.substr(0,50));
	strItem = strItem.replace(/%rating%/g,item.Review.OverallRating);

	return strItem;
}

var cc_objReview = new Array;
function conecoClubParts_loadArea(area){
	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];
		}
	}
	area.title = "";
	if(!area.id || String(area.id) == "undefined"){
		area.id = "cc_"+hash["user_id"];
	}

	cc_objReview[hash["user_id"]] = new cc_ReviewRequest(hash,area.id);
	cc_objReview[hash["user_id"]].getReview();
}

(function(){
	var divs = document.getElementsByTagName("DIV");
	var len = divs.length;
	for(var i=0;i<len;i++){
		if(divs[i].className.indexOf(cc_targetClass) >= 0){
			conecoClubParts_loadArea(divs[i]);
		}
	}
})();
