/**
 * @class Modal popup container
 */

/**
 * @object {TLightboxButton}
 * @property {Function} click Button click handler
 * @property {String} content Custom button content
 * @property {String} label Button label
 * @property {String} name Part of lightbox button id
 */

/**
 * @constructor Creates new instance
 * @param {String} sId ID attribute of main HTML container
 * @param {Object} oParams Object settings
 * 		buttons {TLightboxButton[]} Set of buttons
 * 		noTitle {Boolean} Flag to skip title area
 */
function Lightbox(sId, oParams)
{
	this.id	= sId;
	this.sel	= '#' + sId; // JQ selector
	
	/**
	 * Map of disabled buttons
	 */
	this.disabledButtons	= {};
	
	/**
	 * Map of button click handlers
	 */
	this.handlers	= {};
	
	oParams	= oParams || {};
	oParams.buttons	= oParams.buttons || [];
	
	$(document.body).append([
		'<div id="',
		sId,
		'-overlay" class="lightbox-overlay ui-widget-overlay ui-helper-hidden">',
		'</div>',
        '<div id="',
		sId,
		'" class="lightbox ui-helper-hidden">',
            '<div class="lightbox-close-button" id="',
			sId,
			'-close-button"></div>',
            oParams.noTitle ? '' : '<div class="lightbox-title"></div>',
            '<div class="lightbox-content"></div>',
            '<div class="lightbox-button-pane">',
				this.getButtonsHTML(oParams.buttons),
			'</div>',
        '</div>'
	].join(''));
	
	// setup hide button
	$(this.sel + '-close-button').click($.proxy(this.hide, this));
	
	this.setup(oParams);
	this.setupButtons(oParams.buttons);
}

var oProto	= Lightbox.prototype;

/**
 * Opens lightbox
 */
oProto.open	= function()
{
	var oOverlay	= $(this.sel + '-overlay').show();
	$(this.sel)
		.show()
		.position({
			my: 'center center',
			at: 'center center',
			of: oOverlay
		});
};

/**
 * Hides lightbox
 */
oProto.hide	= function()
{
	$(this.sel + ', ' + this.sel + '-overlay').hide();
};

/**
 * Configures lightbox
 * @param {String} sId ID attribute of main HTML container
 * @param {Object} oParams Object settings
 */
oProto.setup	= function(oParams)
{
	var sSel	= this.sel;

	oParams	= oParams || {};
	oParams.title && setContent('title', oParams.title);
	oParams.content && setContent('content', oParams.content);
	
	
	function setContent(sClass, vContent)
	{
		var oBox	= $(sSel + ' .lightbox-' + sClass);
		
		if (typeof vContent == 'string')
			oBox.html(vContent);
		else
			oBox.append(vContent);
	}	
};

/**
 * Gets HTML for button
 * @param {TLightboxButton} oParams Button settings
 */
oProto.getButtonHTML	= function(oParams)
{
	return [
		'<button id="',
		this.id,
		'-',
		oParams.name,
		'-button" class="lightbox-button">',
		oParams.content || '<span></span><img src="/images/arrow-right.png" />',
		'</button>',
	].join('');
};

/**
 * Gets HTML for list of buttons
 * @param {Object[]} aConfigs Button configs
 */
oProto.getButtonsHTML	= function(aConfigs)
{
	for (var aBuff = [], nIndex = 0, nCount = aConfigs.length; nIndex < nCount; )
		aBuff.push(this.getButtonHTML(aConfigs[nIndex++]));
		
	return aBuff.join('');
};

/**
 * Configures button
 * @param {TLightboxButton} oParams Button settings
 */
oProto.setupButton	= function(oParams)
{
	var oButton	= $(this.sel + '-' + oParams.name + '-button')
		.hover(this.processMouseEnter, this.processMouseLeave)
		.click($.proxy(this.processButtonClick, this));
		
	this.handlers[oParams.name]	= oParams.click;
		
	if (oParams.label)
		$(this.sel + '-' + oParams.name + '-button span').html(oParams.label);
};

/**
 * Configures buttons
 * @param {TLightboxButton[]} aConfigs Button settings
 */
oProto.setupButtons	= function(aConfigs)
{
	for (var nIndex = 0, nCount = aConfigs.length; nIndex < nCount; )
		this.setupButton(aConfigs[nIndex++]);
};

/**
 * Process mouse enter
 */
oProto.processMouseEnter	= function()
{
	$(this).addClass('lightbox-button-hover');
};

/**
 * Process mouse leave
 */
oProto.processMouseLeave	= function()
{
	$(this).removeClass('lightbox-button-hover');
};

/**
 * Gets button
 * @param {String} sName Button name
 */
oProto.getButton	= function(sName)
{
	return $(this.sel + '-' + sName + '-button');
};

/**
 * Disables button
 * @param {String} sName Button name
 * @return {JQObject} Button
 */
oProto.disableButton	= function(sName)
{
	this.disabledButtons[sName]	= true;
	return this.getButton(sName)
		.addClass('lightbox-button-disabled')
		.attr('disabled', true);
};

/**
 * Enables button
 * @param {String} sName Button name
 * @return {JQObject} Button
 */
oProto.enableButton	= function(sName)
{
	this.disabledButtons[sName]	= false;
	return this.getButton(sName)
		.removeClass('lightbox-button-disabled')
		.attr('disabled', false);
};

/**
 * Set enabled state for button
 * @param {String} sName Button name
 * @param {Boolean} bEnabled Enabled state
 * @return {JQObject} Button
 */
oProto.makeButtonEnabled	= function(sName, bEnabled)
{
	return bEnabled ? this.enableButton(sName) : this.disableButton(sName);
};

/**
 * Processes button click
 */
oProto.processButtonClick	= function(oEvent)
{
	var sName	= oEvent.currentTarget.id
		.replace(/-button$/, '').substr(this.id.length + 1);
	
	if (!this.disabledButtons[sName] && this.handlers[sName])
		this.handlers[sName]();
};
