/*
 * $Id: java.js,v 1.8.2.2 2010/05/25 17:17:27 ord Exp $
 */

function showimage(url, width)
{
	var popup = null;
	height=width+100;
	width=width+40;
	if (popup!=null && !popup.closed)
		popup.close();
	popup = window.open(url, 'photobox_popup','width='+width+',height='+height+',resizable,scrollbars');
	if(window.focus)
		popup.focus();
}

function showimage2(url, name, parms) {
 var popup = null;
  if (popup!=null && !popup.closed) popup.close();
   popup = window.open(url, name, parms);
   popup.focus();
}

/*
 * Convert external links to open in a new window.
 */
function externalLinks()
{ 
	if (!document.getElementsByTagName) return; 
	var anchors = document.getElementsByTagName("a"); 
	// alert(anchors + anchors.length);
	for (var i=0; i<anchors.length; i++) { 
		var anchor = anchors[i]; 
		if (anchor.getAttribute("href") && 
			anchor.getAttribute("rel") == "external") 
		{
			anchor.target = "_blank"; 
		}
	} 
} 

/*
 * Javascript viewing.
 *
 * The following variables are previously defined (in XSL output)
 *  album
 *  page
 *  res
 */

function get_photo_url(photo_id)
{
	return(
		"javascript:" + 
			"display_photo('../../thumb.php?album="+album+"&page="+page+"&photo="+photo_id+"&mode=photo&w="+res+"','"+photo_id+"');");
}

function convert_amp(s)
{
	s = s.replace(/&amp;/g, "&");
	return(s);
}

/*
 * Returns the title for an image specified by filename
 */
function getTitle(filename)
{
	var i;

	for(i = 0; i < pics.length; i++)
	{
		if(pics[i][0] == filename)
			return(convert_amp(pics[i][1]));
	}
	return("");
}

/*
 * Returns the description for an image specified by filename
 */
function getDesc(filename)
{
	var i;

	for(i = 0; i < pics.length; i++)
	{
		if(pics[i][0] == filename)
			return(convert_amp(pics[i][2]));
	}
	return("");
}

/*
 * Sets the photo title to title
 */
function setTitle(title)
{
	var s = document.getElementById("photo_title");
	var oldText = s.childNodes;
	var newText = document.createTextNode(title);

	if(oldText && oldText[0])
	{
		oldText[0].data = title;
	} else {
		s.appendChild(newText);
	}
}

/*
 * Sets the photo description to desc
 */
function setDesc(desc)
{
	var s = document.getElementById("photo_desc");
	var oldText = s.childNodes;
	var newText = document.createTextNode(desc);

	if(oldText && oldText[0])
	{
		oldText[0].data = desc;
	} else {
		s.appendChild(newText);
	}
}

/*
 * Sets the photo filename to id
 */
function setFilename(photoid)
{
	var s = document.getElementById("photoid");
	var oldText = s.childNodes;
	var newText = document.createTextNode(photoid);

	if(oldText && oldText[0])
	{
		oldText[0].data = photoid;
	} else {
		s.appendChild(newText);
	}
}

/*
 * Returns previous photo id, if exists, or empty string.
 */
function getPrevPhotoId(photoid)
{
	var i;

	for(i = 0; i < pics.length; i++)
	{
		if(pics[i][0] == photoid && i > 0)
			return(pics[i - 1][0]);
	}
	return('');
}

/*
 * Returns next photo id, if exists, or empty string.
 */
function getNextPhotoId(photoid)
{
	var i;

	for(i = 0; i < pics.length; i++)
	{
		if(pics[i][0] == photoid && i + 1 < pics.length)
			return(pics[i + 1][0]);
	}
	return('');
}

/*
 * Load / displays previous photo
 */
function display_prev_photo(currid)
{
	var previd = getPrevPhotoId(currid);
	display_photo(get_photo_url(previd), previd); 
}

/*
 * Load / displays next photo
 */
