var phytw = {
	duration: 250,
	init: function () {
		/* phy.tw box animation */
		$('a.box').bind(
			'mouseenter',
			function () {
				var o = this;
				$('.box').stop(true, true).each(
					function (i, s) {
						if (s !== o) {
							$(s).fadeTo(phytw.duration, 0.6);
						}
					}
				);
			}
		).bind(
			'mouseleave',
			function () {
				var o = this;
				$('.box').stop(true, true).each(
					function (i, s) {
						if (s !== o) {
							setTimeout(
								function () {
									$(s).not(':animated').fadeTo(phytw.duration, 1);
								},
								150
							);
						}
					}
				);
			}
		);
		/* right col animation */
		phytw.pic.$box = $('#picbox');
		phytw.pic.$pause = $('#pause');
		$.ajax(
			{
				dataType: 'text',
				type: 'GET',
				url: './piclist.txt',
				cache: false,
				success: function (text, status) {
					$.each(
						text.split('\n'),
						function (i, v) {
							if ($.trim(v)) {
								phytw.pic.list[phytw.pic.list.length] = v;
							}
						}
					);
					phytw.pic.list = $.shuffle(phytw.pic.list);
					phytw.pic.next();
				}
			}
		);
		if (!$.browser.msie) {
			/* the following won't fire correctly on IE */
			$(window).bind(
				'blur',
				function () {
					phytw.pic.disabled = true;
				}
			).bind(
				'focus',
				function () {
					phytw.pic.disabled = false;
				}
			).bind(
				'mouseover',
				function () {
					$(this).focus();
				}
			);
		}
	},
	pic : {
		list: [],
		disabled: false,
		aniduration: 1000,
		duration: 5000,
		next: function () {
			var num = phytw.pic.$box.children().length;
			if (num < phytw.pic.list.length) {
				/* prepend new images */
				phytw.pic.$box.append(
					$(document.createElement('img'))
					.hide()
					.bind(
						'load',
						phytw.pic.showpic
					)
				);
				/* if onload fails */
				phytw.pic.timer = setTimeout(
					function () {
						/* remove this url from list */
						//phytw.pic.list = phytw.pic.list.slice(0, num).concat(phytw.pic.list.slice(num+1, phytw.pic.list.length));
						/* remove this <img> */
						//phytw.pic.$box.children(':last').remove();
						/* remove onload binding */
						phytw.pic.$box.children(':last').unbind('load');
						/* next */
						phytw.pic.next();
					},
					1500
				);
				/* assign src to invoke onload */
				setTimeout(
					function () {
						phytw.pic.$box.children(':last').attr('src', phytw.pic.list[num]);
					},
					0
				);
			} else {
				/* bring last image to front */
				phytw.pic.$box.append(
					phytw.pic.$box.children(':first')
					.hide()
				);
				phytw.pic.showpic.apply(phytw.pic.$box.children(':last').get(0));
			}
		},
		showpic: function (ev, shown) {
			clearTimeout(phytw.pic.timer);
			if (!shown) {
				$(this)
				.css(
					{
						top: -($(this).height()-$('#picbox').height())*Math.random(),
						left: -($(this).width()-$('#picbox').width())*Math.random()
					}
				)
				.fadeIn(phytw.pic.aniduration);
			}
			if (phytw.pic.disabled) {
				phytw.pic.$box.fadeTo(phytw.pic.aniduration, 0.5);
				setTimeout(
					function () {
						if (phytw.pic.disabled) {
							phytw.pic.$pause.show();
						}
					},
					phytw.pic.aniduration
				);
				$(window).one(
					'focus',
					function () {
						phytw.pic.$box.fadeTo(0, 1);
						phytw.pic.$pause.hide();
						phytw.pic.showpic.apply(phytw.pic.$box.children(':last').get(0), [ev, true]);
					}
				);
				return;
			}
			$(this)
			.animate(
				{
					top: -($(this).height()-380)*Math.random(),
					left: -($(this).width()-460)*Math.random()
				},
				phytw.pic.duration,
				'swing',
				phytw.pic.next
			);
		}
	}
};
/*
 * jQuery shuffle
 *
 * Copyright (c) 2008 Ca Phun Ung <caphun at yelotofu dot com>
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://yelotofu.com/labs/jquery/snippets/shuffle/
 *
 * Shuffles an array or the children of a element container.
 * This uses the Fisher-Yates shuffle algorithm <http://jsfromhell.com/array/shuffle [v1.0]>
 */
$.shuffle = function(arr) {
	for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
	return arr;
}
$(phytw.init);
