/**
 * suBox - jQuery lib
 * Simple lightbox alternative
 *
 * Examples and documentation at: http://www.subject.sk
 *
 * Copyright (c) 2011 Ondrej Balko
 * That said, it is hardly a one-person project.
 *
 * Version: 0.2 (2011-03-31)
 * Requires: jQuery v1.4.4+
 */

var subox =
{
	init: function (settings, link)
	{
		this.link = link;
		this._mergeSettings(settings);
		
		if (this.link != undefined && this.link != '')
		{
			this._create();
		}
	},
	_mergeSettings: function (settings)
	{
		// settings
		this.settings = jQuery.extend
		(
			{
				'width'					: 800,
				'height'				: 400,
				'overlayColor'			: '#000',
				'overlayOpacity'		: 0.7,
				'padding'				: 10,
				'type'					: 'auto', //auto,image,data,inline,iframe,
				'modal'					: false,
				'hideOnOverlayClick'	: false,
				'centerOnScroll'		: true,
				'enableEscapeButton'	: true,
				'scrolling'				: 'auto', //auto,yes,hidden
				'showCloseButton'		: true,
				'overlayShow'			: true
			},
			settings
		);
	},
	_createEvents: function ()
	{
		if (subox.settings.enableEscapeButton == true)
		{
			$(document).bind
			(
				'keydown.sb',
				function(e)
				{
					if (e.keyCode == 27)
					{
						e.preventDefault();
						subox.close();
					}
				}
			);
		}
		
		if (this.settings.centerOnScroll)
		{
			$(window).bind('scroll.sb', this._center);
		}
		
		$(window).bind('resize.sb', this._center);
		
		if (this.settings.hideOnOverlayClick == true)
		{
			this.overlay.bind
			(
				'click.sb',
				function ()
				{
					subox.close();
				}
			);
		}
		
		if (this.settings.showCloseButton)
		{
			this.closeButton.show();
			this.closeButton.click
			(
				function ()
				{
					subox.close();
				}
			);
		}
	},
	_center: function()
	{
		if (subox.wrap == undefined)
		{
			return;
		}
		
		subox.wrap.stop().animate
		(
			{
				'top'	: parseInt($(document).scrollTop() + ($(window).height() - (subox.content.height() + 2 * subox.settings.padding)) * 0.5),
				'left'	: parseInt($(document).scrollLeft() + ($(window).width() - (subox.content.width() + 2 * subox.settings.padding)) * 0.5)
			},
			typeof arguments[0] == 'number' ? arguments[0] : 200
		);
	},
	_create: function ()
	{
		if ($('#subox-wrap').length)
		{
			return;
		}
		
		$('body').append
		(
			this.loading	= $('<div id="subox-loading"><div></div></div>'),
			this.overlay	= $('<div id="subox-overlay"></div>'),
			this.wrap		= $('<div id="subox-wrap"></div>')
		);
		
		this.outer = $('<div id="subox-outer"></div>')
			.append('<div class="subox-bg" id="subox-bg-n"></div><div class="subox-bg" id="subox-bg-ne"></div><div class="subox-bg" id="subox-bg-e"></div><div class="subox-bg" id="subox-bg-se"></div><div class="subox-bg" id="subox-bg-s"></div><div class="subox-bg" id="subox-bg-sw"></div><div class="subox-bg" id="subox-bg-w"></div><div class="subox-bg" id="subox-bg-nw"></div>')
			.appendTo(this.wrap);
		
		this.outer.append
		(
			this.content		= $('<div id="subox-content"></div>'),
			this.closeButton	= $('<a id="subox-close"></a>'),
			this.title			= $('<div id="subox-title"></div>')
		);
		
		this._showOverlay();
		this._showWrap();
		this._createEvents();
	},
	_showWrap: function()
	{
		if (subox.settings.height > $(window).height() * 0.85)
		{
			var newHeight = $(window).height() * 0.85;
			var konst = subox.settings.height / newHeight;
			subox.settings.width = Math.round(subox.settings.width / konst);
			subox.settings.height = Math.round(newHeight);
		}
		else
		{
			if (subox.settings.width > $(window).width() * 0.85)
			{
				var newWidth = $(window).width() * 0.85;
				var konst = subox.settings.width / newWidth;
				subox.settings.height = Math.round(subox.settings.height / konst);
				subox.settings.width = Math.round(newWidth);
			}
		}
		
		this.hideActivity();
		
		this.content.css
		(
			{
				'background-color'	: '#fff',
				'height'			: subox.settings.height,
				'width'				: subox.settings.width,
				'padding'			: subox.settings.padding,
				'overflow'			: subox.settings.scrolling
			}
		);
		
		this.wrap.removeAttr('style');
		subox.wrap.css
		(
			{
				'top'		: parseInt($(document).scrollTop() + ($(window).height() - (subox.content.height() + 2 * subox.settings.padding)) * 0.5),
				'left'		: parseInt($(document).scrollLeft() + ($(window).width() - (subox.content.width() + 2 * subox.settings.padding)) * 0.5)
			}
		);
		this.wrap.show();
	},
	close: function ()
	{
		this._hideOverlay();
		if (this.loading == undefined)
		{
			$('#subox-loading').remove();
		}
		else
		{
			this.loading.remove();
		}
		if (this.wrap == undefined)
		{
			$('#subox-wrap').remove();
		}
		else
		{
			this.wrap.remove();
		}
	},
	_showOverlay: function ()
	{
		if (this.settings.modal)
		{
			this.settings.overlayShow = true;
			this.settings.hideOnOverlayClick = false;
			this.settings.hideOnContentClick = false;
			this.settings.enableEscapeButton = false;
			this.settings.showCloseButton = false;
		}
		
		if (this.settings.overlayShow == true)
		{
			this.overlay.css
			(
				{
					'background-color'	: this.settings.overlayColor,
					'opacity'			: this.settings.overlayOpacity,
					'cursor'			: this.settings.hideOnOverlayClick ? 'pointer' : 'auto',
					'height'			: $(document).height(),
					'width'				: $(document).width()
				}
			);
			
			this.overlay.show();
		}
		else
		{
			this._hideOverlay();
		}
	},
	_hideOverlay: function ()
	{
		if (this.overlay == undefined)
		{
			$('#subox-overlay').hide();
		}
		else
		{
			this.overlay.hide();
		}
	},
	_error: function ()
	{
		this.contentHtml('<p id="subox-error">The requested content cannot be loaded.<br />Please try again later.</p>');
	},
	showActivity: function()
	{
		this.loading.show();
	},
	hideActivity: function()
	{
		this.loading.hide();
	},
	contentImage: function (link)
	{
		this.link = link;
		
		var imgPreloader = new Image();
		imgPreloader.src = this.link;
		
		imgPreloader.onload = function()
		{
			imgPreloader.onerror = imgPreloader.onload = null;
			
			subox.settings.width = imgPreloader.width;
			subox.settings.height = imgPreloader.height;
			
			subox._create();
			
			subox.showActivity();
			
			$('<img />').attr
			(
				{
					'id'	: 'subox-img',
					'src'	: imgPreloader.src,
					'alt'	: subox.settings.title
				}
			).appendTo(subox.content);
			
			subox.hideActivity();
		};
	},
	contentHtml: function (data)
	{
		this._create();
		this.showActivity();
		this.content.html(data);
		this.hideActivity();
	},
	contentUrl: function (link)
	{
		this.link = link;
		
		this._create();
		this.showActivity();
		this.content.load(this.link);
		this.hideActivity();
	},
	contentIframe: function (link)
	{
		this.link = link;
		
		this._create();
		this.showActivity();
		$('<iframe id="subox-frame" name="subox-frame' + new Date().getTime() + '" frameborder="0" hspace="0" scrolling="' + subox.scrolling + '" src="' + this.link + '"></iframe>').appendTo(this.content);
		this.hideActivity();
	}
};
