(function($) {
	$(window).ready(function() {
		$('h1').famteamlogo();
		
		$('#list .date, #list a').fadeTo(0, 0).fadeTo(250, 0.25);

		$('#list a').each(function() {
			bindMouseOverEffect($(this));
		});

		$('a.noLink').css('cursor', 'default').click(function(event) {
			event.preventDefault();
		});
		$('#slotMachineEffectLink').click(demoSlotMachineTextEffect);
		$('#colorSpreadingEffectLink').click(demoColorSpreadingTextEffect);
		$('#hollowTextEffectLink').click(demoHollowTextEffect);
		$('#squareTiledOverImage').click(demoSquareTiledOverImage);
	});

	function bindMouseOverEffect($element) {
		$element.mouseenter(function() {
			$(this).stop().fadeTo(400, 1);
		}).mouseleave(function() {
			$(this).stop().fadeTo(150, 0.25);
		});
	}

	function demoSquareTiledOverImage(event) {
		event.preventDefault();
		var $anchor = $(this).stop().fadeTo(0, 0.25);
		var $squareTiledOverImage = $('<div/>').css({
			'position': 'fixed',
			'z-index': 20,
			'top': '0px',
			'left': '0px'
		}).fadeTo(0, 0).scaler().overimage().appendTo($('body')).css('cursor', 'pointer').click(function(event) {
			$squareTiledOverImage.add($focusOverImage).fadeTo(200, 0, function() {
				$(this).remove();
			});
		});
		var $focusOverImage = $('<div/>').css({
			'position': 'fixed',
			'z-index': 10,
			'top': '0px',
			'left': '0px'
		}).fadeTo(0, 0).scaler().appendTo($('body')).overimage({
			action: $.overimage.ACTION_FOCUS_OVER_IMAGE
		});
		$squareTiledOverImage.add($focusOverImage).fadeTo(0, 1);
	}
	
	function demoSlotMachineTextEffect(event) {
		event.preventDefault();
		$(this).fadeTo(0, 1).unbind('click').unbind('mouseenter').unbind('mouseleave').texteffects({
			action: $.texteffects.ACTION_SLOT_IN,
			interval: 40,
			onComplete: function($anchor) {
				$anchor.delay(400).fadeTo(400, 0.25, function() {
					var $anchor = $(this);
					$anchor.click(demoSlotMachineTextEffect);
					bindMouseOverEffect($anchor);
				});
			}
		});
	}

	function demoColorSpreadingTextEffect(event) {
		event.preventDefault();
		var color = "rgb(" + Math.floor(Math.random() * 50 + 155) + ', ' + Math.floor(Math.random() * 50 + 155) + ', ' + Math.floor(Math.random() * 50 + 155) + ')';
		$(this).unbind('click').unbind('mouseenter').unbind('mouseleave').texteffects({
			action: $.texteffects.ACTION_WAVE_EFFECT,
			color: color, 
			interval: 600,
			onComplete: function($anchor) {
				$anchor.texteffects({
					action: $.texteffects.ACTION_WAVE_EFFECT,
					color: '#444',
					delay: 600,
					isReverse: true,
					interval: 600,
					onComplete: function($anchor) {
						$anchor.click(demoColorSpreadingTextEffect);
						bindMouseOverEffect($anchor);
						_executeIfMouseLeave($anchor, function($anchor) {
							$anchor.fadeTo(400, 0.25);
						});
					}
				});
			}
		});
	}

	function _executeIfMouseLeave($anchor, executeFunction) {
		var onMouseMoveForCheck = function(event) {
			var anchorOffset = $anchor.offset();
			if (event.mouseX > anchorOffset.left && event.mouseX < anchorOffset.left + $anchor.width() && 
				event.mouseY > anchorOffset.top && event.mouseY < anchorOffset.top + $anchor.height()) {
			}
			else {
				executeFunction($anchor);
			}
			$(window).unbind('mousemove', onMouseMoveForCheck);
		}
		$(window).mousemove(onMouseMoveForCheck);
	}

	function demoHollowTextEffect(event) {
		event.preventDefault();
		var $body = $('body');
		var $anchor = $(this).unbind('click').unbind('mouseenter').unbind('mouseleave');
		var $hollowTextClock = $('<div/>').css({
			'position': 'fixed',
			'top': '0px',
			'left': '0px',
			'text-align': 'center',
			'font-size': '1600%',
			'font-weight': 'bold',
			'cursor': 'pointer'
		}).text(_getCurrentTimeString()).scaler().fadeTo(0, 0.25).click(function() {
			$(this).fadeTo(300, 0, function() {
				bindMouseOverEffect($anchor);
				_hollowTextClockFlag = false;
				$anchor.click(demoHollowTextEffect);
				$(this).remove();
				$body.css('overflow', 'auto');
				_executeIfMouseLeave($anchor, function($anchor) {
					$anchor.fadeTo(400, 0.25);
				});
			});
		}).appendTo($body).fadeTo(400, 1);

		var _hollowTextClockFlag = true;
		function _startHollowTextClock() {
			$hollowTextClock.hollowtext({
				'color': '#444',
				'text': _getCurrentTimeString()
			});
			if (_hollowTextClockFlag) {
				setTimeout(_startHollowTextClock, 200);
			}
		}
		_startHollowTextClock();
	}
	function _getCurrentTimeString() {
		var date = new Date();
		var format = function(number) {
			return number < 10 ? '0' + number : number;
		}
		return format(date.getHours()) + ':' + format(date.getMinutes()) + ':' + format(date.getSeconds());
	}
})(jQuery);

