/* CLIQUE */
/* Photo Enhancements Pattern Javascript
----------------------------------------------------------------------------- */


	(function($) {


	$.fn.cliquePhotos = function(parameters) {
	

		var defaults = {
		
			browser: "thumbnails",
			viewer: false,
			preview: true,
			
			loupeWidth: '320',
			loupeHeight: '240',
			loupeOffsetX: '0',
			loupeOffsetY: '0'

		};

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


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

	
	return this.each(function(index) {


		var $this = $(this);


/* BROWSER - SLIDESHOW
----------------------------------------------------------------------------- */


	if (parameters.browser == "slideshow") {

		$this.addClass('slideshow');

		if ($this.find('ul.actions').length > 0) {
			$this.find('ul.actions a.next').parent.remove();
			$this.find('ul.actions a.back').parent.remove();
		} else {
			$this.append('<ul class="actions"></ul>');
		}

		var slideshow = index;
		var insertion = $(this).children('ul.actions');

		if ($this.find('ul.photos img').length > 1 && $('#' + slideshow + '_next').length == 0) {
			$('<li><a id="' + slideshow + '_next" class="action next" href="#">Next</a></li>').prependTo(insertion);
			$('<li><a id="' + slideshow + '_back" class="action back" href="#">Back</a></li>').prependTo(insertion);
		}

		if (!$this.hasClass('gallery')) {

			$this.children('ul.photos').each(function() {
			
				$(this).children('li').each(function() {
			
					if ($(this).children('a.enlarge').length > 0) {
					
						var image = $(this).children('a.enlarge').attr('href');
						var caption = $(this).children('a.thumbnail').children('img').attr('alt');
						
						$(this).append('<img src="' + image + '" alt="' + caption + '" title="' + caption + '" />');
						$(this).children('a, strong').remove();
		
					} else {
					
						$(this).remove();
					
					}
			
				});
			
			});
		
		}

		$this.children('ul.photos').cycle({

			fx:      'fade',
			speed:   'normal',
			next:    '#' + slideshow + '_next',
			prev:    '#' + slideshow + '_back'

		});

	}


/* VIEWER
----------------------------------------------------------------------------- */


	if (parameters.viewer == true && parameters.browser == "thumbnails") {

		var image = $this.find('ul.photos:visible li:first-child a').attr('href');
		var caption = $this.find('ul.photos:visible li:first-child img').attr('alt');

		$this.prepend('<div class="enlarged"><a href="' + image + '"><img class="selected original" src="' + image + '" alt="' + caption + '" /></a></div>');

		var $enlarged = $this.children('div.enlarged');

		if ($this.find('ul.photos:visible li:first-child').hasClass('large')) {
			$enlarged.addClass('zoom');
		}
		
		$this.find('div.enlarged a').click(function() {
		
			return false;
		
		});

		$this.find('ul.photos a').click(function() {
		
			var image = $(this).attr('href');
			var caption = $(this).children('img').attr('alt');

			$enlarged.empty().prepend('<a href="' + image + '"><img class="selected original" src="' + image + '" alt="' + caption + '" /></a>');

			$enlarged.children('a').click(function() {
			
				return false;
			
			});

			if ($(this).parent().hasClass('large')) {
				loadEnlarger();
			}

			$this.find('ul.photos li').removeClass('selected');
			$(this).parent().addClass('selected');

			return false;
		
		});
		

		// Thumbnail Preview
		
		if (parameters.preview == true) {
		
			$this.find('ul.photos a').hover(function() {
			
				var image = $(this).attr('href');
				var caption = $(this).children('img').attr('alt');

				$enlarged.children('img').addClass('original').hide();
				$enlarged.prepend('<img src="' + image + '" alt="' + caption + '" />');

				$(this).parent().addClass('hover');
			
			}, function() {
				
				$enlarged.children('img:not(.original)').remove();
				$enlarged.children('img').show().removeClass('original');

				$(this).parent().removeClass('hover');
			
			});

		}
		

		// Loupe Enlarger

		if (parameters.zoom == "loupe") {

			function loadEnlarger() {

				try {
				
					var loupeWidth = parseInt(parameters.loupeWidth);
					var loupeHeight = parseInt(parameters.loupeHeight);
					var loupeOffsetX = parseInt(parameters.loupeOffsetX);
					var loupeOffsetY = parseInt(parameters.loupeOffsetY);
				
					$('div.enlarged a').jqzoom({
		
						zoomWidth: loupeWidth,
						zoomHeight: loupeHeight,
						xOffset: loupeOffsetX,
						yOffset: loupeOffsetY,
						showEffect: 'fadein'
					
					});
				
				} catch(errors) {
				
				}

			}

			$("div.widget-item div.styles li").click(loadEnlarger);

			loadEnlarger();
		
		}


		// Slider Enlarger

		if (parameters.zoom == "slider") {
		
			$('div.enlarged img').draggable({
			
				containment: 'parent',
				cursor: 'move'
		
			});
	
		}

	}


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


			});

		};

	})(jQuery);