/**
 * @author Adam Brill (adam@solutionset.com)
 *
 * @see prototype.js v1.6
 * @see prototype_ss.js for v1.6
 * @see SS.Layer.js for v1.6
 */
SS.widgets.Dialog = Class.create(SS.Layer, SS.AJAX, {
	/**
	 * This is a static settings object that is instantiated in the base constructor!
	 * @var Object settings object
	 */
	s: {
		className: 'dialogBox',
		headerSelector: 'div.header',
		contentSelector: 'div.content',
		closeSelector: 'a.close',
		draggable: true,
		resizeable: true,
		overlay: true,
		zIndexHandler: true,
		restrain: true,
		exemplarAnchor: 'center center',
		selfAnchor: 'center center',
		relativeX: 0,
		relativeY: 0,
		closeOnOverlayClick: true
	},
	n: {
		header: null,
		content: null,
		closes: [],
		exemplar: 'viewport',
		resizeHandle: null
	},
	/**
	 * @param function $super
	 * @param object options
	 * @return void
	 */
	initialize: function ($super, options) {
		//run the parent initialize function
		$super(options);
		
		this._findElements();
		this._captureEvents();
		
		this.n.el.position({exemplar: this.n.exemplar, exemplarAnchor: this.s.exemplarAnchor, selfAnchor: this.s.selfAnchor});
		
		this._makeDraggable();
		this._makeResizeable();
		this._makeOverlay();
		this._makeZIndexHandler();
		this._makeRestrain();
	},
	/**
	 * @param Event e
	 * @return void
	 */
	onCloseClick: function(e){
		e.stop();
		this.hide();
		//lets destroy
		this.n.el.remove();
	},
	/**
	 * @param Event e
	 * @return void
	 */
	onCloseOverlayClick: function(e){
		e.stop();
		if($(Event.element(e)).hasClassName('dialogBox'))
		{
			return;
		}
		this.hide();
		//lets destroy
		this.n.el.remove();
	},
	/**
	 * @return void
	 */
	setHeader: function(html) {
		this.n.header.update(html);
	},
	/**
	 * @return void
	 */
	setContent: function(html) {
		if (this.n.content) {
			this.n.content.update(html);
		}
	},
	/**
	 * @param String url
	 * @return void
	 */
	loadURL: function(url) {
		new Ajax.Request(url, {
			onSuccess: function(t) {
				this.setContent(t.responseText);
			}.bind(this)
		});
	},
	/**
	 * @param String url
	 * @return void
	 */
	loadIFrameURL: function(url, id, postLoad) {
		//post loading is useful to force the 
		//browser history to move foward
		postLoad = postLoad || false;
		if(!postLoad)
		{
			id = (typeof id == 'string') ? 'id="'+id+'"' : '';
			this.setContent('<iframe '+id+'src="' + url + '"></iframe>');
		}
		else if(id)
		{
			var attrId = (typeof id == 'string') ? 'id="'+id+'" ' : null;
			var attrName = (typeof id == 'string') ? 'name="'+id+'" ' : null;
			this.setContent('<iframe '+attrId+attrName+'src="/i/s.gif"></iframe>');
			setTimeout(function(){$(id).src = url+'?1';}, 0);
		}
	},
	/**
	 * @return void
	 */
	_findElements: function() {
		this.n.header = this.n.el.select(this.s.headerSelector)[0];
		this.n.content = this.n.el.select(this.s.contentSelector)[0];
		this.n.closes = this.n.el.select(this.s.closeSelector);
	},
	/**
	 * @return void
	 */
	_captureEvents: function() {
		this.n.closes.each(function(el) {
			el.observe('click', this.onCloseClick.bindAsEventListener(this));
		}.bind(this));
	},
	/**
	 * @return void
	 */
	_makeOverlay: function() {
		if(this.s.overlay === true && window.SS.Overlay){
			this.overlay = new SS.Overlay(this.n.el);
			if(this.s.closeOnOverlayClick)
			{
				$(this.overlay.n.background).observe('click', this.onCloseOverlayClick.bindAsEventListener(this));
			}
		}
		
	},
	/**
	 * @return void
	 */
	_makeDraggable: function() {
		if (this.s.draggable === true && window.Draggable && this.n.header) {
			new Draggable(this.n.el, {
				handle: this.n.header,
				onDrag: function() {
					this.n.el.fire('ss:move');
				}.bind(this)
			});
		}
	},
	/**
	 * @return void
	 */
	_makeResizeable: function() {
		if(this.s.resizeable === true && window.SS.Resizeable){
			this.n.resizeHandle = new Element('div').setStyle({
				position: "absolute",
				bottom: "0",
				right: "0",
				cursor: "pointer"
			}).update('Resize');
			this.n.el.insert({bottom:this.n.resizeHandle});
			
			new SS.Resizeable(this.n.el, {
				n: {
					handle: this.n.resizeHandle
				},
				s: {
					minSize: {
						width: 100,
						height: 100
					}
				}
			});
		}
	},
	_makeZIndexHandler: function() {
		if(this.s.zIndexHandler === true && window.SS.ZIndexHandler){
			new SS.ZIndexHandler(this.n.el);
		}
	},
	_makeRestrain: function() {
		if(this.s.restrain === true && window.SS.Restrain){
			new SS.Restrain(this.n.el);
		}
	}
});