﻿// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};

$(document).ready(function() {

	$('.gallery a[href$=".htm"]').each(function(index) {
		var url = $(this).get(0).href;
		var parsedUrl = parseUri(url);
		var host = parsedUrl.host;
		var path = parsedUrl.directory;
		var name = parsedUrl.file.replace('.htm','');
		url = "/panorama.php?name=" + name + "&title=" + $(this).attr('title');
		if($(this).hasClass('360cities')) {
			url = url + "&360cities=true";
		}
		else {
			url = url  + "&path=" + path;
		}
		$(this).attr('href', url);
	});
	
	$('.gallery a').fancybox({
			width			: '98%',
			height			: '95%',
			openEffect		: 'elastic',
			closeEffect		: 'elastic',
			openOpacity		: true,
			closeOpacity	: true,
			openEasing		: 'easeOutBack',
			closeEasing		: 'easeInBack',
			nextEffect		: 'none',
			prevEffect		: 'none',
			arrows			: false,
			closeBtn		: false,
			scrolling		: 'no',
			loop			: false,
			type			: 'iframe',
			helpers : {
				title : {
					type 	: 'inside'
			}
		},	
		keys: {
			next		: [13, 32, 33, 78, 110], // enter, space, page up, N, n
			prev		: [8, 34, 80, 112], // backspace, page down, P , p
			close		: [27] // escape key
		},
		beforeShow : function() {
			var currentPage = this.index+1;
			var maxPage = $.fancybox.group.length;
			// Get previous page title
			var previousPageTitle = '';
			if (currentPage>1) {
				previousPageTitle = $($.fancybox.group[this.index-1]).attr('title');
				previousPageTitle = previousPageTitle!=null&&previousPageTitle!='undefined'&&previousPageTitle.length?'Previous - ' + previousPageTitle:'Previous';
			}
			// Get next page title
			var nextPageTitle = '';
			if (currentPage<maxPage) {
				nextPageTitle = $($.fancybox.group[this.index+1]).attr('title');
				nextPageTitle = nextPageTitle!=null&&nextPageTitle!='undefined'&&nextPageTitle.length?'Next - ' + nextPageTitle:'Next'
			}

			// Get title from url if available
			var newTitle = this.title==null?$($.fancybox.group[this.index]).text():this.title;
			if ($($.fancybox.group[this.index]).hasClass('360cities')) {
				newTitle = '<a title="panorama photos of ' + this.title + ' on 360cities.net" href="http://www.360cities.net/image/' + parseUri($($.fancybox.group[this.index]).attr('href')).queryKey.name + '">' + this.title + '</a>' +
				' in ' +
				'<a href="' + $($.fancybox.group[this.index]).attr('areaHref') + '" title="panoramic images from ' + $($.fancybox.group[this.index]).attr('areaText') + '">' + $($.fancybox.group[this.index]).attr('areaText')+ '</a>';
			}

		    this.title = '<div ' +
		  		(currentPage<=1?'':'onclick="$.fancybox.prev();" ') +
		  		'title="' + previousPageTitle + '" ' +
				'class="fancybox-item fancybox-custom-prev' + (currentPage<=1?' fancybox-custom-disabled':'') + '" ' +
				(currentPage<=1&&currentPage>=maxPage?' style="display:none;" ':' ') + '/>' +
				'	<div ' +
				(currentPage>=maxPage?'':'onclick="$.fancybox.next();" ') +
		  		'title="' + nextPageTitle + '" ' +
				'class="fancybox-item fancybox-custom-next' + (currentPage>=maxPage?' fancybox-custom-disabled':'')+ '" ' +
				(currentPage<=1&&currentPage>=maxPage?' style="display:none;" ':' ') + '/>' +
				'	<div onclick="$.fancybox.close();" title="Close" class="fancybox-item fancybox-close"/>' +
				'<b>' + newTitle + '</b>' + 
				(currentPage<=1&&currentPage>=maxPage?'':'<br>Panoramic ' + currentPage + ' of ' + maxPage)
				;
		}
	});
});

