/** * jQuery Popup Overlay * * @version 1.5.0 * @requires jQuery v1.7.1+ * @link http://vast-eng.github.com/jquery-popup-overlay/ */ ;(function($) { var level = []; var lastclicked = []; $.fn.popup = $.fn.popup = function(customoptions) { var $body = $('body'), $window = $(window), $document = $(document), $el, $newel, $wrapper, options = {}, blurhandler, focushandler, defaults = { type: 'overlay', action: 'click', background: true, color: 'black', opacity: '0.4', horizontal: 'center', vertical: 'center', escape: true, blur: true, fade: 250, opensufix: '_open', closesufix: '_close', keepfocus: true, reposition: false, autozindex: false }; var init = function(el) { if(!$(el).attr('id')){ $(el).attr('id', 'j-popup-' + parseInt(Math.random() * 100000000)); } lastclicked[el.id] = false; level[el.id] = 0; $el = $(el); options = $.extend({}, defaults, customoptions); /** * Repositioning parameter */ if (options.reposition === true) { // @TODO - not so DRY... $newel = $el; $el = $wrapper = $('#' + el.id + '_wrapper'); positionpopup(el); return false; } // initialize on only once if ($el.attr('data-popup-initialized')) { return false; } $el.attr('data-popup-initialized', 'true'); /** * Set variables */ var triggerelement = '.' + el.id + options.opensufix; // class that will open popup /** * Set other options that are related for type: tooltip */ if (options.type == 'tooltip') { options.background = false; options.keepfocus = false; } /** * Hide popups that aren't already hidden with CSS and move it to the top or bottom of the
tag */ $el.css({ display: 'none' }); // append instead of prepend if document is ready // if (((document.readyState === 'interactive') || (document.readyState === 'complete')) && !($.browser.msie && parseFloat($.browser.version) < 8)) { // $body.append(el); // } else { $body.prepend(el); // } /** * Create background div and append to the top or bottom of the body tag */ if ((options.background) && (!$('#' + el.id + '_background').length)) { // Append instead of prepend if possible var popupback = ''; // if (((document.readyState === 'interactive') || (document.readyState === 'complete')) && !($.browser.msie && parseFloat($.browser.version) < 8)) { // $body.append(popupback); // } else { $body.prepend(popupback); // } $('#' + el.id + '_background').css({ backgroundColor: options.color, opacity: options.opacity, position: 'fixed', top: '0', right: '0', bottom: '0', left: '0', display: 'none' }); } /** * Positioning overlay */ if (options.type == 'overlay') { $el.css({ display: 'inline-block', textAlign: 'left', position: 'relative', verticalAlign: 'middle' }).addClass('popup_content'); $el.wrap(''); $wrapper = $('#' + el.id + '_wrapper'); $wrapper.css({ position: 'fixed', top: '0', left: '0', width: '100%', height: '100%', display: 'none', textAlign: 'center' }); $wrapper.append(''); $('.popup_align').css({ display: 'inline-block', verticalAlign: 'middle', height: '100%' }); // overlay horizontal if (options.horizontal == 'right') { $wrapper.css('text-align', 'right'); } else if (options.horizontal == 'left') { $wrapper.css('text-align', 'left'); } // overlay vertical if (options.vertical == 'bottom') { $el.css('vertical-align', 'bottom'); } else if (options.vertical == 'top') { $el.css('vertical-align', 'top'); } $newel = $el; $el = $wrapper; } /** * add data-popup-order attribute */ $(triggerelement).each(function(i, item) { $(item).attr('data-popup-order', i); }); /** * Defining on which event to open/close popup */ if (options.action == 'click') { // open $(triggerelement).on('click', function(e) { if ($el.is(':hidden')) { var or = $(this).attr('data-popup-order'); dopopup(el, or); e.preventDefault(); } }); // $('.' + el.id + options.closesufix).click(function(e) { hidePopUp(el); e.preventDefault(); }); } else if (options.action == 'hover') { $(triggerelement).mouseenter( function() { dopopup(el, $(this).attr('data-popup-order')); }); $(triggerelement).mouseleave( function() { hidePopUp(el); }); } else { $(triggerelement).mouseover( function() { dopopup(el, $(this).attr('data-popup-order')); }); $(triggerelement).mouseout( function() { hidePopUp(el); }); } /** * Close popup on ESC key (binded only if a popup is open) */ if (options.escape) { $(document).keydown(function(e) { if (e.keyCode == 27 && $el.css('display') == 'block') { hidePopUp(el); } }); } /** * Repositioning popup when window resize */ $(window).bind('resize', function() { if (options.type != 'tooltip') { positionpopup(el); } }); /** * Z-index calculation */ if (options.autozindex === true) { var elements = document.getElementsByTagName("*"), len = elements.length, maxZIndex = 0; for(var i=0; i