// allow for alternate syntax for element creation
Element.Properties.children = 
{
	get: function()			{ return this.getChildren(); },
	set: function(value)	{ this.adopt(value); }
};


// Add simple show/hide functionality to all Elements
Element.implement({
	show: function() {
		this.setStyle('display', 'block');
		return this;
	},
	hide: function() {
		this.setStyle('display', 'none');
		return this;
	},
	toggle: function()  {
		this.setStyle('display', ( this.getStyle('display') != 'none' ? 'none' : '' ) );
		return this;
	},
	bgfade: function( type, opts ) {
		var options = {
			duration: 1500,
			success: {
				start: '#beffbe',
				end: '#f6f6f6'
			},
			error: {
				start: '#ffbebe',
				end: '#f6f6f6'
			}
		};
		
		type = type || 'success';
		
		Object.append(options, opts || {} );
		
		this.set('tween', {
			duration: options.duration
		}).highlight( options[type].start, options[type].end );
		
		return this;
	}
});


// mixin to allow other classes to behave as elements
ToElement = new Class({
	toElement: function(){
		return this.element;
	}
});


var Tools = {
	
	xmlLocale: '',
	language: '',
	currency: '',
	cookieDomain: '',
	isHTTPS: null,
	isBasket: false,
	isHome: false,
	siteArea: '',
	
	isiPad: (navigator.userAgent.match(/iPad/i) != null),
	isiPhone: (navigator.userAgent.match(/iPhone/i) != null),
	isiPod: (navigator.userAgent.match(/iPod/i) != null),
	isiOS: (navigator.userAgent.match(/(iPad|iPhone|iPod)/i) != null),
	isAndroid: (navigator.userAgent.match(/(android)/i) != null),
	
	isMobile: function() {
		return this.isiOS || this.isAndroid;
	},
	
	sld: function(hostname) {
		// extract the second level domain from a hostname
		// ex.  location.hostname = secure.forzieri.com
		// sld(location.hostname) = forzieri.com
		var pattern = /[a-z0-9-]+\.[a-z]+$/;
		matches = pattern.exec(hostname);
		if ( matches )
			return matches[0];
		else
			return null;
	},
	
	baseUrlRel: function(url, args) {
		var querystring = '';
		var params = new Hash({l: this.language, c: this.currency});
		params.extend(args);
		params.each(function(value, key) {
			querystring += '&' + key + '=' + value;
		});
		return url + '?' + querystring.substr(1);
	},
	
	hideAllSelects: function() {
		// hide all <select> elements in the page
		// this is a workaround for an IE rendering bug.
		if ( Browser.Engine.trident4 ) {
			$$('body select').setStyle('visibility', 'hidden');
		}
	},
	
	showAllSelects: function() {
		// show all <select> elements in the page
		// this is a workaround for an IE rendering bug.
		if ( Browser.Engine.trident4 ) {
			$$('body select').setStyle('visibility', 'visible');
		}
	},
	
	crossFade: function(fadeIn, fadeOut) {
		tween = new Fx.Morph($(fadeIn), {
			duration: 'normal',
			transition: Fx.Transitions.Sine.easeOut
		}).start({'opacity': 1});
		
		tween2 = new Fx.Morph($(fadeOut), {
			duration: 'normal',
			transition: Fx.Transitions.Sine.easeOut
		}).start({'opacity': 0});
	}
};


// JS fixes for IE6
window.addEvent('domready', function() {
	if (Browser.ie6) {
		$(document.body).addClass('ie6');
	}
});


// update basket items
function addBasketItems() {
	if ( $('shoppingBag') ) {
		new Request.HTML({
			url: Tools.baseUrlRel('/include/i_number_items_Basket.asp'),
			update: 'basket_items'
		}).get();
	}
}
window.addEvent('domready', addBasketItems);





// catch submit events from forms submitted via JS - used by the Privileges Club external popup
if ( window.HTMLElement ) {
	// DOM-compliant Browsers
	function fakeSubmit(event) {
		var target = event ? event.target : this;
		this.fireEvent('submit');
		this._submit();
	}
	HTMLFormElement.prototype._submit = HTMLFormElement.prototype.submit;
	HTMLFormElement.prototype.submit = fakeSubmit;
} else {
	if ( Browser.ie6 ) {
		// IE6-7 does not expose its HTMLElement object or its children
		window.addEvent('domready', function() {
			$$('form').each(function(f) {
				f._submit = f.submit;
				f.submit = function(event) {
					var target = event ? event.target : this;
					target.fireEvent('submit');
					this._submit();
				};
			});
		});
	}
}
