var cake_win;

function cake_popup(id, width, height)
{
	cake_win = window.open('view.php?id='+id, 'cake_popup', 'width='+width+',height='+height+',resizable=yes,scrollbars=yes');
	cake_win.focus();
	return false;
}

function cake_over()
{
	window.status = 'View full-size image';
	return true;
}

function cake_out()
{
	window.status = '';
	return true;
}



$ = YAHOO.util.Dom.get;

YAHOO.util.Event.on(window, 'load', function() {
		var ImageLoader = function(url)
		{
			this.image = null;
			this.loaded = false;
			this.image = document.createElement('img');
			var obj = this;
			YAHOO.util.Event.on(this.image, 'load', function(e){ obj.loaded = true }, false);
			this.image.src = url;
		};

		if ($('rotate_container'))
		{
			var delay = 3500;
			var current_img = 1;
			var loaded = 1; // first two images are loaded at this point (last loaded image is index 1)
			var img1 = $('img1');
			var img2 = $('img2');
			var fore = img1;
			var back = img2;
			var loader = null;

			var dim = [];
			dim[0] = { width: img1.width, height: img1.height };
			dim[1] = { width: img2.width, height: img2.height };

			window.setTimeout(rotate, delay);
		}

		function rotate()
		{
			if (loader)
			{
				if (loader.loaded)
				{
					dim[current_img] = { width: loader.image.width, height: loader.image.height }
					back.width = loader.image.width;
					back.height = loader.image.height;
					back.src = loader.image.src;
					loaded++;
					loader = null;
				}
				else
				{
					window.setTimeout(rotate, 250);
					return;
				}
			}

			YAHOO.util.Dom.setStyle(back, 'opacity', '0');
			YAHOO.util.Dom.setStyle(back, 'display', 'block');

			var hide = new YAHOO.util.Anim(fore, { opacity: { from: 1, to: 0 } }, 0.75);
			var show = new YAHOO.util.Anim(back, { opacity: { from: 0, to: 1 } }, 0.75);

			hide.onComplete.subscribe( function(type, args)	{
				current_img = (current_img == rotate_images.length-1) ? 0 : current_img+1;

				var temp = fore;
				fore = back;
				back = temp;

				YAHOO.util.Dom.setStyle(back, 'display', 'none');
				check_preload();
				window.setTimeout(rotate, delay);
			} );

			hide.animate();
			show.animate();
		}

		function check_preload()
		{
			var path = "images/main/" + rotate_images[current_img];

			if (loaded >= current_img)
			{
				back.width = dim[current_img].width;
				back.height = dim[current_img].height;
				back.src = path;
				loader = null;
			}
			else
				loader = new ImageLoader(path);
		}
	});
