;$j = jQuery.noConflict();

$j(document).ready
(
	function()
	{
		/**
		 * Products images
		 */
		$j('.fancybox').fancybox
		({
			titlePosition: 'inside'
		});
		
		
		/**
		 * Homepage Gallery
		 */
		$gallery = $j('#jgallery ul');
		
		if ($gallery.length)
		{
			var galleryItemWidth = 300;
			var galleryMinWidth = 60;
			var galleryExtraWidth = 240;
			
			function expandGalleryItem($item)
			{
				var $next = $item.next();
				var $prev = $item.prev();
				var $sibling = $next.length ? $next : $prev;
				
				$sibling
					.find('img:first').hide().end()
					.css({width: galleryMinWidth});
		
				$item
					.css({width: galleryItemWidth + galleryExtraWidth})
					.find('ul').show().end()
					.addClass('active');
			}
			
			function collapseGalleryItem($item)
			{
				var $next = $item.next();
				var $prev = $item.prev();
				var $sibling = $next.length ? $next : $prev;
				
				$item
					.find('ul').hide().end()
					.css({width: galleryItemWidth})
					.removeClass('active');
				
				$sibling
					.css({width: galleryItemWidth})
					.find('img:first').show();
			}
			
			$gallery.css('overflow', 'hidden');
			
			$j('> li', $gallery).css({position: 'relative', overflow: 'hidden'});
			
			$j('> li > a', $gallery).click
			(
				function()
				{
					var $item = $j(this).parent();
					var $otherActiveItem = $item.siblings('.active');
					
					if ($otherActiveItem.length)
					{
						collapseGalleryItem($otherActiveItem);
					}
					
					if ($item.hasClass('active'))
					{
						collapseGalleryItem($item);
					}
					else
					{
						expandGalleryItem($item);
					}
					
					return false;
				}
			);
		}
	}
);
