var w=800;
var h=663;
var first=true;

var anim_albums=new animate('albums','anim_albums',135);
var anim_blog=new animate('blog','anim_blog',112);
var anim_bio=new animate('bio','anim_bio',99);
var anim_photos=new animate('photos','anim_photos',195);
var anim_contact=new animate('contact','anim_contact',147);

function event(objID,type,funct) {
    e=el(objID);
    if (e.addEventListener)
		e.addEventListener(type,funct,false);
	else if (e.attachEvent)
		e.attachEvent("on"+type,funct);
}

function init() {      
	el('source').style.visibility='hidden';
//	center("loading");
//	el('loading').style.visibility='visible';
	wait();
}

function animate(name,variable,size) {
	this.name=name;
	this.scroll=0;
	this.size=size;
	this.variable=variable;
	this.speed=100;

	this.sequence_end=function(order) {
		while (order.length)
			this.order.push(order.shift());
	}

	this.sequence_force_end=function(order) {
		this.order=order;
		this.scroll=0;
	}

	this.sequence=function() {
		if (this.order[this.scroll]==null) {
			this.scroll=0;
			return;
		}

		if (this.order[this.scroll]=='!' || this.order[this.scroll]=='*')
			alert(1);

		loc='-'+(this.order[this.scroll]*this.size)+'px 0';
		el(this.name).style.backgroundPosition=loc;

		this.scroll++;
		if (this.scroll>=this.order.length)
			this.scroll=0;

		if (this.order[this.scroll]=='*') {
			for (i=0; i<=this.scroll; i++)
				this.order.shift();
			this.scroll=0;
		}

		if (this.order[this.scroll]!='!')
			this.timeout=setTimeout(this.variable+".sequence()",this.speed);
		else {
			this.scroll=0;
			this.timeout=null;
		}
	}
}

function wait() {
	images=el("source").getElementsByTagName("img");
	m=0;

	for(var i=0;i<images.length;i++) {
        if (images[i].complete)
			m++;
	}
	if (m>=images.length)
		complete();
	else {
//		bodyel('loading').style.visibility='visible';
		setTimeout("wait()", 64);
	}
}

function album_start() {
	if (anim_albums.timeout!=null)
		return;

	anim_albums.order=new Array(0,1,2,3,4,5,6,'*',5,6);
        anim_albums.sequence();
}

function album_end() {
	anim_albums.sequence_end(new Array(4,3,2,1,0,'!'));
}

function blog_start() {
        if (anim_blog.timeout!=null)
                return;

        anim_blog.order=new Array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,13,13,13);
	anim_blog.speed=200;
        anim_blog.sequence();
}

function blog_end() {
        anim_blog.sequence_force_end(new Array(0,'!'));
}

function bio_start() {
        if (anim_bio.timeout!=null)
                return;

        anim_bio.order=new Array(0,1,2,5,3,6,7,8,4,4,4);
        anim_bio.speed=200;
        anim_bio.sequence();
}

function bio_end() {
        anim_bio.sequence_force_end(new Array(0,'!'));
}

function photos_start() {
        if (anim_photos.timeout!=null)
                return;

        anim_photos.order=new Array(0,1,2,3,4,5,6,'*',7);
        anim_photos.speed=50;
        anim_photos.sequence();
}

function photos_end() {
        anim_photos.sequence_end(new Array(6,5,4,3,2,1,0,'!'));
}

function contact_start() {
        if (anim_contact.timeout!=null)
                return;

        anim_contact.order=new Array(0,1,2,3,4,5,6,7,'*',8);
        anim_contact.speed=75;
        anim_contact.sequence();
}

function contact_end() {
        anim_contact.sequence_end(new Array(7,6,5,4,3,2,1,0,'!'));
}

function complete() {
	t=viewport_height()/2-h/2-50;
	if (t<0) t=0;
    l=viewport_width()/2-w/2;
	if (l<0) l=0;
	el('source').style.top=t+'px';
	el('source').style.left=l+'px';

	event('albums','mouseover',album_start);
	event('albums','mouseout',album_end);
	event('blog','mouseover',blog_start);
	event('blog','mouseout',blog_end);
	event('bio','mouseover',bio_start);
	event('bio','mouseout',bio_end);
	event('photos','mouseover',photos_start);
	event('photos','mouseout',photos_end);
 	event('contact','mouseover',contact_start);
	event('contact','mouseout',contact_end);

//	el('loading').style.visibility='hidden';
	el('source').style.visibility='visible';
}

function resize() {
//	if (el('loading').style.visibility=='visible')
//	        center('loading');
//	else {
	        t=viewport_height()/2-h/2-20;
		if (t<0) t=0;
        	l=viewport_width()/2-w/2;
		if (l<0) l=0;
	        el('source').style.top=t+'px';
	        el('source').style.left=l+'px';
//	}
}
