$(function(){
	var agent = navigator.userAgent;
	var temp = new Array();
	if(agent.match(/MSIE/)){
		temp = agent.split('MSIE ');
		temp = temp[1].split('.');
		if(Number(temp[0])>=6)
			history_display();
		else
			return false;
	} else if(agent.match(/Chrome/)){
		history_display();
	} else if(agent.match(/Firefox/)){
		temp = agent.split('Firefox/');
		temp = temp[1].split('.');
		if(Number(temp[0])>=2)
			history_display();
		else
			return false;
	} else if(agent.match(/Opera/)){
		temp = agent.split('Opera/');
		temp = temp[1].split('.');
		if(Number(temp[0])>=9)
			history_display();
		else
			return false;
	} else if(agent.match(/Safari/)){
		safari_flag = true;
		temp = agent.split('Version/');
		temp = temp[1].split('.');
		if(Number(temp[0])>=4)
			history_display();
		else
			return false;
	} else{
		return false;
	}
});

function history_display(){
	var my_url = location.href;

	if(my_url.match(/adult.rental.rakuten.co.jp/)){
		if(my_url.match(/search.adult.rental.rakuten.co.jp/)){
			java_url = 'http://search.adult.rental.rakuten.co.jp/action/adult_search/MRF00_101_01.do';
		} else if(my_url.match(/item.adult.rental.rakuten.co.jp/)){
			java_url = 'http://item.adult.rental.rakuten.co.jp/action/adult_item/MRF00_101_01.do';
		} else{
			java_url = 'http://adult.rental.rakuten.co.jp/action/adult_top/MRF00_101_01.do';
		}
		history_url = 'https://my.rental.rakuten.co.jp/action/my/MRF07_045_01.do?l-id=bh_frame_to_list_adult';
		item_lid = '?l-id=bh_frame_list_item_adult';
		adult_flag = 'adult/';
	} else{
		if(my_url.match(/search.rental.rakuten.co.jp/)){
			java_url = 'http://search.rental.rakuten.co.jp/action/search/MRF00_001_01.do';
		} else if(my_url.match(/item.rental.rakuten.co.jp/)){
			java_url = 'http://item.rental.rakuten.co.jp/action/item/MRF00_001_01.do';
		} else if(my_url.match(/my.rental.rakuten.co.jp/)){
			java_url = 'https://my.rental.rakuten.co.jp/action/my/MRF00_001_01.do?s=1';
		} else{
			java_url = 'http://rental.rakuten.co.jp/action/top/MRF00_001_01.do';
		}
		history_url = 'https://my.rental.rakuten.co.jp/action/my/MRF07_044_01.do?l-id=bh_frame_to_list';
		item_lid = '?l-id=bh_frame_list_item';
		adult_flag = '';
	}

	loading_image = '<img src="/img/rental_button/loading.gif" width="24" height="24" class="loading">';

	if(my_url.match(/\/rental.rakuten.co.jp|\/adult.rental.rakuten.co.jp/)){
		if(my_url.match(/\/r\//)){
			$('div#navi div#search').after(loading_image);
		} else{
			$('div#navi div#rank').before(loading_image);
		}
	} else if(my_url.match(/\/search.*.rental.rakuten.co.jp/)){
		$('div#navi div#rank').after(loading_image);
	} else{
		$('div#navi div#search').after(loading_image);
	}

	$.ajax({
		"url": java_url,
		"dataType": "json",
		"async": false,
		"cache": false,
		"timeout": 5000,
		"success": function(data) {
			if(data.code == 'SUCCESS'){
				works = data.bh;
				works_len = works.length;
				if(works_len == 0){
					$('div#navi img.loading').hide();
					return false;
				}
				html = [
				'<dl id="browseHistory">',
				'<dt class="browseHistory">',
				'<img src="/img/',adult_flag,'side_ttl_history.gif" alt="最近チェックした作品">',
				'<img height="16" width="18" class="popupHelp" alt="閲覧履歴とは" src="/img/icon_help_ttl.gif">',
				'</dt>',
				'<dd>',
				'<div class="list"><a href="',history_url,'">最近チェックした作品一覧</a></div>',
				'<ul>'
				].join('');
				for(i=0;i<works.length;i++){
					works[i].link += item_lid;
					html = [ html,
					'<li>',
					'<div class="image"><a href="',works[i].link,'"><img src="',works[i].image,'" alt="',works[i].title,'" width="64"></a></div>',
					'<div class="note"><a href="',works[i].link,'">',works[i].title,'</a></div>',
					'</li>'
					].join('');
				}
				html = [ html,
				'</ul>',
				'</dd>',
				'</dl>'
				].join('');
				$('div#navi img.loading').after(html);
				popup_help();
			}
		},
		"error": function(d,msg) {
		},
		"complete": function() {
			$('div#navi img.loading').hide();
		}
	});

}
