/* 
 * Abe Yang 2/4/2006
 *
 * This file contains functions required for the main journal page
 * (mainly dropdown functionality & DOM calls)			
 * 									*/

function ShowSlideshow(nodeid) {
	Element.show('linkhide' + nodeid);
	Element.hide('linkshow' + nodeid);
	new Effect.SlideDown('slideshow' + nodeid);
	GetCurrent(nodeid);
	// ShowThumbnails(nodeid);
	// set 'isfirsttime' to false after hitting this fn for the first time
	var slideObj = eval("slideObj" + nodeid);
	if (slideObj['isfirsttime']) slideObj['isfirsttime'] = false;
}

function HideSlideshow(nodeid, duration) {
	Element.hide('linkhide' + nodeid);
	Element.show('linkshow' + nodeid);
	new Effect.SlideUp('slideshow' + nodeid, {duration:duration});
}

function GetCurrent(nodeid) {
	var slideObj = eval("slideObj" + nodeid);
	// if first time, don't do anything (fix for IE6)
	if (!slideObj['isfirsttime']) {
		var i = slideObj['index'];
		GetSlide(i, nodeid, slideObj);
	}
}

function GetNext(nodeid) {
	var slideObj = eval("slideObj" + nodeid);
	var i = slideObj['index'];
	var size = slideObj['size'];
	i = ++i % size;
	GetSlide(i, nodeid, slideObj);
}

function GetPrev(nodeid) {
	var slideObj = eval("slideObj" + nodeid);
	var i = slideObj['index'];
	var size = slideObj['size'];
	if (--i < 0) i = size - 1;	// modulo doesn't work in this case..
	GetSlide(i, nodeid, slideObj);
}

function GetByIndex(index, nodeid) {
	var slideObj = eval("slideObj" + nodeid);
	GetSlide(index, nodeid, slideObj);
}

function ShowThumbnails(nodeid) {
	Element.show('hidethumbs' + nodeid);
	Element.hide('showthumbs' + nodeid);
	var slideObj = eval("slideObj" + nodeid);
	Element.toggle('thumbgallery' + nodeid);
//	$('thumbgallery' + nodeid).style.visibility = 'visible';
	// construct thumbnails only upon hitting the fn for the first time
	if (!slideObj['evershownthumbs']) {
		var i;
		var textholder='';
		for (i=0; i<slideObj['size']; i++) {
			textholder = textholder + '<img src="' + BuildPhotoUrl(slideObj['photos'][i]['id'], '_s') + '" onclick="GetByIndex(' + i + ', ' + nodeid  + ');" />';
		}
		Element.update('longstrip' + nodeid, textholder);
		slideObj['evershownthumbs'] = true;
	}
//	alert(Element.getStyle('thumbgallery' + nodeid,'overflow'));
	$('track' + nodeid).style.visibility = 'visible';
}

function HideThumbnails(nodeid) {
	Element.hide('hidethumbs' + nodeid);
	Element.show('showthumbs' + nodeid);
//	new Effect.Fade('thumbgallery' + nodeid);
//	new Effect.SlideUp('thumbgallery' + nodeid);
	Element.toggle('thumbgallery' + nodeid);
//	$('thumbgallery' + nodeid).style.visibility = 'hidden';
	$('track' + nodeid).style.visibility = 'hidden';
}

function ShowLargeImage(nodeid) {
	var slideObj = eval('slideObj' + nodeid);
	var index = slideObj['index'];
	var link = BuildPhotoUrl(slideObj['photos'][index]['id'], '_o');
/*	var w = slideObj['photos'][index]['maxwidth'];
	var h = slideObj['photos'][index]['maxheight'];*/
//	window.open(link,'image'+nodeid+index,'resizable=0,status=0,menubar=0,toolbar=0,scrollbars=0,location=0,directories=0,width='+w+',height='+h);
	window.open(link,'image'+nodeid+index,'resizable=1,status=0,menubar=0,toolbar=0,scrollbars=0,location=0,directories=0');
}

// toggle visibility (NOT display)
function ToggleViz (node) {
	node = $(node);
	node.style.visibility = (node.style.visibility == "hidden") ? "visible" : "hidden";
}

// Generic function that fetches a particular slideshow obj, given the index
// This should be treated as a "private" method; should not be called from the outside
function GetSlide(index, nodeid, slideObj) {
	slideObj['index'] = index;
	var link = BuildPhotoUrl(slideObj['photos'][index]['id'], '');
//	Element.update('currentphoto' + nodeid, index);
//	Element.update('image' + nodeid, '<img src="' + link + '" alt="" />');
	$('currentphoto' + nodeid).innerHTML = index + 1;
	$('image' + nodeid).innerHTML = '<img src="' + link + '" alt="" />';
	$('caption' + nodeid).innerHTML = (slideObj['photos'][index]['caption'] == '') ? '<p>&nbsp;</p>' : '<p>' + slideObj['photos'][index]['caption'] + '</p>';
}

// Function that constructs a photo's Flickr URL
function BuildPhotoUrl (photoid, append) {
	var photo_url = "http://static.flickr.com/";
	return photo_url + photoid + append + '.jpg';
}

// function maps thumbnail scroller to thumbnail "longstrip"
function ScrolltoPos(id, v) {
     $(id).style.left = (-v) + 'px';
}

