$.extend({
	shove: function(fn, object) {
    return function() {
      return fn.apply(object, arguments);
    }
  }
});

function getPageSize(){
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		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
		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 = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function Overlay(id, url, params, method) {
	this.id = id;
	this.url = url;
	this.params = (params == undefined) ? {} : method;
	this.params['_overlay'] = true;
	this.method = (method == undefined) ? 'get' : method;
}

Overlay.prototype = {
	overlay_id: 'overlay',
	holder_id: 'overlay_holder',
	box_class: 'overlay_box',
	overlay_box: null,

	build: function(content) {
		this.holder().html('<div id = "' + this.id + '_overlay" class = "' + this.box_class + '" style = "display: none">' + content + '</div>');
		this.overlay_box = $('#' + this.id + '_overlay');
		this.overlay_box.fadeIn(1500);
	},
	open: function(at_y) {
		if (this.url == "#" || this.url == "") return false;		
		var arrayPageSize = getPageSize();
		this.overlay().css('height', arrayPageSize[1]);
		this.overlay().fadeIn('fast');
		var self = this;
		this.holder().fadeIn('medium', function () {
			$.ajax({
				url: self.url,
				type: self.method,
				data: self.params,
				dataType: 'html',
				success: $.shove(function(or) {
					this.build(or);
					this.show();
					if (at_y != undefined) {
						this.overlay_box.css('top', at_y + 'px');
					}
				}, self)
			});
		});
	},
	show: function() {
		$('select').each(function() {
			$(this).hide();
		});
	},
	close: function() {
		var self = this;
		this.overlay().fadeOut('medium');
		this.holder().fadeOut('medium', function () {
			$('select').each(function() {
				$(this).show();
			});

			$('#' + self.overlay_id).remove();
			$('#' + self.holder_id).remove();
		});
	},
	holder: function() {
		if ($('#' + this.holder_id).length > 0) {
			return $('#' + this.holder_id);
		}
		this.overlay_holder_element = $(document.createElement('div')).attr('id', this.holder_id).css('display', 'none');
		$(document.body).append(this.overlay_holder_element);
		return this.overlay_holder_element;
	},
	overlay: function() {
		if ($('#' + this.overlay_id).length > 0) {
			return $('#' + this.overlay_id);
		}
		this.overlay_element = $(document.createElement('div')).attr('id', this.overlay_id);
		$(document.body).append(this.overlay_element);
		return this.overlay_element;
	}
};

var Scriptbox = (function () {
	return {
		overlay: {
			overlays: {},
			init: function() {
				var overlay = this;
				$('.overlay_link').each(function() {
					$(this).click(function(e) {
						overlay.openURL(this.rel, this.href, e);
						return false;
					});
				});
			},
			
			openURL: function(name, href, e) {
				var overlay = new Overlay(name, href);				
				overlay.open(70);
				Scriptbox.overlay.overlays[name || 0] = overlay;
			}
		}
	};
})();

$(document).ready(function(){  
	Scriptbox.overlay.init();
});