/*
Created On: 05/15/2006
Last Updated On: 08/23/2006
Created By: Nicholas Schlueter -- http://www.SimpltryVideo.com
Version: 0.6

This is freely available for noncommercial and commerical use.	I am not responsible for support and 
if this script causes harm directly or indirectly I am not liable for any damages.	Using any part of this 
Code means you agree to the aformentioned conditions.  Please alter this code however you see fit. I do 
ask that you leave every from this point up in tact.

Dependencies: 
	Prototype: 1.5.0_rc0+
	script.aculo.us: 1.6.1+
		-effects
		-dragdrop
*/
if(!SimpltryVideo) var SimpltryVideo = {};
SimpltryVideo.WindowProperties = {
	getHorizontalScroll: function() {
		return window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0;
	},
	getVerticalScroll: function() {
		return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
	},
	getBrowserSize: function() {
		var bWidth;
		var bHeight;
		if(document.documentElement && document.documentElement.clientHeight) {
			bWidth = document.documentElement.clientWidth - 8;
			bHeight = document.documentElement.clientHeight;
		} else {
			bWidth = document.body.clientWidth;
			bHeight = document.body.clientHeight;
		}
		return {width: bWidth, height: bHeight};
	},
	getContentSize: function() {
		var bodyHeight;
		var bodyWidth;
		if (window.innerHeight && window.scrollMaxY) {
			bodyHeight = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){
			bodyHeight = document.body.scrollHeight;
		} else {
			bodyHeight = document.body.offsetHeight;
		} 
		if (window.innerWidth && window.scrollMaxX) {
			bodyWidth = document.body.scrollWidth + window.scrollMaxX;
		} else if (document.body.scrollWidth > document.body.offsetWidth){
			bodyWidth = document.body.scrollWidth;
		} else {
			bodyWidth = document.body.offsetWidth;
		}
		var browserSize = SimpltryVideo.WindowProperties.getBrowserSize();
		var calculatedHeight = browserSize.height;
		var calculatedWidth = browserSize.width;
		
		if(bodyWidth > calculatedWidth) calculatedWidth = bodyWidth;
		if(bodyHeight > calculatedHeight) calculatedHeight = bodyHeight;
		return {height: calculatedHeight, width: calculatedWidth};
	}
};

SimpltryVideo.Dialog = {};
SimpltryVideo.Dialog.State = {
	activeDialog: [],
	opaqueDivUp: null,
	hasActiveDialogs: function() {return SimpltryVideo.Dialog.State.activeDialog.length > 0},
	highestIndex: 1000,
	highestId: 0
};
SimpltryVideo.Dialog.css = {
	title: "SimpltryVideoDialogTitle",
	dialog: "SimpltryVideoDialog"
};
SimpltryVideo.Dialog.DefaultOptions = {
	opacity: .6,
	width: 640,
	height: 505,
	title: "Confirm?",
	makeDraggable: true,
	repositionOnScroll: true,
	displayTitle: true,
	height:null
};

SimpltryVideo.Dialog.removeAll = function() {SimpltryVideo.Dialog.State.activeDialog.each(function(dialog){dialog.removeDialog()})};

