$(document).ready(function() {

	// デベロッパーIDとアフィリエイトID
	var developerId = '07e4ee1bdd7145cd36026b3afb823506';
	var affiliateId = '';
	
	// キーワードで検索
	
	// アクセス先のURLを作成
	var url = 'http://api.rakuten.co.jp/rws/3.0/json?';
	url += '&developerId=' + developerId;
	url += '&keyword=' + encodeURIComponent(keyword);
	url += '&operation=ItemSearch&version=2010-08-05&callBack=?';
	url += '&shopCode=selection-j';
	
	//変更可能エリア/////////////////////////////////////////////////////
	
	// ソートの種類
	url += '&sort=-updateTimestamp';
	
	/////////////////////////////////////////////////////////////////////

	// 表示ページ番号
	url += '&page=' + page_no;
	
	// 除外コード
	url += '&NGKeyword=' + encodeURIComponent(NGKeyword);
	
	// 検索モード
	url += '&field=' + field_mode;
	
	// or検索
	url += '&orFlag=' + or_flg;
	
	// 件数※上限30まで
	url += '&hits=' + hits_count;
  
	// JSONPで情報を得る
	column_flg=0;
	row_flg=0;
	if( "turn_row" in window){}else{turn_row = 1;};
	if( "bx_mode" in window){}else{bx_mode = 'fade';};

	$.getJSON(url, function(data) {
		if (data.Header.Status == 'Success') {
			var items = data.Body.ItemSearch.Items.Item;
			var html = '';
			for (var i = 0, j = items.length; i < j; i++) {
				var item = items[i];
				if(column_flg == 0 && row_flg == 0){
						html += '<div id="middleclumn" class="clear">';
				};
				column_flg++;
				
				//拡大画像URLの作成
				big_imageURL=item.mediumImageUrl.replace("128x128", "400x400");
				
				// Yahoo用URLの作成
					yahoo_url1="http://store.shopping.yahoo.co.jp/selection-j/";
					yahoo_url2=item.itemUrl.replace("http://item.rakuten.co.jp/selection-j/", "");
					yahoo_url2=yahoo_url2.replace("/", "");
					yahoo_url3=".html";
					yahoo_url=yahoo_url1 + yahoo_url2 + yahoo_url3;
				
				// 商品名や画像のデータを元に、HTMLを組み立てる1

				
				if(document.uniqueID){
					html += '<div id="column"><div id="column_container">';
					html += '<div id="imgzone">';
					html += '<img id="itemimage" src="' + item.mediumImageUrl + '" alt="' + item.itemName + '"/>';
					html += '</div><br />';
				}
				else{
					html += '<div id="column"><div id="column_container">';
					html += '<div id="imgzone"><a class="item" rel="item_group" href="' + big_imageURL + '" target="_blank">';
					html += '<img id="itemimage" src="' + item.mediumImageUrl + '" alt="' + item.itemName + '"/>';
					html += '</a></div><br />';
				}
				
				
				html += '<div id="itemname">' + item.itemName + '</div>';

				html += '<div id="itemprice">' + '価格 '+ item.itemPrice + '円(税込) </div>';
				html += '<table id="icontabele"><tr>';
				html += '<td width="80"><p class="rakutenicon"><a href="' + item.itemUrl + '" onclick=\"javascript:pageTracker._trackPageview(\'/outsidelink/newarrival/api/rakuten/' + yahoo_url2 + '\')\" target="_blank" id="buy_txt">';
				html += '楽天へ';
				html += '</a></p></td>';
				
					

				html += '<td width="80"><p class="yahooicon"><a href="' + yahoo_url + '" onclick=\"javascript:pageTracker._trackPageview(\'/outsidelink/newarrival/api/yahoo/' + yahoo_url2 + '\')\" target="_blank" id="buy_txt">';
				html += 'Yahoo!へ';
				html += '</a></p></td>';
				html += '</tr></table>';
				html += '</div></div>';

				// IDが「rakuten」の要素にHTMLを流し込む
				$('#rakuten_api').html(html);
				
				if(column_flg == turn_column){
					html += '<br style="clear:both;">';
					column_flg=0;
					row_flg++;
				
					if(row_flg == turn_row){
						html += '</div>';
						row_flg=0;
					}
				}
			}
		}
		else if (data.Header.Status == 'NotFound') {
		// 商品が見つからなかった場合
		$('#rakuten_api').html('<p>商品が見つかりませんでした。</p>');
		}
		else {
			// 検索エラーの場合
			$('#rakuten_api').html('<p>検索時に以下のエラーが発生しました。<br />' + data.Header.StatusMsg + '</p>');
		}
		
		
				// jquery
					if(document.uniqueID){
						/*Internet Explorer用*/
					}
					else{
						// fancyboxの指定。
						$("a[rel=item_group]").fancybox({
							'transitionIn'		: 'fade',
							'transitionOut'		: 'fade',
							'titlePosition' 	: 'inside',
							'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
								return '<span id="fancybox-title-over" style="background: url();">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
							}
						});
					}
				

					
					
	});
	
					
});


