﻿function _CPictures() {
	this.preLoad = _preLoad;
	this.insertBasePath = _insertBasePath;

	function _preLoad(containerId)
	{
		var container = containerId ? $(containerId) : $("body").first();
	
		// Common preparation to all images		
		container.find('img[src$=.jpg]').each(function (index){
			var img = $(this);
			if (this.alt && this.alt.indexOf('[RND]') == 0 && this.title) {
				// Contains a semicolon separated list of urls
				var altList = this.alt.substr('[RND]'.length).split(';');
				var titleList = this.title.split(';');
				var index = Math.min(Math.floor(Math.random() * (altList.length + 1)), altList.length - 1);
				this.src = Pictures.insertBasePath(altList[index]);
				this.alt = titleList[index].replace(/\\n/g, '\n');;
				this.title= titleList[index].replace(/\\n/g, '\n');;
				img.css('visibility', 'visible');			
			}
			else {
				this.src = Pictures.insertBasePath(this.src);
				var title = (this.title ? this.title : (this.alt ? this.alt : '')).replace(/\\n/g, '\n');
				var alt = (this.alt ? this.alt : '').replace(/\\n/g, '\n');
		
				//if (title.length > 0 && alt.length > 0 && title.indexOf(alt) < 0) title += ' - ' + alt;
				this.title = title; 
				this.alt = alt;
			}
			if (this.title == null || this.title.length == 0) this.title = this.alt
		});
		
		container.find('img[name=nozoom]').each(function (index){
			var img = $(this);
			img.css('cursor', 'auto');			
			img.css('visibility', 'visible');			
		});
		
		// Connect a fancybox to all images with name zoom
		container.find('img[name=zoom],img[name=zoom-onesize]').each(function (index){
			var src = this.src;
			var onesize = (this.name == 'zoom-onesize');
			var idx = src.lastIndexOf('.');
			var srcZoom = (onesize) 
						? src 
						: (src.indexOf('-t.') > 0) 
							? src.substr(0, src.indexOf('-t.')) + src.substr(idx)
							: src.substr(0, idx) + '-z' + src.substr(idx);
			this.onclick = null;
			var title = (this.title ? this.title : (this.alt ? this.alt : '')).replace(/\\n/g, '\n');
			var alt = (this.alt ? this.alt : '').replace(/\\n/g, '\n');
	
			this.title = title + '\n\n(Klikk for å vise stort bilde)'; 
			if (title.length > 0 && alt.length > 0 && title.indexOf(alt) < 0) title += ' - ' + alt;

			var img = $(this);
			img.fancybox(
				{
					showCloseButton		:	true,
					hideOnContentClick	:	true,
					centerOnScroll		:	true,
					titlePosition		:	'inside',	// 'outside', 'inside' or 'over'
					overlayOpacity		:	0.8,
					overlayColor		:	'#ddd',				
					transitionIn		:	'elastic',	// 'elastic', 'fade' or 'none'
					transitionOut		:	'elastic',	// 'elastic', 'fade' or 'none'
					href				: 	srcZoom,
					title				:	title.replace(/\n/g, '<br />')
				}
			);
			img.css('cursor', 'hand');
			img.css('visibility', 'visible'); 
		});
		
		// Connect a fancybox to all anchors with name zoom
		container.find("a[name=zoom]").each(function(index) {
			var a = $(this);
			var img = a.find('img').first();
			var src = img.attr('src');
			var idx = src.lastIndexOf('.');
			var srcZoom = (src.indexOf('-t.') > 0) 
							? src.substr(0, src.indexOf('-t.')) + src.substr(idx)
							: src;
			
			var href = this.href ? this.href : srcZoom;
			var title = this.title ? this.title : img.attr('title');
			var alt = this.alt ? this.alt : img.attr('alt');
			if (title.length > 0 && alt.length > 0 && title.indexOf(alt) < 0) title += ' - ' + alt;
			a.fancybox(
				{
					showCloseButton		:	true,
					hideOnContentClick	:	true,
					centerOnScroll		:	true,
					titlePosition		:	'inside',	// 'outside', 'inside' or 'over'
					overlayOpacity		:	0.8,
					overlayColor		:	'#ddd',				
					transitionIn		:	'elastic',	// 'elastic', 'fade' or 'none'
					transitionOut		:	'elastic',	// 'elastic', 'fade' or 'none'
					href				: 	href,
					title				:	title.replace(/\n/g, '<br />')
				}
			);
		});	
	}
	
	function _insertBasePath(href) {
		var path = Misc.basePath();
		var imagePath = _findImagePath(href);
		return path + imagePath;
	}
	
	function _findImagePath(href) {
		var i;
		if ((i = href.indexOf('Images/')) >= 0) return href.substr(i);
		if ((i = href.indexOf('images/')) >= 0) return href.substr(i);
		if ((i = href.indexOf('pedigrees/')) >= 0) return href.substr(i);
		return 'Images/' + href.substr(Misc.pagePath().length);
	}
}

var Pictures = new _CPictures();


