var cached_position;
var FlashMessage = {
  // some basic setup
  sticky: true,  // should the message be sticky by default? (move when the page is scrolled)
  show_buttons: false, // should the buttons div be shown by default?
  css_class: 'notice', // default css_class of #flash_message_dialog
  lightbox: true, // show the lightbox effect by default
  show_effect: Effect.Appear, // Script.aculo.us show Effect called: show_effect('flash_message_dialog',{options})
  close_effect: Effect.Fade, // Script.aculo.us close Effect called: close_effect('flash_message_dialog',{options})
  position:'top', // position of the flash_message_dialog (top, middle, bottom)
  // you can add this buttins... for example:  FlashMessage.buttons.insertButton('close')
  closeButton: '<img src="/images/button_close.gif" alt="Close" border="0" style="cursor:pointer" onClick="FlashMessage.close()" />',
  closeOkButton: '<img src="/images/button_confirm.gif" alt="OK" border="0" style="cursor:pointer" onClick="FlashMessage.close()" />',
  cancelButton: '<img src="/images/button_cancel.gif" alt="Cancel" border="0" style="cursor:pointer" onClick="FlashMessage.close()" />',
  confirmButton: '<img src="/images/button_confirm.gif" alt="OK" border="0" />',
  // initialize - create an overlay element and optionally change the basic setup...
  initialize: function(options) {
    options = options || {};
    if(options.sticky)
      this.sticky = options.sticky;
    if(options.show_buttons)
      this.show_buttons = options.show_buttons;
    if(options.lightbox)
      this.lightbox = options.lightbox;
    if(options.css_class)
      this.css_class = options.css_class;
    if(options.show_effect)
      this.show_effect = options.show_effect;
    if(options.close_effect)
      this.close_effect = options.close_effect;
    if(options.position)
      this.position = options.position;
    
    // thanks to the fabulous lightbox.js by Lokesh Dhakar - http://www.huddletogether.com for this snippet
    var objBody = document.getElementsByTagName("body").item(0);
    var objOverlay = document.createElement("div");
    objOverlay.setAttribute('id','flash_message_overlay');
    objOverlay.style.display = 'none';
    objOverlay.style.position = 'absolute';
    objOverlay.style.top = '0';
    objOverlay.style.left = '0';
    objOverlay.style.zIndex = '90';
    objOverlay.style.width = '100%';
    
    objBody.insertBefore(objOverlay, objBody.firstChild);
    this.arrayPageSize = this.getPageSize();
  },
  // getPageScroll() - from the cool lightbox.js by Lokesh Dhakar - http://www.huddletogether.com
  // Returns array with x,y page scroll values.
  // Core code from - quirksmode.org
  getPageScroll: function(){
    var yScroll;
    if (self.pageYOffset) {
      yScroll = self.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
      yScroll = document.documentElement.scrollTop;
    } else if (document.body) {// all other Explorers
      yScroll = document.body.scrollTop;
    }
  
    arrayPageScroll = new Array('',yScroll) 
    return arrayPageScroll;
  },
  // getPageSize() - from the cool lightbox.js by Lokesh Dhakar - http://www.huddletogether.com
  // Returns array with page width, height and window width, height
  // Core code from - quirksmode.org
  // Edit for Firefox by pHaez
  getPageSize: function() {
  
    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;
    }
  
  
    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
    return arrayPageSize;
  },
  // set the position of the flash message
  // possible options are: top, middle, bottom
  setPosition: function(position) {
    if(typeof position == 'string') {
      cached_position = position;
    }
    else {
      position = cached_position || this.position;
    }
    
    $('flash_message_dialog').style.top ='';
    $('flash_message_dialog').style.bottom ='';
    if(position == 'top') {
      if($('flash_message_dialog').style.position=="fixed") {
        $('flash_message_dialog').style.top = 0;
      }
      else {
        $('flash_message_dialog').style.top = this.getPageScroll()[1] + "px";
      }
    }
    else if(position == 'middle') {
      // hmm... how can I do this nicer?!?!
      // it is not working very well because I cant get the height of a not displayed element
      $('flash_message_dialog').style.top = ((this.getPageSize()[1]-$('flash_message_dialog').getHeight())/2) +"px";
    }
    else if(position == 'bottom') {
      if($('flash_message_dialog').style.position == "fixed" && !navigator.appVersion.match(/\bMSIE\b/)) {
        $('flash_message_dialog').style.bottom = "0px";
      }
      else {
        $('flash_message_dialog').style.bottom = "-" + this.getPageScroll()[1] + "px";
      }
      
    }
  },
  
  // show the FlashMessage
  show: function(flash_options) {
    flash_options = flash_options || {};
    var lightbox = typeof flash_options.lightbox == 'undefined' ? this.lightbox : flash_options.lightbox;
    css_class     = flash_options.classname || this.css_class;
    sticky        = typeof flash_options.sticky == 'undefined' ? this.sticky : flash_options.sticky;
    show_buttons  = typeof flash_options.buttons == 'undefined' ? this.show_buttons : flash_options.buttons;
    position      = flash_options.position || this.position;
    cached_position = false;
    // show the overlay div if you want it like a lightbox
    if(!$('flash_message_overlay')) {
      this.initialize();
    }
    if(lightbox) {
      // show and resize the overlay div
      //$('flash_message_overlay').style.height=(this.getPageSize()[1] + 'px');
      //$('flash_message_overlay').style.height="100%"; 
      $('flash_message_overlay').style.height=(this.getPageSize()[1] + 'px');
      $('flash_message_overlay').show();
    }
    
    // make sure that the z-intex of the flash is high enough to appear on top
    $('flash_message_dialog').style.zIndex='101';
    // set the css class
    $('flash_message_dialog').className=css_class;
    
    // make the flash positioned 
    $('flash_message_dialog').style.position="absolute";
    
    // make the flash sticky ?
    if(sticky) {
      if(!navigator.appVersion.match(/\bMSIE\b/)) {
          $('flash_message_dialog').style.position="fixed";
          
      }
      else {
           $('flash_message_dialog').style.position="absolute";
           Event.observe(window,'scroll',move_flash_message,false);
      }
    }
   
    // shall we show the buttons div?
    if(show_buttons) {
      Element.show('flash_message_dialog_buttons');
    }
    else {
      this.buttons.hide();
    }
    
    // vertical position of the flash (options are: top, middle, bottom)
    this.setPosition(position);
    
    // finally show the flash
    this.show_effect('flash_message_dialog',{duration: 0.4});
    
  },
  show_with_close_button: function(flash_options) {
    flash_options = flash_options || {};
    flash_options.buttons = true;
    
    this.buttons.clear();
    this.buttons.insertButton('close');
    
    this.show(flash_options);
  },
  load: function(url,flash_options,ajax_options) {
    ajax_options = ajax_options || {};
    flash_options = flash_options || {};
    flash_options.buttons = typeof flash_options.buttons == 'undefined' ? false : flash_options.buttons;
    
    ajax_options.onComplete = function() { FlashMessage.show(flash_options); };
    ajax_options.asynchronous = true;
    ajax_options.evalScripts = false;
    
    new Ajax.Updater('flash_message_content', url, ajax_options);
  },
  close: function() {
    if($('flash_message_overlay')) {
      $('flash_message_overlay').hide();
    }
    this.close_effect('flash_message_dialog',{duration: 0.4});
    Event.stopObserving(window,'scroll',move_flash_message,false);
  },
  buttons: {
    hide: function() {
      $('flash_message_dialog_buttons').hide();
    },
    clear: function() {
      $('flash_message_dialog_buttons').innerHTML='';
    },
    insertHTML: function(value) {
      value = value + " ";
      new Insertion.Bottom('flash_message_dialog_buttons', value);
    },
    insertNode: function(node) {
      $('flash_message_dialog_buttons').appendChild(node);
    },
    insertButton: function(button) {
      this.insertHTML(eval('FlashMessage.'+button+'Button'));
    },
    insertLink: function(url,text,html_options) {
      html_options = html_options || {};
      html_options.href = html_options.href || url;
      link = Builder.node('a',html_options,text);
      
      this.insertNode(link);
    },
    insertLinkToFunction: function(func,text,html_options) {
      html_options = html_options || {};
      html_options.href    = html_options.href || "#";
      
      link = Builder.node('a',html_options,text);
      link.onclick = func;
      this.insertNode(link);
    },
    insertLinkToClose: function(text,html_options) {
      this.insertLinkToFunction(function() { FlashMessage.close();return false; },text,html_options);
    }
  }
};

