// input elements with the class "autoclear" are automatically emptied when
// the user clicks in them and are reinitialized with the original value if
// nothing is inputted into them
Event.observe(document, "dom:loaded", function() {
	$$('input.autoclear').each(function(e) {
		if (!Prototype.Browser.IE) {
			if (e.readAttribute('type') == "password") {
				e.writeAttribute('type','text');
				e.originaltype = "password";
			}
		}
		
		e.onfocus = function() {
			if (this._cleared) return
			this.addClassName('filled');
			if (this.originaltype == "password") { this.writeAttribute('type','password'); }
			this.original = $F(this);
			this.clear();
			this._cleared = true;
		}
		e.onblur = function() {
			if ($F(this) == "") {
				this._cleared = false;
				this.value = this.original;
				if (this.originaltype == "password") { this.writeAttribute('type','text'); }
				this.removeClassName('filled');
			}
		}
	});
	
});

// loads the results of an ajax call into an html element
// also displays a loader message and an error message if necessary
function loadFragmentInToElement(url, element_id) {
	var notice = $(element_id);
	notice.update('<em>Loading...<em>');
	new Ajax.Request(url, {   
		method: 'get',
		onSuccess: function(transport) {
			var notice = $(element_id);
			notice.update(transport.responseText);
		},
		onFailure: function(transport) {
			var notice = $(element_id);
			notice.update('An error (' + transport.status + ') occured.<br/>Please try again. ');
		}
		
	}); 
}

function fadeAndRemove(obj, url) {
	
	new Effect.Fade(obj, {
		afterFinish:function() {
			new Ajax.Request(url, {
				method:'get',
				onSuccess: function(transport){
					var text = transport.responseText;
					if (text == '') {
						obj.up().remove();
					} else {
						obj.update(transport.responseText);
						obj.show();
					}
				},
				onFailure: function(transport){
					obj.up().remove();
				}
			});
		}
	});
	
	return false;

}


/**
FROM: http://snippets.dzone.com/posts/show/3411
new Dialog.box([element ID]);
$([element ID]).show();
$([element ID]).hide();

(2010-03-16) not yet used online?

*/

