(function($) { $.fn.simplepopup = function(event) { var simplepopup = { settings: { hashtag: "#/", url: "popup", event: event || "click" }, initialize: function(link) { var popup = $(".js__popup"); var body = $(".js__p_body"); var close = $(".js__p_close"); var routepopup = simplepopup.settings.hashtag + simplepopup.settings.url; var cssclasses = link[0].classname; if (cssclasses.indexof(" ") >= 0) { cssclasses = cssclasses.split(" "); for (key in cssclasses) { if (cssclasses[key].indexof("js__p_") === 0) { cssclasses = cssclasses[key] } }; } var name = cssclasses.replace("js__p_", ""); // we redefine the variables if there is an additional popap if (name !== "start") { name = name.replace("_start", "_popup"); popup = $(".js__" + name); routepopup = simplepopup.settings.hashtag + name; }; link.on(simplepopup.settings.event, function() { simplepopup.show(popup, body, routepopup); return false; }); $(window).on("load", function() { simplepopup.hash(popup, body, routepopup); }); body.on("click", function() { simplepopup.hide(popup, body); }); close.on("click", function() { simplepopup.hide(popup, body); return false; }); $(window).keyup(function(e) { if (e.keycode === 27) { simplepopup.hide(popup, body); } }); }, centering: function(popup) { var marginleft = -popup.width()/2; return popup.css("margin-left", marginleft); }, show: function(popup, body, routepopup) { simplepopup.centering(popup); body.removeclass("js__fadeout"); popup.removeclass("js__slide_top"); location.hash = routepopup; }, hide: function(popup, body) { popup.addclass("js__slide_top"); body.addclass("js__fadeout"); location.hash = simplepopup.settings.hashtag; }, hash: function(popup, body, routepopup) { if (location.hash === routepopup) { simplepopup.show(popup, body, routepopup); } } }; return this.each(function() { var link = $(this); simplepopup.initialize(link); }); }; })(jquery); $(function() { $(".js__p_start, .js__p_another_start").simplepopup(); });