//show thumbnails after they have all loaded
function showImages() {
	for(var i=0; i<document.images.length; i++) {
		document.images[i].style.visibility = 'visible';
	}
}



//position and set opacity change on mouseover a floating scaling thumbnail
function thuN(name) {
	var thname = "thumbN/" + name + ".jpg";
	document.write("<img src='" + thname + "' onclick=showpage('" + name + "') />")
	}
			
//called by click on thumbnail: go to the associated page
function showpage(name) {location.href='pages/' + name + ".html";}


// Function to write auto-updating copyright text
function copy() {
	var d=new Date(); 
	yr=d.getFullYear();
	document.write("Photographs on this web site are (except where noted)<br>");
	document.write("are copyright &copy; Dr Brian Watson 1962 - " + yr);
}



// script to embed Flash movie taken from pano2VR
function p2q_EmbedFlash(sFile,sWidth,sHeight) {
	document.writeln('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"');
	document.writeln('  codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"');
	document.writeln('  width="' + sWidth + '" height="' + sHeight + '" >');
	document.writeln('  <param name="movie" value="' + sFile + '">');
	for(i=3;i<arguments.length;i+=2) {document.writeln('  <param name="' + arguments[i] + '" value="' + arguments[i+1] + '">');}
	document.writeln('<embed width="' + sWidth + '" height="' + sHeight + '"');
	document.writeln('	pluginspage="http://www.macromedia.com/go/getflashplayer"');
	document.writeln('	type="application/x-shockwave-flash"');
	document.writeln('	src="' + sFile + '"');
	for(i=3;i<arguments.length;i+=2) {document.writeln('  ' + arguments[i] + '="' + arguments[i+1] + '"');}
	document.writeln('	/>');
	document.writeln('</object>');
}




// position/hide/unhide popups on index pages (except pano & zoom)
function thumb(name,orient,caption) {
	var thname = "thumbnails/" + name + ".jpg";
	var nameB = name + "B";
	document.write("<img src='" + thname 
		+ "' class='" + orient 
		+ "' onmouseover=show('" + nameB + "','" + caption.replace(/\s+/g,"&nbsp;") 
		+ "') onmouseout=hide('" + nameB 
		+ "') onclick=goto('" + name + "') alt=''/>");

	switch (orient) {
		case "land":
			document.write("<img src='thumbnails/" + name + "B.jpg' class = cland id = '" + name + "B' alt=''/>")
			break;
		case "port":
			document.write("<img src='thumbnails/" + name + "B.jpg' class = cport id = '" + name + "B' alt=''/>")
			break;
		default: alert("Unrecognised 'orient' parameter: " + orient);
		}			
	}
		
	function show(pic,caption) {
		var what = document.getElementById(pic);
		what.style.visibility = "visible";
		what.style.zIndex = "2";
		document.getElementById('capt').innerText = caption;
	}
	function hide(pic) {
		document.getElementById(pic).style.visibility = "hidden";
		document.getElementById('capt').innerText = '' ;
	}

	function goto(page) {
		//on click, clear caption, hide popup, go to the page
		document.getElementById('capt').innerText = '';
		document.getElementById(page + "B").style.visibility = "hidden";
		location.href="pages/" + page + ".html";
	}
// end of thumbnail positioning etc


// loadNextImage : pre-load next image (taken from galerieslideshow.js)
function loadNextImage(num){
if (document.images){
    if (navigator.userAgent.indexOf("Opera") != -1){
        nextimage = new Image();
        nextimage.src = "../mediafiles/l"+ num +".jpg";
        }
    else if (navigator.userAgent.indexOf("MSIE") != -1 && navigator.userAgent.indexOf("Mac") != -1)
        {
        ;
        }
    else{
        nextimage = new Image();
        nextimage.src = "../mediafiles/l"+ num +".jpg";
        }
    }
}


// change the class of an img by clicking the image     : small<-->large ========;cursor: url(PNGs/grab.cur), pointer;
function toggle(elem,size1,size2){elem.className = (elem.className==size1)?size2:size1;}


function showpic(picnum,alttext) {
//Get browser window height in any browser (Rob Zand), then size picture smaller on small screens
var wHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
var tall=wHeight >= 800;
document.write('<img class="bordered" src="../mediafiles/l' + picnum + '.jpg" height="');
if(tall===true){document.write('800" width="600');}
else{document.write('400" width="300');}
document.write('" alt="' + alttext + '">');
}


function buy(theText) {
	alert(theText);
	return;
}
	
function goPage(theUrl) {
	document.location.href = theUrl;
	return;
}

// get picture name from query-coded url of checkout.html ==
function picturename() { 
var end = location.search.substr(1).split("?");
var name = end[0];
name = "'" + unescape(name) + "'";
return name;
}
	
	
// get picture thumbnail URL===
function thumbfile() { 
var end = location.search.substr(1).split("?");
var thumb = "'" + end[1] + "'";
return thumb;
}
	
// get horizontal pixels====
function hpix() { 
	var end = location.search.substr(1).split("?");
	var hp = end[2] ;
	return hp;
}
	

