var screenx=640;
function displaybanner(w) {
	if (w < 160) {
		screenx=0;
		document.writeln('<h5>Kyoikenkai Karate Club</h5>');
		return;
	} else if (w < 240) {
		screenx=160;
	} else if (w < 320) {
		screenx=240;
	} else if (w < 480) {
		screenx=320;
	} else if (w < 640) {
		screenx=480;
	}
	document.writeln('<img id="toplogo" width="'+screenx+'" src="images/banner'+screenx+'.png" class="newblock">');
}

function displaymenu() {
	document.writeln('<a name="top" /><div class="small">');
	document.writeln('<a href="http://www.hardykarate.org.uk/home.html">Home</a>&nbsp;');
	document.writeln('<a href="http://www.hardykarate.org.uk/instructors.html">Instructors</a>&nbsp;');
	document.writeln('<a href="http://www.hardykarate.org.uk/syllabus.html">Syllabus</a>&nbsp;');
	document.writeln('<a href="http://www.hardykarate.org.uk/events.html">News &amp; Events</a>&nbsp;');
	//document.writeln('<a id="gallerylink" href="http://www.hardykarate.org.uk/gallerylist.php?w='+screenx+'">Gallery</a>&nbsp;');
	document.writeln('<a id="gallerylink" href="http://m.flickr.com/photos/hardykarate/collections/">Gallery</a>&nbsp;');
	document.writeln('<a href="http://www.hardykarate.org.uk/testimonals.html">Testimonals</a>&nbsp;');
	document.writeln('<a href="http://www.hardykarate.org.uk/details.html">Details</a>&nbsp;');
	document.writeln('<a href="http://www.hardykarate.org.uk/contacts.html">Contacts</a></div><br />');
}

function eliminatemargin() {
	document.writeln('<style type="text/css">body{margin:0px;padding:0}p{margin:0 0 1em 0}');
	document.writeln('h1{margin:1px;font-size:1.2em;}');
	document.writeln('h2{margin:1px;font-size:1.1em;}');
	document.writeln('.pconly{display:none;visibility:hidden}');
	document.writeln('.mobonly{display:block;visibility:visible}');
	if (is_iphone) {
		document.writeln('.small{font-size:15pt;line-height:1em;}');
	}
	document.writeln('</style>');
}

function handheldimages() {
	var imglist = document.body.getElementsByTagName('img');
	// skip 1st image which will be the logo, for each of the rest do loop
    for (var i=1;i<imglist.length;i++) {
		var fsrc = imglist[i].src;
        if (fsrc.indexOf('http://www.hardykarate.org.uk/images/')==0) {
			// SRC was pointing to current domain in image directory
            imglist[i].src=fsrc.replace(/images/i,'handheld/images');
		}
	}
}

function UseIphoneGallery() {
	var anchorlist = document.body.getElementsByTagName('a');
	is_landscape=(window.orientation==-90 || window.orientation==90);
	screenx=(is_landscape)?480:320;
    for (var i=0;i<anchorlist.length;i++) {
        if (anchorlist[i].host=='odbc3.fotopic.net') {
			// SRC was pointing to a gallery, so change it to go via my PHP
            anchorlist[i].href='/displayalbum.php?a='+anchorlist[i].pathname+'&w='+screenx;
		}
	}
}

// typically only used by iphone, but perhaps later by N95
function updateOrientation() {
	is_landscape=(window.orientation==-90 || window.orientation==90);
	screenx=(is_landscape)?480:320;
	var topimg = document.getElementById('toplogo');
	topimg.src='images/banner'+screenx+'.png';
	topimg.width=screenx;
	window.scrollTo(0, 1);
	//var gallerylink = document.getElementById('gallerylink');
	//gallerylink.setAttribute('href','http://www.hardykarate.org.uk/gallerylist.php?w='+screenx);
}

function iphone_init() {
	window.scrollTo(0, 1);
	if (location.pathname!="/displayalbum.php"){	// links on this page are to individual pics
		//UseIphoneGallery();	// all other gallery links should be redirected
	}
}

var ismobile=false;
var is_iphone=false;
var is_landscape=false;
if (!parent || !parent.frames || parent.frames.length == 0) {
	// no frameset so page is standalone. Now display banner and menu
	ismobile=true;
	is_iphone = (navigator.userAgent.match(/iPhone;/) || navigator.userAgent.match(/iPod;/));
	eliminatemargin(); // makes top banner fit snug, and force switch to showing mobile only stuff
	var w=screen.width;
	if (is_iphone) {
		is_landscape=(window.orientation==-90 || window.orientation==90);
		w=(is_landscape)?480:320;
		window.onorientationchange=updateOrientation;
		setTimeout(iphone_init, 800);
	}
	displaybanner(w);
	displaymenu();
}
