// gallery.js
// 	Note: it uses slideshow.js 
//		( downloaded from http://slideshow.barelyfitz.com/ )

d = true;	// adjust slideshow debugging

var keep = new Object();
var UNDEF = 'undefined';


// --- functions to handle incoming json data from google spreadsheets

// Notes:
//	Want column names to be short, as they can sent with each cell
//	Maybe have a second row that has long names or merged cells
//	  with some help text for Joi and Dan

var gcat = new Object();
var gitems = new Object();

function handleJsonCategories(root)
{
	gcat = root;

	var arr = root.feed.entry;
	for (var i=0; i<arr.length; i++){
		var txt = arr[i].content.$t;
		var tmp = txt.split(',');
		var id = tmp[0].replace(/^ *[a-z]+: */,'');
		var title = tmp[1].replace(/^ *[a-z]+: */,'');
		new Category(id,title);

	}
	mkNavBar();

}

function mkNavBar(cntBool)
{
	if (typeof cntBool == UNDEF) cntBool = false;
	var htm = new Array();

	for (var x in keep){

		var id = keep[x].id;
		
		if (id.toLowerCase() == 'new') continue;
		var title = keep[x].title;

		if (cntBool && keep[x].arr.length == 0)
		  htm.push('<li class=empty>',title,'</li>');
		else
		  htm.push('\n<li><a href=javascript:show(this,"',id,'")',
			' onfocus=this.blur()','>',
			title,'</a></li>');
	}

	var el = document.getElementById('nav-category-list');
	if (el) el.innerHTML = htm.join('');
		

}

//var gdata = new Array();
var gitems = new Object();

function handleJsonItems(root)
{
	gitems = root;
	var arr = root.feed.entry;
	for (var i=0; i<arr.length; i++){

		var txt = arr[i].content.$t;
		var tmp = txt.split(',');

		var cols = new Array();
		for (var j=0; j<tmp.length; j++){
			cols[j] = tmp[j].replace(/^ *[a-zA-Z]+: */,'');
		}
		mkItem(cols);
	}

	mkNavBar(true);   // li or txt based on keep[x].arr.length;
}




// ----------------------------------------------------------------------



function Category(id,title)	// constructor
{
	if (typeof title == UNDEF) title = id;
if(d)console.log('category ', id, ' ', title);
	this.id = id;
	this.title = title; 
	this.arr = new Array();
	this.ss = null;
	keep[id] = this;
}


var errmsg = '';

function mkItem(col)
{
	// arr - same order as spreadsheet
	//	cat,num,img_file,title,desc,thumbnail_dir,img_dir

	// create object to hold this information
	var obj = new Object();
	cat      = col[0];
	obj.num  = col[1];
	img      = col[2];
	obj.name = col[3];
	obj.desc = col[4]; // usually just w x h
	obj.video = col[5];

	obj.tnail = 'http://www.lachausseeglass.com/all_thb/' + img;
	obj.img   = 'http://www.lachausseeglass.com/all_img/' + img;


	// put new object into as many categories as it belongs
	var arr = cat.split(/[ \n\r\t]+/);
	for (var i=0; i<arr.length; i++){

		var x = arr[i];		// i-th category
		if ( typeof keep[x] == UNDEF) {
			errmsg += 'unknown category ['+x+']\n;'
			//keep[x] = new Category(x);
		}
		else {
			keep[x].arr.push(obj);
		}
	}
}



function mk_slide(ss,obj)
{
	var s = new slide();

	s.src = obj.img;
	//s.link = obj.img;
	//s.title = '';
	s.text = '<span class=copyright>image &copy;Joi LaChausse&eacute;</span>'  
		+ '<br><span class=itemname>'+obj.name +'</span>' 
		+ '<br><span class=itemsize>'+obj.desc +'</span>' ;

	ss.add_slide(s);
}
	

function mk_slideshow(ssInit,categoryName)
{
if(d)console.log('mk_slideshow for ',categoryName);

	var cat = keep[categoryName];
	if (typeof cat == UNDEF){ 
		alert('cannot find ['+categoryName+'] category');
		return;
	}

	if (cat.ss) return cat.ss;

	cat.ss = new slideshow();

	for (var x in ssInit) cat.ss[x] = ssInit[x];


	// -- make one slide for each item
	var arr = keep[categoryName].arr;
if(d)console.log('mk_slideshow for ',categoryName,' ',arr.length,' items');
	for (var i=0; i<arr.length; i++){
		mk_slide(cat.ss,arr[i]);
	}

	return cat.ss;
	
}


