function openCaktusLightbox(content, width, height)
{
  var options = {
    width: width,
    height: height
  };
  $.CaktusFreeLightbox.open(content, options);
//alert("OK");
//alert("OK");
}

function closeCaktusLightbox()
{
  $.CaktusFreeLightbox.close();
}




(function($)
{

	var CaktusFreeBox = function(content, options)
	{

		var self = this;

		this._content = content;
		this._options = options;

		this.options = null;

		this.mask = null;
		this.mainContent = null;
// 		this.menuContent = "";
		this.content = "";

		this.defaults = {
			boxid: null,
			boxclass: null,
			type: 'dialog',
			title: '',
			width: 0,
			height: 0,
			timeout: 0,
			draggable: true,
			modal: true,
			focus: null,
			position: 'center',
			overlay: 55,
			contentType: 'text',
			contentChange: false,
			clickClose: false,
			zIndex: 999,
			animate: false,
			trigger: null,
			onclose: null,
			onopen: null,
			onok: null,
// 			menuAjax: null,
			contentAjax: null
		};


		this.initOptions = function()
		{

			self.options = $.extend({}, self.defaults, self._options);
		}

		this.setMask = function()
		{
			var css = {
				opacity: self.options.overlay / 100,
				filter: 'alpha(opacity=' + self.options.overlay + ')',
				width: self.bwidth(),
				height: self.bheight(),
				zIndex: self.options.zIndex
			};

			self.mask = $("<div class='dialog-maskfree' onclick='closeCaktusLightbox();'></div>").appendTo('body').hide().css(css);
//self.mask="";

		}

		this.preshowAll = function()
		{
			self.mask.fadeIn("fast");
		}
		this.showAll = function()
		{
			self.setCenterPosition();
			self.mainContent.fadeIn("fast");
		}

		this.loadEvents = function()
		{
// 			if (self.options.menuAjax)
// 			{
// 				$.get(self.options.menuAjax, function(data)
// 				{
// 					self.menuContent = data;
// 					self.setMenuContent(data);
// 				});
// 			}

			self.preshowAll();

			if (self.options.contentAjax)
			{
				$.get(self.options.contentAjax, function(data)
				{
					self.content = data;
					self.setContent(data);

					self.showAll();
				});
			}
			else
			{
				self.content = self._content;
				self.setContent(self._content);
				self.showAll();
			}

			self.mainContent.find(".close").unbind('click').click(function() { self.close(); });
		}

		this.close = function()
		{
			self.mainContent.fadeOut("normal", function()
			{
				self.mainContent.hide();
			});

			if (self.mask)
			{
				self.mask.fadeOut("normal", function()
				{
					self.mask.hide();
				});
			}

			self.mainContent.remove();
			if (self.mask)
			{
				self.mask.remove();
			}
		}

// 		this.setMenuContent = function(content)
// 		{

// 			self.mainContent.find('.dialog-title').html(content);
// 		}

		this.setContent = function(content)
		{
			self.mainContent.find('.dialog-contentfree').html(content);
			this.setCenterPosition();
		}

		this.changeContent = function(content, changeOptions)
		{
			if (changeOptions.url)
			{
				$.get(changeOptions.url, function(data)
				{
					self.content = data;
					self.setContent(data);
				});
			}
			else
			{
				if (changeOptions.layerId)
				{
					self.mainContent.find('#' + changeOptions.layerId).html(content);
					self.content = self.mainContent.find('#' + changeOptions.layerId).html();
				}
				else
				{
					self.content = content;
					self.setContent(content);
				}
			}
		}

		this.initBox = function()
		{


      var html = "<table cellpadding=0 cellspacing=0 border=0 class='dialog-boxfree' style='width: " + self.options.width + "px'>" +
                 
                    "<tr class='dialog-middlefree'>" +
                     "<td class='dmiddlefree' align=center><table cellpadding=0 cellspacing=0 border=0><tr><td>" +
                      "<div class='dialog-contentfree'></div>" +
                     "</td></tr></table></td>" + 
                    "</tr>" +
                   
                 "</table>";




			self.mainContent = $(html)
						.appendTo('body').hide().css({
							position: 'absolute',
							overflow: 'hidden',
							zIndex: self.options.zIndex + 10
						});
			if (self.options.boxid)
			{
				self.mainContent.find('.dialog-contentfree').attr('id', self.options.boxid);
			}
			if (self.options.height > 0)
			{
				//self.mainContent.find('.dialog-content').css('height', self.options.height);
			}
			if (self.options.width > 0)
			{
				//self.mainContent.find('.dialog-content').css('width', (self.options.width - 40));
			}
			self.mainContent.bgiframe();
		};



		this.bheight = function()
		{
			if ($.browser.msie && $.browser.version < 7)
			{
				var scrollHeight = Math.max(
                                        document.documentElement.scrollHeight,
                                        document.body.scrollHeight
                                );
				var offsetHeight = Math.max(
                                        document.documentElement.offsetHeight,
                                        document.body.offsetHeight
                                );

				if (scrollHeight < offsetHeight)
				{
					return $(window).height();
				} else
				{
					return scrollHeight;
				}
			} else
			{
				return $(document).height();
			}
		}

		this.bwidth = function()
		{
			if ($.browser.msie && $.browser.version < 7)
			{
				var scrollWidth = Math.max(
                                        document.documentElement.scrollWidth,
                                        document.body.scrollWidth
                                );
				var offsetWidth = Math.max(
                                        document.documentElement.offsetWidth,
                                        document.body.offsetWidth
                                );

				if (scrollWidth < offsetWidth)
				{
					return $(window).width();
				} else
				{
					return scrollWidth;
				}
			} else
			{
				return $(document).width();
			}
		}

		this.setCenterPosition = function()
		{
			var wnd = $(window), doc = $(document),
                                pTop = doc.scrollTop(), pLeft = doc.scrollLeft();
			pTop += (wnd.height() - self.mainContent.height()) / 2;
			pLeft += (wnd.width() - self.mainContent.width()) / 2;
			self.mainContent.css({ top: pTop, left: pLeft });
		}

		this.setElementPosition = function()
		{
			var trigger = $("#" + self.options.trigger);
			if (trigger.length == 0)
			{

				self.close();
				return false;
			}
			var scrollWidth = 0;
			if (!$.browser.msie || $.browser.version >= 7)
			{
				scrollWidth = $(window).width() - document.body.scrollWidth;
			}

			var left = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft) + trigger.position().left;
			if (left + self.dh.width() > document.body.clientWidth)
			{
				left = trigger.position().left + trigger.width() + scrollWidth - self.dh.width();
			}
			var top = Math.max(document.documentElement.scrollTop, document.body.scrollTop) + trigger.position().top;
			if (top + self.dh.height() + trigger.height() > document.documentElement.clientHeight)
			{
				top = top - self.dh.height() - 5;
			} else
			{
				top = top + trigger.height() + 5;
			}
			self.dh.css({ top: top, left: left });
			return true;
		}

		//PNG
		this.correctPNG = function()
		{
			for (var i = 0; i < document.images.length; i++)
			{
				var img = document.images[i]
				var imgName = img.src.toUpperCase()
				if (imgName.substring(imgName.length - 3, imgName.length) == "PNG")
				{
					var imgID = (img.id) ? "id='" + img.id + "' " : ""
					var imgClass = (img.className) ? "class='" + img.className + "' " : ""
					var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
					var imgStyle = "display:inline-block;" + img.style.cssText
					if (img.align == "left") imgStyle = "float:left;" + imgStyle
					if (img.align == "right") imgStyle = "float:right;" + imgStyle
					if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
					var strNewHTML = "<span " + imgID + imgClass + imgTitle + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";" + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
					img.outerHTML = strNewHTML;
					i = i - 1;
				}
			}
		}


		this.initialize = function()
		{
			self.initOptions();
//alert("OK1");
			self.setMask();
//alert("OK2");
			self.initBox();
//alert("OK3");

			self.loadEvents();
//alert("OK4");
//			self.showAll();
//alert("OK");
			return self;
		}

		this.initialize();
	}

	var CaktusFreeLightbox = function()
	{
		var self = this;
		this.box = null;

		this.open = function(content, options)
		{
			self.box = new CaktusFreeBox(content, options);
		}

		this.changeContent = function(content, options)
		{
			self.box.changeContent(content, options);
		}

		this.close = function()
		{
			self.box.close();
		}

		$(window).scroll(function()
		{
			if (self.box != null)
				self.box.setCenterPosition();
		});

		$(window).resize(function()
		{
			if (self.box != null)
				self.box.setCenterPosition();
		});
	}
	$.extend({ CaktusFreeLightbox: new CaktusFreeLightbox() });

})(jQuery);
