(function ($) {
	$.loewy = $.loewy || {};
	
	$.fn.loewyFlashBox = function(options) {
		return this.each(function() {
			new $.loewy.loewyFlashBox(this,options);
		});
	}
	
	$.loewy.loewyFlashBox = function(element, options) {
		var self = this;
		
		this.options = $.extend({
			overlay: 50,
			maxWidth:685,
			maxHeight:510
		}, options);
		
		this.element = element;
		this.href = "";
		this.idx = undefined;
		
		this.init();
	}
	
	$.extend($.loewy.loewyFlashBox.prototype, {
		init: function() {
			var self = this;
			
			
			
			$(self.element).click(function() {
				self.show();
				return false;
			});
		},
		
		show: function() {
			var self = this;
			
			params = self.parseQuery($(self.element).attr("rel"));
			//href = $(self.element).attr("href");
			href = self.element.href.split("#",2);
			href = "#"+href[1];
			self.href = href;
			
			self.idx = params['idx'];
			
			if (params.height > self.options.maxHeight || params.width > self.options.maxWidth || mwFO_pop[self.idx] ) {
				NewWindow(mwFO_popup[self.idx],'loewy_flash',params.width,params.height,'no');
				
				return false;
			}
			
			NewWindow(mwFO_popup[self.idx],'loewy_flash',params.width,params.height,'no');
			return false;
			
			var div = '<div id="flashBoxOverlay"></div><div id="flashBoxContainer"><div id="flashBoxContent"><div id="flashBoxContentHolder"><div id="flashBoxClose"><a href="'+mwFO_popup[self.idx]+'" title="" class="popup">Open in new window</a><a href="#" title="Close" class="close">Close</a></div></div></div></div>';
			$("body").append(div);
			
			self.resizeBoxes();
			self.repositionBoxes({
				nHeight: 150,
				nWidth: 300
			});
			
			$("#flashBoxOverlay, #flashBoxContainer").hide();
			$("#flashBoxOverlay").css({
				opacity: self.options.overlay/100
			});
			/*
			clone = $(href).clone();
			id = $(clone).attr("id");
			$(clone).attr("id",id+"_clone");
			*/
			
			clone = $('<div id="flash'+self.idx+'_container" class="slide_flash" style="'+mwFO_style[self.idx]+'"></div>');
			$("#flashBoxContentHolder").prepend(clone);
			clone = $("#flash"+self.idx+"_container");
			self.current = clone;
			
			$("#flashBoxOverlay").show();
			$("#flashBoxContainer").show();
			clone.show();
			
			//clone.html(mwFO_html[self.idx]);
			mwFO[self.idx].write('flash'+self.idx+'_container');
			
			/*
			$('#flashBoxContentHolder').animate({width:params.width, height:params.height}, 'normal', function() 
			{ clone.show(); });
			*/
			$('#flashBoxContentHolder').css({
				width: params.width+"px",
				height: (parseInt(params.height)+18)+"px"
			});
			
			self.repositionBoxes({
				nHeight: (parseInt(params.height)+18),
				nWidth: params.width
			});
			
			$("a.popup","#flashBoxClose").click(function(ev) {
				try {
					ev.cancelBubble = true;
				} catch(e){}
				
				
				NewWindow(this.href,'loewy_flash',params.width,params.height,'no');
				
				return false;
			});
			$("a.close","#flashBoxClose").click(function(ev) {
				self.remove();
				return false;
			});
			
			$("#flashBoxOverlay, #flashBoxContainer").click(function(e) {
				/*
				if (e.target.tagName == "EMBED" || e.target.tagName == "OBJECT") {
					return false;
				} else {
					self.remove();
					return false;
				}
				*/
				if (e.target.tagName == "DIV") {
					self.remove();
					return false;
				}
			});
			
			$(window).resize(function () { 
				self.repositionBoxes({
					nHeight: (parseInt(params.height)+18),
					nWidth: params.width
				});
			});
		},
		
		remove: function() {
			var self = this;
			$("#flash"+self.idx+"_flash").remove();
			$("#flashBoxOverlay").remove();
			$("#flashBoxContainer").remove();
			
			
		},
		
		parseQuery: function ( query ) {
		   var Params = {};
		   if ( ! query ) {return Params;}// return empty object
		   var Pairs = query.split(/[;&]/);
		   for ( var i = 0; i < Pairs.length; i++ ) {
		      var KeyVal = Pairs[i].split('=');
		      if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
		      var key = unescape( KeyVal[0] );
		      var val = unescape( KeyVal[1] );
		      val = val.replace(/\+/g, ' ');
		      Params[key] = val;
		   }
		   return Params;
		},
		
		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;
			}
			
			//
			var largestWidth;
			var largestHeight;
			var smallestWidth;
			var smallestHeight;
			//
			if ( pageWidth >= windowWidth )
			{	largestWidth = pageWidth; smallestWidth = windowWidth;	}
			else
			{	largestWidth = windowWidth; smallestWidth = pageWidth;	}
			//
			if ( pageHeight >= windowHeight )
			{	largestHeight = pageHeight; smallestHeight = windowHeight;	}
			else
			{	largestHeight = windowHeight; smallestHeight = pageHeight;	}
			
			/*
			//code for a future replacement of all the above rubbish
			//var arrayPageSize = {'pageWidth':w,'pageHeight':h,'windowWidth':w,'windowHeight':h};
			$.Lightbox.debug($(document.body).width());
			$.Lightbox.debug($(document.body).height());
			$.Lightbox.debug('--');
			$.Lightbox.debug($(window).width());
			$.Lightbox.debug($(window).height());
			$.Lightbox.debug('--');
			$.Lightbox.debug(pageWidth);
			$.Lightbox.debug(pageHeight);
			$.Lightbox.debug('--');
			$.Lightbox.debug(windowWidth);
			$.Lightbox.debug(windowHeight);
			$.Lightbox.debug('--');
			$.Lightbox.debug(largestWidth);
			$.Lightbox.debug(largestHeight);
			$.Lightbox.debug('--');
			$.Lightbox.debug(xScroll);
			$.Lightbox.debug(yScroll);
			$.Lightbox.debug('--');
			$.Lightbox.debug(largestWidth-smallestWidth);
			$.Lightbox.debug(largestHeight-smallestHeight);
			$.Lightbox.debug('===');
			//return arrayPageSize;
			*/
			
			// Return
			var arrayPageSize = {'pageWidth':pageWidth,'pageHeight':pageHeight,'windowWidth':windowWidth,'windowHeight':windowHeight,'largestWidth':largestWidth,'largestHeight':largestHeight};
			return arrayPageSize;
		},
		
		getPageScroll: function ( ) {
			var xScroll, yScroll;
			if (self.pageYOffset) {
				yScroll = self.pageYOffset;
				xScroll = self.pageXOffset;
			} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
				yScroll = document.documentElement.scrollTop;
				xScroll = document.documentElement.scrollLeft;
			} else if (document.body) {// all other Explorers
				yScroll = document.body.scrollTop;
				xScroll = document.body.scrollLeft;	
			}
			var arrayPageScroll = {'xScroll':xScroll,'yScroll':yScroll};
			return arrayPageScroll;
		},
		
		resizeBoxes: function ( )
		{
			// Get the page size
			var pageSize = this.getPageSize();
			
			// Style overlay and show it
			$('#flashBoxOverlay').css({
				width:		pageSize.largestWidth,
				height:		pageSize.largestHeight
			});
		},
		
		repositionBoxes: function ( options )
		{
			// Options
			options = $.extend({}, options);
			
			// Get the page size
			var pageSize = this.getPageSize();
			
			// Get page scroll
			var pageScroll = this.getPageScroll();
			
			
			// Figure it out
			var nHeight = options.nHeight || parseInt($('#flashBoxContentHolder').height(),10) || pageSize.largestHeight/3;
			var nWidth = options.nWidth || parseInt($('#flashBoxContentHolder').width(),10) || pageSize.largestWidth/3;
			
			// Display lightbox in center
			var nTop = pageScroll.yScroll + (pageSize.windowHeight - nHeight) / 2.5;
			var nLeft = pageScroll.xScroll;
			
			//$('#flashBoxContentHolder').animate({left:nLeft, top:nTop}, 'normal');
			$('#flashBoxContentHolder').css({
				left: nLeft,
				top: nTop
			});
		}
	});
})(jQuery);
