/* Caution: these extra methods can only be called as Element.method(element) not as element.method() */
Element.ExtraMethods = {

	extendBuilderWithTagsnames:function(tagnames){
		$w(tagnames).each(function(e) {
			window['$' + e] = function() {
				var text, attributes = {}, children = $A();

				for (var i = 0; i < arguments.length; i++) {
					var argument = arguments[i];
					if(!argument) continue;
					if (typeof argument == 'string')
						children.push(document.createTextNode(argument));
					else if (argument.nodeType == 1)
						children.push(argument);
					else if (argument.constructor == Array)
						children = children.concat(argument);
					else if (typeof argument == 'object')
						attributes = argument;
				}

				var element = new Element(e, attributes);
				children.each(function(c) { element.appendChild(c); });

				return element;
			}
		});
	},

	pngFix: function(element){
		var selectors = arguments.length==1 ? DD_belatedSelectors : arguments[1];
		selectors.each(function(s){
			Element.select(element,s).each(function(ss) {
				DD_belatedPNG.fixPng( ss );
			});
		});
	},

	removeChildren: function(element){
		element=$(element);
		while(element.firstChild)  element.removeChild(element.firstChild);
	},

	setText: function(element, text) {
		if(element.firstChild){
			var txt = element.firstChild;
			txt.data = text;
		} else {
			var txt = document.createTextNode(text);
			element.appendChild(txt);
		}

		return $(element);
	}
}
Object.extend(Element,Element.ExtraMethods);

Element.extendBuilderWithTagsnames('a br div img input option select span table textarea tr td tbody');


// set 'wait'-cursor for Ajax request
Ajax.Responders.register({
	onCreate: function() {
		if ( Ajax.activeRequestCount == 1 ) {
			document.body.addClassName( 'ajax-wait' );
		}
	},
	onComplete: function() {
		if ( Ajax.activeRequestCount == 0 ) {
			document.body.removeClassName( 'ajax-wait' );
		}
	}
});



// stereolabels.js v1.0, 2007-08-15
// Copyright (c) 2007 Stereo :: Interactive & Design (http://blog.stereodevelopment.com)
var stereolabels = Class.create();

stereolabels.prototype = {
  labels: [],

  initialize: function(options) {

    this.options = Object.extend({
			className : 'inside'
		}, options || {});
    this.labels = $$('label.'+this.options.className);
    $A(this.labels).each(function(label) {
      this.initLabel(label);
    }.bind(this));

    $A(document.forms).each(function(form) {
    	Element.extend(form);
    	if ( typeof form.down( '.ignore-stereolabels' ) == "undefined" )
      	Event.observe(form, "submit", function() { this.uninit() }.bind(this))
    }.bind(this));
  },

  // called on form submit
  // - clear all labels so they don't accidentally get submitted to the server
  // - WOULD CAUSE BUG IF FIELD CONTENTS WAS IN FACT MEANT TO EQUAL LABEL VALUE
  uninit: function() {
    $A(this.labels).each(function(label) {
      var el = $(label.htmlFor);
      if (el && el.value == el._labeltext) this.hide(el)
    }.bind(this));
  },

  // initialize a single label.
  // - only applicable to textarea and input[text] and input[password]
  // - arrange for label_focused and label_blurred to be called for focus and blur
  // - show the initial label
  // - for other element types, show the default label
  initLabel: function(label) {
  	try {
  		var input     = $(label.htmlFor);
  		if(typeof ispopup=="undefined" && input.id=='popup_login_password') return;
  		var inputTag  = input.tagName.toLowerCase();
  		var inputType = input.type;

  		if (inputTag == "textarea" || (inputType == "text" || inputType == "password")) {
  		  Element.setStyle(label, { position: 'absolute', visibility: 'hidden'});
  			Object.extend(input, {
  		    _labeltext: label.childNodes[0].nodeValue,
  		    _type: inputType
  		  });
  			Event.observe(input, 'focus', this.focused.bind(this));
  			Event.observe(input, 'blur', this.blurred.bind(this));
  			this.blurred({'target':input});
  		} else {
  		  Element.setStyle(label, { position: 'static', visibility: 'visible' });
  		}
  	}
  	catch (e) {
  	  Element.setStyle(label, { position: 'static', visibility: 'visible' });
  	}
  },

  focused: function(e) {
  	var el = Event.element(e);
  	if (el.value == el._labeltext) el = this.hide(el)
  	el.select();
  },

  blurred: function(e) {
    var el = e.target? e.target: Event.element(e);
  	if (el.value == "") el = this.show(el);
  },

  hide: function(el) {
  	if (el._type == "password") el = this.setInputType(el, "password");
  	el.value = "";
	  Element.removeClassName(el, this.options.className);
  	return el;
  },

  show: function(el) {
  	if (el._type == "password") el = this.setInputType(el, "text");
  	Element.addClassName(el, this.options.className);
  	el.value = el._labeltext;
  	return el;
  },

  setInputType: function (el, type) {
    try {
      el.type = type;
  	if(el.type=='password')
  	el.focus();
      return el;
    }
    catch (e) { //IE can't set the type parameter
      var newEl = document.createElement("input");
      newEl.type = type;
  		for (prop in el) {
  			try {
  			  // crazy bug that still exists in ie 7 with width and heights, use class name if necessary instead!
   				if (prop != "type" && prop != "height" && prop != "width") {
  				  newEl[prop] = el[prop];
  				}
  			}
  			catch(e) { }
  		}
  		Event.observe(newEl, 'focus', this.focused.bind(this));
			Event.observe(newEl, 'blur', this.blurred.bind(this));
  		el.parentNode.replaceChild(newEl, el);
  		if(newEl.type=='password')
  		newEl.focus();
  		return newEl;
    }
  }
}

Event.observe(window, 'load', stereolabelsInit, false);

var myLabels = null;
function stereolabelsInit() {
	myLabels = new stereolabels();
}