SimpltryVideo.Dialog.Button = Class.create();
SimpltryVideo.Dialog.Button.prototype = {
	initialize: function(buttonText, dialogBox, options) {
		this.options = Object.extend({onClick: Prototype.emptyFunction}, options || {});
		this.element = document.createElement('input');
		this.element.type = 'submit';
		this.element.value = buttonText;
		this.element.onclick = function(event) {this.options.onClick(event);dialogBox.removeDialog();return false;}.bindAsEventListener(this);
	}
};
SimpltryVideo.Dialog.Base = {};
SimpltryVideo.Dialog.Base.prototype = {
    initialize: function(options, buttons){
        this.setup(options, buttons);
        this.show();
    },
	_show: Prototype.emptyFunction,
	onDisplay: Prototype.emptyFunction,
	show: function() {
		this.id = ++SimpltryVideo.Dialog.State.highestId;
		if(SimpltryVideo.Dialog.State.opaqueDialog == null) {
			var opaqueLayer = document.createElement('div');
			$(opaqueLayer);
			var contentSize = SimpltryVideo.WindowProperties.getContentSize();
			Element.setStyle(opaqueLayer, {position: "absolute", top:"0", left:"0", width: contentSize.width + "px", height: contentSize.height + "px", display: "block", zIndex: 1000, background: "blue"});
			opaqueLayer.id = "dialog_opaque_layer";
			Element.setOpacity(opaqueLayer, this.options.opacity);
			document.body.appendChild(opaqueLayer);
			SimpltryVideo.Dialog.State.opaqueDialog = opaqueLayer;
			$$('select').each(function(element) {Element.hide(element)});
		}
				
		this.dialogLayer = document.createElement('div');
		this.dialogLayer.id = "dialog_layer" + this.id;
		$(this.dialogLayer);
		this.dialogLayer.addClassName("dialog");
		Element.setStyle(this.dialogLayer, {position: "absolute", zIndex: ++SimpltryVideo.Dialog.State.highestIndex});
		var browserSize = SimpltryVideo.WindowProperties.getBrowserSize();
		if(typeof(this.options.width) == 'number') {
			this.dialogLayer.style.width = this.options.width + "px";
		} else {
			this.dialogLayer.style.width = this.options.width;
		}
		
		if(this.options.height) {
			if(typeof(this.options.height) == 'number') {
				this.dialogLayer.style.height = this.options.height + "px";
			} else {
				this.dialogLayer.style.height = this.options.height;
			}
			this.dialogLayer.style.overflow = "auto";
		}
		
		if(this.options.displayTitle) {
			var titleLayer = document.createElement('div');
			titleLayer.id = "dialog_title" + this.id;
			$(titleLayer);
			titleLayer.addClassName("dialog_title");
			titleLayer.appendChild(document.createTextNode(this.options.title));
			this.dialogLayer.appendChild(titleLayer);
		}
		
		this._show();
		
		document.body.appendChild(this.dialogLayer);
		this.positionDialog();
		this.hasBeenMoved = false;
		if(this.options.makeDraggable) {
			new Draggable(this.dialogLayer,{handle: titleLayer});
			titleLayer.style.cursor = "move";
			Draggables.addObserver(this);
		}
		Event.observe(window, "keypress", this.onKeyPress.bindAsEventListener(this), false);
		if(this.options.repositionOnScroll) Event.observe(window, "scroll", this.onScroll.bindAsEventListener(this), false);
		var inputs = $A(this.dialogLayer.getElementsByTagName('input'));
		if(inputs.length > 0 && !inputs[0].disabled) inputs.first().focus();
		this.isRemoved = false;
		SimpltryVideo.Dialog.State.activeDialog.push(this);
		this.onDisplay();
	},
	positionDialog: function() {
		var dims = this.dialogLayer.getDimensions();
		if(!this.dims || dims.width != this.dims.width || dims.height != this.dims.height) {
			this.dims = dims;
			var browserSize = SimpltryVideo.WindowProperties.getBrowserSize();
			this.topOffset = (browserSize.height / 2) - (this.dims.height / 2);
			this.leftOffset = (browserSize.width / 2) - (this.dims.width / 2);
		}
		this.dialogLayer.style.top = (SimpltryVideo.WindowProperties.getVerticalScroll() + this.topOffset) + "px";
		this.dialogLayer.style.left = (SimpltryVideo.WindowProperties.getHorizontalScroll() + this.leftOffset) + "px";
	},
	onKeyPress: function(event) {
		if(event.keyCode == Event.KEY_ESC && !this.isRemoved) {
			this.removeDialog();
			Event.stop(event);
		}
	},
	onScroll: function(event) {
		if(!this.isRemoved) {
			this.positionDialog();
		}
	},
	onEnd: function() {
		this.setOffset();
	},
	removeDialog: function() {
		this.dialogLayer.style.display = "none";
		Element.remove(this.dialogLayer);
		this.isRemoved = true;
		SimpltryVideo.Dialog.State.activeDialog = SimpltryVideo.Dialog.State.activeDialog.reject(function(dialog){return dialog.id == this.id;}.bind(this))
		if(!SimpltryVideo.Dialog.State.hasActiveDialogs()) {
			SimpltryVideo.Dialog.State.opaqueDialog.style.display = "none";
			Element.remove(SimpltryVideo.Dialog.State.opaqueDialog);
			if(this.options.makeDraggable) Draggables.removeObserver(this);
			$$('select').each(function(element) {Element.show(element)});
			SimpltryVideo.Dialog.State.opaqueDialog = null;
		}
	},
	setOffset: function() {
		var offset = Position.positionedOffset(this.dialogLayer);
		this.leftOffset = offset[0] - SimpltryVideo.WindowProperties.getHorizontalScroll();
		this.topOffset = offset[1] - SimpltryVideo.WindowProperties.getVerticalScroll();
		var reposition = false;
		var browserSize = SimpltryVideo.WindowProperties.getBrowserSize();
		if((this.topOffset + this.dialogLayer.getDimensions().height + 20) > browserSize.height) {
			this.topOffset -= (this.topOffset + this.dialogLayer.getDimensions().height) - (browserSize.height - 20);
			reposition = true;
		}
		if((this.leftOffset + this.dialogLayer.getDimensions().width + 20) > browserSize.width) {
			this.leftOffset -= (this.leftOffset + this.dialogLayer.getDimensions().width) - (browserSize.width - 20);
			reposition = true;
		}
		
		if(reposition) this.positionDialog();
	}
}

