﻿var query = '' ;
var pagecnt = 0 ;

function BecomeShoppingAPI(data){
	var nodataflg = 0 ;

	// RET if no result or Error
	if(typeof data.service_response == "undefined") nodataflg = 1 ;
	if(typeof data.service_response.service_response == "undefined") nodataflg = 1 ;
	if(typeof data.service_response.service_response.results.meta.num == "undefined") nodataflg = 1 ;
	var hitnum = data.service_response.service_response.results.meta.num;
	if( isNaN(hitnum) ) nodataflg = 1 ; 
	if( ! hitnum || hitnum == 0 ) nodataflg = 1 ; 

	var src = '';

	if ( nodataflg == 1 ) {
	src += '';

	}else{

	var item_title = '';
	var item_price = '';
	var item_shop = '';
	var item_url = '';
	var item_img_url = '';
	var total_hitnum = '';
	var more_flg = 0 ;

	if(typeof data.service_response.service_response.results.meta.num_results != "undefined") total_hitnum = data.service_response.service_response.results.meta.num_results;
	if( isNaN(total_hitnum) ) total_hitnum = ''; 

	src += '<div class="box-type6"><h2><a href="http://shopping.fresheye.com/" target="_top">ショッピング</a></h2><div class="shpg">' ;

	for(i in data.service_response.service_response.results.result){

		if ( i == 2 ) {
			more_flg = 1 ;
			break ;
		}

		item_title = item_price = item_shop = item_url = item_img_url = ''
		if(typeof data.service_response.service_response.results.result[i].title != "undefined") item_title = data.service_response.service_response.results.result[i].title;
		if(typeof data.service_response.service_response.results.result[i].merchant != "undefined") item_price = data.service_response.service_response.results.result[i].merchant.price;
		if(typeof data.service_response.service_response.results.result[i].merchant != "undefined") item_shop = data.service_response.service_response.results.result[i].merchant.name;
                if(typeof data.service_response.service_response.results.result[i].merchant != "undefined") item_url = data.service_response.service_response.results.result[i].merchant.url;
		if(typeof data.service_response.service_response.results.result[i].image_url != "undefined") item_img_url = data.service_response.service_response.results.result[i].image_url;

		// Image
		if( item_img_url.length > 0 ){
			src += '<div class="shp-detl">';
			if( item_url.length > 0 ) src += '<a class="shopimg" href="'+ item_url +'" target="_top">' ;
			src += '<img src="'+ item_img_url +'" width="100" border="0"/>' ;
			if( item_url.length > 0 ) src += '</a>' ;
			//src += '</p>';
		}

		// Title
		if( item_title.length > 0 ){
			src += '<p class="link-text">';
			if( item_url.length > 0 ) src += '<a href="'+ item_url +'" target="_top">' ;
			src += item_title;
			src += '</a>';
		}

		// Price
		if( item_price.length > 0 ){
			src += '<br /><span class="price"><br />';
			while( item_price != ( item_price = item_price.replace(/^(\d+)(\d{3})/,"$1,$2")) ){}
			src += '' + item_price + '円';
			if( item_url.length > 0 ) src += '</span>' ;
		}

		src += '</p></div>';

	}

	if (more_flg == 1) {
	src += '<p id="be-more"><a href="http://shopping.fresheye.com/pl/search.cgi?'+ query +'" target="_top"class="go-right">フレッシュアイショッピングでもっとみる</a></p>' ;
	}
	src += '<p class="poweredby"><a href="http://www.become.co.jp" target="_top"><img src="http://kotochu.fresheye.com/img/common/poweredbyBecome.png" alt="powered by BECOME" /></a></p></div></div>' ;
	
    }

	document.getElementById("sub-shopping").innerHTML = src;
	oShoppingJ.removeScriptTag();
}

// Constructor -- pass a REST request URL to the constructor
function JsonRequest(keyword, cs, channel) {

	var query = "" ;
	query += '&q=' + keyword;
	query += '&ie=' + cs;
	query += '&channel=' + channel;

	// REST request path
	this.fullUrl = 'http://partner2.become.co.jp/json?partner=bj_fresh_public&num=5&callback=BecomeShoppingAPI' + query ;

//document.write(this.fullUrl);

	(this.fullUrl.indexOf('?') > 0) ? this.noCacheIE = '&' : this.noCacheIE = '?';
	// Keep IE from caching requests
	this.noCacheIE += 'noCacheIE=' + (new Date()).getTime();
	// Get the DOM location to put the script tag
	this.headLoc = document.getElementsByTagName("head").item(0);
	// Generate a unique script tag id
	this.scriptId = 'JscriptId' + JsonRequest.scriptCounter++;
}

// Static script ID counter
JsonRequest.scriptCounter = 1;

// buildScriptTag method
JsonRequest.prototype.buildScriptTag = function () {

	// Create the script tag
	this.scriptObj = document.createElement("script");
	
	// Add script object attributes
	this.scriptObj.setAttribute("type", "text/javascript");
	this.scriptObj.setAttribute("charset", "utf-8");
	this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
	this.scriptObj.setAttribute("id", this.scriptId);
}

// removeScriptTag method
JsonRequest.prototype.removeScriptTag = function () {
	this.headLoc.removeChild(this.scriptObj);
}

// addScriptTag method
JsonRequest.prototype.addScriptTag = function () {
	this.headLoc.appendChild(this.scriptObj);
}

