// JavaScript Document

// PDF download
function getPdfDownload () {
	window.open('collect_pdf.php','CompletePDF','scrollbars=yes,toolbar=yes,resizable=yes');
}

var original_value = '';
function clearSearch (value) {
	original_value = value;
	var searchField = document.getElementById('searchField');
	searchField.value = '';
}

function setSearch (value) {
	if (value == '') {
		var searchField = document.getElementById('searchField');
		searchField.value = original_value;
	}
}

function changeImage(field, status) {
	var file = document.getElementById(field);
	if (status == '1') { file.src = "htmlReader/gfx/btn_" + field + "_off.png"; } else { file.src = "htmlReader/gfx/btn_" + field + ".png"; }
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) { createCookie(name,"",-1); }

var shownFlash = 0;
function showFlashBox () {
	var cookie = readCookie('showFlash');
	//alert(cookie);
	if (cookie != 'no') {
		var cookie2 = readCookie('flashShown');
		jQuery(window).bind("load", function() {
		$('#flashBox').fadeIn();
			setTimeout(function(){
				$('#flashBox').fadeOut();
			}, 10000);
		});
		shownFlash = 1;
		createCookie('flashShown','yes',1);
	}
}

function dontShowAgain () {
	var box = document.getElementById('dontShow');
	if (box.checked == true) { createCookie('showFlash','no',1); } else { createCookie('showFlash','yes',1); }
}

function setFlashBoxPosition(newWindowWidth) {
	var flashBoxWidth = '400';
	var newMarginLeft = ((newWindowWidth-flashBoxWidth)/2)-10 + 'px';
	$("#flashBox").css("display", 'block');
	$("#flashBox").css("margin-left", newMarginLeft);
	return;
}

function showZoomPage(imageUrl,currentSpread) {
	var zoompage = document.getElementById('zoom_page');
	zoompage.style.display = 'block';
	
	var zoomImage = document.getElementById('zoomImage');
	zoomImage.src = imageUrl;
	
	var spreadNum = currentSpread;
	// Set the front help only if it is the first page
	if (isNaN(spreadNum) || spreadNum == 0) { 
		var fronthelp = document.getElementById('frontHelp'); 
		fronthelp.style.display = 'none'; 
	}
	
	var pageContainer = document.getElementById('page_holder');
	pageContainer.style.display = 'none';
	
	var Txtbtn = document.getElementById('triggerDiv');
	Txtbtn.style.display = 'none';

	resizeZoom();
}

function showPageContainer(currentSpread) {
	var zoompage = document.getElementById('zoom_page');
	zoompage.style.display = 'none';
	
	var zoomImage = document.getElementById('zoomImage');
	zoomImage.src = 'htmlReader/gfx/empty.gif';
	
	var spreadNum = currentSpread;
	//alert(spreadNum);
	// Set the front help only if it is the first page
	if (isNaN(spreadNum) || spreadNum == 0) {
		var fronthelp = document.getElementById('frontHelp');
		fronthelp.style.display = 'block';
	}
	
	var pageContainer = document.getElementById('page_holder');
	pageContainer.style.display = 'block';
	
	var Txtbtn = document.getElementById('triggerDiv');
	Txtbtn.style.display = 'block';

	resizePage();
}



