function open_popup_dialog( link ) {
	var dialog = document.createElement("div");
	var options = link.getAttribute( "options" );
	var buttons = link.getAttribute( "buttons" );

	dialog.setAttribute( "title", link.getAttribute( "title" ) ? link.getAttribute( "title" ) : '');
	
	if( !options ) {
		alert("É preciso especificar a altura e largura da janela" );
		return false;
	}
	
	options = parseOptions( options );
	
	if( !options.width ) {
		alert("É preciso especificar a largura da janela.");
		return false;
	}
	
	if( !options.height ) {
		alert("É preciso especificar a altura da janela." );
		return false;
	}
	
	if( buttons )
		buttons = parseButtons( buttons );
	
	document.body.appendChild(dialog);
	
	$(dialog).load( link.getAttribute( "href" ) || link, function(){ if( typeof onLoad == 'function' ) onLoad( ); } );
	$(dialog).dialog({
		width: options.width,
		height: options.height,
		position: "center",
		modal: options.modal,
		close: function() {
			$(this).dialog("destroy");
			document.body.removeChild( this );
		},
		open: function() {
			$(dialog).dialog( "option", "position", "center" );
		},
		buttons: buttons ? buttons : {}
	});
}

function show_popup_dialog( href, buttons, width, height, modal )
{
	var link = document.createElement( 'a' );
	
	link.setAttribute( 'href', href );
	
	// Cria os botões
	var strButtons = '';
	for( var property in buttons ){
		strButtons += "'" + property + "':";
		strButtons += buttons[property];
	}
	
	link.setAttribute( 'buttons', strButtons );
	
	link.setAttribute( 'options', "width: " + width + "; height: " + height + "; modal: " + modal );
	
	open_popup_dialog( link );
}

function parseOptions( options ) {
	var arrOptions = options.split(/\;/);
	var obj = "{";
	
	if( /^$/.test(arrOptions[arrOptions.length - 1]) )
		arrOptions.pop();
		
	for(var i = 0; i < arrOptions.length; ++i ) {
		obj += arrOptions[i];
		
		if( i != ( arrOptions.length - 1 ) )
			obj += ",";
	}
	obj += "}";
	
	return eval('(' + obj + ')');
}

function parseButtons( buttons ) {
	var arrButtons = buttons.match(/[^\:]+\:[^\{]+\{[^\}]+\}/g);
	var obj = "{";
	
	for( var i = 0; i < arrButtons.length; ++i ) {
		obj += arrButtons[i];
		
		if( i != arrButtons.length - 1 )
			obj += ',';
	}
	
	obj += "}";
	return eval( "(" + obj + ")" );
}

function apply_links(parent) {

   
	$(parent).find(".popup_dialog").click(function( event ) {
      //this.onclick = function( ) {
         event.preventDefault();
         open_popup_dialog( this );
		 
         //return false;
      //}
	});
}

$(document).ready(function() {
   apply_links(this);
});
