var browser_o=(function(){
	var userAgent = window.navigator.userAgent.toLowerCase();
	var platform=navigator.platform.toLowerCase();
	var browser_o= {
		win:/\bwin\b/.test(userAgent) || /\bwindows\b/.test(userAgent),
		mac:/\bmac\b/.test(userAgent) || /\bmacintosh\b/.test(userAgent),
		android:/\bandroid\b/.test(userAgent),
		symbian:(/\bsymbian\b/.test(userAgent) || /\bseries60\b/.test(userAgent)) && /webkit/.test(userAgent),
		wince:/windows ce/.test(userAgent) || /wince/.test(platform),
		blackberry:/\bblackberry\b/.test(userAgent),
		iphone:/\biphone\b/.test(userAgent),
		ipad:/\bipad\b/.test(userAgent),
		version: (userAgent.match(/.+(?:rv|it|ra|msie)[\/: ]([\d.]+)/) || [])[1],
		safari: /\bapplewebkit\b/.test(userAgent) && /\bsafari\b/.test(userAgent),
		opera: /\bopera\b/.test(userAgent),
		msie: /\bmsie\b/.test(userAgent) && !/opera/.test(userAgent),
		netscape:/\bnetscape\b/.test(userAgent),
		mozilla: /\bmozilla\b/.test(userAgent) && !/(compatible|webkit)/.test(userAgent),
		chrome: /\bchrome\b/.test(userAgent)
	};
	browser_o.mobile=browser_o.android || browser_o.symbian || browser_o.wince || browser_o.blackberry || browser_o.iphone || browser_o.ipad;
	return browser_o;
})();

var MSG_CONFIRM01="DBook Readerをインストールしますか？";

function calldbook(seq, url){
	var host="http://www.gakken.co.jp/";
	var fid='';
	var customURI='com.kdmt.dmookviewer://'+host+'/?xml='+encodeURI('<Info fid="'+fid+'" seq="'+seq+'" FolderPath="" URL="'+url+'"/>')+"&url="+encodeURI(document.location.href);
	var pcurl='';


	if(browser_o.android){
		var timerID=-1;
		var iframe = document.createElement('iframe');
		iframe.style.position = 'absolute';
		iframe.style.left = '-300px';
		iframe.style.height = '1px';
		iframe.style.width = '1px';
		document.body.appendChild(iframe);
		iframe.onload=function(){
			clearTimeout(timerID);
			document.body.removeChild(iframe);
			if(confirm(MSG_CONFIRM01)){
				var installURL = 'market://details?id=air.com.kdmt.dmookviewer7';
				try{
					if(window.opener){
						try{
							window.opener.top.location = installURL;
							window.opener.top.focus();
							self.close();
						}catch(ae){
							window.opener.location = installURL;
							window.opener.focus();
							self.close();							
						}
					}else if(window.parent){
						try{
							window.parent.top.location = installURL;
							window.parent.top.focus();
						}catch(ae){
							window.parent.location = installURL;
							window.parent.focus();					
						}
					}else{
						window.location = installURL;
					}
				}catch(e){
					window.location = installURL;
				}
			}else if(window.opener){
				self.close();
			}
		};
		iframe.contentWindow.location=customURI;
		if(window.opener){
			timerID=setTimeout(function(){
				self.close();
			},1000);                                                                                                                                                                                                                   
		}
	}else if(browser_o.iphone || browser_o.ipad){
		var iOSTimeOut;
		if(browser_o.iphone){
			iOSTimeout = 10000;
		}else{
			iOSTimeout = 1000;
		}
		var start = new Date();
		setTimeout(function() {
				if (new Date()-start>(iOSTimeout+1000)) {
					return;
				}
				if(confirm(MSG_CONFIRM01)){
					var installURL = 'itms-apps://itunes.com/app/DBookReader';
					try{
						if(window.opener){
							try{
								window.opener.top.location = installURL;
								window.opener.top.focus();
								self.close();
							}catch(ae){
								window.opener.location = installURL;
								window.opener.focus();
								self.close();							
							}
						}else if(window.parent){
							try{
								window.parent.top.location = installURL;
								window.parent.top.focus();
							}catch(ae){
								window.parent.location = installURL;
								window.parent.focus();					
							}
						}else{
							window.location = installURL;
						}
					}catch(e){
						window.location = installURL;
					}
				}else if(window.opener){
					window.close();
				}
		}, iOSTimeout);
		window.location=customURI;
	}else{
		pcurl = url + '/default1.html';
		openCatalog( pcurl );
	}
}

function openCatalog( strURL ){
            var screen_width = screen.availWidth - 10;
            var screen_height = screen.availHeight - 30;
            strFeatures = "screenX=0,screenY=0,width=" + screen_width + ",height=" + screen_height +",";
            strFeatures += "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes";
            var windowname;
            windowname=window.open( strURL, "DBookWin", strFeatures );
            try{win.focus();}catch(e){}
            return;
}

