// phCommon.js

var image_group_title;
var skipcaptions = false;
var gotcaption = false;
var phCaptions,phCaptionRefs,phNumCaptions;

var pageImages = new Array;
var IMG_TITLE_HEIGHT = 32;
var next_index;
var prev_index;


//------------------------------------------
// Old routines
//------------------------------------------

function WriteSongItem(songTitle, songFileName)
{
	document.writeln('<tr><td class="songListItem" align="center" nowrap>');
	if (songFileName != '')
	{
		document.writeln('<a href="'+ file_dir + songFileName+'">');
	}			
	document.writeln(songTitle);
	if (songFileName != '')
	{
		document.writeln('</a>');
		
	}
	document.writeln('</td><td width="10%" nowrap>');
	document.writeln('&nbsp;');
	document.writeln('</td></tr>');
}


function StartSongItems()
{
	document.writeln("<p align='center'><b>Click on song titles for MP3's");
	if (docroot == "/")	// means we are running from server, not file:// url
	{
		var name = file_dir + docroot + ".m3u";
		document.writeln(", or click <a href='"+ name +
			"'>HERE</a> to hear this entire Album");
	}
	document.writeln("</b></p>");
	document.writeln('<table width="90%" align="center" cellspacing="2" cellpadding="2" border="0" cols="2">');
}


function EndSongItems()
{	
	document.writeln('</table>');
}	


function ChartAndKML(name,title,align)
{
	var ext=".jpg";
	var found_ext = name.match(/(.*)(\..*?)$/);
	if (found_ext) 
	{
		name = found_ext[1];
		ext = found_ext[2];
	}
	
	var pi = addPageImage(name,ext,title);
	var full_root = file_dir + name;
	document.writeln("<table align='"+align+"' cellpadding='12'><tr><td align='center'>");

	// document.write("<a href='javascript:slideShow("+pi.index+")'>");
	document.write("<a href='"+docloc+"?slideShow("+pi.index+")'>");
	document.writeln("<img src='"+full_root+ext+"'></a><br>");
	document.writeln("<span class='small'>GPS Track of "+title+"</span><br>");
	document.writeln("<span class='small'>");
	document.writeln("Click on the map above for bigger version,<br>");
	document.writeln("or click <a href='"+full_root+".kml'>here</a> to view ");
	document.writeln("in <b>Google Earth</b>");
	document.writeln("</span>");
	document.writeln("</td></tr></table>");
}


//---------------------------------------------------------
// new Routines
//---------------------------------------------------------

function resolveSymbolicRef(name)
{
	var parts = name.split(/::/);
	if (parts.length > 1)
	{
		if (parts[0] == "index")
			name = root_dir + "/" + parts[1];
		else
			name = root_dir + "files/" + parts[0] + "/" + parts[1];
	}
	else	// main (non-symbolic) case
	{
		name = file_dir + name;
	}
	return name;
}


function resolveLink(linkurl)
{
	if (linkurl) linkurl = "" + linkurl; else linkurl = "";
	if ((linkurl != "") && !linkurl.match(/^(http:|mailto:)/))
	{
		if (linkurl.match(/\.html$/))
			linkurl = root_dir + "html/" + linkurl;
		else
			linkurl = resolveSymbolicRef(linkurl);
	}
	return linkurl;
}


function processCaption(caption)
{
	if (caption) caption = "" + caption; else caption = "";
	caption = caption.replace(/\[/g,"<");
	caption = caption.replace(/\]/g,">");
	return caption;
}


function phImage(name,just,caption,flags,linkurl)
	// name = symbolic reference (id::name) to other pages content
	//    otherwise, root filename of the image to display.
	//    the default extension, if none provided, is jpg
	// just = has no default at this time
	// title = can contain [] bracketed html and 's
	// linkurl = http: or mailto: external reference
	//     bare name of site local html file (i.e. Jazztrip.html)
	//     bare name of a page local non-image file (something.wmv, etc)
{
	flags = "" + flags;
	var nobig = flags.match(/nobig/)?true:false;
		
	var ext=".jpg";
	var found_ext = name.match(/(.*)(\..*?)$/);
	if (found_ext) 
	{
		name = found_ext[1];
		ext = found_ext[2];
	}
	var linkref = resolveLink(linkurl);
	var full_root = resolveSymbolicRef(name);
	caption = processCaption(caption);
	
	if (linkref == "")
	{
		var pi = addPageImage(name,ext,caption);
		linkref = docloc + "?slideShow("+pi.index+")";
		if (nobig) linkref = "";
	}
	
	if (just == "center") document.writeln("<br clear='both'>");
	document.write("<table border='0' align='" + just + "'><tr><td valign='top' align='center'>");
	if (linkref != "")
		document.write("<a href='" + linkref + "'>");
	document.write("<img src='" + full_root + ext + "'");
	if (caption && caption != "")
		document.write(" alt=\"" + caption + "\"");
	document.write(" hspace='6px'");
	document.write(">");
	if (linkref != "")
		document.write("</a>");
	if (caption != "")
		document.write("<br><span class='small'>" + caption + "</span>");
	document.writeln("</td></tr></table>");
}