function resizePage () {
	$("body").css("background-color", '#FFFFFF');
	var screenHeight = $(window).height();
	var screenWidth = $(window).width();

	var newScreenWidth = Math.round((screenWidth/100) * 90);
	var newScreenHeight = Math.round((screenHeight/100) * 90);
	//alert(newScreenWidth);

	// Set menu size
	var menuTable = document.getElementById('menu-table');
	//alert(menuTable.width);
	var newWidth = 0;
	//alert(newScreenWidth);
	if (newScreenWidth >= 942) {
		newWidth = 942;
	} else if (newScreenWidth < 520) {
		newWidth = 520;
	} else {
		newWidth = newScreenWidth;
	}
	
	//alert(newWidth);
	$("#menu-table").css("width", newWidth);
	
	$("#panelScroll").css("height", (newScreenHeight-100));
	// end setting menu size
	
	// Set the width and height of the pages
	var allImages = document.images;
	var cii = 0;
	var new_width;
	var new_height;
	var spreadwidth = 0;
	for (var ci = 0; ci < allImages.length; ci++) {
		if (allImages[ci].className == 'pageImage') {
		
			var image_width = allImages[ci].width;
			var image_height = allImages[ci].height;
			var height_ratio  = image_height / newScreenHeight;
			var width_ratio   = image_width / newScreenWidth;
			var old_height_ratio = image_height/100;
			var old_width_ratio = image_width/100;
			//alert(height_ratio + '-' + width_ratio);
			if (height_ratio > width_ratio) {
				new_height = Math.round(newScreenHeight - 10);
				var ratio_perc = new_height/old_height_ratio;
				new_width = Math.round(old_width_ratio*ratio_perc);
				
				spreadwidth = new_width*2;
				//alert(spreadwidth + '-' + newScreenWidth);
				if (spreadwidth > newScreenWidth) {
					new_width = Math.round((newScreenWidth - 50)/2);
					ratio_perc = new_width/old_width_ratio;
					new_height = Math.round(old_height_ratio*ratio_perc);
					
				} else {
					new_width = Math.round((spreadwidth - 50)/2);
					ratio_perc = new_width/old_width_ratio;
					new_height = Math.round(old_height_ratio*ratio_perc);
					//alert(new_width);alert(new_height);
				}
			} else {
				new_width = Math.round((newScreenWidth - 50)/2);
				spreadwidth = new_width*2;
				ratio_perc = new_width/old_width_ratio;
				new_height = Math.round(old_height_ratio*ratio_perc);
			}
			
			if (new_width < 250){
				new_width = 250;
				spreadwidth = new_width*2;
				ratio_perc = new_width/old_width_ratio;
				new_height = Math.round(old_height_ratio*ratio_perc);
			}
			
			document.getElementById('page_holder').style.width = new_width*2;
			document.getElementById('page_holder').style.height = new_height;
			
			document.getElementById('contentHolder').style.width = (new_width*2)+20;
			document.getElementById('contentHolder').style.height = (new_height)+20;
			
			allImages[ci].height = new_height;
			allImages[ci].width = new_width;
		} 
	}
	// end setting width and height of pages
	
	
	// Set the front help only if it is the first page
	if (isNaN(spreadNum) || spreadNum == 0) {
		var fronthelp = document.getElementById('frontHelp');
		
		if (new_width < 272) {

			fronthelpWidth = (fronthelp.width*(ratio_perc/100));
			fronthelpHeight = (fronthelp.height*(ratio_perc/100));

			$("#frontHelp").css("width", fronthelpWidth);
			$("#frontHelp").css("height", fronthelpHeight);
			
			
		} else {
			$("#frontHelp").css("width", '272');
			fronthelpWidth = 272;
			$("#frontHelp").css("height", '94');
			fronthelpHeight = 94;
		}
		
		
		fronthelp.style.marginTop = ((new_height-fronthelp.height)/2) + 'px';
		fronthelp.style.marginLeft = ((new_width-fronthelp.width)/2) + 'px';
		
		$("#frontHelp").css("margin-top", ((new_height-fronthelpHeight)/2));
		$("#frontHelp").css("margin-left", ((new_width-fronthelpWidth)/2));
	}
}


function resizeZoom () {
	var screenHeight = $(window).height();
	var screenWidth = $(window).width();

	var newScreenWidth = Math.round((screenWidth/100) * 90);
	var newScreenHeight = Math.round((screenHeight/100) * 90);
	
	var allImages2 = document.images;
	var bii = 0;
	for (var bi = 0; bi < allImages2.length; bi++) {
		if (allImages2[bi].className == 'zoom_image') {
			
			var zoom_image_width = allImages2[bi].width;
			var zoom_image_height = allImages2[bi].height;
			
			var zoom_old_width_ratio = zoom_image_width/100;
			var zoom_old_height_ratio = zoom_image_height/100;

			if (newScreenWidth < 530) {
				newScreenWidth = 520;
			}
			
			var new_zoom_width = (newScreenWidth)-50;
			allImages2[bi].style.width = (new_zoom_width) + 'px';
			var zoom_ratio_perc = new_zoom_width/zoom_old_width_ratio;
			var new_zoom_height = Math.round(zoom_old_height_ratio*zoom_ratio_perc);
			allImages2[bi].style.height = (new_zoom_height) + 'px';
		}
	}
	
	var zoom_page = document.getElementById('zoom_page');
	zoom_page.style.width = (newScreenWidth-50) + 'px';
	zoom_page.style.height = '100%';
	
	document.getElementById('contentHolder').style.width = (newScreenWidth-50) + 'px';
	document.getElementById('contentHolder').style.height = '100%';
}
