/**
 * @author leonard.martin <leonard.martin@heathwallace.com>
 * @author amended luke.cuthbertson <luke.cuthbertson@heathwallace.com>
 * @projectDescription Import content from the current page or via AJAX into an overlay
 * @version 1.1
 */

(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, options){
		if(content && content.clone){
			content = content.clone();
		};
		var o = PortalManager.open(content,options);
		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,options) {
						content = content||'';
						var obj = this;
						if(!this.mask) {
							this.makeMask();
						};
						this.mask.show();
						this.portals++;
						var win = new Portal(this.portals,content,options);
						this.sortLayers();
						win.bind('overlay.close',function(){obj.close(); $(this).remove();});
						return win;
					},
		makeMask:	function() {
						this.mask = $(document.body).append('<div class="overlay"></div><iframe class="overlay"></iframe>').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,options) {
		this.content = content;
		this.id = n;
		return this.open(content,options);
	};
	
	Portal.prototype = {
		markup:		'<div class="overlayWrapper">\
						<div class="overlayWrapper01">\
							<div class="overlayWrapper02">\
								<div class="overlayClose00"><a href="#">Close</a></div>\
								<div class="overlayContentWrapper">\
									<div class="overlayContent"></div>\
								</div>\
							</div>\
						</div>\
						<div class="overlayFooter00"></div>\
					</div>',
		
		open:		function(content,options){
						options = $.extend({className:''},options);
						var obj = this;
						this.win = $(document.body).append('<div id="portal'+this.id+'" style="display:none;"></div>').find('#portal'+this.id);
						this.win.html(this.markup).addClass(options.className);
						this.win.find('div.overlayWrapper').css('zIndex',this.id*10);
						this.win.find('.overlayClose00 a').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'});
					}
	};

})(jQuery);