function phImageGroup(group,title,suppress_captions,take_regular)
{
	var gotcaption = false;
	if (suppress_captions)
		skipcaptions = true;
	else
		skipcaptions = false;
	image_group_title = title;
	phNumCaptions = 0;
	phCaptions = new Array;
	phCaptionRefs = new Array;
	document.writeln("<br clear='both'><p align='center'>");
}


function phGroupImage(name,caption,flags,linkurl)
{
	flags = "" + flags;
	var nobig = flags.match(/nobig/)?true:false;

	var ext=".jpg";
	var found_ext = name.match(/(.*)(\..*?)$/);
	if (found_ext) 
	{
		name = found_ext[1];
		ext = found_ext[2];
	}
	var linkref = resolveLink(linkurl);
	var full_root = resolveSymbolicRef(name);
	caption = processCaption(caption);
	
	if (nobig)
		linkref = "";
	else if (linkref == "")
	{
		var pi = addPageImage(name,ext,caption);
		// linkref = "javascript:slideShow("+pi.index+")";
		linkref = docloc + "?slideShow("+pi.index+")";
	}

	var use_small = "_small";
	if (flags.match(/take_regular/)) use_small = "";

	if (linkref != "")
		document.write("<a href='" + linkref + "'>");
	document.write("<img src='" + full_root + use_small + ext + "'");
	if (caption != "")
		document.write(" alt=\"" + caption + "\"");
	document.write("'>");
	if (linkref != "")
		document.writeln("</a>");

	phCaptions[phNumCaptions] = caption;	
	phCaptionRefs[phNumCaptions++] = linkref;
	if (caption != "") gotcaption = true;
}


function phEndImageGroup()
{
	document.write("<br clear='both'>");
	if (image_group_title != null && image_group_title != "")
		document.write("<span class='normal'><i>"+ image_group_title + "</i></span>");
	if (gotcaption && !skipcaptions)
	{
		document.write("<table width='85%' border='0' align='center'>");
		document.write("<tr><td align='center'><span class='small'>");
		for (var i=0; i<phNumCaptions; i++)
		{
			document.write("<a href='" + phCaptionRefs[i] + "'>");
			document.write( i+1 );
			document.write("</a>");
			document.write(".&nbsp;");
			var s = phCaptions[i];
			var p = s.indexOf(' ');
			while (p >= 0)
			{
				var t = s.substring(0,p);
				document.write(t + "&nbsp;");
				s = s.substring(p+1,s.length);
				p = s.indexOf(' ');
			}				
			if (s.length) 
			{	
				document.write(s);
				document.write("&nbsp;&nbsp; ");
			}			
		}
		document.write("</span></td></tr></table>");
		document.writeln("<br clear='both'>");
	}		
	document.writeln("</p>");
}


//------------------------------------
// slideShow support
//------------------------------------

function pageImage(name,ext,caption)
{
	this.index = pageImages.length + 1;
	this.name = name;
	this.ext = ext;
	this.caption = caption;
}


function addPageImage(name,ext,caption)
{
	var pi = new pageImage(name,ext,caption);
	pageImages.push(pi);
	return pi;
}


function sizeDiv(div,x,y,w,h)
{
	div.style.left = x + "px";
	div.style.top = y + "px";
	div.style.width = w + "px";
	div.style.height = h + "px";
}


function createDiv(id,width,top,height)
{
	var div = document.createElement("div");
	div.id = id;
	div.style.position = "absolute";
	sizeDiv(div,0,top,width,height);
	return div;
}


function createButton(label,class_name,type_data,onclick,title)
{
	var div = document.createElement("div");
	div.innerHTML = label;
	div.className = class_name;
	div.type_data = type_data;
	div.title = title;
	div.onclick = onclick;
	return div;
}