var Dialog = {};
Dialog.Box = Class.create();
Object.extend(Dialog.Box.prototype, {
  initialize: function(id, url) {

	if (url != '') { new Ajax.Updater(id, url, {method: 'get'}); }
  
	this.createOverlay();

	this.dialog_box = $(id);
	this.dialog_box.show = this.show.bind(this);
	this.dialog_box.persistent_show = this.persistent_show.bind(this);
	this.dialog_box.hide = this.hide.bind(this);

	this.parent_element = this.dialog_box.parentNode;

	this.dialog_box.style.position = "absolute";

	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 = this.getScrollTop() + ((this.winHeight() - (e_dims.width/2))/2) + 'px';
	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',
		top: 0,
		left: 0,
		zIndex: 90,
		width: '100%',
		backgroundColor: '#000',
		display: 'none'
	  });
	  if (document.forms.length > 0) {
	    document.forms[0].insertBefore(this.overlay, document.forms[0].childNodes[0]);
	  } else {
	    document.body.insertBefore(this.overlay, document.body.childNodes[0]);
	  }
	  
	}
  },

  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() {
	var overlayHeight = this.bodyHeight();
	if (this.winHeight() > this.bodyHeight()) { overlayHeight = this.winHeight(); }
	this.overlay.style.height = overlayHeight + 'px'; //this.bodyHeight()+'px';
	this.moveDialogBox('out');
	
	this.overlay.onclick = this.hide.bind(this);
		
	this.selectBoxes('hide');
	new Effect.Appear(this.overlay, {duration: 0.1, from: 0.0, to: 0.3});
	this.dialog_box.style.display = '';

	this.dialog_box.style.left = '0px';
 
	var e_dims = Element.getDimensions(this.dialog_box);
	
	this.dialog_box.style.left = ((this.winWidth()/2) - (e_dims.width)/2) + 'px';
	
	this.dialog_box.style.top = this.getScrollTop() + ((this.winHeight() - (e_dims.width/2))/2) + 'px';
	
  },
  
  getScrollTop: function() {
	return (window.pageYOffset)?window.pageYOffset:(document.documentElement && document.documentElement.scrollTop)?document.documentElement.scrollTop:document.body.scrollTop;
  },
  
  persistent_show: function() {
	this.overlay.style.height = this.bodyHeight()+'px';
	this.moveDialogBox('out');
 
	this.selectBoxes('hide');
	new Effect.Appear(this.overlay, {duration: 0.1, from: 0.0, to: 0.3});
	
	this.dialog_box.style.display = '';
	this.dialog_box.style.left = '0px';
	var e_dims = Element.getDimensions(this.dialog_box);
	this.dialog_box.style.left = ((this.winWidth()/2) - (e_dims.width)/2) + 'px';
	
  },

  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=''});
  },

  selectBoxes: function(what) {
	$A(document.getElementsByTagName('select')).each(function(select) {
	  Element[what](select);
	});

	if(what == 'hide')
	  $A(this.dialog_box.getElementsByTagName('select')).each(function(select){Element.show(select)})
  },
  
  bodyWidth: function() { return document.body.offsetWidth || window.innerWidth || document.documentElement.clientWidth || 0; },
  bodyHeight: function() { return document.body.offsetHeight || window.innerHeight || document.documentElement.clientHeight || 0; },

  winWidth: function() {
	 var viewportwidth;
	 
	 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	 
	 if (typeof window.innerWidth != 'undefined')
	 {
		  viewportwidth = window.innerWidth;
	 }
	 
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	
	 else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
	 {
		   viewportwidth = document.documentElement.clientWidth;
	 }
	 
	 // older versions of IE
	 
	 else
	 {
		   viewportwidth = document.getElementsByTagName('body')[0].clientWidth
	 }
	 return viewportwidth;
  
   },

   winHeight: function() { 
	 var viewportheight;
	 
	 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	 
	 if (typeof window.innerHeight != 'undefined')
	 {
		  viewportheight = window.innerHeight
	 }
	 
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	
	 else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientHeight != 'undefined' && document.documentElement.clientHeight != 0)
	 {
		   viewportheight = document.documentElement.clientHeight;
	 }
	 
	 // older versions of IE
	 
	 else
	 {
		   viewportheight = document.getElementsByTagName('body')[0].clientHeight;
	 }
	 return viewportheight;

  }

}); 


// http://jehiah.cz/archive/prototype-powered-popup-script
var Popup = {
  open: function(options)
  {
    this.options = {
      url: '#',
      width: 600,
      height: 500,
      name:"_blank",
      location:"no",
      menubar:"no",
      toolbar:"no",
      status:"yes",
      scrollbars:"yes",
      resizable:"yes",
      left:"",
      top:"",
      normal:false
    }
    Object.extend(this.options, options || {});

    if (this.options.normal){
        this.options.menubar = "yes";
        this.options.status = "yes";
        this.options.toolbar = "yes";
        this.options.location = "yes";
    }

    this.options.width = this.options.width < screen.availWidth?this.options.width:screen.availWidth;
    this.options.height=this.options.height < screen.availHeight?this.options.height:screen.availHeight;
    var openoptions = 'width='+this.options.width+',height='+this.options.height+',location='+this.options.location+',menubar='+this.options.menubar+',toolbar='+this.options.toolbar+',scrollbars='+this.options.scrollbars+',resizable='+this.options.resizable+',status='+this.options.status
    if (this.options.top!="")openoptions+=",top="+this.options.top;
    if (this.options.left!="")openoptions+=",left="+this.options.left;
    window.open(this.options.url, this.options.name,openoptions );
    return false;
  }
}
