<?php
/*
 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 +																			 +
 + The index.php is used for the detecting the device and redirecting to		 +
 +  the page based on the position of the I-pad.								 +
 +																		     +
 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 */
require_once('config.php');
require_once('lib/bookinfo.php');//parse and detail book settings
$isiPad = (bool) strpos($_SERVER['HTTP_USER_AGENT'],'iPad');
if(BROWSERACCESS == "OFF" && $isiPad===false){//TODO: Remove Debug
		echo "Best viewd in the I-pad<br>Under Development";
		die;
}

$pos=123;/*Detect the get varaible and redirect to portrait/landscape page*/
if(isset($_GET['pos'])){
	$pos= trim($_GET['pos']);
}

if($IS_LANDSCAPE_PUB || $pos==1){	
	include("portrait.php");
}
else if($pos==0){	
	include("landscape.php");
}
/*This is for the site opened first time, detects the position and then redirects*/
else {
?>
<script>
	
	
	if(window.orientation==0){
		window.location = "index.php?pos=1";
	}else if(window.orientation==90) {
		window.location = "index.php?pos=0";
	}else if(window.orientation==-90) {
		window.location = "index.php?pos=0";
	}else{
		window.location = "index.php?pos=1";
	}

	window.onorientationchange = function(){
		var orientation = window.orientation;
		if (orientation === 0){
			/* iPad is in Portrait mode.*/
			window.location = "index.php?pos=1";
		}
		else if (orientation === 90){
			/* iPad is in Landscape mode. The screen is turned to the left.*/
			window.location = "index.php?pos=0";
		}
		else if (orientation === -90){
			/* iPad is in Landscape mode. The screen is turned to the right.*/
			window.location = "index.php?pos=0";
		}
		else{/*Default portrait*/
			window.location = "index.php?pos=1";
		}	
	}	
	</script>
<?php }  ?>