function initOnLoad()
{
	showDiv('home');
}
	
	
function gallery(key)
{
	// called from left nav bar
	var win = window;
	var img_id = 'ss_img';
	var txt_id = 'ss_text';

	showDiv('gallery');
	toggleVis(win,'thumbnails','slideshow');

	// -- stop running slideshow, if any
	var el = win.document.getElementById(img_id);
	if (el.galleryObj) el.galleryObj.ss.pause();  

	galleryStartSlideShow(key,win,img_id,txt_id);

}

function toggleVis(win,from,to)
{
if(d)dbg('<li>toggleVis from '+from+ ' to ' + to);
	var doc = win.document;
	var el = doc.getElementById(from);
	if (el) el.style.display = 'none';

	var el = doc.getElementById(to);
	if (el) el.style.display = 'block';
}



function galleryStartSlideShow(key,win,img_id,txt_id)
{
	var doc = win.document;

	var obj = new Object();
	obj.prefetch=2;
	obj.playSpeed=3000;
	obj.txtEl  = doc.getElementById(txt_id);
	obj.imgEl =  doc.getElementById(img_id);

	obj.gallery = keep[key];
	obj.ss_prefix = img_id.replace(/_.*$/,'_');
	obj.ss_win = win;

	var ss = mk_slideshow(obj,key);
if(d)console.log('ss',ss);

	var el = obj.imgEl;
	if (typeof el.galleryObj == UNDEF) el.galleryObj = new Object();
	el.galleryObj.ss = ss;
	el.galleryObj.key = key;

	var el = doc.getElementById('ss_title');
	if (el) el.innerHTML = obj.gallery.title;
	
	ss.toolbarCb = function(){ toolbar_adjust(this); }
	ss.updateCb = function(){ galleryShowPosition(this); };

	//ss.start;	// always at slide 0
	ss.restart();	// at previous slide
}

function galleryStopSlideShow(key,win,img_id)
{
	var el = win.document.getElementById(img_id);
	var ss = el.galleryObj.ss;
	ss.pause();
}


function galleryShowPosition(ss)
{
	var id = ss.ss_prefix + 'progress';
	var el = ss.ss_win.document.getElementById(id);

	var i = 1 + ss.current;
	var j =     ss.slideArr.length;

	var htm = '' + i + ' of ' + j;
	if (el) el.innerHTML = htm;
}


function galleryAction(verb,win,img_id)
{
	if (typeof win == UNDEF) win =  window;
	if (typeof img_id == UNDEF) img_id =  'ss_img';

	var el = win.document.getElementById(img_id);

	if (!el) {  
		alert('error - cannot find img_id element for '+img_id);
		return;
	}
	if ( !el.galleryObj){ 
		alert('error - cannot find el.galleryObj for '+img_id);
		return;
	}

	var ss = el.galleryObj.ss;
	var key = el.galleryObj.key;
	if ( !ss){ 
		alert('error - cannot find el.galleryObj.ss for '+img_id);
		return;
	}



	if (verb == 'toggleMode')  ss.toggleMode(); // start/stop 
	else if (verb == 'faster') ss.faster();
	else if (verb == 'slower') ss.slower();
	else if (verb == 'prev')   ss.previous(); 
	else if (verb == 'next')   ss.next(); 
	else if (verb == 'all')    galleryShowThumbnails(key,win,img_id);
	else if (verb == 'DEBUG')  galleryShowAll(key,win,img_id);
}


function toolbar_adjust(ss)
{
	var doc = ss.ss_win.document;
	var pre = ss.ss_prefix;

//if(d)console.log('toolbar_adjust ss.stopped=',ss.stopped);

	var el = doc.getElementById(pre+'toggleMode');
	if (ss.stopped)
		el.innerHTML = 'resume';
	  else 	el.innerHTML = 'stop';

	var el = ss.ss_win.document.getElementById(pre+'speed');
	if (ss.stopped) 
		el.style.visibility = 'hidden';
	   else el.style.visibility = 'visible';

}



