/**
 * popup made simple by HorsMark :o)
 * usage name="popup_[type]_[id]_[width*height]
 */
$(document).ready(
	function() {
		$('a[name^=popup]').each(
			function(i, item) {
				var cfg = $(item).attr('name').split('_');
				if ( cfg.length < 2 ) return
				// Basics
				var type	= cfg[1]
				var width	= 540
				var height	= 400
				var error	= false
				// Check if height & width parsed
				if ( cfg.length > 3 ) {
					var size = cfg[3].split('x')
					if ( size.length == 2 ) {
						width	= parseInt(size[0])
						height	= parseInt(size[1])
						cfg.splice(3,1) // remove size from config
					}
				}
				// Get popup by type
				if ( type == 'ajax' ) {
					var href = '/_a'+'jax/' + cfg[2]
					$(item).attr('href',href)
				} else {
				// old types
					if ( cfg.length >= 3 ) {
						var id = cfg[2]
						var params = 'id=' + id + '&w='+width+'&h='+height
						switch(type){
							case 'form':
								$(item).attr('href','/a'+'ction/module/talkback/form/?' + params)
								break;
							case 'publish':
								$(item).attr('href','/a'+'ction/module/publish/?' + params)
								break;
							case 'media':
								$(item).attr('href','/a'+'ction/module/media/?' + params)
								break;
							default:
								error = true
								break;
						}
					}
				}

				// Append popup if no errors
				if ( !error ) {
					$(item).fancybox(
						{
							'frameWidth'			: width,
							'frameHeight'			: height,
							'hideOnContentClick'	: false,
							'showLoading'			: true,
							'callbackOnStart'		: function () { $.fn.fancybox.showLoading(); },
							'callbackOnShow' 		: function() {
								$(".fancy_loading").hide();
								if(typeof('popup.init') == 'function') popup.init(this)
							}
						}
					);
				}
			}
		);
	}
);