//$.getScript("ui/jquery.scrollTo-min.js",function(){
$.getScript("ui/jquery-ui-1.7.1.custom.min.js",function(){
$(document).ready(function(){

	shuffleTiles();
	doResize(); cursorX = windowX / 2; cursorY = windowY / 2;
	initBehaviors();
	
});});//});

var tilesTotal = 78; var tilesVisible = 0; var tarotDisplay = new Array(tilesTotal); var tarotTile = "thoth"; var tarotDualDirection = false;
var cursorX = 0; var cursorY = 0; var windowX = 0; var windowY = 0;
	
function initBehaviors(){	$("#apoContext").click(function(event){
				if (isEmptyTile($(event.target))) {	assignTile($(event.target), randomizeTile());
				} else if (isTile($(event.target))) {	killTile($(event.target));
				} else { 				makeTile();}});

				$("#apoNotice").click(function(event){	makeTile();});
				
				$("#apoHeader").click(function(){	shuffleTiles(); });
				$(window).konami(function(){		shuffleTiles(); tarotDualDirection = true; tarotTile = "rws";});
				$(document).mousemove(function(e){	cursorX = e.pageX; cursorY = e.pageY });
				$(window).bind("resize", doResize);	$("#apoNotice").html("Enjoy Clicking"); }

function makeTile(){		if (!$("#apoNotice").is(":hidden")) $("#apoNotice").fadeOut(400);
				var rotationDeg  = (Math.floor(Math.random() * 7) - 3.5);
				if (tarotDualDirection)	rotationDeg += ((Math.random() > 0.5) ? 1 : 0) * 180;
				if(tilesVisible < tilesTotal) with($("#apoContext")) {
				append("<div></div>");
				children(":last-child").attr("class", "apoTile").addClass(tarotTile).
				css("top", cursorY - 144).css("left", cursorX - 90).
				css("-webkit-transform","rotate(" + rotationDeg + "deg)").attr("rel", "").fadeIn(200);
				children("div").draggable({stack: {group:'#apoContext div', min: 256, revert: 'invalid'}});	}
				tilesVisible ++; }

function randomizeTile(){	var gotDestination = false; while (!gotDestination){
				var temporalDestination = Math.floor(Math.random() * tilesTotal);
				if (tarotDisplay[temporalDestination-1][1] != true) gotDestination = true;}
				return temporalDestination;}
								
function assignTile(target, n){	tarotDisplay[n-1][1] = true;
				target.attr("rel", n-1).css("background", ("url(" + tarotTile + "/" + ( (n < 10) ? "0" : "" ) + n + ".jpg)")); }

function killTile(target){	tarotDisplay[target.attr("rel")][1] = false; tilesVisible --; target.fadeOut(400); }

function isTile(target){	return target.is("#apoContext div"); }

function isEmptyTile(target){	return ((target.attr("rel") == "") && isTile(target)); }

function shuffleTiles(){	for (var j = 0; j < tilesTotal; j++) tarotDisplay[j]=new Array(2);
				$("div.apoTile").slowEach(100, function(){	killTile($(this));	});
	
				var temporalFY = new Array(tilesTotal);
				for (var i = 0; i < tilesTotal; i++) temporalFY [i] = i; fisherYates(temporalFY);
				for (var i = 0; i < tilesTotal; i++) tarotDisplay[i][0] = temporalFY [i]; tilesVisible = 0; }

function doResize(){		windowX = $(window).width(); windowY = $(window).height();
				$("#apoContext").css("height", (windowY + "px")); }

jQuery.fn.slowEach = function (interval, callback){ var array = this; if( !array.length ) return; var i = 0; next(); 
function next(){  if( callback.call( array[i], i, array[i] ) !== false ) if( ++i < array.length ) setTimeout( next, interval );}};

jQuery.fn.konami = function(callback, code){
code = (code == undefined) ? "38,38,40,40,37,39,37,39,66,65" : code; return this.each(function() { var kkeys = []; $(this).keydown(function(e){
kkeys.push( e.keyCode ); if ( kkeys.toString().indexOf( code ) >= 0 ){$(this).unbind('keydown', arguments.callee); callback(e); }}, true);});}

function fisherYates (dest){ var i = dest.length; if ( i == 0 ) return false; while (--i){ var j = Math.floor( Math.random() * (i+1));
var temp = dest[i]; dest[i] = dest[j]; dest[j] = temp;}}