function picsort(t) {
    new Ajax.Request('pictool.php?op=sort&e='+$('id').value+'&t='+t+'&'+Sortable.serialize('piclist'));
}

function addpic(t) {
    new Ajax.Updater('addpic', 'pictool.php?op=add&t='+t);
    $('addpic').style.display = 'block';
}

function sprm(t,p) {
    var e = $('id');
    var id = 0;
    if (e) {
	id = e.value;
    }
    new Ajax.Request('pictool.php?op=del&t='+t+'&p='+p+'&e='+id);
    $('pic_'+p).style.display = 'none';
}

function apsearch(t) {
    new Ajax.Updater('addpic', 'pictool.php?op=search&t='+t+'&kw='+$('apkw').value+'&desc='+$('desc').value+'&p='+$F('property')+'&ph='+$F('photographer'));
}

function apadd(t,p) {
    var e = $('id');
    var id = 0;
    if (e) {
	id = e.value;
    }
    $('addpic').style.display = 'none';
    if (t == 'd') {
	$('picture').value = p;
	$('piclist').innerHTML = '';
    }
    new Ajax.Updater('piclist', 'pictool.php?op=add2&t='+t+'&p='+p+'&e='+id, {insertion: Insertion.Bottom, onComplete:function(){ Sortable.create('piclist', {tag:"div",only:'picbox',constraint:'horizontal',onUpdate:function(){picsort(t);}}); }});
}

var _slide = 1;
var _lastslide = 0;
function slideshow() {
    var e, e2;
    if (_lastslide) {
      e = $('slideimg0');
      d = $('slidediv0');
      d2 = $('slidediv1');
    } else {
      e = $('slideimg1');
      d = $('slidediv1');
      d2 = $('slidediv0');
    }
    _lastslide = 1-_lastslide;
    d.style.opacity = 0;
    d.style.filter = "alpha(opacity = 0)";
    e.src = slides[_slide];
    d2.style.zIndex = 1;
    d.style.zIndex = 2;
    new Effect.Opacity(d, { from: 0.0, to: 1.0, duration: 2.0 });
    new Effect.Opacity(d2, { from: 1.0, to: 0.0, duration: 2.0 });
    _slide++;
    if (_slide >= slides.length) {
	_slide = 0;
    }
    setTimeout('slideshow()', 4*1000);
}

var tw1 = null;
var tw2 = null;
var twbusy = 0;
var twwidth = 812;
var twp = 0;
var twtim = null;

function picmore(adj, i) {
    if (twbusy) { return; }
    twbusy = 1;
    if (!tw1) {
	tw1 = $('thumb1');
	tw2 = $('thumb2');
    }
    new Ajax.Updater(tw2, '/thumbnails.php?i='+i+'&a='+adj, {onComplete: function() {
		if (adj > 0) {
		    tw2.style.left = ''+twwidth+'px';
		    offset = -twwidth;
		} else {
		    tw2.style.left = '-'+twwidth+'px';
		    offset = twwidth;
		}
		new Effect.Move(tw1, { x:offset, y:0, mode:'relative', transition: Effect.Transitions.sinoidal, afterFinish: function() { twbusy=0; }});
		new Effect.Move(tw2, { x:offset, y:0, mode:'relative', transition: Effect.Transitions.sinoidal});
		x = tw1;
		tw1 = tw2;
		tw2 = x;
	    }});
}


var _lastprop = 0;
function showtnpic(i,c,p) {
    $('displaypic').src = "/files/pic"+i+"-l.jpg";
    $('caption').innerHTML = c;
    if (p > 0) {
	if (p != _lastprop) {
	    _lastprop = p;
	    new Ajax.Updater('propdetail', '/propdetail.php?p='+p);
	}
    } else {
	_lastprop = 0;
	$('propdetail').innerHTML = '';
    }
    if (twtim) { clearTimeout(twtim); }
    twtim = setTimeout('gallerynext()', 7*1000);
}

function gallerynext(id) {
    twp++;
    if (twp >= slides.length) { twp = 0; }
    showtnpic(slides[twp], captions[twp], properties[twp]);
}

function dosearch() {
    window.location = '/search.php?q='+escape($F('search'));
}

function domap(q) {
    window.open("/map.php?"+q, "mvptmap", "width=500,height=400");
}

function addattach(page) {
    new Ajax.Updater('addattachment','attachtool.php?op=add&p='+page);
}

function loadattach(t, page) {
    window.open('attachtool.php?op=load&t='+t+'&p='+page, 'mvptload', 'width=500,height=400');
}

function selectattach(id,name,page) {
    var s = '<div id="a_'+id+'" class=attachbox><a href="/a.php/'+id+'/'+escape(name)+'\">'+name+'</a> <span class=delattach onClick="deleteattach('+id+','+page+')">Delete</span></div>';
    $('attachlist').innerHTML += s;
    $('addattachment').innerHTML = '';
}

function deleteattach(id,page) {
    new Ajax.Request('attachtool.php?op=del&id='+id+'&p='+page);
    $('a_'+id).style.display = 'none';
}

function ppreplace(t) {
    new Ajax.Updater('addpic', 'pictool.php?op=add&t='+t);
}

function checksearch(e) {
    var characterCode;
    if (e && e.which) {
	e = e;
	characterCode = e.which;
    } else {
	e = event;
	characterCode = e.keyCode;
    }
    if (characterCode == 13) {
	dosearch();
	return(false);
    } else {
	return(true);
    }
}

function dirsort() {
    new Ajax.Request('directory.php?op=sort&p='+$('id').value+'&'+Sortable.serialize('diritems'));
}


