/* CLIQUE */
/* Item Widget Enhancements Pattern Javascript
----------------------------------------------------------------------------- */


	(function($) {

	$.fn.cliqueDragZoom = function(parameters) {
	
		var defaults = {
		
			debug: false

		};

		var parameters = $.extend(defaults, parameters);


/* PRESERVE CHAINING
----------------------------------------------------------------------------- */


	return this.each(function(index) {

		var $this = $(this);


/* ADD CLASS
----------------------------------------------------------------------------- */


		$this.addClass('draggable');


/* SET CURSOR
----------------------------------------------------------------------------- */


		cursor();
		

/* DRAGGING
----------------------------------------------------------------------------- */


		$this.mousedown(function(e) {
		
			// Add Class
			$(this).addClass('dragging');
		
			// Find Positions
			handle = new Object;
			handle.mouseX = e.pageX;
			handle.mouseY = e.pageY;
			handle.left = e.pageX - $(this).parent().position().left - parseInt($(this).css('left')) + $(this).parent().position().left;
			handle.top = e.pageY - $(this).parent().position().top - parseInt($(this).css('top')) + $(this).parent().position().top;
			handle.minLeft = ($(this).children('img').width() - $(this).parent().width()) * -1;
			handle.minTop = ($(this).children('img').height() - $(this).parent().height()) * -1;
	
			// Drag
			$('body').mousemove(function(e) {
			
				var newLeft = e.pageX - handle.left;
				var newTop = e.pageY - handle.top;
			
				if (newLeft <= 0 && newLeft >= handle.minLeft) $this.css('left', newLeft);
				if (newTop <= 0 && newTop >= handle.minTop) $this.css('top', newTop);
								
				return false;
								
			});
	
			return false;
		
		});
		
		// Stop Dragging
		$('body').mouseup(function() {
		
			// Add Class
			$('a.dragging').removeClass('dragging');
			$('body').unbind('mousemove');
			return false;
			
		});


/* ZOOMING
----------------------------------------------------------------------------- */

	
		$this.click(function(e) {
		
			if (!$(this).hasClass('expanded')) {
			
		
// Zoom In

				
				// Get Enlarged Image Size
				var originalWidth = $this.children('img').css('width');
				$this.children('img').css('width', 'auto');
				var imgWidth = $this.children('img').width();
				var imgHeight = $this.children('img').height();
				$this.children('img').css('width', originalWidth);

				var containerWidth = $this.width();
				var containerHeight = $this.height();
				var left = (imgWidth - containerWidth) / -2;
				var top = (imgHeight - containerHeight) / -2;
				
				// Hide Caption
				$(this).children('strong').hide();
			
				// Animate
				$(this).addClass('expanded').children('img').animate({ width: imgWidth });
				$(this).animate({ top: top, left: left });
			
				// Swap Cursor
				$(this).css('cursor', 'move');

			} else {
			
				// Set Zoom Out vs Drag Tolerance 
				var tolerance = Math.abs(handle.mouseY - e.pageY) + Math.abs(handle.mouseX - e.pageX);
						
				if (tolerance <= 5) {

		
// Zoom Out

	
					// Animate
					$(this).removeClass('expanded').animate({ top: 0, left: 0 }, function() {
					
						// Show Caption
						$(this).children('strong').fadeIn();
					
					});
					
					$(this).children('img').animate({ width: '100%' });
					
					// Swap Cursor
					cursor();
				
				}
				
			}
			
			return false;
					
		});
		

/* FUNCTION: SET CURSOR
----------------------------------------------------------------------------- */


		function cursor() {
		
			if (navigator.userAgent.match('Firefox')) {
			
				$this.css('cursor', 'url(css/i/aos/gallery/cursor-magnify.png), -moz-zoom-in');
				
			} else if (navigator.userAgent.match('AppleWebKit')) {
			
				$this.css('cursor', 'url(http://dev.media.sparkart.net/common/patterns/item/cursor-magnify.png), auto');
	
			}
		
		}


/* END PLUG-IN
----------------------------------------------------------------------------- */


			});

		};

	})(jQuery);