/**
 * Popup block
 * (requires jQuery)
 *
 * @author Stepan Reznikov [stepan.reznikov@gmail.com]
 * @version 2.0 (2008-07-07)
 *
 * @param {Object} options Object with options
 */

 function litebox_hot_show() {
 document.getElementById('litebox_hot').style.display = 'block';
 document.getElementById('hot_show').style.display = 'none';
 document.getElementById('jqi').style.top = '5%';
 }
 function litebox_hot_hide() {
 document.getElementById('litebox_hot').style.display = 'none';
 document.getElementById('hot_show').style.display = 'block';
 document.getElementById('jqi').style.top = '15%';
 }

var popup_block = function (options) {

	var that = {};

	var keep = false,
	    documentClickHandler,
	    documentKeyDownHandler;


	that.hide = function (event) {

		if (keep) {
			keep = false;
			return;
		} else {
			if (options.onBlockHide) {
				options.onBlockHide(options);
			}
		}

		if (options.fader) {
			options.fader.addClass("hidden");
			$("select").css("visibility","visible");
		}
		options.container.addClass("hidden");

		$(document).unbind("click", documentClickHandler);
		$(document).unbind("keydown", documentKeyDownHandler);
	};


	that.cancel = function (event) {
		var code = event.keyCode ? event.keyCode : event.which ? event.which : null;
		if (code === 27) {
			that.hide(event);
		}
	};


	that.show = function (event) {

		options.container.removeClass("hidden");
		if (options.onBlockShow) {
			options.onBlockShow(options);
		}

		if (options.fader) {
			options.fader.removeClass("hidden");
			//$("select").css("visibility","hidden");
			options.fader.css("height",$(document).height());
		}

		documentClickHandler = function (event) {
			that.hide(event);
		};

		documentKeyDownHandler = function (event) {
			that.cancel(event);
		};

		//$(document).click(documentClickHandler);
		$(document).click(
			function(evt){
				if( (keep || options.keep) && (!evt.which || evt.which === 1) ){
					that.hide();
				}
			}
		);
		$(document).keydown(documentKeyDownHandler);
	};


	that.toggle = function (event) {
		event.preventDefault();
		event.stopPropagation();

		if (options.container.hasClass("hidden")) {
			that.show(event);
		} else {
			that.hide(event);
		}
	};


	options.container.click(function (event) {
		keep = true;
	});

	options.link.click(function (event) {
		that.toggle(event);
	});

	if (options.close) {
		options.close.click(function (event) {
			that.toggle(event);
		});
	}

	return that;
};