
jQuery.extend({
	openBox: function(txt,options){
		$().box.open(txt,options);
	},
	closeBox: function(){
		$().box.close();
	},
	editBox: function(txt){
		$().box.edit(txt);
	},
	shakeBox: function(times){
		$().box.shake(times);
	},
	loadingBox: function(){
		$().box.loading();
	}/*,
	confirmBox: function(confirmTxt,options){
		$().box.open('<form><h2>'+confirmTxt+'</h2><button type="button" class="btn" id="jYes">'+options.lYes+'</button> <button type="button" name="edit" class="btn" id="jNo">'+options.lNo+'</button></form>',options);

		$().click(function(event){
			t = event.target;
			if ($(t).is("#jYes")) {
				return true;
			}else if($(t).is("#jNo")) {
				return false;
			}
		});
	}*/
});


(function($) {

	var opts;

	$.fn.box = {
		open: function(text, options){
			// build main options before element iteration
			opts = $.extend({}, $.fn.box.defaults, options);

			$("body").prepend('<div id="jBgBox"></div><div id="jBox">'+text+'</div>');

			var arrPageSizes = _getPageSize();
			$("#jBgBox").css({opacity: opts.bgOpacity, height: arrPageSizes[1]}).fadeIn();

			var arrPageScroll = _getPageScroll();
			$("#jBox").css({top: (arrPageScroll[1]+100+'px')}).fadeIn();

			$(window).resize(function(){
				var arrPageScroll = _getPageScroll();
				$("#jBox").css('top', (arrPageScroll[1]+100+'px'));
			});

			$(window).scroll(function(){
				var arrPageScroll = _getPageScroll();
				$("#jBox").css('top', (arrPageScroll[1]+100+'px'));
			});

			this.isOpen = true;

			$("#jBgBox").click(function(){
				if ( !($.fn.box.isFreeze) ){
					$.fn.box.close();
				}
			});

			return;
		},

		close: function(){
			$('#jBox').remove();
			$('#jBgBox').fadeOut(function(){$('#jBgBox').remove();});
			this.isOpen = false;
			return;
		},

		edit: function(txt){
			this.isFreeze = false;
			$('#jBox').html(txt);
			return;
		},

		shake: function(times){
			if ( times == 'undefined' ) times = 3;
			for (var x = 1; x <= times; x++){$('#jBox').animate({marginLeft: -275},10).animate({marginLeft: -250},50).animate({marginLeft: -225},10).animate({marginLeft: -250},50);}
			return;
		},

		loading: function(){
			var txt = '<div id="jLoadingBox"></div>';
			if (this.isOpen){
				this.edit(txt);
			} else {
				this.open(txt);
			}
			// Congelo in modo che nn si possa chiudere il box ;)
			this.isFreeze = true;
			return;
		},

		isOpen: false,
		isFreeze: false
	}

	function _getPageSize() {
		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;
	};

	function _getPageScroll() {
		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;	
		}
		arrayPageScroll = new Array(xScroll,yScroll) 
		return arrayPageScroll;
	};

	$.fn.box.defaults = {
		bgOpacity: 0.8
	};
})(jQuery);

