/*
*		 Class:     BasicDialogLink
*    Author:    Aaron Conran
*    Date:      Feb. 27, 2007
*    Site:      http://www.divergingpath.com
*
*    Configurable script to implement popup windows
*    by simply setting a class on anchor tags.
*/

var BasicDialogLink = function() {
	/* user configurable defaults */
	var defaultHeight = 500;
	var defaultWidth = 700;
	var defaultSrc = 'about:blank';
	var defaultTitle = '';

	/* what class in the links would you like to popup */
	var clsName = 'window';

	/* variable to hold the dialog */
	var myDialog;

	/* return the public interface */
	return {
		/* our pseudo-constructor */
		init: function() {
			/* find all links with specified classname */
			var overrideLinks = Ext.DomQuery.select('a.' + clsName);
			/* loop over all the links to be overriden */
			for (var i = 0; i < overrideLinks.length; i++)
			{
				/* attach an onclick event handler */
				Ext.fly(overrideLinks[i]).on('click',
					/* all event handlers accept an event object */
					/* this event object has been standardized cross-browser */
					function(myEvent) {
						myEvent.preventDefault();
						BasicDialogLink.popUp(myEvent.getTarget());
					}
				);
			}
		},
		/* popUp a link into a BasicDialog */
		/* the link argument is an HTMLElement anchor link */
		popUp : function(link) {
			/* pull out the title, location, height and width */
			var title = link.getAttribute('alt') || link.getAttribute('title') || defaultTitle;
			var src = link.getAttribute('href') || defaultSrc;
			var height = link.getAttribute('height') || defaultHeight;
			var width = link.getAttribute('width') || defaultWidth;

			/* if myDialog has been created simply update it */
			if (myDialog)
			{
				/* set the title of the Dialog */
				myDialog.setTitle(title);
				/* check to see if we should resize */
				if ((myDialog.width != width) || (myDialog.height != height))
				{
					/* resizing seems to move position so center afterwards */
					/* this doesn't seem to work regardless :-( */
					myDialog.resizeTo(width, height);
					myDialog.center();
				}
				/* update the src of the iframe */
				Ext.fly('my-dlg-iframe').dom.setAttribute('src', src);

			/* if myDialog has NOT been created, create it */
			} else {
    			myDialog = new Ext.BasicDialog('my-dlg', {
                autoCreate:{
                    tag:'div',
                    cls:'x-dlg',
                    children:[ {
                        tag:'div',
                        id: 'my-dlg-hd',
                        cls:'x-dlg-hd',
                        html:title
                    },{
                        tag:'iframe',
                        cls:'x-dlg-bd',
                        src:src,
                        frameborder: 0,
                        id: 'my-dlg-iframe'
                    },{
                        tag:'div',
                        id: 'my-dlg-ft',
                        cls:'x-dlg-ft'
                    }]
                },
                height:height,
                width:width,
                shadow: true,
                modal: true,
                draggable:true,
                fixedcenter:true
            });
      }
      /* show the dialog regardless, animate from the link */
			myDialog.show(link);
		}
	};
}();
Ext.EventManager.onDocumentReady(BasicDialogLink.init, BasicDialogLink, true);
