/*
 * 	Rotate Photo 1.0 - jQuery plugin
 *	written by Martin Stanek
 *
 *	Copyright (c) 2009 Martin Stanek (http://www.martinstanek.cz)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */
 
(function($) {

	$.fn.rPhoto = function(options){
	  
		// default configuration properties
		var defaults = {			
			distance: 		50,
			maxAngle:		30,
			overlap:		0.5,
			itemsInRow:		6,
			showBubble:		true
		}; 
		
		var options = $.extend(defaults, options);  
				
		this.each(function() {  
			var obj = $(this); 				
			var l = $(".reference", obj).length;
			var w = $(".reference", obj).width(); 
			var h = $(".reference", obj).height(); 
			var clickable = true;
			
			var angle = 0;
			var positionLeft = 0;
			var positionTop = 0;
			var count = 0;
			var row = 0;	
			
			obj.removeClass("no_rotate");
			
			/* Necessary for bubble */
			var beingShown = false;
            var shown = false;
            var hideTimeout; // If you enter new photo, the bubble will not hide
            var bContainer = $(document.createElement("div")).attr("id", "reference-bubble");
            bContainer.css({
            	"-moz-border-radius":"6px",
            	"-webkit-border-radius":"6px"
            });	
                   
            var tip = $(document.createElement("span")).attr("id", "tip");            
			var active = null;
			var mousePos = {left: 0, top: 0}; 
			var offset = obj.offset();
			
			obj.mousemove(function(e){      			
      			offset = obj.offset(); // Current container offset
      			mousePos.left = e.pageX + $(this).scrollLeft() - offset.left;
      			mousePos.top = e.pageY + $(this).scrollTop() - offset.top;
      		      	 			
		    });		    
      									
			$(".title", obj).css("display","none");
						
			$(".reference", obj).each(function() {
				count++;
				var r = Math.random();
				angle = (count % 2) ? Math.round(r * 30)*(-1) : Math.round(r * 30);
				
				var s = Math.sin(angle);
				var c = Math.cos(angle);
							
				$(this).css({
					'-webkit-transform': 'rotate('+angle+'deg)',
					'-moz-transform': 'rotate('+angle+'deg)',
					'transform': 'rotate('+angle+'deg)',
					'position': 'absolute',
					'left': positionLeft+'px',
					'top': positionTop+'px',
					'z-index':'1'
				});
				
				positionLeft += options.overlap * Math.round(w*Math.abs(c) + h*Math.abs(s));
				if(count % options.itemsInRow == 0) {
					row++;
					positionLeft = 0;
				}
				positionTop = (row*h*0.6) + Math.round(s*40);
				
				/* Bubble */			 
				$(this).mouseenter(function () {
					clearTimeout(hideTimeout);
					active = $(this);
					active.css({"z-index": "3"});
										
					switchText(); // Fill the bubble	
					
					bContainer.css({ 
						"position": "absolute",
						"left": (mousePos.left + 40) + "px", 
						"top": (mousePos.top - 20) + "px" 
					});
										    
				    bContainer.fadeIn("fast");			
					shown = true;    
							
	
				}).mouseleave(function () {
					active.css({"z-index": "2"});					
					hideTimeout = setTimeout(function() {
				      	bContainer.fadeOut("fast");
				    }, 150);					
				}).mousemove(function(e){
						bContainer.css({ 
							"position": "absolute",
							"left": (mousePos.left + 40) + "px", 
							"top": (mousePos.top - 20) + "px" 
						});
						
				});
				
				
			});
						
			function switchText() {
				bContainer.html(""); // Clear content	
				var bubble = $(".title", active).clone();
				bubble.css('display', 'block');
				bContainer.append(bubble);
				bContainer.append(tip);
			}
			
			
			// init
			bContainer.hide();
			obj.append(bContainer);						
		});
	  
	};

})(jQuery);




