// plugin alrightbox - light/thick-box à la Artopod
	
	(function(jQuery) {

		jQuery.fn.alrightBox = function(opt) {
			opt = jQuery.extend({},jQuery.fn.alrightBox.defaults, opt);
			return this.click(function(e){
				var w=this.href.replace(/.*[\&\?]w=(\d+).*?$/,"$1")*1;
				var h=this.href.replace(/.*[\&\?]h=(\d+).*?$/,"$1")*1;
				if (w) opt.windowwidth=w;
				if (h) opt.windowheight=h;
				jQuery.fn.alrightBox.create(opt,e,this);
				return false;
			});
		};
		jQuery.fn.alrightBox.create = function(opt,e,obj) {
			// get document dimensions
			if (document.documentElement.clientWidth)
				var w=document.documentElement.clientWidth;
			if (document.documentElement.clientHeight)
				var h=document.documentElement.clientHeight;
			if (!w) { w=document.body.clientWidth; h=document.body.clientHeight; }
			if (!w) { w=window.innerWidth; h=window.innerHeight; }
			
			var fullh=window.innerHeight + window.scrollMaxY;
			if(isNaN(fullh)) fullh=h;
			
			//alert(h+" och med scroll: "+fullh)
			
			if (!e) e=window.event;
			var t=Math.round(h/2 - opt.windowheight/2);
			var l=Math.round(w/2 - opt.windowwidth/2);
			if (t<0) t=0;
			// add overlay and lightbox nodes and fix css
			jQuery("body").append('<div id="arb_overlay"></div>');
			jQuery("#arb_overlay")
				.css("opacity",opt.overlayopacity)
				.css("height",fullh)
				.css("top","0")
				.click(jQuery.fn.alrightBox.destroy);
			
			jQuery("body").append('<div id="arb_window"></div>');
			jQuery("#arb_window")
				.addClass( opt.windowProcessClass )
				.css( { "width" : "10px", "height" : "10px" } )
				.css("top",e.pageY+"px")
				.css("left",e.pageX+"px")
				.animate( { height: opt.windowheight +"px", width: opt.windowwidth +"px", "top" : t+"px", "left" : l+"px" }, function(){ 
					jQuery.fn.alrightBox.fill(opt,e,obj); 
					// close button - must be made configurable
					jQuery("body").append('<a href="#" id="arb_closebtn"><img src="../img/artov_close.png" height="43" width="43" alt="Stäng" /></a>');
					jQuery("#arb_closebtn")
						.css("position","absolute")
						.css("zIndex","1020")
						.css("top",(t-20)+"px")
						.css("left",(l+opt.windowwidth-27)+"px");
				
				});
		};
		jQuery.fn.alrightBox.fill = function(opt,e,linkobj) {
			jQuery.ajax({
				url : linkobj.href,
				cache : false,
				dataType: "html",
				type : "get",
				contentType: "text/html; charset=" +opt.charset,
				success : function(html) {
					jQuery("#arb_window")
						.html(html)
						.removeClass(opt.windowProcessClass)
						.addClass(opt.windowFinishedClass);
					jQuery.fn.alrightBox.makeCloseBtn();
				}
			});
/*			jQuery("#arb_window").load(linkobj.href,function() {
				jQuery(this).removeClass( opt.windowProcessClass )
				.addClass( opt.windowFinishedClass );
			}); */
		};
		jQuery.fn.alrightBox.makeCloseBtn = function() {
			jQuery("#arb_window .arb_closebtn, #arb_closebtn")
				.click(jQuery.fn.alrightBox.destroy);
		}
		jQuery.fn.alrightBox.destroy = function() {
			jQuery("#arb_overlay").remove();
			jQuery("#arb_window").remove();
			jQuery("#arb_closebtn").remove();
			return false;
		};

		jQuery.fn.alrightBox.defaults = {
			windowProcessClass : "window-opening",
			windowFinishedClass : "window-opened",
			charset : "utf-8",
			windowheight : 500, // these can be overridden by w and h in link href
			windowwidth : 500,
			overlayopacity : 0.6
		};
		/**
		 * getPageSize() by quirksmode.com
		 * @return Array Return an array with page width, height and window width, height
		 */
		jQuery.fn.alrightBox.getPageSize=function() {
			var xScroll, yScroll;
			if (window.innerHeight && window.scrollMaxY) {	
				xScroll = window.innerWidth + window.scrollMaxX;
				yScroll = window.innerHeight + window.scrollMaxY;
			} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
				xScroll = document.body.scrollWidth;
				yScroll = document.body.scrollHeight;
			} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}
			var windowWidth, windowHeight;
			if (self.innerHeight) {	// all except Explorer
				if(document.documentElement.clientWidth){
					windowWidth = document.documentElement.clientWidth; 
				} else {
					windowWidth = self.innerWidth;
				}
				windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			} else if (document.body) { // other Explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}	
			// for small pages with total height less then height of the viewport
			if(yScroll < windowHeight){
				pageHeight = windowHeight;
			} else { 
				pageHeight = yScroll;
			}
			// for small pages with total width less then width of the viewport
			if(xScroll < windowWidth){	
				pageWidth = xScroll;		
			} else {
				pageWidth = windowWidth;
			}
			arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
			return arrayPageSize;
		};
})(jQuery);
	
