var mogrifying = false;
var galleryWidth = 640;
var galleryHeight = 420;

function boot() {
    for (var i = 0; i < gallery.length; i++) {
		var ele = document.getElementById('person' + i);
		ele.onclick = new Function('showPictures(' + i + ')');
		ele.onmouseover = new Function('waveOn(' + i + ')');
		ele.onmouseout = new Function('waveOff(' + i + ')');
		window.setTimeout(new Function('waveOnOff(' + i + ')'), Math.random() * 2000);
    }		
}

function waveOnOff(i) {
    waveOn(i, true);
    window.setTimeout(function() { waveOff(i) }, Math.random() * 1000);
}    

function waveOn(i, ignore) {
    if (!ignore) $('status').innerHTML = gallery[i].title;
    $('person' + i).style.backgroundImage = 'url("people/' + i + '.jpg")';
}

function waveOff(i) {
	$('status').innerHTML = '';
    $('person' + i).style.backgroundImage = 'none';
}

function showGallery() {
    if (mogrifying) return;
	window.setTimeout(function(){
		$('overlay').style.display = 'block';
	    $('gallery').style.display = 'block';
		var gc = $('gallery_content');
		if (gc) {
			mogrifying = true;
			gc.style.display = 'none';
			tickGallery('in', 0);
		} else {
			$('gallery').style.height = "" + galleryHeight + "px";
		}
	}, 100);
}

function hideGallery() {
    if (mogrifying) return;
    mogrifying = true;
    $('gallery_content').style.display = 'none';
    tickGallery('out', 1);
}

function tickGallery(direction, progress) {
    if (progress < 0) progress = 0;
    if (progress > 1) progress = 1;
	quad_prog = progress * progress;
    $('gallery').style.height = "" + (quad_prog * galleryHeight) + "px";
    $('overlay').style.opacity = quad_prog / 2;
    $('overlay').style.filter = "alpha(opacity=" + (quad_prog * 50) + ")";
    if (direction == 'in') {
        if (progress < 1) {
            window.setTimeout("tickGallery('in'," + (progress + 0.05) + ")", 50);
        } else {
            mogrifying = false;
            $('gallery_content').style.display = 'block';
        }
    } else if (direction == 'out') {
        if (progress == 0) {
            mogrifying = false;
            $('overlay').style.display = 'none';
            $('gallery').style.display = 'none';
        } else {
            window.setTimeout("tickGallery('out'," + (progress - 0.05) + ")", 50);
        }
    }
}

function showPictures(i) {
	waveOff(i);
	new Ajax.Updater("gallery", "partials/gallery.php?id=" + i, { onSuccess: function() { showGallery(); }});
}

function showInformation() {
	new Ajax.Updater("gallery", "partials/about_me.php", { onSuccess: function() { showGallery(); }});
}

function switchPage(page, id) {
    url = "partials/" + page + ".php";
    if (id) url += "?id=" + id
	new Ajax.Updater("gallery", url);
}

function toEx(id) { switchPage('exhibition', id); }
