var LbWait = 0;
var LbTog = {};

function lb_change() {
	var lbs = $('lb_select');
	var id = lbs.value;
	var url = 'lbset.php?cmd=setlb&id='+id;

	// clear the old lightbox checks
	var m = PgMedia;
	for (var i in m) {
		id = m[i];
		var s = $('vss_lightbox_image'+id);
		if (s)
			s.innerHTML = "<a href='javascript:lbtog(" + id + ", 1)'><img src='./images/add_lightbox.gif' border='0' alt='Add to Lightbox' title='Add to Lightbox'></a>";
	}

	// blank the count box
	var lbc = $('lbcount');
	lbc.value = '';

	dbg2(lbs);
	dbg2(lbc);

	// disable the select box & count box
	lbs.disabled = lbc.disabled = true;

	LbWait = 1;

	makeReq(url,'update_lb');
	// get the ID of the new lightbox, send XHR request
}

// Note: Currently clearing all, then setting the boxes.
//  could do clear/set in one pass, but then need to copy LbMedia

// Todo:  can optimize this by using assoc array of ids for LbMedia/PgMedia, 
//   and only resetting those in LbMedia and PgMedia
function update_lb(d) {

	// enable select/count boxes
	var lbs = $('lb_select');
	var lbc = $('lbcount');
	lbs.disabled = lbc.disabled = false;

	// set new checkboxes
	eval(d);
	for (var i in LbMedia) {
		id = LbMedia[i];
		var s = $('vss_lightbox_image'+id);
		if (s)
			s.innerHTML = "<a href='javascript:lbtog(" + id + ",0)'><img src='./images/rem_lightbox.gif' border='0' alt='Remove from Lightbox' title='Remove from Lightbox'></a>";

	}

	// update image count
	$('lbcount').value = LbMedia.length;

	LbWait = 0;
}

function lb_upd(d) {
	eval("var m = " + d);
	var s = $('vss_lightbox_image'+m.id);

	// update checkbox
	if (s) {

	if (m.set) {
		s.innerHTML = "<a href='javascript:lbtog(" + m.id + ",0)'><img src='./images/rem_lightbox.gif' border='0' alt='Remove from Lightbox' title='Remove from Lightbox'></a>";
	} else {
		s.innerHTML = "<a href='javascript:lbtog(" + m.id + ",1)'><img src='./images/add_lightbox.gif' border='0' alt='Add to Lightbox' title='Add to Lightbox'></a>";
	}
	} else {
		alert("Error finding image in page");
	}
	LbTog[m.id] = 0;

	// update lightbox count
	lbc = $('lbcount');
	if (lbc) lbc.value = m.total;

}

function lbtog(id,mode) {
	if (LbWait || LbTog[id]) return;
	LbTog[id] = 1;
	var url = 'lbset.php?cmd=lbtog&id=' + id + '&set=' + mode;
	makeReq(url,'lb_upd');
}

function lba_upd() {
	var n = $('lb_action').value;
	switch (n) {
		case '0':
			$('lb_id').style.display = 'none';
			$('lb_name').style.display = 'inline';
			break;
		case '1':
			$('lb_id').style.display = 'inline';
			$('lb_name').style.display = 'none';
			break;
	}
}

function setLb() {
	var id=$('lb_active').value;
	location.href = 'lbview.php?cmd=setlb&id=' + id;
}

function delete_lightbox() {
	if (confirm("Are you sure you want to delete the current lightbox?")) {
		location.href = 'lbview.php?cmd=delete';
	}
}

// BOZO:  instead of IDs, etc, could just use position in DOM ...
function subCatMenus() {
	// expand/collapse all links
	var l = $('catmenus');
	d = ce('div');
	d.id = 'menuctl';
	d.innerHTML = '<a href="javascript:expandAll()">[Expand All]</a> <a href="javascript:collapseAll()">[Collapse All]</a>';
	l.parentNode.insertBefore(d,l);

	// sub menus
	for (var id in CatList) {
		var li = $('cat_'+id);
		if (li) {
			d = ce('div');
			var cl = CatList[id];
			var url = Cats[id][0];
			// add the arrow
			var i = new Image();
			i.id = 'but'+id;
			i.src = '/images/dn2.gif';
			i.title = 'Expand Category';
			li.appendChild(i);
			i.onclick = eval('new Function("mtog(' + id + ')")');
			var h = '<ul class=scbox>';
			for (var k in cl) {
				var n = cl[k];
				var c = SubCats[n];
				//if (c) h += '<li><a class=catlink style="width: 150px" id=cl_'+ n + ' href="index.php?category=' + url + '/' + c[1] + '">' + c[0] + '</a></li>';
				if (c) h += '<li><a class=catlink style="width: 150px" id=cl_'+ id + '_' + n + ' href="index.php?category=' + c[1] + '">' + c[0] + '</a></li>';
			}
			h += '</ul>';
			d.innerHTML = h;
			d.id = 'subm'+id;
			d.style.display = 'none';
			li.appendChild(d);
		}
	}
}
function mtog(n) {
	var b = $('but'+n);
	var m = $('subm'+n);
	var o = OpenMenus[n];
	if (o) {
		b.src = '/images/dn2.gif';
		m.style.display = 'none';
		b.title = 'Expand Category';
	} else {
		b.src = '/images/up2.gif';
		m.style.display = 'block';
		b.title = 'Collapse Category';
	}
	OpenMenus[n] = (o) ? 0 : 1;
}

function expandAll() {
	for (var id in CatList) 
		if (!OpenMenus[id]) mtog(id);
}
function collapseAll() {
	for (var id in CatList) 
		if (OpenMenus[id]) mtog(id);
}
function selCat(n,s) {
	if (Cats[n]) mtog(n);
	if (s) id = 'cl_' + n + '_' + s;
	else id = 'cl_' + n;
	b = $(id);
	if (b) b.className = 'selcat';	
}
