pageshow_flag = false;
safari_flag = false;

if(window.addEventListener){
	window.addEventListener("pageshow", onPageShow, false);
	window.addEventListener("pagehide", function() {pageshow_flag=true;}, false);
}

function onPageShow(){
	if(pageshow_flag)
		agentCheck();
}

$(function(){
	if(pageshow_flag==false)
		agentCheck();
});

function agentCheck(){
	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)
			rental_button();
		else
			return false;
	} else if(agent.match(/Chrome/)){
		rental_button();
	} else if(agent.match(/Firefox/)){
		temp = agent.split('Firefox/');
		temp = temp[1].split('.');
		if(Number(temp[0])>=2)
			rental_button();
		else
			return false;
	} else if(agent.match(/Opera/)){
		temp = agent.split('Opera/');
		temp = temp[1].split('.');
		if(Number(temp[0])>=9)
			rental_button();
		else
			return false;
	} else if(agent.match(/Safari/)){
		safari_flag = true;
		temp = agent.split('Version/');
		temp = temp[1].split('.');
		if(Number(temp[0])>=4)
			rental_button();
		else
			return false;
	} else{
		return false;
	}
}

function rental_button(){

	$('#searchmain li.rentalButton a,#searchmain div.spotAction a,#searchmain td.month a,#searchmain td.spot a,#tbl td.month a,#tbl td.spot a').each(function (){
		if(safari_flag == false){
			$('img.loading',$(this).parent()).remove();
			$(this).show();
			$('img[src$="icon_help.gif"]',$(this).parent()).show();
		}
	})
	.unbind('click')
	.click(function (){

			//.blockクラスを持っていたら何もしない
			if ($(this).hasClass("block") == true) {
				return false;
			}

		var alt = $('img',this).attr('alt');

		if(alt.match(/予約リスト/)){
			success_text = '<p class="ajaxResult"><a href="https://my.rental.rakuten.co.jp/action/my/MRF07_008_01.do">予約リスト追加完了</a></p>';
			timeout_text = '<p class="ajaxResult"><a href="https://my.rental.rakuten.co.jp/action/my/MRF07_008_01.do">予約リストを確認</a><br><span>エラー：タイムアウト</span></p>';
			error_text = '<p class="ajaxResult"><a href="https://my.rental.rakuten.co.jp/action/my/MRF07_008_01.do">予約リストを確認</a>';
			error_alert = '予約追加処理がタイムアウトしました。お手数ですが、予約リストを確認してください。';
			java_url = 'https://my.rental.rakuten.co.jp/action/my/MRF07_008_05.do';
			event_name="event2";
		} else if(alt.match(/スポットレンタル/)){
			success_text = '<p class="ajaxResult"><a href="https://my.rental.rakuten.co.jp/action/my/MRF07_008_01.do">スポットリスト追加完了</a></p>';
			timeout_text = '<p class="ajaxResult"><a href="https://my.rental.rakuten.co.jp/action/my/MRF07_008_01.do">スポットリストを確認</a><br><span>エラー：タイムアウト</span></p>';
			error_text = '<p class="ajaxResult"><a href="https://my.rental.rakuten.co.jp/action/my/MRF07_008_01.do">スポットリストを確認</a>';
			error_alert = 'スポットレンタル処理がタイムアウトしました。お手数ですが、スポットリストを確認してください。';
			java_url = 'https://my.rental.rakuten.co.jp/action/my/MRF07_036_05.do';
			event_name="event16";
		} else{
			return false;
		}


		//td.spot a全部に､".block"属性を追加する
		$('#searchmain  td.spot  a ').addClass("block");


		button_scope = $(this).parent();
		var href = $('a',button_scope).attr('href');

		if(safari_flag==false){
			var my_url = location.href;
			$(this).hide();
			$('img[src$="icon_help.gif"]',$(this).parent()).hide();
			if(my_url.match(/MRF07_044_01.do|MRF07_045_01.do/))
				button_scope.append('<img src="/img/browsing_history/loading.gif" width="24" height="24" class="loading">');
			else
				button_scope.append('<img src="/img/rental_button/loading.gif" width="24" height="24" class="loading">');
		}

		var hashes = new Array();
		var hash = new Array();
		vars = new Array();

		hashes = href.slice(href.indexOf('?') + 1).split('&');

		for(var i = 0; i < hashes.length; i++) {
			hash = hashes[i].split('=');
			vars[hash[0]] = hash[1];
		}

		$.jsonp({
			"url": java_url+"?callback=?",
			data: {
				i:vars['i'],
				yama:vars['yama']
			},
			"async":"false",
			"timeout": 5000,
			"success": function(data) {
				//td.spot a全部から､".block"属性を削除する
				$('#searchmain td.spot a ').removeClass("block");
				if(data.code == 'SUCCESS'){
					button_scope.html(success_text);
				} else if(data.code == 'ERROR'){
					alert(data.message);
					button_scope.html(error_text);
				} else if(data.code == 'AJAXNG'){
					location.href = href;
				} else{
					alert('大変申し訳ございません。システムエラーのため、サービスのご利用を継続することができません。');
					button_scope.html(error_text);
				}
			},
			"error": function(d,msg) {
			        //td.spot a全部から､".block"属性を削除する
				$('#searchmain td.spot a ').removeClass("block");
				if(msg == 'error'){
					location.href = href;
				} else if(msg == 'timeout'){
					alert(error_alert);
					button_scope.html(timeout_text);
				}
			},
			"complete": function() {
				return false;
			}
		});

		s.linkTrackVars="eVar1,eVar2,eVar3,eVar4,eVar5,eVar6,eVar7,eVar9,products,events";s.linkTrackEvents=event_name;s.events=event_name;s.tl(this,'o',event_name);

		return false;
	});
}

