var Tools = {
	
	language: '',
	currency: '',
	cookieDomain: '',
	isHTTPS: null,
	isBasket: 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');
		}
	},
	
	
	/*
	*  - Toggle an HTML Element
	*/
	toggle: function( elm )
	{
		if ($(elm).getStyle('display') == 'block')
		{
			$(elm).setStyle('display', 'none');
		}
		else
		{
			$(elm).setStyle('display', 'block');
		}
	},
	
	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});
	},
	
	
	/*
	*  - Toggle Two HTML Elements with a opacity transition
	*/
	toggleElements: function( anchor, elm1, elm2 ) {
		
		var tween1 = null;
		var tween2 = null;
		
		tween1 = new Fx.Morph(elm1, {duration: 300, transition: Fx.Transitions.Sine.easeOut});
		tween2 = new Fx.Morph(elm2, {duration: 300, transition: Fx.Transitions.Sine.easeOut});
		tween1.removeEvents('onComplete');
		tween2.removeEvents('onComplete');
		
		if ($(elm1).getStyle('display') == 'none')
		{
			tween2.start({ 'opacity': 0 });
			tween2.addEvent('onComplete', function() {
				$(elm2).setStyles({
					'display': 'none',
					'opacity': 0
				});
				$(elm1).setStyles({
					'display': 'block',
					'opacity': 0
				});
				tween1.start({ 'opacity': 1 });
			});
		}
		else
		{
			// Add mouseout function if anchor is defined
			if (anchor != null) {
				anchor.onmouseout = function() {
					Tools.toggleElements(false, elm1, elm2);
					anchor.onmouseout = null;
				};
			}
			
			tween1.start({ 'opacity': 0 });
			tween1.addEvent('onComplete', function() {
				$(elm1).setStyles({
					'display': 'none',
					'opacity': 0
				});
				$(elm2).setStyles({
					'display': 'block',
					'opacity': 0
				});
				tween2.start({ 'opacity': 1 });
			});
			
		}
		
	},
	
	/*
	*  - Replace Text with the flash for the Sidebar
	*  @text1-text2: The strings
	*  @color: The color for the font
	*/
	
	replaceTextSidebar: function(text1, text2, color) {
		
		var flashvars = {
			f_row: text1,
			s_row: text2,
			text_color: color
		};
		var params = {
			wmode: 'transparent'
		};
		var currentTime = new Date();
		currentTime = currentTime.getTime();
		
		$script.ready('swfobject', function() {
			swfobject.embedSWF("/flash/sidebarh3title.swf?time="+currentTime, "sidebar_title", "190", "40", "8.0.0", "/flash/expressInstall.swf", flashvars, params);
		});
		
	},
	
	/*
	*  - Replace Text
	*  @element: The $element to rewrite
	*  @text1-text2: The strings
	*  @color: The color for the font
	*/
	
	replaceText: function(element, text1, text2, color, width) {
		
		if (width == null) { width = 190; }
		
		var flashvars = {
			f_row: text1,
			s_row: text2,
			text_color: color
		};
		var params = {
			wmode: 'transparent'
		};
		var currentTime = new Date();
		currentTime = currentTime.getTime();
		
		$script.ready('swfobject', function() {
			swfobject.embedSWF("/flash/sidebarh3title.swf?time="+currentTime, element, width, "40", "8.0.0", "/flash/expressInstall.swf", flashvars, params);
		});
	},
	
	/*
	*  - Replace Single Text
	*  @element: The $element to rewrite
	*  @text1: The string
	*  @width: The width of the element
	*  @color: The color for the font
	*/
	
	replaceSingleText: function(element, text1, width, color) {
		
		var flashvars = {
			f_row: text1,
			text_color: color
		};
		var params = {
			wmode: 'transparent'
		};
		var currentTime = new Date();
		currentTime = currentTime.getTime();
		
		text1 = text1.replace(/^\s+|\s+$/g, '');
		
		$script.ready('swfobject', function() {
			swfobject.embedSWF("/flash/titles_medium.swf?time="+currentTime, element, width, "24", "8.0.0", "/flash/expressInstall.swf", flashvars, params);
		});
	},

	/*
	*  - Insert in an HTML code inside an element from an URL by AJAX
	*/

	replaceContent: function(url, destination, func) {
		
		dataLoader = new Request({url: url});
		dataLoader.send();
		dataLoader.onSuccess = function( responseText ) {
			$(destination).empty();
			$(destination).set('html', responseText);
			
			if (func != null) {
				func();
			}
		};
		
	},
	
	/*
	*  - Insert in an HTML code inside an element from an URL by AJAX and executes scripts
	*/

	replaceContentScript: function(url, destination, func) {
		
		dataLoader = new Request({
			url: url,
			evalScripts: true
		});
		dataLoader.send();
		dataLoader.onSuccess = function( responseText ) {
			$(destination).empty();
			$(destination).set('html', responseText);
			
			if (func != null) {
				func();
			}
		};
		
	},
	
	/*
	* - Replace Text with a Flash
	*/
	
	replaceTextWithFlash: function(element, text, flash, width, color) {
		
		var flashvars = {
			f_row: text,
			text_color: color
		};
		var params = {
			wmode: 'transparent'
		};
		var currentTime = new Date();
		currentTime = currentTime.getTime();
		
		text = text.replace(/^\s+|\s+$/g, '');
		
		$script.ready('swfobject', function() {
			swfobject.embedSWF(flash+"?time="+currentTime, element, width, "18", "8.0.0", "/flash/expressInstall.swf", flashvars, params);
		});
	},
	
	/* - submit form and fire event: submit */
	submitFireEvent: function(frmName){
		$$('form[name=' + frmName + ']').each(function(frm) {
			frm.fireEvent('submit');
			frm.submit();
		});
	},
		
	getJsonData: function(url, options, callBack) {
		
		// Request
		var request = new Request.JSON({
				method: 'get',
				url: url,
				evalScripts: true,
				onSuccess: function(responseJSON, responseText) {
					callBack(responseJSON, options);
				}
			}).send();
	},
	
	replaceContent: function(url, el) {
		
		// Request
		var request = new Request.HTML({
				method: 'get',
				url: url,
				evalScripts: true,
				update: el
			}).send();
	}

};