SimpltryVideo.Dialog.Ajax = Class.create();
Object.extend(Object.extend(SimpltryVideo.Dialog.Ajax.prototype, SimpltryVideo.Dialog.Base.prototype),
	{
		setup:function(options, buttons) {
		    options = Object.extend(Object.extend({},SimpltryVideo.Dialog.DefaultOptions), options || {});
			this.options = Object.extend({
				additionalText: "loading . . . "
			}, options);
			this.options.makeDraggable = false;
			this.options.displayTitle = false;
			this.buttons = buttons || [];
		},
		_show: function(){
			var additionalTextLayer = document.createElement('div');
			additionalTextLayer.id = "dialogLoadingLayer";
			additionalTextLayer.style.margin = "5px";
			additionalTextLayer.style.paddingLeft = "4px";
			additionalTextLayer.style.paddingRight = "4px";
			additionalTextLayer.innerHTML = this.options.additionalText;
			this.dialogLayer.appendChild(additionalTextLayer);
			var ajaxUpdateLayer = document.createElement('div');
			ajaxUpdateLayer.id = "ajaxUpdateLayer";
			this.dialogLayer.appendChild(ajaxUpdateLayer);
		},
		onDisplay: function() {
			new Ajax.Updater('ajaxUpdateLayer', this.options.url, {
				onComplete: function() {Element.remove($('dialogLoadingLayer'));this.positionDialog();}.bind(this),
				evalScripts: true,
				method: "get"
			});
		}
	});

SimpltryVideo.Dialog.Confirm = Class.create();
Object.extend(Object.extend(SimpltryVideo.Dialog.Confirm.prototype, SimpltryVideo.Dialog.Base.prototype), {
	setup: function(options, buttons) {
		options = Object.extend(Object.extend({},SimpltryVideo.Dialog.DefaultOptions), options || {});
		this.options = Object.extend({
			additionalText: null
		}, options);
		this.buttons = [];
		(buttons || []).each(function(button) {
			this.addButton(button.text || "ok", button);
		}.bind(this));
	},
	addButton: function(buttonText, options) {
		var newButton = new SimpltryVideo.Dialog.Button(buttonText, this, options);
		this.buttons.push(newButton);
	},
	_show: function() {
		if(this.options.additionalText) {
			var additionalTextLayer = document.createElement('div');
			additionalTextLayer.style.paddingLeft = "4px";
			additionalTextLayer.style.paddingRight = "4px";
			additionalTextLayer.innerHTML = this.options.additionalText;
			this.dialogLayer.appendChild(additionalTextLayer);
		}
		var buttonLayer = document.createElement('div');
		buttonLayer.style.textAlign = "center";
		buttonLayer.style.marginTop = "5px";
		buttonLayer.style.marginBottom = "2px";
		this.buttons.each(function(currentButton) { buttonLayer.appendChild(currentButton.element) } );
		this.dialogLayer.appendChild(buttonLayer);
	}
	
});

SimpltryVideo.Dialog.Alert = function(alertText, options) {
	options = Object.extend({title: "Alert!", additionalText: alertText}, options || {});
	var dialog = new SimpltryVideo.Dialog.Confirm(options,[{}]);
	return dialog;
};