// can I do this better? this is pretty much the same as FlashMessage.setPosition() 
// I have a namespace problem when calling that function from a observer (to position the flash in IE)
// this is for IE only!
function move_flash_message(event) {
  position = cached_position || FlashMessage.position;
  
  $('flash_message_dialog').style.top ='';
  $('flash_message_dialog').style.bottom ='';
  if(position == 'top') {
    $('flash_message_dialog').style.top = FlashMessage.getPageScroll()[1] + "px";
  }
  else if(position == 'middle') {
    // hmm... how can I do this nicer?!?!
    // it is not working very well because I cant get the height of a not displayed element
    $('flash_message_dialog').style.top = ((FlashMessage.getPageSize()[1]-$('flash_message_dialog').getHeight())/2) +"px";
  }
  else if(position == 'bottom') {
    $('flash_message_dialog').style.top = (FlashMessage.getPageSize()[3] + FlashMessage.getPageScroll()[1]-$('flash_message_dialog').getHeight()) + "px";
  }
}

function _alert(text,options) {
	options     = options || {};
	ok_text     = options.ok_text || "OK";
	
	FlashMessage.buttons.clear();
	FlashMessage.buttons.insertLinkToClose(ok_text);
	
	$('flash_message_content').innerHTML = text;
	FlashMessage.show({buttons:true});
	return false;
}
function _confirm(text,func,options) {
	if(typeof func != 'function' && typeof func == 'object' && func.href) {
		node = func;
		func = function() { document.location = node.href; return false; };
	}
	
	options     = options || {};
	ok_text     = options.ok_text || "OK";
	cancel_text = options.cancel_text || "Cancel";
	
	FlashMessage.buttons.clear();
	FlashMessage.buttons.insertLinkToClose(cancel_text);
	FlashMessage.buttons.insertLinkToFunction(func,ok_text);
	
	$('flash_message_content').innerHTML = text;
	FlashMessage.show({buttons:true});
	return false;
}