﻿// Gallery scipt functions 
function _CGallery() {

	this.initialize = _initialize;
	this.startInFancybox = _startInFancybox;
	
	function _initialize() {
		// Prepare all a with name=gallery
		$('a[name=gallery]').each(function(index) {
			var a = $(this);			
			var galleryId = a.attr('id') + 'Gallery';
			a.attr('href', document.location.href + '#' + galleryId);
			var title = a.attr('title');
			var mainImg = a.children('img').first();
			if (!title) {
				if (mainImg) title = mainImg.attr('title');
			}
			if (title) {
				title = title.replace(/\\n/g, '<br />');
				title = title.replace(/\{/g, '<b>');
				title = title.replace(/\}/g, '</b>');
			}
			a.fancybox(
				{
					showCloseButton		:	true,
					hideOnContentClick	: false	,
					onStart				: Gallery.startInFancybox,
					centerOnScroll		:	true,
					overlayOpacity		:	0.8,
					overlayColor		:	'#ddd',				
					transitionIn		: 'elastic',	// 'elastic', 'fade' or 'none'
					transitionOut		: 'fade',	// 'elastic', 'fade' or 'none'
					title				: title,
					titlePosition		: 'inside'
				}
			);
			/*
			a.css('position', 'relative').css('z-index', 1).css('float', mainImg.css('float'));
			mainImg.css('position', 'relative').css('z-index', 2);	
			var galleryIcon = a.prepend('<img src="' + Misc.basePath() + 'Images/Icons/Gallery.gif" style="position:absolute; top:0px; right:10px"/>').children().first();
			galleryIcon.css('z-index', 220);
			*/

			_prepareGallery(a);

			/*
			var container = a.find('.gallery-container').first();
			var images = container.find('img').detach();
			var gallery = container.append('<div id="' + galleryId + '" class="ad-gallery"></div>').children().last();
			gallery.append('<div class="ad-image-wrapper"></div>');
			gallery.append('<div class="ad-controls"></div>');
			var navigator = gallery.append('<div class="ad-nav"></div>').children().last();
			var thumbs = navigator.append('<div class="ad-thumbs"></div>').children().last();	
			var thumbsList = thumbs.append('<ul class="ad-thumb-list"></ul>').children().last();
			
			images.each(function(index) {
				var href = this.src;
				if (href.indexOf('-t.') > 0) {
					// Thumbnail image, remove the '-t' for the href
					href = href.substr(0, href.indexOf('-t.')) + href.substr(href.indexOf('-t.') + 2);
				}
				var img = $(this);
				var alt = img.attr('alt');
				var title = img.attr('title');
				var longdesc = img.attr('longdesc');
				img.attr('longdesc', (longdesc ? longdesc : alt).replace(/\n/, '<br />'));
				this.alt = null;
				if (alt == title) this.title = '';
				img.attr('height', '60');
				var li = thumbsList.append('<li></li>').children().last();
				var a = li.append('<a href="' + href + '"></a>').children().last();
				a.append(img);
			});
			container.css('display', 'block');			
			*/
		});
	}
	
	function _prepareGallery(a) {
		var galleryId = a.attr('id') + 'Gallery';
		if ($('#' + galleryId).length > 0) return; // Already prepared
		
		var container = a.find('.gallery-container').first();
		var images = container.find('img').detach();
		var gallery = container.append('<div id="' + galleryId + '" class="ad-gallery"></div>').children().last();
		gallery.append('<div class="ad-image-wrapper"></div>');
		gallery.append('<div class="ad-controls"></div>');
		var navigator = gallery.append('<div class="ad-nav"></div>').children().last();
		var thumbs = navigator.append('<div class="ad-thumbs"></div>').children().last();	
		var thumbsList = thumbs.append('<ul class="ad-thumb-list"></ul>').children().last();
		
		images.each(function(index) {
			var href = this.src;
			if (href.indexOf('-t.') > 0) {
				// Thumbnail image, remove the '-t' for the href
				href = href.substr(0, href.indexOf('-t.')) + href.substr(href.indexOf('-t.') + 2);
			}
			var img = $(this);
			var alt = img.attr('alt');
			var title = img.attr('title');
			var longdesc = img.attr('longdesc');
			img.attr('longdesc', (longdesc ? longdesc : alt).replace(/\n/, '<br />'));
			this.alt = null;
			if (alt == title) this.title = '';
			img.attr('height', '60');
			var li = thumbsList.append('<li></li>').children().last();
			var a = li.append('<a href="' + href + '"></a>').children().last();
			a.append(img);
		});
		container.css('display', 'block');			
	}
	
	function _startInFancybox(selectedArray, selectedIndex, selectedOpts) {
		var target = $(selectedArray[selectedIndex]);
		var galleryId = target.attr('id') + 'Gallery';
		
		if (_galleries[galleryId] != null) return;

		var gallery = $('#' + galleryId).first();
		var parent = gallery.parent();
	    _galleries[galleryId] =  gallery.adGallery(
			{
				width: Math.min(600, parent.width()), // Width of the image, set to false and it will read the CSS width
				height: Math.min(400, parent.height()), // Height of the image, set to false and it will read the CSS height			
				effect: 'resize', // 'slide-hori' or 'slide-vert', 'resize', 'fade', 'none' or false	
				cycle: true
			}
		)[0];
	}
}

var _galleries = [];
var Gallery = new _CGallery();

