var Dialog = {};
Dialog.Box = Class.create();
Object.extend(Dialog.Box.prototype, {
	 moOptions : {
	 	 dimmingOpacity 		: 0.75
		,dimmingBackground 		: '#000000'
		,closeOnClickBackground	: true
	}
	
  	,initialize: function(id,oOptions) 
  	{
  		if(!Object.isUndefined(oOptions))
	  		Object.extend(this.moOptions,oOptions)
	  	
  		if(Object.isUndefined(RCSF_Base_Util))
  			alert("Dialog class required the RCSF_Base_Util class to be loaded");
  		
  		if(Object.isUndefined(Effect))
  			alert("Scriptaculous Effect class is required.");
  		
  		// Set the dialog options
	    this.dialog_box = $(id);
	    this.dialog_box.show = this.show.bind(this);
	    this.dialog_box.hide = this.hide.bind(this);
	    
	    this.dialog_box.style.display = 'none';
	    
	    // Create the overlayer
	    this.createOverlay();
	
	    this.parent_element = this.dialog_box.parentNode;
	   	// Position the overlayer to the absolute middle
      	this.positionOverlay();
      	
		// Position the dialog box in the middle
		this.positionDialogBox();
		
		// Set the window resize observer
		Event.observe(window,'resize', function() {
			this.positionOverlay();
			this.positionDialogBox();
		}.bind(this));

		// Set the default css properties of the dialog
		this.dialog_box.style.position = 'absolute';
		this.dialog_box.style.zIndex = this.overlay.style.zIndex + 1;
	}

	,createOverlay: function() 
	{
		if($('dialog_overlay')) 
	    	this.overlay = $('dialog_overlay');
	    else 
	    {
	    	this.overlay = document.createElement('div');
	      	this.overlay.id = 'dialog_overlay';
	   
	      	Object.extend(this.overlay.style, {
	      		 position	: 'absolute'
	      		,zIndex		: 500
	      		,display	: 'none'
	      		,background : this.moOptions.dimmingBackground
	      		,top 		: '0px'
	      		,left 		: '0px'
	      	});
	      	// Insert the overlayer at the end of the body
	      	document.body.insertBefore(this.overlay, document.body.childNodes[0]);
	    }
	}

	,positionOverlay : function()
	{
		this.page = RCSF_Base_Util.getPageSize();
		var e_dims = Element.getDimensions(this.dialog_box);
		
		var h = (Prototype.Browser.IE && this.dialog_box && this.overlay) 
				? parseInt(this.overlay.style.top) + this.page.height
				: 0; 
		
		var new_height = Math.max(this.page.pageHeight,e_dims.height);
		
		new_height = Math.max(new_height,h);
		
		this.overlay.style.width  = document.viewport.getWidth() + 'px';
		this.overlay.style.height = Math.max(Math.max(this.page.pageHeight,e_dims.height), h) + 'px';
	}

	,positionDialogBox : function()
	{
	    var e_dims = Element.getDimensions(this.dialog_box);
	    var b_dims = Element.getDimensions(this.overlay);

		this.dialog_box.style.left = ((b_dims.width/2) - (e_dims.width/2)) + 'px';
		this.dialog_box.style.top = Math.max((this.page.height - e_dims.height) / 2,0) + 'px';
	}

  	,moveDialogBox: function(where) 
  	{
    	Element.remove(this.dialog_box);
  		if(where == 'back')
      		this.dialog_box = this.parent_element.appendChild(this.dialog_box);
    	else
      		this.dialog_box = this.overlay.parentNode.insertBefore(this.dialog_box, this.overlay);
  	}

	
	
	,show: function() 
	{
		this.moveDialogBox('out');
	  	if(this.moOptions.closeOnClickBackground)
      		this.overlay.onclick = this.hide.bind(this);
      	
      	this.selectBoxes('hide');
      	if(this.overlay.style.display == 'none')
	      	new Effect.Appear(this.overlay, {duration: 0.1, from: 0.0, to: this.moOptions.dimmingOpacity});
    	
    	Object.extend(this.dialog_box.style, {
      		 position: 'absolute'
      		,display: ''
      		,visibility : 'visible'
      	});
	}
	
	/*
	* Only usuable for images
	*/
	,growFrom : function(oSource,oDestination)
	{
		var o_source = $(oSource)
		 ,a_dialog_dims = Element.getDimensions(this.dialog_box)
		 ,a_source_dims = Element.getDimensions(o_source)
		 ,a_source_pos	= o_source.cumulativeOffset()
		
		this.dialog_box.style.display = 'block';
		this.dialog_box.style.position = 'absolute';
		
		//alert(e_dims.width);
		/*o_source.setStyle({
			 width : a_source_dims.width
			,height : a_source_dims.height
		});
		*/
	//	Element.clonePosition(this.dialog_box,o_source);
		//this.show();
		/*
		new Effect.Scale(this.dialog_box,100,{
			 scalemode 		: 'box'
			,originalHeight	: a_source_dims.height
			,originalWidth	: a_source_dims.width
		})
		*/
		
		// Apply size change		
		alert(o_source.left);
		alert(o_source.top);
		
		this.changeSize(
			1,
			{ 
				x: a_source_pos.left, //this.thumbLeft + this.thumbOffsetBorderW - this.offsetBorderW,
				y: a_source_pos.top, // + this.thumbOffsetBorderH - this.offsetBorderH,
				w: a_source_dims.width,
				h: a_source_dims.height,
			
				o: 3
			},
			{
				x: this.dialog_box.style.left,
				y: this.dialog_box.style.top,
				w: a_dialog_dims.width,
				h: a_dialog_dims.height,
			
				o: 0
			},
			250,
			10

		);
		
	 	//new Effect.Appear(this.overlay, {duration: 0.1, from: 0.0, to: this.dimmingOpacity});
	 	//
	}
  	
  	,changeSize : function(up, from, to, dur, steps) {
	
		if (up && this.objOutline && !this.outlineWhileAnimating) 
			this.objOutline.setPosition(this, this.x.min, this.y.min, this.x.span, this.y.span);
		/*
		else if (!up && this.objOutline) {	
			if (this.outlineWhileAnimating) this.objOutline.setPosition(this, from.x, from.y, from.w, from.h);
			else this.objOutline.destroy();
			}	
				
		if (!up) { // remove children
			var n = this.wrapper.childNodes.length;
			for (var i = n - 1; i >= 0 ; i--) {
				var child = this.wrapper.childNodes[i];
				if (child != this.content) hs.discardElement(child);
			}
		}
			
		if (this.fadeInOut) {
			from.op = up ? 0 : 1;
			to.op = up;
		}
		*/
		var t,
		exp = this,
		easing = Math[this.easing] || Math.easeInQuad;
		
		/* if (!up) easing = Math[this.easingClose] || easing; */
				
		for (var i = 1; i <= steps; i++) {
			t = Math.round(i * (dur / steps));
			(function(){
				var pI = i, size = {};
				
				for (var x in from) {
					size[x] = easing(t, from[x], to[x] - from[x], dur);
					if (/[xywh]/.test(x)) size[x] = Math.round(size[x]);
				}
				
				setTimeout ( function() {
					if (up && pI == 1) {
						exp.dialog_box.style.visibility = 'visible';
						/* exp.a.className += ' highslide-active-anchor'; */
					}
					exp.setSize(size);
				}, t);				
			})();		
		}
	
		/*
		if (up) { 
			
			setTimeout(function() {
				if (exp.objOutline) exp.objOutline.table.style.visibility = "visible";
			}, t);
			setTimeout(function() {
				if (exp.caption) exp.writeCaption();
				
			}, t + 50);
			
		}
		else setTimeout(function() { exp.afterClose(); }, t);
		*/	
	}

	,setSize : function (to) {
		try {
				//this.wrapper.style.width = (to.w + 2*this.offsetBorderW) +'px';
				this.dialog_box.style.width = to.w +'px';
				//if (hs.safari) this.content.style.maxWidth = this.content.style.width;
				this.dialog_box.height = to.h +'px';
			/*if (to.op) hs.setStyles(this.wrapper, { opacity: to.op }); */
					
			/*
			if (this.objOutline && this.outlineWhileAnimating) {
				var o = this.objOutline.offset - to.o;
				this.objOutline.setPosition(this, to.x + o, to.y + o, to.w - 2 * o, to.h - 2 * o, 1);
			}
			*/	
			
			this.dialog_box.setStyle ( {
				'visibility': 'visible',
				'left': to.x +'px',
				'top': to.y +'px'
				,'z-index' : 1000
			});
			
		} catch (e) { window.location.href = this.src;	}
	}
  	,hide: function() 
	{
	    this.selectBoxes('show');
	    new Effect.Fade(this.overlay, {duration: 0.1});
	    this.dialog_box.style.display = 'none';
	    this.moveDialogBox('back');
	    $A(this.dialog_box.getElementsByTagName('input')).each(function(e){if(e.type!='submit')e.value=''});
	}
  	
  	,hidebox : function()
  	{
	  	this.dialog_box.style.display = 'none';
  	}
  	
  	,selectBoxes: function(what) 
  	{
  		$$('select').each(function(select){
  			if(select.hasClassName('ie6hidden') && Prototype.Browser.IE6)
	  		{
	  			if(what=='hide')
  					select.hide();
  				else
  					select.show();
  			}
  		});

    	if(what == 'hide')
     		 $A(this.dialog_box.getElementsByTagName('select')).each(function(select){Element.show(select)})
  	}
});
