/*
 * KFBox
 * jQuery plugin for displaying images or AJAX content in lightbox style popup.
 * Supports smart paging with page numbers. Inspired by Thickbox 2.0 by Cody Lindley.
 *
 * Copyright (c) 2008 Karel Fučík
 * Licensed under the MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

(function($){

$.fn.confBox = function(options)
{
	new confBox(this, options);
	return this;
};

var confBox = function($elements, options)
{
	this.$elements = $elements;
	this.options = $.extend({
		hashPrefix: 'kfbox-',
		minWidth: 800,
		maxWidth: 800,
		topOffset: 40,
		pageSpan: 3,
		pageLeaders: ' … ',
		historyLoadCallback: null,
		afterShowCallback: null,
		langCode: null,
		htmlTemplate: "<div id='kfb-window' class='age-confirm'><div class='break'></div><div id='kfb-content' style='font-size: 16px; text-align: center; color: black;'><h2 id='kfb-caption' style='margin-top: 4em; margin-bottom: 1em; text-align: center;'>Kontrola věku</h2><p>Prohlašuji, že jsem starší 18 let a jsem si vědom, že tyto stránky obsahují informace o tabákových výrobcích.</p><p style='margin-top: 3em'><input type='button' id='kfb-close-btn' value='Ano' onclick='document.location=\"?pass18=true\"' /> <input type='button' id='kfb-leave-btn' value='Ne' onclick='document.location=\"http://www.seznam.cz\"' /></p></div></div>"
	}, options);

	this.langCode = this.options.langCode || $('html').attr('xml:lang') || 'cs';
	this.isOpen = false;
	this.oldHash = '';

	var that = this;
	this.$elements.bind('click', function(event)
	{
		that.oldHash = window.location.hash.replace('#', '');
		if(typeof that.options.historyLoadCallback == 'function') that.options.historyLoadCallback(that.getHash(this));
		else that.showByHash(that.getHash(this));
		return false;
	});
};

confBox.prototype = new function()
{
	this.getHash = function(element)
	{
		return this.options.hashPrefix + (this.$elements.index(element) + 1);
	};

	this.showByHash = function(hash)
	{
		if(hash.indexOf(this.options.hashPrefix) !== 0){
			this.hideBox();
			return false;
		}
		this.activeIndex = parseInt(hash.replace(this.options.hashPrefix, '')) - 1;
		this.$activeLink = $(this.$elements[this.activeIndex]);
		this.renderBox();
		this.initGroup(this.$activeLink);
		return true;
	};

	this.renderBox = function()
	{
		if(!this.isOpen)
		{
			this.showOverlay();
			var $htmlTemplate = $(this.options.htmlTemplate);
			$("body").append($htmlTemplate);
			this.positionBox(this.options.minWidth);
			this.isOpen = true;
		}
	};

	this.closeBox = function(event)
	{
		that = event.data.kfBox;
		if(that.isOpen)
		{
			if(typeof that.options.historyLoadCallback == 'function') that.options.historyLoadCallback(that.oldHash);
			return that.hideBox();
		}
	};

	this.hideBox = function()
	{
		$('#kfb-window, #kfb-confirm, #kfb-iframe').remove();
		$(document).unbind('keyup');
		this.isOpen = false;
		return false;
	};

	this.initGroup = function()
	{
		var that = this;
		var title = this.$activeLink.attr('title');

		/* Generate paging... */
		this.preloadContent(this.$activeLink.attr('href'));
	};

	this.preloadContent = function(href)
	{
		/*
			var regexp = /\.jpg|\.png|\.gif$/;
			if(href.toLowerCase().search(regexp) !== -1) this.preloadImage(href);
			else this.preloadHtml(href);
		*/
	};

	this.preloadImage = function(href)
	{
		var that = this;
		$('#kfb-caption, #kfb-desc, #kfb-image-anchor').removeClass('hidden');
		$("#kfb-content").addClass('hidden');

		var $imageAnchor = $('#kfb-image-anchor');
		$imageAnchor
			.height($imageAnchor[0].offsetHeight + "px")
			.find('img').remove().end();

		$('#kfb-content').empty();
		var imgPreloader = $(document.createElement('img'))
			.bind('load', function(e)
				{
					this.onload = null;

					var imageWidth = this.width;
					var imageHeight = this.height;

					var $img = $('img', that.$activeLink);

					var caption = $img.attr('alt') || that.$activeLink.attr('title');
					var desc = $img.attr('title');
					var longdesc = $img.attr('longdesc');

					var enhdesc = [];
					if(desc) enhdesc.push(desc);
					if(longdesc) enhdesc.push('<a href="' + longdesc + '">' + longdesc + '</a>');
					enhdesc = enhdesc.join(', ');

					if(caption)	$("#kfb-caption").removeClass("hidden").html(caption);
					else $("#kfb-caption").addClass("hidden").empty();

					if(enhdesc) $("#kfb-desc").removeClass("hidden").html(enhdesc);
					else $("#kfb-desc").addClass("hidden").empty();

					$('#kfb-image-anchor').removeClass('hidden');

					var boxWidth = imageWidth;
					if(boxWidth < that.options.minWidth) boxWidth = that.options.minWidth;

					that.positionBox(boxWidth);

					this.alt = caption;
					if(desc) this.title = desc;
					$('#kfb-image-anchor').height("auto").empty().append(this).bind('click', function() { that.hideBox(); return false; } );

					$("#kfb-window").css({display:"block"});

					if(typeof that.options.afterShowCallback == 'function') that.options.afterShowCallback(that.$activeLink[0]);
				})
			.attr('src', href);
	};

	this.preloadHtml = function(href)
	{
		var that = this;
		$("#kfb-content").removeClass('hidden').addClass('loader');
		$('#kfb-caption, #kfb-desc, #kfb-image-anchor').addClass('hidden');

		var $imageAnchor = $('#kfb-image-anchor').find('img').remove();
		boxWidth = that.options.maxWidth;
		$("#kfb-content").load(href, null, function()
		{
			$("#kfb-content").removeClass('loader');
			if(typeof that.options.afterShowCallback == 'function') that.options.afterShowCallback(that.$activeLink[0]);
		});
	};

	this.positionBox = function(boxWidth)
	{
		var scrollTop = $(window).scrollTop();
		var windowWidth = 440;
		$("#kfb-window").css({marginLeft: '-' + parseInt(windowWidth / 2) + 'px', width: windowWidth + 'px', top: (scrollTop + this.options.topOffset) + 'px'});
	};

	this.showOverlay = function()
	{
		if($("#kfb-iframe").size() == 0)
		{
			$("body").append("<iframe id='kfb-iframe'></iframe><div id='kfb-confirm'></div>");
	  	var documentHeight = $(document).height();
	  	var windowHeight = $(window).height();
		  var maxHeight = documentHeight > windowHeight ? documentHeight : windowHeight;
			$("#kfb-confirm, #kfb-iframe").css("height", maxHeight);
		}
	};
};

$.confBox = confBox;

})(jQuery)