function galleryShowThumbnails(key,win,img_id)
{
	// this creates the thumbnail display for one category

	var el = win.document.getElementById(img_id);
	if (!el.galleryObj) return;

	var ss = el.galleryObj.ss;
	ss.pause();
	var currSlide = ss.current;


	var currKey = el.galleryObj.key;

	// how many columns ??
	var imax = 5; var icol = imax;

	var htm = new Array();
	htm.push('<span id=tn_hint>click any image to return to slide show</span>');
	htm.push('<table cellpadding=4 cellspacing=0 >');

	var imax = 6; var icol = imax;


	var arr = keep[currKey].arr;

if(d)console.log('mkAll for [',currKey,'] ',arr.length, ' items');
	for (var i=0; i<arr.length; i++){
		if (++icol>imax){
			icol = 1;
			htm.push('\n</tr><tr>');
		}

		// put empty a tag - will be populated by syncThumbnails below
		//htm.push('\n<td align=center valign=bottom><a><img src="',arr[i].tnail,'" border=0></a>');

		htm.push('\n<td><a><img src="',arr[i].tnail,'"></a>');

		htm.push( '</td>');
	}
	htm.push('</tr></table></div>');

	var doc = win.document;
if(d)dbg('<li>setting slideshow dpy to none');
	doc.getElementById('slideshow').style.display='none';


	var div = doc.getElementById('thumbnails');
	div.innerHTML = htm.join('');
	div.style.display = 'block';

	var arr = div.getElementsByTagName('a'); 
	for (var i=0; i<arr.length; i++){
		arr[i].href='javascript:galleryGoto(window,'+i+',"'+img_id+'")';
		if (i == currSlide){
			// set border-color on img
			arr[i].firstChild.style.borderColor = '#AAAAAA';
		}
	}

	// save ids of this slideshow
	// this will NOT work if multiple slideshows in window
	win.galleryObj = new Object();
	win.galleryObj.ss = el.galleryObj.ss;
	win.galleryObj.key = el.galleryObj.key;

}


function galleryShowAll(key,win,img_id)
{
	// this creates debug images for one category

	var el = win.document.getElementById(img_id);
	if (!el.galleryObj) return;

	var ss = el.galleryObj.ss;
	ss.pause();
	var currSlide = ss.current;


	var currKey = el.galleryObj.key;


	var htm = new Array();

	var arr = keep[currKey].arr;

if(d)console.log('mkAll for [',currKey,'] ',arr.length, ' items');
	for (var i=0; i<arr.length; i++){
		var j=i+1;
		htm.push('\n<hr>',j, '. ', arr[i].img);
		htm.push('\n<br>',arr[i].name);
		htm.push('\n  (<i>', arr[i].desc, '</i>)');
		htm.push('\n<br><img src="',arr[i].img,'">');
	}

	var doc = win.document;
if(d)dbg('<li>setting slideshow dpy to none');
	doc.getElementById('slideshow').style.display='none';


	var div = doc.getElementById('thumbnails');
	div.innerHTML = htm.join('');
	div.style.display = 'block';

	// save ids of this slideshow
	// this will NOT work if multiple slideshows in window
	win.galleryObj = new Object();
	win.galleryObj.ss = el.galleryObj.ss;
	win.galleryObj.key = el.galleryObj.key;

}




function galleryGoto(win,i,img_id)
{
	// called from click on thumbnails
	toggleVis(win,'thumbnails','slideshow');

	win.galleryObj.ss.pause();  // just in case not done before 
	win.galleryObj.ss.gotoSlide(i);
}
	


var currID = 'home';
function showDiv(id)
{
if(d)dbg('<li>setting floater to none');
	document.getElementById('floater').style.display = 'none';

	var el = document.getElementById(id);
	if (el){ 
		var el1 = document.getElementById(currID);
if(d)dbg('<li>setting ' +id+ ' to none'); 

		if (el1) el1.style.display = 'none';
		else alert('cannot hide id='+currID);

if(d)console.log(currID,'.display is ',el1.style.display);

		el.style.display = 'block';
		currID = id;
if(d)console.log(id,'.display is ',el.style.display);
	}
	else alert('cannot find '+id);

}




