/**
 * @author Vlad Yakovlev (red.scorpix@gmail.com)
 * @link www.scorpix.ru
 * @version 0.1
 * @requires jQuery
 * @requires jCommon
 */
$(function() {
	var defWidth = 500;
	var defHeight = 400;

	$('.modal').each(function() {
		var el = $(this);
		var sizes = getSizes(this);

		var height = sizes.height ? sizes.height : defHeight;
		var width = sizes.width ? sizes.width : defWidth;

		$c.popupBlock('#popup', {
			fader: '#fader',
			link: el,
			close: '#popup .close',
			beforeShow: function() {
				$('#popup').css({
					height: height,
					width: width,
					marginLeft: -Math.round(width / 2),
					marginTop: -Math.round(height / 2)
				});

				var contentEl = el.find('.modal_content');

				if (contentEl.length) {
					$('#popup .popup_content').html(contentEl.html());
				} else {
					$('#popup .popup_content').html('<img alt="' + el.attr('title') + '" src="' + el.attr('href') + '" width="' + width + '" height="' + height + '" />');
				}

			}
		});


		function getSizes(el) {
			var prefix = 'modal_';
			var classes = $(el).attr('class').split(' ');
			var sizes = {};

			for (var i = 0; i < classes.length; i++) {
				if (prefix == classes[i].substr(0, prefix.length)) {
					var parts = classes[i].substr(prefix.length).split('_');

					if (undefined !== parts[0]) {
						sizes.width = parseInt(parts[0], 10);
					}
					if (undefined !== parts[1]) {
						sizes.height = parseInt(parts[1], 10);
					}

					return sizes;
				}
			}

			return sizes;
		}
	});
});

$(function() {
	$c.popupBlock('.facebook_popup', {
		link: '.facebook_button',
		close: '.facebook_popup .bottom .close'
	});
});