String.prototype.trim = function () {
	return this.replace(/(^\s+)|\s+$/g, "")
}


function getPopup() {
	var $pp = $('#popup');
	if ($pp.length) $pp.remove();
	$('body').append('<div id="popup"></div>');
	return $('#popup');
}


function mediaPopup(file, ttl) {
	if (typeof ttl == 'undefined') ttl = '';
	$.ajax({
		data: {
			'void': 'front-ajax',
			action: 'media-props',
			file: file
		},
		success: function(d){
			if (d.ok) {
				var $pp = getPopup();
				$pp.html('<img src="/assets/media/' + file + '" />');
				$pp.dialog({
					draggable: false,
					resizable: false,
					modal: true,
					title: ttl,
					width: d.data.w + 27,
					height: d.data.h + 50
				});
			}
		}
	});
}


$(document).ready(function() {
	$.metadata.setType('class');
	$.ajaxSetup({
		 url: '/ajax.php',
		 global: true,
		 type: 'POST',
		 dataType: 'json',
		 timeout: 22222,
		 data: vars.ajax.data
	});

	$('.image-with-popup').click(function(e){
		var $t = $(e.target);
		//console.dir(arguments);
		var $el = $t.is('a') ? $t : $t.parents('a');
		var m = $el.metadata();
		//console.log($el.attr('rel'));
		var $pp = getPopup();
		$pp.html('<img src="' + $el.attr('rel') + '" />');
		$pp.dialog({
			draggable: false,
			resizable: false,
			modal: true,
			title: m.t,
			width: m.w + 27,
			height: m.h + 50,
			open: function(e, ui) {
//				$('html').css('overflow', 'hidden');
			},
			close: function(e, ui) {
//				$('html').css('overflow', 'auto');
			}
		});
	});
});
