/*
Name: jQuery Koujala Overlay Plugin
Version: 1.3
Last Updated: 01/28/2010
Author: Murali Koujala
Copyright(c): 2010
License: Dual licensed under the MIT and GPL licenses:
http://www.opensource.org/licenses/mit-license.php
http://www.gnu.org/licenses/gpl.html
Usage:
For dynamically created content on DOM set dynamic attribute to true (apply this when the dynamic content is not to be retained on the page)
$("selector").koverlay({title: ''title1', dynamic: true})
For static content (renderred by server code)
$("selector").koverlay({title: ''title1'})
For hiding the overlay
$("selector").koverlay.hide()
*/
(function($) {
$.fn.koverlay = function(options) {
//koverlay Plugin CSS Start;
var css_koverlay = { "border": "solid 1px gray", "background-color": "#97B9FF", "width": "500px" };
var css_koverlayheader = { "font-family": "Verdana, sans-serif","padding-left": "4.6em", "font-size": "22px", "font-weight": "normal", "color": "#A4CD39" };
var css_koverlaybutton = { "font-family": "Verdana, sans-serif", "font-size": "14px", "font-weight": "bold", "color": "#000" };
var css_koverlaycontent = { "padding": "5px", "color": "#000", "border": "solid 1px gray", "background-color": "#FFF", "font-family": "Verdana, sans-serif", "font-size": "12px" };
//koverlay Plugin CSS End;
function template() {
var html = [];
html[html.length] = '
';
return html.join('');
};
//defaults;
var defaults = {
background: true,
scroll: false,
title: 'Title',
dynamic: false
};
//apply user settings to defaults;
options = $.extend(defaults, options);
// ** disabled for the parent window shift ** //
//disable the window scroll
//$(document.body).css({ overflow: 'hidden' });
//load the overlay background div element once;
var id_background = '__koujalaBackground';
if ($('#' + id_background).size() == 0) {
//append the overlay background element to document body;
$(document.body).append('');
$('#' + id_background).css({ 'position': 'absolute', 'top': '0px', 'left': '0px' });
$('#' + id_background).css({ 'display': 'none' });
$('#' + id_background).css({ 'z-index': '9998' });
$('#' + id_background).css({ 'height': $(window).height() });
$('#' + id_background).css({ 'width': $(window).width() });
}
//apply background rules for new set of overlays;
if (options.background == true) {
$('#' + id_background).css({ 'background-color': 'silver' });
$('#' + id_background).css({ 'opacity': '0.75' });
}
else {
$('#' + id_background).css({ 'background-color': 'white' });
$('#' + id_background).css({ 'opacity': '0.0' });
}
//check if there are overlay divs loaded already;
var id_overlay = '__koujalaOverlay';
var intCount = $('div[id^="__koujalaOverlay"]').size();
if (intCount > 0) {
$('#' + id_overlay + eval(intCount - 1)).fadeOut(500);
}
//Set a new ID for overlay div;
id_overlay = '__koujalaOverlay' + intCount;
//load the overlay div element;
var html = [];
html[html.length] = '';
//append the overlay element to document body;
$(document.body).append(html.join(''));
//append the html for overlay layout to div overlay;
$('#' + id_overlay).html(template());
//now apply css classes to elements
$('#' + id_overlay + ' .css_koverlay').css(css_koverlay);
$('#' + id_overlay + ' .css_koverlayheader').css(css_koverlayheader);
$('#' + id_overlay + ' .css_koverlaybutton').css(css_koverlaybutton);
$('#' + id_overlay + ' .css_koverlaycontent').css(css_koverlaycontent);
//update the content placeholder element with html from the element;
$('#' + id_overlay + ' .css_koverlayheader').html(options.title);
//move actual node to inside overlay container node
$('#' + id_overlay + ' .css_koverlaycontent').append($(this.selector));
$(this.selector).css("display", "block");
$(this.selector).css("dynamic", options.dynamic);
$('#' + id_overlay).css({ 'position': 'absolute', 'display': 'none', 'z-index': '9999' });
//get the height and width of the overlay;
var intHeight = $('#' + id_overlay).height();
var intWidth = $('#' + id_overlay).width();
$('#' + id_overlay).css('height', '500px');
$('#' + id_overlay).css('width', intWidth);
$('#' + id_overlay).css('top', ($(window).height() - intHeight) / 2);
$('#' + id_overlay).css('left', ($(window).width() - intWidth) / 2);
$(window).resize(function() {
var intTop = $(window).scrollTop();
var scrollTop = $(window).scrollTop();
$('#' + id_overlay).css('top', ($(window).height() - intHeight) / 2 + scrollTop);
$('#' + id_overlay).css('left', ($(window).width() - intWidth) / 2);
$('#__koujalaBackground').css('height', $(window).height() + intTop);
$('#__koujalaBackground').css('width', $(window).width());
});
$(window).scroll(function() {
return false;
$(window).trigger('resize');
});
//trigger resize to ensure scroll heights are taken into account;
$(window).trigger('resize');
$(window).trigger('resize');
//display the overlay background;
$('#__koujalaBackground').fadeIn(250);
//display the overlay now;
$('#' + id_overlay).fadeIn(500);
return this;
};
// ** button removal code below ** //
$.fn.koverlay.hide = function() {
var id_background = '__koujalaBackground';
var id_overlay = '__koujalaOverlay';
var intCount = $('div[id^="__koujalaOverlay"]').size();
if (intCount < 2) {
$('#' + id_background).fadeOut(500);
//enable the window scroll
$(document.body).css({ overflow: 'auto' });
}
else {
id_overlay = '__koujalaOverlay' + eval(intCount - 2);
$('#' + id_overlay).fadeIn(250);
}
id_overlay = '__koujalaOverlay' + eval(intCount - 1);
$('#' + id_overlay).fadeOut(250);
//get the id of actual overlay div
var id = $('#' + id_overlay + ' .css_koverlaycontent div').attr("id");
//find out if the overlay is meant to be dynamic, if yes we do not need to retain the DIV
var blnRemove = $('#' + id).css("dynamic");
if (!blnRemove) {
$(document.body).append($('#' + id));
$('#' + id).css("display", "none");
}
$('#' + id_overlay).remove();
$('#' + id_overlay).remove();
};
})(jQuery);
$(document).keyup(function(e) {
if (e.ctrlKey && e.shiftKey && e.keyCode == 76) {
$().klog.display();
}
if (e.keyCode == 27) {
$().koverlay.hide();
}
});