function slideShow(index)
{
	var body = document.body; 
	body.style.overflow = "hidden";
	 
	var body_width = body.clientWidth;
	var body_height = body.clientHeight;

	var img_title = createDiv('img_title',body_width,0,IMG_TITLE_HEIGHT);
	img_title.innerHTML = "&nbsp;&nbsp;"+document.title + "<br><span id='img_title_text'></span>";
	img_title.appendChild(createButton(
		"X","button_img_close","",closeSlideShow,
		"Close picture and go BACK to the previous page"));
	if (pageImages.length > 1)
	{
		img_title.appendChild(createButton(
			"&lt;","button_img_prev","",prevSlide,
			"Go to the previous picture"));
		img_title.appendChild(createButton(
			">","button_img_next","",nextSlide,
			"Go to the next picture"));
	}		
	body.innerHTML = "";
	body.appendChild(img_title);

	var img_div = createDiv('img_div',body_width,IMG_TITLE_HEIGHT,body_height-IMG_TITLE_HEIGHT);
	img_div.resizeSlideShow = resizeSlideShow;
	img_div.style.overflow = "auto";
	body.appendChild(img_div);

	loadImage(index);
}


function loadImage(index)
{
	index = parseInt(index);
	var len = pageImages.length;
	next_index = index + 1;
	prev_index = index - 1;
	if (next_index>len) next_index = 1;
	if (prev_index<1) prev_index = len;
	var pi = pageImages[index-1];
	var img_name = file_dir + pi.name + "_big" + pi.ext;

	var img_div = document.getElementById("img_div");
	var img_title_text = document.getElementById("img_title_text");

	img_title_text.innerHTML = 
		"&nbsp; " +
		"<span class='small'>" +
		pi.name + pi.ext +
		" &nbsp; " +
		"(" + index + "/" + len + ")" +
		 " &nbsp;&nbsp; " +
		pi.caption +
		"</span>";
	
	var img = document.createElement("img");
	img.div = img_div;
	img.scaling = 1;
	img.style.position = "absolute";
	img.onclick = clickOnSlide;
	img.title = pi.caption;
	// img.style.display = "none";
	img.style.position = "absolute";
	img.className = "te_link";
	img.name = pi.name;
	img.onclick = clickOnSlide;
	img_div.image_loading = img;
	img.src = img_name; 
	setTimeout("resizeSlideShowOnLoad();",300);
}


function resizeSlideShowOnLoad()
{
	var img_div = document.getElementById("img_div");
	var img = img_div.image_loading;
	if (img.complete && 
		img.height && 
		img.width && 
		(img.height > 60) && 
		(img.width > 60))
	{
		img.org_height = img.height;
		img.org_width = img.width;
		img_div.image_loaded = img;
		img_div.resizeSlideShow();
		img_div.innerHTML = "";	
		img_div.appendChild(img);
	}
	else
		setTimeout("resizeSlideShowOnLoad()" ,300);
}


function resizeSlideShow()
{
	var img = this.image_loaded;
	if (!img) return;
	var win_width = this.offsetWidth;
	var win_height = this.offsetTop + this.offsetHeight - IMG_TITLE_HEIGHT;
	if ((win_height >= img.org_height) &&
		(win_width >= img.org_width))
	{
		img.height = img.org_height;
		img.width = img.org_width;
	}
	else 
	{
		var aspect_high = 1;
		var aspect_wide = 1;
		if (img.scaling>0)
		{
			aspect_high = win_height / img.org_height;
			aspect_wide = win_width / img.org_width;
		}
		if (aspect_high < aspect_wide)
		{
			img.height = img.org_height*aspect_high;
			img.width = img.org_width*aspect_high;
		}
		else
		{
			img.height = img.org_height*aspect_wide;
			img.width = img.org_width*aspect_wide;
		}
	}
}


function clickOnSlide()
{
	if (this.scaling>0) this.scaling=0; else this.scaling=1;
	this.div.resizeSlideShow();
}

function prevSlide()
{
	loadImage(prev_index);
}

function nextSlide()
{
	loadImage(next_index);
}

function closeSlideShow()
{
	history.go(-1);
}


function commonOnResize()
{
	var img_div = document.getElementById("img_div");
	if (img_div)
	{
		var img_title = document.getElementById("img_title");
		var body_width = document.body.clientWidth;
		var body_height = document.body.clientHeight;
		sizeDiv(img_title,0,0,body_width,IMG_TITLE_HEIGHT);
		sizeDiv(img_div,0,IMG_TITLE_HEIGHT,body_width,body_height-IMG_TITLE_HEIGHT);
		img_div.resizeSlideShow();
	}
}

onresize = commonOnResize;

