// Scripts for Digibibles application
// Last updated: 2009-07-22

function setContentDimensions(){
	var baseHeight;
	var baseWidth;
	if(document.documentElement.clientHeight){
		/* baseHeight=document.documentElement.clientHeight-145; */
		baseHeight=document.documentElement.clientHeight-185;
		baseWidth=document.documentElement.clientWidth-20;
	}
	else{
		/* baseHeight=document.body.clientHeight-135; */
		baseHeight=document.body.clientHeight-175;
		baseWidth=document.body.clientWidth-20;
	}

	if(document.getElementById('layout1')) {
		handleHeight(document.getElementById('scanview'), baseHeight);
		handleWidth(document.getElementById('scanview'), baseWidth);
	}
	else if(document.getElementById('layout2a')) {
		handleHeight(document.getElementById('scanview'), Math.floor(baseHeight/2));
		handleHeight(document.getElementById('transcrview'), Math.floor(baseHeight/2));
		handleWidth(document.getElementById('scanview'), baseWidth);
		handleWidth(document.getElementById('transcrview'), baseWidth);
	}
	else if(document.getElementById('layout2b')) {
		handleHeight(document.getElementById('scanview'), baseHeight);
		handleHeight(document.getElementById('transcrview'), baseHeight);
		handleWidth(document.getElementById('scanview'), Math.floor(baseWidth/2));
		handleWidth(document.getElementById('transcrview'), Math.floor(baseWidth/2));
	}

	/*
	if(baseWidth > 1100){
		document.frmNav.part.style.width='300px';
	}
	else if(baseWidth > 1050){
		document.frmNav.part.style.width='250px';
	}
	else {
		document.frmNav.part.style.width='200px';
	}
	*/
}

function handleHeight(element,newHeight) {
	if(newHeight>100&&element.style.height!=newHeight.toString()+'px') {
		element.style.height=newHeight.toString()+'px';
	}
}

function handleWidth(element,newWidth) {
	if(newWidth>100&&element.style.width!=newWidth.toString()+'px') {
		element.style.width=newWidth.toString()+'px';
	}
}

function change_bible(){
	var bible=document.frmNav.bible.value;
	var layout=document.frmNav.layout.value;
	var red="http://"+window.location.hostname+window.location.pathname+"?bible="+bible+"&layout="+layout;
	this.location.href=red;
}

function change_part(){
	var bible=document.frmNav.bible.value;
	var part=document.frmNav.part.value;
	var layout =document.frmNav.layout.value;
	var red="http://"+window.location.hostname+window.location.pathname+"?bible="+bible+"&part="+part+"&layout="+layout;
	this.location.href=red;
}

function change_subpart(){
	var bible=document.frmNav.bible.value;
	var item=document.frmNav.subpart.value;
	var page;
	var sub='';
	if(item.indexOf('[')>-1&&item.indexOf(']')==item.length-1){
		page=item.substr(0, item.indexOf('['));
		sub=item.substr(item.indexOf('[')+1,item.length-page.length-2);
	}
	else{
		page=item;
	}
	var layout =document.frmNav.layout.value;
	var redirect="http://"+window.location.hostname+window.location.pathname+"?bible="+bible+"&page="+page;
	if(sub!=''){
		redirect=redirect+"&sub="+sub;
	}
	redirect=redirect+"&layout="+layout;
	this.location.href=redirect;
}

function change_layout(){
	var bible=document.frmNav.bible.value;
	var part=document.frmNav.part.value;
	var item=document.frmNav.subpart.value;
	var page;
	var sub=0;
	if(item.indexOf('[')>-1){
		page=item.substr(0, item.indexOf('['));
		sub=item.substr(item.indexOf('[')+1,1);
	}
	else{
		page=item;
	}
	var layout =document.frmNav.layout.value;
	var redirect="http://"+window.location.hostname+window.location.pathname+"?bible="+bible+"&page="+page;
	if(sub>0){
		redirect=redirect+"&sub="+sub;
	}
	redirect=redirect+"&layout="+layout;
	this.location.href=redirect;
}

function togglePageView(image) {
	if (document.frmNav.layout.value=='1') {
		document.frmNav.layout.value='2';
		change_layout();
	}
	else if (document.frmNav.layout.value=='2') {
		document.frmNav.layout.value='1';
		change_layout();
	}
}

function prevPart(){
	document.frmNav.part.selectedIndex=document.frmNav.part.selectedIndex-1;
	change_part();
}

function nextPart(){
	document.frmNav.part.selectedIndex=document.frmNav.part.selectedIndex+1;
	change_part();
}

function prevSubpart(){
	document.frmNav.subpart.selectedIndex=document.frmNav.subpart.selectedIndex-1;
	change_subpart();
}

function nextSubpart(){
	document.frmNav.subpart.selectedIndex=document.frmNav.subpart.selectedIndex+1;
	change_subpart();
}

function goToPage(pageseqno){
	var bible=document.frmNav.bible.value;
	var layout =document.frmNav.layout.value;
	var redirect="http://"+window.location.hostname+window.location.pathname+"?bible="+bible+"&page="+pageseqno+"&layout="+layout;
	this.location.href=redirect;
}

function prevPage(){
	var testString="&"+window.location.search.substr(1)+"&";
	var pos1=testString.indexOf("&page=")+6;
	var pos2=testString.indexOf("&", pos1);
	var pageseqno=parseInt(testString.substring(pos1, pos2));
	goToPage(pageseqno-1);
}

function nextPage(){
	var testString="&"+window.location.search.substr(1)+"&";
	var pos1=testString.indexOf("&page=")+6;
	var pos2=testString.indexOf("&", pos1);
	var pageseqno=parseInt(testString.substring(pos1, pos2));
	goToPage(pageseqno+1);
}

function prevColumn() {
	var bible=document.frmNav.bible.value;
	var testString="&"+window.location.search.substr(1)+"&";
	var pos1=testString.indexOf("&page=")+6;
	var pos2=testString.indexOf("&", pos1);
	var page=parseInt(testString.substring(pos1, pos2));
	var col;
	if(document.getElementById('scanview').scrollLeft>400){
		col=1;
	}else{
		page=page-1;
		col=2;
	}
	var layout =document.frmNav.layout.value;
	var redirect="http://"+window.location.hostname+window.location.pathname+"?bible="+bible+"&page="+page+"&col="+col+"&layout="+layout;
	this.location.href=redirect;
}

function nextColumn() {
	var bible=document.frmNav.bible.value;
	var testString="&"+window.location.search.substr(1)+"&";
	var pos1=testString.indexOf("&page=")+6;
	var pos2=testString.indexOf("&", pos1);
	var page=parseInt(testString.substring(pos1, pos2));
	var col;
	if(document.getElementById('scanview').scrollLeft>400){
		page=page+1;
		col=1;
	}else{
		col=2;
	}
	var layout =document.frmNav.layout.value;
	var redirect="http://"+window.location.hostname+window.location.pathname+"?bible="+bible+"&page="+page+"&col="+col+"&layout="+layout;
	this.location.href=redirect;
}