function display_next_photo(currid)
{
	var nextid = getNextPhotoId(currid);
	display_photo(get_photo_url(nextid), nextid); 
}

/*
 * Displays a photo specified by  url / photoid.
 */
function display_photo(url, photoid)
{
	/* Set mainphoto src to the new url */
	var img = document.images.mainphoto;
	var s = document.getElementById("photo_title");

	setTitle("Loading, please wait...");
	img.src=url;
	//while(!document.images.mainphoto.complete)
	while (0 && ! img.complete /*&&
	       (typeof img.naturalWidth == "undefined" || img.naturalWidth == 0) */)
	{
		//sleep(1);
	}
	// alert("Finished loading");
	//setTitle(title);
	/* Set the title and description for photo */
	setTitle(getTitle(photoid));
	setDesc(getDesc(photoid));
	setFilename(photoid);

	/* Update links for previous and next photos */
	var prev = document.getElementById("photoprev");
	var prevlink = getPrevPhotoId(photoid);
	var next = document.getElementById("photonext");
	var nextlink = getNextPhotoId(photoid);
	if(prevlink != '')
		prev.href = "javascript:display_prev_photo('"+photoid+"')";
	if(nextlink != '')
		next.href = "javascript:display_next_photo('"+photoid+"')";
	else
		next.href = 'javascript:void(0)';

	/* Update links for as popup */
	var popup_link = document.getElementById("popup-link");
	popup_link.href = "javascript:showimage('../../popup.php?album="+album+"&page="+page+"&photo="+photoid+"&popup=1',"+res+")";

	/* Update link for as photo / main page */
	var photo_link = document.getElementById("photo-link");
	photo_link.href = photoid+".html";
	
}

var slideshow_timer = null;
function slideshow_update(supto)
{
	var freq=5;
	var ss = document.getElementById("start_slideshow");
	if(supto < pics.length)
	{
		i = supto;
		id=pics[supto][0];
		url='../../thumb.php?album='+album+'&page='+page+'&photo='+id+'&w='+res+'&mode=photo';
		display_photo(url,id);
		var j=supto+1;
		slideshow_timer = setTimeout("slideshow_update("+j+");",(freq*1000) * (1));
	} else {
		slideshow_timer = null;
		ss.title = "Start Slideshow";
	}
	supto=200;
}

function do_slideshow()
{
	var ss = document.getElementById("start_slideshow");
	if(slideshow_timer != null)
	{
		clearTimeout(slideshow_timer);
		ss.title = "Start Slideshow";
	} else {
		ss.title = "Stop Slideshow";
		slideshow_update(0);
	}
}

/* Toggles an input box with photobox markup representation of photo
 */
function toggleTag(id, source, text)
{
	var eNew; 

	if( ( eNew = document.getElementById( id ) ) )
	{
		eNew.parentNode.removeChild( eNew );
	}
	else
	{
		eNew = document.createElement("input");
		eNew.setAttribute("id", id);
		eNew.setAttribute("type", "text");
		eNew.setAttribute("value", text );
		eNew.setAttribute("class", "copytag" );
		source.parentNode.parentNode.appendChild(eNew);
		eNew.select();
	}
}

/********************
 * To call different things as startup, we need to queue them,
 * and then execute them.
 * Add anything to start onload use addOnLoad(fn)
 *
 * This is ripped from weblog, and we don't need to do the addOnLoad
 * stuff, but it's good to have incase we ever do.
 */

var onLoads = new Array();
//var doc = document;

/* Adds a function to load at start
 *
 */
function addOnLoad(f)
{
	onLoads.push(f);
}

/*
 * Execute functions on start.
 */
function startOnLoads()
{
	for(i=0; i<onLoads.length; i++) {
		//alert(onLoads[i]);
		var func = onLoads[i];
		func();
		// alert(func);
	}
}

addOnLoad(externalLinks);
window.onload = startOnLoads;
