var Dialog = Class.create()

// TODO: Make this a proper singleton. Thankfully we are indempotent!
Dialog.instance = function() {
  return new Dialog()
}

Dialog.prototype = {
  initialize: function() {
    this.dialog = document.createElement('div')
  	this.dialog.setAttribute('id', 'dialog')
  	this.dialog.style.display = 'none'
  
    this.overlay = document.createElement('div')
  	this.overlay.setAttribute('id', 'overlay')
  	this.overlay.style.display = 'none'
  	
  	Dialog._instance = this
  },
  
  open: function(options) {
    if (this.state == 'open') return
    this.state = 'open'
    
    if (options.title)   this.title   = options.title
    if (options.content) this.content = options.content
		if (options.close_button) this.close_button = options.close_button
		if (options.overwrite_div_id) this.overwrite_div_id = options.overwrite_div_id

    this.applyOverlay(options)
    this.openDialog()
  },

  dismiss: function() {
    if (this.state == 'closed') return
    this.state = 'closed'
    
    this.dismissDialog()
    this.removeOverlay()
  },
  
  replace: function(options) {
    getBody().removeChild($('dialog'))
    
    if (options.title)   this.title   = options.title
    if (options.content) this.content = options.content

    this.openDialog()
  },
  
  openDialog: function() {
  	getBody().appendChild(this.dialog)
		if (this.overwrite_div_id) {
			// overwriting div_id serves as wrapper to this dialog
			// used to customize shared partials
			custom_wrapper = document.createElement('div')
			custom_wrapper.setAttribute('id', this.overwrite_div_id)
			this.dialog.appendChild(custom_wrapper)
		}
		else {
			custom_wrapper = null
		}

    if (this.title) {
      var title = document.createElement('div')
      title.setAttribute('id', 'dialog_title')
      title.setAttribute('class', 'title')
      title.innerHTML = '<p>' + this.title + '</p>'
			if (this.close_button) {
				title.innerHTML += '<a class="close" onclick="Dialog.instance().dismiss();return false;" href="#">&times;</a>'
			}
			
			// if overwrite div exists, wrap it around title and dialog
			if (custom_wrapper) {
				custom_wrapper.appendChild(title)
			}
			else {
				this.dialog.appendChild(title)
			}
  	}

  	var content = document.createElement('div')
  	content.setAttribute('id', 'dialog_content')
  	content.setAttribute('class', 'content')
  	content.innerHTML = this.content.stripScripts(); // prevent Firefox from evaluating <script>s when the content is inserted

		// if overwrite div exists, wrap it around title and dialog
		if (custom_wrapper) {
			custom_wrapper.appendChild(content)
		}
		else {
			this.dialog.appendChild(content)
		}
		this.content.evalScripts(); // now force all browsers to evaluate <script>s inside the content

    var pageSize = getPageSize()
    
    sT = pageSize[4] /* scroll Top */
    wS = pageSize[2] /* window width */

    /* IE6 is retarded */
    if (is_ie6) 
        this.dialog.style.width = "300px"

   	dW = Element.getDimensions(this.dialog).width  /* dialog width */

 	this.dialog.style.top = (sT + 100) + "px";
  	this.dialog.style.left = Math.round((wS - dW) / 2) + "px"; 
  	
    if (is_ie6 || is_safari) 
     	this.dialog.style.display = "block";
    else 
        Effect.Appear('dialog', { duration: 0.1, from: 0.0, to: 1.0 })

    if (is_ie6) this.dialog.style.width = 
        (Element.getDimensions(content).width  + 40) + "px"
    
  	if (typeof(title) != "undefined") {
  	    /* IE7 */
      	if (is_ie6up && !is_ie6) title.style.width = dW + "px";
      	/* IE6 */
      	else if (is_ie6) {}
      	/* The rest */
      	else { 
       	  title.style.width = "100%";
        }
    }
  },
  
  applyOverlay: function(options) {
    var pageSize = getPageSize()
    this.overlay.style.height = pageSize[1] + 'px'
  	getBody().appendChild(this.overlay)
  	
  	if (options.no_overlay)
  	    new Effect.Appear('overlay', { duration: 0, from: 0.0, to: 0.0 })
  	else
      	new Effect.Appear('overlay', { duration: 0.1, from: 0.0, to: 0.5 })
    
  },
  
  dismissDialog: function() {
    new Effect.Fade('dialog', { duration: 0.1, afterFinish: function() { getBody().removeChild($('dialog')) } } )
  },
  
  removeOverlay: function() {
    if ($('overlay')) {
        Effect.Fade('overlay', { duration: 0.0, afterFinish: function() { getBody().removeChild($('overlay')) } } )
    }
  }
}

function getBody() {
  return document.getElementsByTagName('body').item(0)
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize() {
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		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
		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 = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	
	var scrollTop =  is_ie ? document.documentElement.scrollTop : window.pageYOffset;

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight,scrollTop) 
	return arrayPageSize;
}
