/** * @author leonard.martin * @author amended luke.cuthbertson * @projectDescription Import content from the current page or via AJAX into an overlay * @version 1.1 * * @auther amended ivor.ng * @version 1.2 */ (function($){ //apply onclick to open overlay to multiple elements $.fn.portalLink = function(type, contentOrURL, callback, options){ this.each(function(){ $(this).click(function(){ $[type](contentOrURL, callback, options); return false; }); }); }; $.portal = function(content, callback){ if(content && content.clone){ content = content.clone(); }; /* ver 1.2 */ if(content && (content.match(/\.[gif|jpg|png]/))){ content = "\"\""; } /* end of code added on ver 1.2 */ var o = PortalManager.open(content); if(callback) { if(typeof callback == 'function') {callback.apply(o,[]);}; }; return o; }; $.ajaxportal = function(url, callback, options){ var defaults = { contentSelector: '#overlayContent', cache: true }; var options = $.extend({}, defaults, options); var o = $.portal(null, null, options); o.hide(); //do post load content stuff function doStuff(cont){ $(cont).find('a.close').click(function(){o.trigger('overlay.close');return false;}); o.trigger('overlay.loaded'); if(callback) { if(typeof callback == 'function') {callback.apply(cont,[]);}; }; }; var container = o.find('.overlayContent'); //is it in cache if(options.cache && PortalManager.cache[url+' '+options.contentSelector]){ container.html(PortalManager.cache[url+' '+options.contentSelector]); doStuff(container); } else{ container.load(url+' '+options.contentSelector,null,function(){ doStuff($(this)); PortalManager.cache[url+' '+options.contentSelector] = $(this).html(); }); }; return o; }; PortalManager = { portals: 0, isOpen: false, open: function(content) { content = content||''; var obj = this; if(!this.mask) { this.makeMask(); }; this.mask.show(); this.portals++; var win = new Portal(this.portals,content); this.sortLayers(); win.bind('overlay.close',function(){obj.close(); $(this).remove();}); return win; }, makeMask: function() { this.mask = $(document.body).append('
').find('.overlay'); }, sortLayers: function() { this.mask.css({zIndex:(this.portals*10)-5,height:$(document).height()}); }, close: function() { this.portals--; if(this.portals == 0) { this.mask.hide(); } this.sortLayers(); }, cache: {} }; Portal = function(n,content) { this.content = content; this.id = n; return this.open(content); }; Portal.prototype = { markup: '
\
\
\ \
\
\
\
\
\
\
', open: function(content){ var obj = this; this.win = $(document.body).append('').find('#portal'+this.id); this.win.html(this.markup); this.win.find('div.overlayWrapper').css('zIndex',this.id*10); /* ver 1.1 this.win.find('.overlayClose00 a').click(function(){return obj.close();}); */ /* ver 1.2 */ var closeButton = this.win.find('.overlayClose00 a'); if(window.location.href.indexOf("/tc/") > -1){ closeButton.html("關閉"); }else if(window.location.href.indexOf("/sc/") > -1){ closeButton.html("关闭"); } closeButton.click(function(){return obj.close();}); $("iframe.overlay").hide(); $("div.overlay").bind("click",function(){ return obj.close(); }); this.content = this.win.find('div.overlayContent'); this.setContent(content); this.win.extend({ setContent:function(c){ obj.setContent(c); return this; } }); this.win.bind('overlay.loaded',function(){obj.centre();}); setTimeout(function(){obj.win.trigger('overlay.open');},0); return this.win; }, close: function() { this.win.trigger('overlay.close').remove(); return false; }, setContent: function(c) { this.content.html(c); this.centre(); }, centre: function() { this.win.css({position:'absolute',visibility:'hidden'}).show(); var h = this.win.find('div.overlayWrapper').height() + 100, w = this.win.find('div.overlayWrapper').width(); winh = $(window).height(); winw = $(window).width(); this.win.css({position:'',visibility:''}); this.win.find('div.overlayWrapper').css({top:Math.max((winh-h)/2,0) + $(window).scrollTop() + 'px',left:Math.max((winw-w)/2,0) + 'px'}); this.win.show(); $('.overlay').css({height:$(document).height()+'px'}); /* version 1.3 */ var that = this; $('.overlay').css({width:$(document).width()+'px'}); $(window).resize(function(){ that.centre(); }); } }; })(jQuery);