/**
 * LaPrème Window
 *
 * @copyright LaPrème Nijmegen
 * @author Leon van der Veen
 */
var lpWindow = lpClass({
	/**
	 * Options
	 */
	'defaultOptions': {
		'windowMainClass': 'windowContainer',
		'windowClass': 'windowContent',
		'headClass': 'windowHead',
		'bodyClass': 'windowBody',
		'fadeSpeed': 'slow',
		'width': null,
		'height': null,
		'position': null, // middle of screen
		'closeOnClickOutside': true,
		'closeOnClickClose': true,
		'title': null
	},
	
	
	/**
	 * Temp HTML dump
	 */
	
	/*
	
	<div class="windowContainer" id="lpWindow-0">
		<div class="windowBg">
			<a class="windowClose" href="javascript:jQuery.lpWindow("lpWindow-0");"></a>
		</div>
		<div class="windowContent">
			<div class="windowHead">
				<h2></h2>
			</div>
			<div class="windowBody">
				
			</div>
		</div>
	</div>
	
	*/
	
	/**
	 * Init
	 *
	 * @param object Options
	 */
	'initialize': function(options)
	{
		// Set options
		this.setOptions(options);
		
		// Make window
		this.win = jQuery('<div/>')
			.attr('id', 'lpWindow-' + lpWindow.instanceCounter++)
			.attr('class', this.options.windowMainClass);
		
		// Make background
		this.winBackground = jQuery('<div/>')
			.attr('class', 'windowBg');
		this.win.append(this.winBackground);
		
		// Make outside click close window
		this.winClose = jQuery('<a></a>')
			.attr('class', 'windowClose');
		this.winBackground.append(this.winClose);
		
		// Make window content
		this.winContent = jQuery('<div/>')
			.attr('class', 'windowContent');
		this.win.append(this.winContent);
		
		// Make head
		this.head = jQuery('<div/>')
			.attr('class', 'windowHead')
			.html('&nbsp;');
		this.winContent.append(this.head);
		
		// Make body
		this.body = jQuery('<div/>')
			.attr('class', 'windowBody')
			.html('&nbsp;');
		this.winContent.append(this.body);
		
		// Resize
		jQuery(window).resize(jQuery.proxy(this.fixPosition, this));
		
		// Close on click outside?
		if (this.options.closeOnClickOutside)
		{
			jQuery('html').click(jQuery.proxy(function()
			{
				// Ignore close event?
				if (this.windowHidden)
				{
					return;
				}
				
				// Hide
				this.hide();
			}, this));
			this.win.click(function(ev)
			{
				ev.stopPropagation();
			});
		}
		
		// Close on click close?
		if (this.options.closeOnClickClose)
		{
			jQuery('> a.windowClose', this.win).live('click', jQuery.proxy(function(ev)
			{
				// Ignore close event?
				if (this.windowHidden)
				{
					return;
				}
				
				// Hide
				this.hide();
			}, this));
		}
		
		// Default hidden
		this.windowHidden = true;
		this.win.hide();
		
		// Add to dom
		jQuery('body').append(this.win);
		
		// Title?
		if (this.options.title)
		{
			this.setTitle(this.options.title);
		}
	},
	
	/**
	 * Show
	 *
	 * @param mixed Speed
	 * @return This
	 */
	'show': function(speed)
	{
		this.fixPosition();
		
		// Fade in
		this.win.fadeIn(speed || this.options.fadeSpeed, jQuery.proxy(function()
		{
			this.windowHidden = false;
		}, this));
		
		return this;
	},
	
	/**
	 * Hide
	 *
	 * @param mixed Speed
	 * @return This
	 */
	'hide': function(speed)
	{
		// Remove from body
		this.win.fadeOut(speed || this.options.fadeSpeed, jQuery.proxy(function()
		{
			this.windowHidden = true;
		}, this));
		
		return this;
	},
	
	/**
	 * Set title
	 *
	 * @param string Title
	 * @return This
	 */
	'setTitle': function(title)
	{
		// Set title
		if (typeof title == 'undefined' || typeof title == 'string')
		{
			this.head.html('<h2>' + title + '</h2>' || '&nbsp;');
		}
		else
		{
			this.head.empty().append('<h2>' + title + '</h2>');
		}
		
		return this;
	},
	
	/**
	 * Set body
	 *
	 * @param string Body
	 * @return This
	 */
	'setBody': function(body)
	{
		// Set body
		if (typeof body == 'undefined' || typeof body == 'string')
		{
			this.body.html(body || '&nbsp;');
		}
		else
		{
			this.body.empty().append(body);
		}
		
		// Rest arguments?
		for (var i = 1; i < arguments.length; ++i)
		{
			this.body.append(arguments[i]);
		}
		
		return this;
	},
	
	/**
	 * On resize
	 */
	'fixPosition': function()
	{
		// Position mode
		if (this.options.position)
		{
			if (this.options.position.x && this.options.position.y) // cordinate
			{
				this.win.css({
					'left': this.options.position.x,
					'top': this.options.position.y
				});
			}
			else if (this.options.position.relativeTo) // relative to
			{
				this.win.css({
					'left': this.options.position.relativeTo.position().left + (this.options.position.dx || 0),
					'top': this.options.position.relativeTo.position().top + (this.options.position.dy || 0)
				});
			}
		}
		else
		{
			this.win.css({
				'left': Math.floor((jQuery(window).width() - this.win.outerWidth(true)) / 2.0),
				'top': Math.floor((jQuery(window).height() - this.win.outerHeight(true)) / 2.0)
			});
		}
	}
});

/**
 * Static vars
 */
lpWindow.instanceCounter = 0;

