var floaty, delay, lag, currsrc;
var floaties;
function moveFloaties() {
	floaties = $('home-photo-header').getElements('div.floaty');
	delay = 0;
	lag = 20;
	var idx = 0;
	shuffle(floaties);
	for (var f in floaties) {
		floaty = $(floaties[f].id);
		currsrc = this.id;
		if (floaty==null) continue;
		
		var fx = new Fx.Morph(floaty, { 'duration': delay } );
		fx.start( { 'opacity':1 } ).chain(function() {
			this.setOptions( {
				duration: 700,
				transition: Fx.Transitions.Back.easeInOut,
				link: 'chain'
			} );
			this.callChain();
		}).chain(function() {
			var xy = where(idx++,floaties.length);
			this.start( { 'left':xy[0], 'top':xy[1] });		
		});
		delay += lag;
	}
}

var item, tableidx, zleft, zright, xoffset, zwidth, zheight, ztop, zbottom, yoffset, samples, yvar, chose, extray, extrax, xmod;
var coordtable = new Array();
var pi=3.141592653589793;
var offsets = {
	summerselect:50,
	gapyearselect:300,
	customizedselect:600
};


function where(items, total) {
	if (items==null || items<1) items=0;
	zleft = 0;
	zright = 850;
	zwidth = Math.abs(zright - zleft);
	xscale = 0.8;
	ztop = 230;
	zbottom = 500;
	zheight = Math.abs(zbottom - ztop);
	xoffset = 0+parseInt(offsets[currsrc]);
	tableidx = currsrc;
	samples = 2;
	yvar = 60;
	if (coordtable[tableidx]==null || coordtable[tableidx].length<1) {
		coordtable[tableidx] = [];
		for (x = zleft ; x <= zright ; x++) {
			y = Math.max(0, Math.cos(Math.PI*2*(x)*xscale/zwidth+xoffset) * (zheight/2) + (zheight/2)) + ztop;
			if (y>ztop) coordtable[tableidx].push([x,y]);
		}
	}
	//double up some photos
	xmod =3;
	extray = Math.floor(Math.random()*40*(items%xmod)) + 20*(items%xmod);
	extrax = Math.floor(Math.random()*40*(items%xmod)) + 20*(items%xmod);
	item = Math.max(0, Math.floor(coordtable[tableidx].length/total) * items - 1);
	if (coordtable[tableidx]==null || coordtable[tableidx].length<1) return [0,0];
	chose = coordtable[tableidx][item];
	if (chose==null) {
		//console.log('couldn\'t choose: currsrc='+currsrc+' total: '+total+' items: '+items+' item: '+item+' length: '+coordtable[tableidx].length);
		return [0,0];
	}
	var x = chose[0] + extrax;
	var y = chose[1] + Math.floor(Math.random()*(yvar)) + extray;
	return [x,y];
}

function rand(min,max) {
	var val = min+Math.floor(Math.random()*(max-min+1));
	return val;
}

function shuffle ( myArray ) {
	var i = myArray.length;
	if ( i == 0 ) return false;
	while ( --i ) {
		var j = Math.floor( Math.random() * ( i + 1 ) );
		var tempi = myArray[i];
		var tempj = myArray[j];
		myArray[i] = tempj;
		myArray[j] = tempi;
	}
}


function swapImages () {

	floaties = $('page').getElements('div.floaty');
	shuffle(floaties);

	var fx0 = floaties[0].get('tween', { property:'opacity' });
	var fx1 = floaties[1].get('tween', { property:'opacity' });
	
	fx0.setOptions({duration: 1500, link: 'chain'});
	fx1.setOptions({duration: 1500, link: 'chain'});

	fx1.start(0);
	fx0.start(0).chain( function () {

		tf = floaties[0].clone([true, true]);
		floaties[0].setStyle('zindex', floaties[1].getStyle('zindex'));
		floaties[1].setStyle('zindex', tf.getStyle('zindex'));

		image0 = floaties[0].getElement('img');
		image1 = floaties[1].getElement('img');
		tmp = image0.clone([true, true]);
		
		image0.setProperty('src', image1.getProperty('src'));
		image0.setProperty('alt', image1.getProperty('alt'));
		image0.setProperty('title', image1.getProperty('title'));
		image0.setProperty('width', null);
		image0.setProperty('height', null);
		if (image1.getProperty('height')!=null) image0.setProperty('height', '100%');
		else image0.setProperty('width', '100%');

		image1.setProperty('src', tmp.getProperty('src'));
		image1.setProperty('alt', tmp.getProperty('alt'));
		image1.setProperty('title', tmp.getProperty('title'));
		image1.setProperty('width', null);
		image1.setProperty('height', null);
		if (tmp.getProperty('height')!=null) image1.setProperty('height', '100%');
		else image1.setProperty('width', '100%');

		this.callChain();
	} ).chain( function () {
		fx0.start(1);
		fx1.start(1);
	});
	
	
	
}
//swapImages.periodical(6000);