YUI().use('tooltip', function(Y) {
	Y.TooltipPlugin.parseMarkup();
});

function confirmDelete() {
	return confirm("Are you sure you want to delete this?");
}


function checkForm(check_type){
var bOK = true;
	var data_message = document.getElementById('message');
	var data_name = document.getElementById('name');
	var data_company = document.getElementById('company');
	var data_email = document.getElementById('email');
	
	var holder_message = document.getElementById('holder_message');
	var holder_name = document.getElementById('holder_name');
	var holder_company = document.getElementById('holder_company');
	var holder_email = document.getElementById('holder_email');
	
	if(data_message.value == ''){
		holder_message.className = 'input-padding input-form-error';
		bOK = false;
	} else {
		holder_message.className = 'input-padding';
	}

	if(data_name.value == ''){
		holder_name.className = 'input-padding input-form-error';
		bOK = false;
	} else {
		holder_name.className = 'input-padding';
	}

	if(check_type != 'simple'){
		if(data_company.value == ''){
			holder_company.className = 'input-padding input-form-error';
			bOK = false;
		} else {
			holder_company.className = 'input-padding';
		}
	}

	if(data_email.value == ''){
		holder_email.className = 'input-padding input-form-error';
		bOK = false;
	} else {
		if(isValidEmail(data_email.value)) {
			holder_email.className = 'input-padding';
		} else {
			holder_email.className = 'input-padding input-form-error';
			bOK = false;
		}
	}
	
	//if(!bOK)
	//alert('Vänligen kontrollera rödmarkerade fält.');
	
	return bOK;
}

function isValidEmail(str) {
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}





// YUI().use('tooltip');

function showField( name ) {
	YUI().use('node', 'anim', function(Y) {
		var field = Y.one('#field_' + name);

		//Animate it if it isn't already shown
		if (field.getStyle('display') != 'block') {
			//Find the elements height
			field.setStyle( 'visibility', 'hidden');
			field.setStyle( 'position', 'absolute');
			field.setStyle( 'display', 'block' );
			field.setStyle( 'height', ''); //Remove the height property
// 			var pb = parseFloat(field.getStyle('padding-bottom'));
// 			var pt = parseFloat(field.getStyle('padding-top'));
			var height = field.get('clientHeight');// - pb - pt; //TODO
// 			console.log(field.getStyle('padding'), height);
			field.setStyle( 'display', 'none' );
			field.setStyle( 'visibility', 'inherit');
			field.setStyle( 'position', 'static');
			field.setStyle( 'overflow', 'hidden');


			var anim = new Y.Anim({
				node: field,
				from: {
					height: 0,
					opacity: 0
				},
				to: {
					height: height,
					opacity: 1
				},
				easing: Y.Easing.easeOut,
				duration: 0.2
			});

			anim.on('start', function() {
				field.setStyle( 'height', 0);
				field.setStyle( 'display', 'block' );
			});
			anim.on('end', function() {
				field.setStyle( 'overflow', 'visible' );
				field.setStyle( 'height', ''); //Remove the height property
			});
			anim.run();
		}

	// 	Dom.setStyle(field, 'display', 'block');

		Y.all('[name=button_show_' + name + ']').setStyle('display', 'none');
		Y.all('[name=button_hide_' + name + ']').setStyle('display', 'block');
	});
}

function hideField( name ) {
	YUI().use('node', 'anim', function(Y) {
		var field = Y.one('#field_' + name);

		if (field.getStyle('display') == 'block') {
			var anim = new Y.Anim({
				node: field,
				from: {
					opacity: 1
				},
				to: {
					height: 0,
					opacity: 0
				},
				easing: Y.Easing.easeIn,
				duration: 0.2
			});
			anim.on('start', function() {
				field.setStyle( 'overflow', 'hidden');
			});
			anim.on('end', function() {
				field.setStyle( 'display', 'none' );
			});
			anim.run();
		}

		Y.all('[name=button_show_' + name + ']').setStyle('display', 'block');
		Y.all('[name=button_hide_' + name + ']').setStyle('display', 'none');
	});
}


