﻿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 ) {

        //alert(nodataflg) ;
	//src += '';

	}else{

	var item_title = '';
	var item_price = '';
	var item_shop = '';
	var item_url = '';
	var item_img_url = '';
	var total_hitnum = '';
	var hit_number = 0 ;
	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 += '<a name="nwshop" id="nwshop">&nbsp;</a>' ;
	src += '<div class="bshop-in"><ul class="ShopList">' ;

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

		if ( hit_number == 4 ) {
			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 ){
			hit_number++;
			src += '<li><p class="BecomeImg">';
			if( item_url.length > 0 ) src += '<a 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="cap">';
			if( item_url.length > 0 ) src += '<a href="'+ item_url +'" target="_top">' ;
			src += item_title;
			if( item_url.length > 0 ) src += '</a>' ;
		}

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

		// item_shop
		if( item_shop.length > 0 ){
			src += '<p class="shp">';
			if( item_url.length > 0 ) src += '<a href="'+ item_url +'" target="_top">' ;
			src += item_shop;
			if( item_url.length > 0 ) src += '</a>' ;
		}

			src += '</p>';

		src += '</li>';

	}

	src += '</ul>';

	if (more_flg == 1) {
	src += '<div id="be-more"><a href="http://shopping.fresheye.com/pl/search.cgi?cs=euc'+ query +'" class="go-right">フレッシュアイショッピングでもっとみる</a></div>' ;
	}

	
	//src += '<div id="be-logo"><img src="http://img.fresheye.com/fe/news/fenw/img/poweredbyBecome.png" alt="powered by BECOME"></div>' ;
	src += '<div id="be-logo"><a href="http://www.become.co.jp/">powered by BECOME</a></div>' ;
	src += '</div>' ;

        }

	document.getElementById("bshop").innerHTML = src;
	oShoppingJ.removeScriptTag();
}

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

        var page = (((num + 1) * 4) - 3);
        query = "" ;
        query += '&q=' + keyword;
        query += '&ie=' + cs;
        query += '&channel=' + channel;
        query += '&start=' + page ;

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

	(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);
}

