// Number.addZero();
// Always returns a number with fixed number of digits

Number.extend ({
	addZero: function ( digits )
	{
		var num = this;
		if ( ! $defined ( digits ) ) digits = 2;
		(digits - this.toString().length).times ( function() {
			num = "0" + num;
		} );
		return num;
	}
});

// iframeRequest functions
// for sending files without page reload

function iframeRequestDo ()
{
	vars['iframe_query'] = true;
	this.target = this.id + "_frame";
	this.submit();
}

function iframeRequest ( formID, callback )
{
	vars['iframe_query'] = false;
	var transportFrame = new Element ( "iframe" );
	transportFrame.id = formID + "_frame";
	transportFrame.name = formID + "_frame";
	transportFrame.style.display = "none";
	transportFrame.injectInside ( document.body );
	transportFrame.onload = function() {
		if ( vars['iframe_query'] == true )
		{
			var iFrameBody = $(formID + "_frame").contentWindow.document.body;
			var resp = iFrameBody.innerHTML;
			callback ( resp );
			(function(){$(formID + "_frame").remove();}).delay(100);
		}
	}
	iframeRequestDo.delay(100, $(formID));
}

// legacyWindowSize
// Returns proper sizes for the window in quirks mode too
// From quirksmode.org

Window.extend ({
	legacyWindowSize: function () {
		var d = ( document.documentElement.clientHeight ) ? document.documentElement : document.body;
		if (self.innerHeight) // all except Explorer
		{
			innerx = self.innerWidth;
			innery = self.innerHeight;
		}
		else {
			innerx = d.clientWidth;
			innery = d.clientHeight;
		}
		if (self.pageYOffset) // all except Explorer
		{
			scrollx = self.pageXOffset;
			scrolly = self.pageYOffset;
		}
		else 
		{
			scrollx = d.scrollLeft;
			scrolly = d.scrollTop;
		}
		if (document.body.scrollHeight > document.body.offsetHeight) // all but Explorer Mac
		{
			sizex = document.body.scrollWidth;
			sizey = document.body.scrollHeight;
		}
		else // Explorer Mac;
		//would also work in Explorer 6 Strict, Mozilla and Safari
		{
			sizex = document.body.offsetWidth;
			sizey = document.body.offsetHeight;
		}
		return { inner: { x: innerx, y: innery }, scroll: { x: scrollx, y: scrolly }, size: { x: sizex, y: sizey } }
	}
});

// mBox class
// a modal window for showing information, prompts and such

var mBox = new Class ({
	options: {
		buttons: [],
		width: 300,
		effectDuration: 1000
	},
	initialize: function ( heading, content, options ) {
		this.setOptions(options);
		if ( this.options.buttons.length == 0 )
			this.options.buttons = [ { label: "Ok", event: this.hide.bind(this) } ];
		
		if ( $('adBox_shade') == null )
		{
			this.shade = new Element ( "div", {
				id: "adBox_shade",
				styles: {
					"position": "absolute",
					"top": "0px",
					"left": "0px",
					"width": Window.legacyWindowSize().size.x,
					"height": Window.legacyWindowSize().size.y,
					"background-color": "#000",
					"opacity": 0
				}
			} );
			this.shade.inject ( document.body );
		}
		else
			this.shade = $('adBox_shade');
		
		this.shade.effect ( "opacity", { duration: 300 } ).start ( 0.5 );
		
		if ( $('adBox') == null )
			this.container = new Element ( "div", {
				id: "adBox",
				styles: {
					"width": this.options.width + "px",
					"position": "absolute",
					"left": Window.legacyWindowSize().scroll.x + ( Window.legacyWindowSize().size.x / 2 ).round() - ( this.options.width / 2 ).round() + "px",
					"opacity": 0
				}
			} );
		else {
			this.container = $('adBox');
			this.container.setHTML ( '' );
		}
	
		var header = new Element ( "h2", {
			id: "adBox_header"
		} );
		header.setHTML ( heading );
		var text = new Element ( "div", {
			id: "adBox_content"
		} );
		text.setHTML ( content );
		var buttonsDiv = new Element ( "div", {
			id: "adBox_buttons"
		} );
		this.options.buttons.each ( function ( button ) {
			buttonsDiv.adopt ( new Element ( "input", {
				"type": "button",
				"value": button.label,
				"events": {
					"click": function () {
						if ( ! button.event ) button.event = Class.empty;
						this.hide ( button.keepShade, button.event );
					}.bind(this)
				}
			} ) );
		}.bind(this) );
		this.container.adopt ( header, text, buttonsDiv );
		this.container.inject ( document.body );
		this.container.setStyle ( "top", Window.legacyWindowSize().scroll.y + ( Window.legacyWindowSize().inner.y / 2 ).round() - ( this.container.getSize().size.y / 2 ).round() + "px" );
		( function () { this.container.effect ( "opacity", {
			duration: this.options.effectDuration
		} ).start ( 1 ) }.bind(this) ).delay ( 300 );
		
	},
	hide: function (keepShade, onComplete) {
		this.container.effect ( "opacity", { 
			duration: 500,
			onComplete: function () {
				if ( $type ( onComplete ) == "function" ) onComplete ();
				if ( keepShade != true )
					this.shade.effect ( "opacity", { 
						duration: 300
					} ).start ( 0 );
			}.bind(this) 
		} ).start ( 0 );
	}
});
mBox.implement ( new Events, new Options );