X.createSingleton('BHG.ModalFactoryPhoto',
	// Constructor
	function BHGModalFactory()
	{
	    jQuery(document).ready(function() {
	        jQuery(".modalphoto").bind("click", function(e) {
                e.preventDefault();
                var holdingNode = jQuery(this).closest(".person-sbImgHold, .office-sbImgHold");
                var descHTML = jQuery(".textHoldInner:first", holdingNode).html();
                descHTML = jQuery.trim(descHTML);

                var modalSettings =
                {
                    wrapperClasses: ['bhg-modal-photo-wrap'],
                    x: e.pageX,
                    y: e.pageY,
                    fixed: false,
                    header: "&nbsp;",
                    body: '<img id="PhotoModalImage" src="' + this.href + '" \/>' + descHTML
                };
                BHG.ModalFactoryPhoto.create(modalSettings);
            });
	    });
	},
	// Prototype Members
	{
		create: function(userSettings)
		{
			var settings =
		    {
			    wrapperClasses: ['bhg-modal'],
			    x: null,
			    y: null,
			    fixed: true,
			    showHandler: this.onModalShow,
			    createHandler: this.onModalCreate
		    };
		    
		    // Take care of user settings
		    if (typeof(userSettings) !== "undefined")
		    {
		        if (userSettings.wrapperClasses != null)
		        {
		            settings.wrapperClasses = userSettings.wrapperClasses;
		        }
		        if (userSettings.x != null)
		        {
		            settings.x = userSettings.x;
		        }
		        if (userSettings.y != null)
		        {
		            settings.y = userSettings.y;
		        }
		        if (userSettings.fixed != null)
		        {
		            settings.fixed = userSettings.fixed;
		        }
		        if (typeof(userSettings.createHandler) === "function")
		        {
		            settings.createHandler = this.onModalCreate.delegate(this, userSettings.createHandler);
		        }
		    }

		    var modalBody = [];
		    modalBody[modalBody.length] = '<div class="bhg-modal-photo">';
		    modalBody[modalBody.length] = '<div class="bhg-modal-photoTop"><div class="first"></div><div></div></div>';
		    modalBody[modalBody.length] = '<div class="bhg-modal-photoBd">' + userSettings.body + '</div>';
		    modalBody[modalBody.length] = '<div class="bhg-modal-photoBot"><div class="first"></div><div></div></div>';
		    modalBody[modalBody.length] = '</div>';
		    X.ModalFactory.create(modalBody.join(''), settings);
		},
		onModalShow: function(modal, callback)
		{
		    var $modal = jQuery(modal.selector);
		    //assuming mouse position if X and Y are not null
		    if((modal.settings.x != null)&&(modal.settings.y != null)) {
		        var excessWidth = $modal.find(".bhg-modal-photoBd").outerWidth() - $modal.find(".bhg-modal-photoBd").width();
		        $modal.width(jQuery("#PhotoModalImage").width()+ excessWidth);
		        $modal.parent().width(jQuery("#PhotoModalImage").width()+ excessWidth);
		        $modal.find(".bhg-modal-photo").width(jQuery("#PhotoModalImage").width()+ excessWidth);
		        $modal.find(".bhg-modal-photoBd").width(jQuery("#PhotoModalImage").width());
		        var mOffset_x = 20;
		        var mOffset_y = 20;
	            var reset_x = (modal.settings.x - $modal.width()) + mOffset_x;
	            var reset_y = (modal.settings.y - $modal.height()) + mOffset_y;
		        $modal.parent().css("left", reset_x); 
		        $modal.parent().css("top", reset_y);
		        
		    }
		    if (typeof(callback) === "function")
		    {
	            callback(modal);
	        }
		},
		onModalCreate: function(modal, callback)
		{
		   jQuery('#layers-modal-cover').append('<a href="#" class="total-link"><\/a>').click(function(e)
	        {
	            e.preventDefault();
	            modal.hide();
	        });
	        if (typeof(callback) === "function")
		    {
	            callback(modal);
	        }
		}
	}
);
