/*
 * @name jquery.jUmble.js
 * @version 0.4 by Sean Ockert <http://catalyticat.com>
 * @date June 13 2008
 * A plugin for jQuery <http://query.com>
 */
 
 $(document).ready(function(){
 	$('.folder').children('span').css('visibility','hidden').css('position','absolute'); //So it degrades when javascript is disabled
	$('.folder a').lightBox();
 	$('.folder a').css('border-bottom','none').css('text-decoration', 'none').css('background-color','transparent'); //Take out this line
 
	$('.folder').toggle(function() { 
		$(this).jUmble(); // Call main function on click
	}, function() {
		$(this).UnjUmble(); 
    });
	
	$('.folder').children('span').hover(function() {
		$(this).css('z-index', 5).css('opacity',  1); //highlight selected image. Can be omitted
	}, function() {
		$(this).css('z-index', 4).css('opacity', 0.8); 
	});
});




// jUmble - This function animates a bunch of images being randomly spread out next to a folder
jQuery.fn.jUmble = function(imgs) {
 // Why does this bring up a property undefined error?
 //var defaults = {
	//	imgs: 5,
	//	distance: 120,
	//	angle: 45,
	//	fixtop: true,
	//	speed: 600,
	//	spread: 5,
	//	easetype: 'easeOutCirc'
 // };
  
 //var opts = $.extend(defaults, options);

 //if(options) {
 //     jQuery.extend(defaults, options);
 // };
 //alert(distance);	
		
	var distance = 90; // distance in pixels, basis for randomisation.
	var angle = 45; // maximum rotation angle of the images in degrees
	var fixtop = false; // true = fixes the top position at 0px, false = randomised using distance variable
	var speed = 550; // easing animation speed in milliseconds
	var spread = 5; // 0 = minimal spread, 5 = default, 10 = wide spread
	var easetype = 'easeOutCirc'; //jQuery easing plugin - use any one you like
		
	if (!imgs){imgs = 5};
		
	this.children('span').click(function(e) { 
		e.stopPropagation(); //Stops click event from bubbling up to the parent function
	});
		
	this.children('span').css('visibility','visible').css('opacity', 1); //Reset visiblity and opacity of images
		
	var rndY = rndX = Math.floor(Math.random()*distance);
	if (fixtop == true) {rndY = 0;}
		
	this.children('span').each(function(i) {
		$(this).animate({'left': rndX + distance, 'top': rndY/2 - distance}, speed, easetype);
		y = Math.floor(Math.random()*distance);
		rndY = rndY + y + 10*spread;
		x = Math.floor(Math.random()*distance);
		if ( i < Math.floor(imgs/2)) { 
			rndX = rndX + x + spread;
		} else {
			rndX = rndX/2 + x - 10*spread; //If more than halfway through the images bring them back towards the folder
		};
		
		});
		
	this.children('span').each(function() {
		var a = 2*(Math.floor((Math.random()*angle)-angle/2));
		$('.r_').rotate(a); //needs abstracting with $(this)
	});
		
	this.addClass('folder_empty');
};

// UnjUmble - This function  returns the images to their folder. To Fix: Needs to claim only one namespace 
jQuery.fn.UnjUmble = function(m) {
	this.children('span').animate({'left': 0, 'top': 0, 'opacity': 0}, 300, function(){
		$(this).css('visibility','hidden');	
		$(this).parent().removeClass('folder_empty');	
	});
}; 
 
/* Copyright © 2001 Robert Penner */
jQuery.extend(jQuery.easing,
{
	def: 'easeOutBounce',
	swing: function (x, t, b, c, d) {
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeOutCirc: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	},	
	easeOutBounce: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	}
});

/* By tjerk.teesters http://code.google.com/p/jquery-rotate/ */
jQuery.fn.rotate = function(angle,whence) {
	var p = this.get(0);
	if (!p) {return false; };
	// we store the angle inside the image tag for persistence
	if (!whence) {
		p.angle = ((p.angle==undefined?0:p.angle) + angle) % 360;
	} else {
		p.angle = angle;	
	}

	if (p.angle >= 0) {
		var rotation = Math.PI * p.angle / 180;
	} else {
		var rotation = Math.PI * (360+p.angle) / 180;
	}
	var costheta = Math.cos(rotation);
	var sintheta = Math.sin(rotation);

	if (document.all && !window.opera) {
		var canvas = document.createElement('img');

		canvas.src = p.src;
		canvas.height = p.height;
		canvas.width = p.width;

		canvas.style.filter = "progid:DXImageTransform.Microsoft.Matrix(M11="+costheta+",M12="+(-sintheta)+",M21="+sintheta+",M22="+costheta+",SizingMethod='auto expand')";
	} else {
		var canvas = document.createElement('canvas');
		if (!p.oImage) {
			canvas.oImage = new Image();
			canvas.oImage.src = p.src;
		} else {
			canvas.oImage = p.oImage;
		}

		canvas.style.width = canvas.width = Math.abs(costheta*canvas.oImage.width) + Math.abs(sintheta*canvas.oImage.height);
		canvas.style.height = canvas.height = Math.abs(costheta*canvas.oImage.height) + Math.abs(sintheta*canvas.oImage.width);

		var context = canvas.getContext('2d');
		context.save();
		if (rotation <= Math.PI/2) {
			context.translate(sintheta*canvas.oImage.height,0);
		} else if (rotation <= Math.PI) {
			context.translate(canvas.width,-costheta*canvas.oImage.height);
		} else if (rotation <= 1.5*Math.PI) {
			context.translate(-costheta*canvas.oImage.width,canvas.height);
		} else {
			context.translate(0,-sintheta*canvas.oImage.width);
		}
		context.rotate(rotation);
		context.drawImage(canvas.oImage, 0, 0, canvas.oImage.width, canvas.oImage.height);
		context.restore();
	}
	canvas.id = p.id;
	canvas.angle = p.angle;
	p.parentNode.replaceChild(canvas, p);
}


