
var px = document.all ? 0 : "px";

function addScript(src) {
	var _s = document.createElement("script");
	_s.src= src;
	document.body.appendChild(_s);
}
addScript("http://us.js2.yimg.com/us.js.yimg.com/lib/common/utils/position_1.1.0.js");
addScript("http://us.js2.yimg.com/us.js.yimg.com/lib/common/utils/animation_1.1.0.js");
addScript("http://us.js2.yimg.com/us.js.yimg.com/lib/common/utils/event_0.9.1.js");

var flickrImages = [];

function onLoaded() { 
	var images = document.getElementsByTagName("img");

	for(var i = 0; i < images.length; i++)
	{
		var img = images[i];
		if(img.id.match(/photo_[0-9]*/))
		{
			flickrImages[flickrImages.length] = img;
			var pos = ygPos.getPos(img);

			img.style.left = pos[0] + px;
			img.style.top = pos[1] + px;
			
		}
	}

	for(var i = 0; i < flickrImages.length; i++)
	{
		var img = flickrImages[i];
		img.style.position = 'absolute';
		img.style.zIndex = 10;
	}
}

function animateRandom() {
	var j = Math.floor(Math.random() * (flickrImages.length+1));
	var k = Math.floor(Math.random() * (flickrImages.length+1));

	if(j == k)
	{
		return;
	}

	var img0 = flickrImages[j];
	var img1 = flickrImages[k];

	var end0 = ygPos.getPos(img1);
	var end1 = ygPos.getPos(img0);

	var mid0 = [(end0[0] + end1[0])/2 - 150,
				(end0[1] + end1[1])/2 - 100] ;
	var mid1 = [(end0[0] + end1[0])/2 + 150,
				(end0[1] + end1[1])/2 + 100];
	var tween0 = [mid0, end0];
	var tween1 = [mid1, end1];
	
    var oAnim0 = new ygAnim_Move(img0, 1, tween0);	
	var oAnim1 = new ygAnim_Move(img1, 1, tween1);
	
	img0.style.zIndex = 42;
	img1.style.zIndex = 42;

	oAnim0.animate();
	oAnim1.animate();
	oAnim0.onComplete = function ()
						{
							img0.style.zIndex = 10;
						}
	oAnim1.onComplete = function ()
						{
							img1.style.zIndex = 10;
						}
};


/* nicked off Scott Andrew's example */
function addEvent(elm, evType, fn, useCapture) 
{
	if (elm.addEventListener) 
	{
		elm.addEventListener(evType, fn, useCapture);
			return true;
	}
	else if (elm.attachEvent) 
	{
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else if(elm != window) 
	{
		elm['on' + evType] = fn;
	}
}

addEvent(window, "load", function () { 
	onLoaded();
	animateRandom();
	window.setInterval( function() { animateRandom(); }, 2000 );
}, true);