YUI.add('expand', function(Y) {
	function ExpandPlugin(config) {
		ExpandPlugin.superclass.constructor.apply(this, arguments);
	}

	ExpandPlugin.NS = 'expand';
	ExpandPlugin.ATTRS = {};

	Y.extend( ExpandPlugin, Y.Plugin.Base, {

		initializer: function(cfg) {
			Y.augment(this.get('host'), Y.EventTarget);
			this.get('host').publish('startExpand');
			this.get('host').publish('expanded');
			this.get('host').publish('startCollapse');
			this.get('host').publish('collapsed');
		},

		collapse: function() {
			var field = this.get('host');

			var anim = new Y.Anim({
				node: field,
				from: {
					opacity: 1
				},
				to: {
					height: 0,
					opacity: 0
				},
				easing: Y.Easing.easeIn,
				duration: 0.2
			});
			anim.on('start', function() {
				field.setStyle( 'overflow', 'hidden');
				this.fire('startCollapse');
			}, this.get('host'));
			anim.on('end', function() {
				field.setStyle( 'display', 'none' );
				this.fire('collapsed');
			}, this.get('host'));
			anim.run();
		},

		expand: function() {
			var field = this.get('host');

			//Find the elements height
			field.setStyle( 'visibility', 'hidden');
			field.setStyle( 'position', 'absolute');
			field.setStyle( 'display', 'block' );
			field.setStyle( 'height', ''); //Remove the height property
// 			var pb = parseFloat(field.getStyle('padding-bottom'));
// 			var pt = parseFloat(field.getStyle('padding-top'));
			var height = field.get('clientHeight');// - pb - pt; //TODO
// 			console.log(field.getStyle('padding'), height);
			field.setStyle( 'display', 'none' );
			field.setStyle( 'visibility', 'inherit');
			field.setStyle( 'position', 'static');
			field.setStyle( 'overflow', 'hidden');


			var anim = new Y.Anim({
				node: field,
				from: {
					height: 0,
					opacity: 0
				},
				to: {
					height: height,
					opacity: 1
				},
				easing: Y.Easing.easeOut,
				duration: 0.2
			});

			anim.on('start', function() {
				field.setStyle( 'height', 0);
				field.setStyle( 'display', 'block' );
				this.fire('startExpand');
			}, this.get('host'));
			anim.on('end', function() {
				field.setStyle( 'overflow', '' );
				field.setStyle( 'height', ''); //Remove the height property
				this.fire('expanded');
			}, this.get('host'));
			anim.run();
		},

		toggle: function() {
			if (this.get('host').getStyle('display') == 'block') {
				this.collapse();
			} else {
				this.expand();
			}
		}

	});

	Y.ExpandPlugin = ExpandPlugin;
}, '0.0.1', {requires: ['base', 'plugin', 'node', 'anim', 'event']});

YUI.add('tooltip', function(Y) {

	function TooltipPlugin(config) {
		TooltipPlugin.superclass.constructor.apply(this, arguments);
	}

	TooltipPlugin.NS = 'tooltip';
	TooltipPlugin.ATTRS = {
		text: {
			value: null,
			setter: function(text) {
				if (this._tooltipText) {
					this._tooltipText.set('innerHTML', text);
				}
				return text;
			}
		},
		type: {
			value: 'auto'
		}
	};

	Y.extend( TooltipPlugin, Y.Plugin.Base, {
		_tooltip: null,
		_tooltipText: '',
		_timerId: 0,

		initializer: function(cfg) {
			this._tooltipText = Y.Node.create('<div/>');
			this._tooltipText.set('innerHTML', this.get('text'));

			this._tooltip = Y.Node.create('<div/>');
			this._tooltip.append(this._tooltipText);
			this._tooltip.addClass('tooltip');
			this._tooltip.setStyle('position', 'absolute');
			this._tooltip.setStyle('display', 'none');

			Y.one('body').append(this._tooltip);
			this._host = this.get('host');
			this._host.on('mouseover', this._mouseover, this);
			this._host.on('mouseout', this._mouseout, this);
		},

		hide: function() {
			this._tooltip.setStyle('display', 'none');
		},

		show: function() {
			var host = this.get('host');
			var tooltip = this._tooltip;
			var x = host.getX() + (host.get('offsetWidth')/2) - 15;
			tooltip.setStyle('visibility', 'hidden');
			tooltip.setStyle('display', '');
			tooltip.setXY([x,host.getY()-52]);
			tooltip.setStyle('visibility', 'visible');
		},

		_mouseover: function() {
			if (this.get('type') == 'manual') {
				return;
			}
			if (this._timerId) {
				clearTimeout(this._timerId);
				this._timerId = 0;
			}

			if (this.get('type') == 'instant') {
				this.show();
			} else {
				var t = this;
				this._timerId = setTimeout(function() { t.show() }, 750);
			}
		},

		_mouseout: function() {
			if (this.get('type') == 'manual') {
				return;
			}
			if (this._timerId) {
				clearTimeout(this._timerId);
			}
			
			if (this.get('type') == 'instant') {
				this.hide();
			} else {
				var t = this;
				this._timerId = setTimeout(function() { t.hide(); }, 500);
			}
			//var t = this;
			//this._timerId = setTimeout(function() { t.hide(); }, 500);
		}
	});

	TooltipPlugin.parseMarkup = function() {
		Y.all('[tooltip]').each(function(e) {
			var type = 'auto';
			if (e.getAttribute('tooltipType') == 'instant') {
				type = 'instant';
			}
			e.plug(Y.TooltipPlugin, { text: e.getAttribute('tooltip'), type: type });
		});
	};

	Y.TooltipPlugin = TooltipPlugin;

}, '0.0.1', {requires: ['base', 'plugin', 'node']});

