// ------------------------------------------------------------
// JavaScript :: single page	[ 2010.05.30 ] Mayumi.Ogawa
// ------------------------------------------------------------
// 1. 楽天商品検索API
// 2. ツールチップ
// 3. 文字列トリム
// 4. 数値を3桁ごとにカンマで区切る
// ------------------------------------------------------------


// 楽天商品検索API : デベロッパID
var developerId = '4c7c3186b6dedb6bb3e58506a5bb3c81';

// 楽天商品検索API : アフィリエイトID
var affiliateId = '044ece18.410dc436.044ece19.0ebe9673';

// 楽天商品検索API : バージョン
var version = '2009-04-15';

// 文字列トリム関連
var trmstr = "";
var strleng = 0;
var trmstrs = "";


// ------------------------------------------------------------
// ロード時の初期処理
// ------------------------------------------------------------
$(function(){
	// 関連商品があれば、
	if($('#entrybody').children().is("#relateditem")){
		// タグを置換する
		var strhtml = $("#relateditem ul").html();
		var newhtml = strhtml.replace(/［/g, '<');
		newhtml = newhtml.replace(/］/g, '>');
		$("#relateditem ul").html(newhtml);
		//
		// 関連商品を常に最下部にする
		var copyitems = $("#relateditem").clone();
		$("#relateditem").remove();
		$("div#entrybody").append(copyitems);
	}
	//
	// 検索キーワードがあれば関連商品を作成
	if($('#entrybody').children().is("#rtapi_key")){
		var rtkeyw = $("#rtapi_key").val();
		var rtgrid = $("#rtapi_gid").val();
		rtnsearch(rtkeyw,rtgrid);
	}
})


// ------------------------------------------------------------
// 楽天商品検索API　検索＆表示
// ------------------------------------------------------------
function rtnsearch(keyw,grid) {
	$(document).ready(function() {
		var keyword = keyw;
		var url = 'http://api.rakuten.co.jp/rws/2.0/json?';
		url += '&developerId=' + developerId;
		url += '&affiliateId=' + affiliateId;
		url += '&keyword=' + encodeURIComponent(keyword);
		url += '&operation=ItemSearch';
		url += '&version=' + version;
		url += '&callBack=?';
		url += '&hits=6&genreId=' + grid;
		url += '&imageFlag=1&sort=standard&page=1';
		$.getJSON(url, function(data) {
			if (data.Header.Status == 'Success') {
				$("div#entrybody").append('<div id="relateditem"><h3>Related Items:</h3><ul id="rltitems"></ul></div>');
				var items = data.Body.ItemSearch.Items.Item;
				for (var i = 0, j = items.length; i < j; i++) {
					var item = items[i];
					strtrim(item.itemName,22);
					var trimName = trmstrs;
					//
					strtrim(item.itemCaption,200);
					var trimCap = trmstrs;
					//
					var itemttl = item.itemName;
					var itemprc = num3Format(item.itemPrice);
					//var itemprc = item.itemPrice;
					var itemurl = item.affiliateUrl;
					var imgeurl = item.smallImageUrl;
					var shpname = item.shopName;
					//
					$("ul#rltitems").append('<li><a rel="tooltip" title="<strong>'+trimName+'</strong><hr />'+itemprc+'円（'+shpname+'）<hr />'+trimCap+'" href="'+itemurl+'" target="_blank"><img src="'+imgeurl+'" border="0" /></a></li>');
				}
			}
			else if (data.Header.Status == 'NotFound') {
				//商品が見つからなかった時の処理
				return false;
			}
			else {
				//検索エラー時の処理
				return false;
			}
		});
	});
}

// ------------------------------------------------------------
// ツールチップ
// ------------------------------------------------------------
$(function() {
	//Select all anchor tag with rel set to tooltip
	$('a[rel=tooltip]').live("mouseover", function(e) {
	   
		//Grab the title attribute's value and assign it to a variable
		var tip = $(this).attr('title');   
		
		//Remove the title attribute's to avoid the native tooltip from the browser
		$(this).attr('title','');
		
		//Append the tooltip template and its value
		$(this).append('<div id="tooltip"><div class="tipHeader"></div><div class="tipBody">' + tip + '</div><div class="tipFooter"></div></div>');      
			 
		//Show the tooltip with faceIn effect
		$('#tooltip').fadeIn('500');
		$('#tooltip').fadeTo('10',0.9);
               
	}).live("mousemove", function(e) {
		//Keep changing the X and Y axis for the tooltip, thus, the tooltip move along with the mouse
		$('#tooltip').css('top', e.pageY + 5 );
		$('#tooltip').css('left', e.pageX + 5 );
               
	}).live("mouseout", function(e) {
		//Put back the title attribute's value
		$(this).attr('title',$('.tipBody').html());
		//Remove the appended tooltip template
		$(this).children('div#tooltip').remove();
		
	});
});


// ------------------------------------------------------------
// 文字列トリム
// ------------------------------------------------------------
function strtrim(str,leng){
	trmstr = " ...";
	strleng = str.length;
	if(strleng < leng){
		trmstr = "";
	}
	trmstrs = str.substring(0,leng) + trmstr;
}


// ------------------------------------------------------------
// 数値を3桁ごとにカンマで区切る
// ------------------------------------------------------------
function num3Format(str){
	var temp1 = (new String(str)).split(".")[0].match(/./g).reverse().join("");
	temp1 = temp1.replace(/(\d{3})/g,"$1,");
	temp1 = temp1.match(/./g).reverse().join("").replace(/^,/,"");
	if(!!(new String(str)).split(".")[1]) temp1 = temp1 +"."+ str.split(".")[1];
	return temp1;
}