// 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()  {
		if ( this.getStyle('display') == 'block' ) {
			this.setStyle('display', 'none');
		} else {
			this.setStyle('display', 'block');
		}
	}
});


// mixin to allow other classes to behave as elements
ToElement = new Class({
	toElement: function(){
		return this.element;
	}
});




var DraggableWindow = new Class({
	Implements: [Options, Events],
	
	options: {
		width: 615
	},
	
	initialize: function(options) {
		var self = this;
		this.setOptions(options);
		this.create();
	},
	
	create: function() {
		var self = this;
		
		this.container = new Element('div', {
			id: this.options.id + 'Container',
			'class': 'draggableWindow',
			styles: {
				left: (window.getCoordinates().width / 2) - 300,
				width: this.options.width
			}
		});
		
		this.toolbar = new Element('div', {
			id: this.options.id + 'Toolbar',
			'class': 'toolbar'
		});
		
		this.closeButton = new Element('a', {
			href: '#',
			html: 'close',
			'class': 'closeButton',
			events: {
				click: function(e) {
					e.preventDefault();
					self.close();
				}
			}
		});
		
		this.content = new Element('div', {
			id: this.options.id + 'Content',
			'class': 'content'
		});
		
		this.container.adopt(this.toolbar.grab(this.closeButton), this.content).inject($(document.body), 'top');
		
		var drag = new Drag(this.container, {
			handle: this.toolbar
		});
		
		this.fireEvent('create');
	},
	
	open: function(args) {
		Tools.hideAllSelects();
		this.container.show();
		this.fireEvent('open', args);
	},
	
	close: function(args) {
		this.container.hide();
		Tools.showAllSelects();
		this.fireEvent('close', args);
	}
});