// get vertical pixels=====
function vpix() { 
	var end = location.search.substr(1).split("?");
	var vp = end[3];
	return vp;
}
	
	
// show ppi of final print====
function showppi() {
	var h = hpix();
	var selectedsize = document.getElementById('picWidth').value;
	var printwidth = selectedsize / 25.4; // convert mm to inches
	var ppi = h / printwidth;
	ppi = Math.round(ppi);
	if (ppi > 300) { // no point printing @ >300ppi
		ppi = 300;
	}
	return(ppi) ;
}

	
// calculate print height (mm)====
function showpicheight() {
	var h = hpix() ;
	var v = vpix() ;
	
	var longer = Math.max(h,v);
	var shorter = Math.min(h,v);
	
	var widthStr = document.getElementById('picWidth').value ;
	
	var widthN = parseInt(widthStr,0);
	var calculatedheight = widthN * shorter / longer ;
	var cropbyN = 0
	
	// if eg width is 297mm, height can't be more than 210mm
	switch (widthStr) {
		case "297":
			calculatedheight = Math.min(calculatedheight,210); // on A4
			break;
		case "406":
			calculatedheight = Math.min(calculatedheight,254); // on 16 x 10"
			break;
		case "580":
			if (calculatedheight > 210) {
			cropby = calculatedheight - 210 ;
			cropby = parseInt(cropby) ;
			alert("At 580mm (longer side), the picture must be cropped by " + cropby + "mm to make the shorter side 210mm, to fit an A4 printer.") ;
			}
			
			calculatedheight = Math.min(calculatedheight,210); // on my printer
			break;
		case "762":
			calculatedheight = Math.min(calculatedheight,508); // on 30 x 20"
			break;
		case "914":
			if (calculatedheight > 305) {
			cropby = calculatedheight - 305 ;
			cropby = parseInt(cropby) ;
			alert("At 914mm (longer side), the picture must be cropped by " + cropby + "mm to make the shorter side 305mm (to fit 36 x 12 inches).") ;
			}
			
			calculatedheight = Math.min(calculatedheight,305); // on 36 x 12"
			break;
		default: alert("Unrecognised widthStr: " + widthStr);
		}

	calculatedheight = Math.round(calculatedheight) ;
	return calculatedheight ;
}



// use picture type (portrait, landscape, panorama) to set appropriate menu choices in the <Width> menu
function writeMenu() {
	var themenu = document.getElementById('picWidth');
	themenu.length=1;
		
	themenu.options[0] = new Option('297mm','297');
	themenu.options[1] = new Option('406mm','406');
	themenu.options[2] = new Option('580mm','580');
	themenu.options[3] = new Option('762mm','762');
	themenu.options[4] = new Option('914mm','914');
	
	calculateAll();
}


// On Checkout.html, recalculate totals, and fill in invisible (for PayPal) and visible fields
// on load and whenever qty, size, or shipping changes
//=========================CALCULATE====================
function calculateAll() {
var unitprice = 0;
	var subtotal = 0;
	var postage = 3;
	var total = 0;
	var tempname = " ";
	
// get the qty from the menu and qtytext to put in image name data
	var qtytext = "One" ;
	var qty = document.getElementById('howmany').value ;
	switch (qty) {
	case '1': qtytext = "One "   ; break;
	case '2': qtytext = "Two " 	 ; break;
	case '3': qtytext = "Three " ; break;
	case '4': qtytext = "Four "  ; break;
	case '5': qtytext = "Five "  ; break;
	default: alert("Unrecognised number (qty)");
	}

// get the unit price & size according to the value of the print width popup's text:
	var index = document.getElementById('picWidth').options.selectedIndex;	
	var unitsize = document.getElementById('picWidth').options[index].text;
	unitsize = unitsize.substring(0,3);

	switch (unitsize) {
		case '297': unitprice="12"; break; // on A4
		case '406': unitprice="25"; break; // on 16 x 10"
		case '508': unitprice="40"; break; // on 20 x 30"
		case '580': unitprice="30"; break; // on my printer
		case '762': unitprice="40"; break; // on 30 x 20"
		case '914': unitprice="45"; break; // on 36 x 12"
		}
			
	// read UK/foreign radiobutton to get postage cost 0 or 3
	var p = document.getElementById('postRB');    //forms[1]     postage is 3 if foreign radiobutton checked:
	if(p.checked) {postage=0;}

	subtotal = qty * unitprice ; // subtotal without postage
	total = subtotal + postage ; // total with postage
	
	// set the name (includes number and size), unit price, subtotal, postage, and total fields
	document.getElementById('myP').innerText 		= "\u00A3" + postage ;
	document.getElementById('picPrice').innerText 	= "\u00A3" + unitprice ;
	document.getElementById('subtotal').innerText 	= "\u00A3" + subtotal ;
	document.getElementById('total').innerText 		= "\u00A3" + total ;
	

	if (qty == "1") {
		tempname = qtytext + " borderless, " + unitsize + "mm wide print of " + picturename()  ;
	} else {
		tempname = qtytext + " borderless, " + unitsize + "mm wide prints of " + picturename()  ;
	}


	if (postage == "3") {
		tempname = tempname + ", shipping overseas."  ;
	} else {
		tempname = tempname + ", shipping to UK." ;
	}
	document.getElementById('pname').innerText = tempname ;

// display the height a print would be at this width
	var pheightmm = showpicheight(); 
	document.getElementById('calculatedheight').innerText = pheightmm + "mm";

// display the ppi at this width
	document.getElementById('ppidisplay').innerText = showppi() ;

// set the hidden fields: 'item_name' (hidden) is copied from the picture name field (visible)
	document.getElementsByName('item_name')[0].value = tempname ;
	document.getElementsByName('amount')[0].value = total ;
}