/*
	A collection of tweaks to the mootools toolkit, built specifically for the select italy website
*/

//Element methods
Element.implement({
	/*
	Property: hide
		Hides an element (as used in Mootools v1.1)

	Example:
		$('myNode').hide();
	*/
	hide: function() {
		this.setStyle('visibility', 'hidden');
		return this;
	/*
	Property: hide
		Shows an element (as used in Mootools v1.1)

	Example:
		$('myNode').show();
	*/
	} ,
	show: function() {
		this.setStyle('visibility', 'visible');
		return this;
	} ,
	/*
	Property: center
		Centers an element in the viewport

	Example:
		$('myNode').center();
	*/
	'center': function() {
		var size = this.getSize();
		var windowsize = window.getSize();
		var x = window.getScrollLeft() + (windowsize.x  - size.x) / 2;
		var y = window.getScrollTop()  + (windowsize.y - size.y) / 2; 
		this.moveTo(x > 0 ? x : 0, y > 0 ? y : 0);
		return this;
	} ,
	/*
	Property: moveTo
		Move an element to a specific dimension, setting its position to absolute if needed

	Arguments:
		x - (interger) the number of pixels from the left of the viewport
		y - (integer) number of pixels from the top of the viewport

	Example:
		$('myNode').moveTo(200, 100); //moves the element to left: 200px, top: 100px
	*/
	'moveTo': function(x, y) {
		this.setStyles( {
				'left': x,
				'top': y,
				'position': 'absolute'
			});
	},
	/*
	Property: getFormElements
		Get all form type elements in the element, similar to the method that used to be used in MooTools 1.11

	Example:
		var formElements = $('myForm').getFormElements();
	*/
	getFormElements: function(){
                //return $$(this.getElementsByTagName('input'), this.getElementsByTagName('select'), this.getElementsByTagName('textarea'));
		return this.getElements('input, select, textarea');
        }
});

//extend some added functionality to the String prototype
String.implement({
	/*
	Property: nl2br
		Converts new lines to <br /> tags in a string, similar to PHP's nl2br() function

	Example:
		var text = $('myNode').getText().nl2br();
	*/
	nl2br: function() {
		return this.replace(/(\r\n|\r|\n)/g, '<br />');
	},
	/*
	Property: br2nl
		Converts br tags into new lines, filtering out any trailing line breaks after the tag
	Example:
		var text = $('myNode').getText().br2nl();
	*/
	br2nl: function() {
		return this.replace (/\<br ?\/?\>\n?/g, "\n");
	},
	/*
	Property: htmlspecialchars
		Converts select characters to HTML-safe strings
		Implements from the php.js code (http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_htmlspecialchars/)
	Example:
		var text = 'you & me'.htmlspecialchars();
	quote_style options:
		ENT_COMPAT   	Will convert double-quotes and leave single-quotes alone (default)
		ENT_QUOTES 	Will convert both double and single quotes
		ENT_NOQUOTES 	Will leave both double and single quotes unconverted 
	*/
	htmlspecialchars: function(quote_style) {
		var string = this;

		string = string.replace('/&/g', '&amp;');
		string = string.replace('/</g', '&lt;');
		string = string.replace('/>/g', '&gt;');

		if (quote_style == 'ENT_QUOTES') {
			string = string.replace('/"/g', '&quot;');
			string = string.replace('/\'/g', '&#039;');
		} else if (quote_style != 'ENT_NOQUOTES') {
			string = string.replace('/"/g', '&quot;');
		}

		return string;
	},
	/*
	Property: htmlspecialchars_decode
		Converts html special encoding to the literal characters
		Implemented from the php.js code (http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_htmlspecialchars_decode/)
	Example:
		var text = $('myInput').getValue().htmlspecialchars_decode(options)
	quote-style options:
		ENT_COMPAT   	Will convert double-quotes and leave single-quotes alone (default)
		ENT_QUOTES 	Will convert both double and single quotes
		ENT_NOQUOTES 	Will leave both double and single quotes unconverted 
	*/
	htmlspecialchars_decode: function(quote_style) {
		var string = this;

		string = string.replace('/&amp;/g', '&');
		string = string.replace('/&lt;/g', '<');
		string = string.replace(/&gt;/g, '>');

		if (quote_style == 'ENT_QUOTES') {
			string = string.replace('/&quot;/g', '"');
			string = string.replace('/&#039;/g', '\'');
		} else if (quote_style != 'ENT_NOQUOTES') {
			string = string.replace('/&quot;/g', '"');
		}

		return string;
	}
});

