(function($) {
	var fancyboxTypeMapping = {
		'swf': ['swf', 'flv'],
		'image': ['jpg', 'png', 'gif', 'jpeg' ,'tiff', 'bmp']
	};

	function getFancyboxTypeBySuffix(suffix, defaultType)
	{
		for (type in fancyboxTypeMapping)
		{
			if ($.inArray(suffix, fancyboxTypeMapping[type]) >= 0)
				return type;
		}
		return defaultType;
	};

	$.fn.lrworld_OpenLightbox = function()
	{
		this.each(function() {
			if ($(this).data('_lrworld_openLightbox') === 'initialized')
				return
			$(this).data('_lrworld_openLightbox', 'initialized');

			$(this).click(function(e) {
				e.stopPropagation();
				e.preventDefault();

				var href = this.href.replace(/^(.*?)\?.*/, '$1').replace(window.document.baseURI, ''),
					fileSuffix = href.replace(/.*\./, ''),
					width = 800,
					height = 600,
					type = getFancyboxTypeBySuffix(fileSuffix, type),
					rel = $.trim($(this).attr('rel')),
					parts = null,
					autoDimensions = true;

				if (rel.length > 0)
				{
					$(rel.split(',')).each(function(index, value) {
						parts = value.split('=');

						if (parts[0] === 'width')
							width = parseInt(parts[1]);
						else if (parts[0] === 'height')
							height = parseInt(parts[1]);
						else if (parts[0] === 'type')
							type = parts[1];
					});
				}

				if (type == 'ajax' || type == 'inline')
					autoDimensions = false;

				var fancyboxHref = this.href.replace(window.location.protocol +'//' + window.location.host + '/', '');
				if (fileSuffix === 'flv')
				{
					fancyboxHref = '/fileadmin/lrcore/system/templates/flash/player.swf?auto=1&videoFile=/' + fancyboxHref;
				} // if

				var options = {
					'padding':       10,
					'autoScale':     false,
					'autoDimensions': autoDimensions,
					'transitionIn':  'none',
					'transitionOut': 'none',
					'width':         width,
					'height':        height,
					'href':          fancyboxHref,
					'type':          type,
					'swf': {
						'wmode':           'transparent',
						'allowfullscreen': true
					}
				};

				$(document).trigger('hideLayer');
				$.fancybox(options);
			});
		});
	};
})(jQuery);
