﻿// JavaScript Document

var Popup = {
	open: function(url, name, params) {
		delete params.name;
		if(!Object.isUndefined(params.height))
			params.height = !isNaN(parseInt(params.height)) ? params.height + 'px' : parms.height;
		if(!Object.isUndefined(params.width))
			params.width = !isNaN(parseInt(params.width)) ? params.width + 'px' : params.width;
		
		var config = new Hash(params).map(function(pair){
			return typeof pair.value == 'function' ? undefined : pair.key + '=' + pair.value;
		}).compact().join(', ');
		//alert(config);
		var win = window.open(url, name, config);
		win.focus();
	}
}


Element.getScrollbarWidth = (function() {
	var width = 0;
	return function() {
		if(width != 0)
			return width;
		var inner = document.createElement('p');
		inner.style.cssText = 'height:200px';
		
		var outer = document.createElement('div');
		outer.style.cssText = 'position:absolute;top:0px;left:0px;visibility:hidden;width:200px;'
			+ 'height:150px;overflow:hidden;';
		outer.appendChild(inner);
		
		document.body.appendChild (outer);
		var w1 = inner.offsetWidth;
		outer.style.overflow = 'scroll';
		var w2 = inner.offsetWidth;
		if (w1 == w2) w2 = outer.clientWidth;
		document.body.removeChild (outer);
		
		return width = w1 - w2;
	}
})();


Form.Element.DefaultText = Class.create(function(){
	function isForm(element) {
		return element && element.nodeType == Node.ELEMENT_NODE && 
			element.tagName.toLowerCase() == 'form';
	}
	
	return {
		onChanged: Prototype.K,
		initialize: function(element, text) {
			this.element = $(element);
			this.text = text;
		},
		
		apply: function(){
			this.registerEvents();
			this.onBlur();
		},
		
		setText: function(text){
			this.text = text;
		},
		getText: function(){
			return this.text;
		},
		registerEvents: function() {
			this.element.observe('focus', this.onFocus.bindAsEventListener(this));
			this.element.observe('blur', this.onBlur.bindAsEventListener(this));
			var form = this.element.ancestors().find(isForm);
			if(form) {
				form.observe('submit', this.onFormSubmitting.bindAsEventListener(this));
			}
		},
		applyDefaultText: function(){
			this.element.value = this.text;
		},
		isDefault: function(){
			return this.element.value == this.text;
		},
		isEmpty: function(){
			return !this.element.value;
		},
		onFocus: function(e) {
			if(this.isDefault()) {
				this.element.value = '';
				this.onChanged();
			}
		},
		onBlur: function(e){
			if(this.isEmpty()) {
				this.applyDefaultText();
				this.onChanged();
			}
		},
		onFormSubmitting: function(e){
			if(this.isDefault()) {
				this.element.value = '';
			}
		}
	}
}());