/**
 *
 *
 */
 
if( !console ) {
	var console = {'log':function(a,b,c,d,g){} }	
}

var Monktools = {};
Monktools.version = 0.10;
Monktools.Form = {};
Monktools.Form.Checker = new Class(
{
	elForm:false,
	strWrapperElement:false,
	strErrorElement:false,
	strErrorTriggerClass:false,
	funcSubmitHandler:false,
	
	initialize:function()
	{
		this.Error._parent = this;
	},
	
	setFormWrapper:function( argElForm )
	{
		this.elForm = $(argElForm);
		this.elForm._parent = this;
		this.elForm.onsubmit = function(){
			
			return this._parent.Send();
		};
	},
	
	setWrapperElement:function( argStrWrapperElement )
	{
		if( $type(argStrWrapperElement) != 'string' ){alert('setWrapperElement argument is not a string (xpath)')}
		this.strWrapperElement = argStrWrapperElement;
	},
	
	setErrorElement:function( strErrorElement )
	{
		if( $type(strErrorElement) != 'string' ){ alert('setErrorElement argument is not a string (xpath)') }
		this.strErrorElement = strErrorElement;
		return this.Error;
	},
	
	setSubmitHandler:function(fn){
		this.funcSubmitHandler = function(){ fn.call(this) };
	},
	
	Error:{
		setTriggerClass:function( strClass )
		{
			if( $type(strClass) != 'string' ){ alert('setTriggerClass argument is not a string') }

			this._parent.strErrorTriggerClass = strClass;
		},
		
		setError:function( bln, el )
		{
			if( bln )
			{
				if( this._parent.strErrorTriggerClass )
				{
					el.addClass( this._parent.strErrorTriggerClass );
				}
				else
				{
					el.setStyle('display','block');
				}
			}
			else
			{
				if( this._parent.strErrorTriggerClass )
				{
					el.removeClass( this._parent.strErrorTriggerClass );
				}
				else
				{
					el.setStyle('display','none');
				}				
			}
		}
	},
	
	Send:function()
	{
		if( !this.__hasErrorCheck() )
		{
			if( this.funcSubmitHandler )
			{
				this.funcSubmitHandler();
				return false;
			}
			else
			{
				// this.elForm.submit();
			}
		}
		else
		{
			return false;
		}
		
		return false;
	},
	
	checkMail:function(argStrMail)
	{
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (filter.test(argStrMail))
		{
			return true;
		}
		else
		{
			return false;
		}
	},

	checkPostcode:function(argStrPostcode)
	{
		
		var filter = /[0-9]{4} ?[A-Z]{2}/i;
		if (filter.test(argStrPostcode))
		{
			return true;
		}
		else
		{
			return false;
		}
	},

	__hasErrorCheck:function()
	{
		var blnHasError = false;
		
		this.elForm.getElements( this.strWrapperElement ).each(function(elNode,intI)
		{
			
			var arrElError = elNode.getElements( this.strErrorElement );
										
			if( arrElError.length > 0 )
			{
				arrElInput = elNode.getElements('input');	
				arrElTextarea = elNode.getElements('textarea');
				arrElSelect = elNode.getElements('select');
				
				if( arrElInput.length > 0 )
				{
					var blnCurrentError = false;
					var blnNeedOne = false;
					
					arrElInput.each(function(elNode, intI)
					{
						if( elNode.name.indexOf('email')>=0  )
						{
							if( !this.checkMail(elNode.value) )
							{
								blnHasError = true;
								
								this.Error.setError(true, arrElError[0] );
							}
							else if( !blnCurrentError )
							{
								this.Error.setError(false, arrElError[0] );
							}
						}
						else if( elNode.name.indexOf('geslacht')>=0  )
						{
							if( elNode.checked == false && intI == 1 && !blnNeedOne )
							{
								blnHasError = true;
								this.Error.setError( true, arrElError[0] );
							}
							else if( !blnCurrentError && elNode.checked != false )
							{
								blnNeedOne = true;
								this.Error.setError(false, arrElError[0] );
							}
						}
						else if( elNode.name.indexOf('postcode')>=0  )
						{										
							if( !this.checkPostcode(elNode.value) )
							{
								blnHasError = true;
								blnCurrentError = true;
								this.Error.setError( true, arrElError[0] );
							}
							else if( !blnCurrentError )
							{
								this.Error.setError(false, arrElError[0] );
							}
						}
						else
						{
							if( elNode.value == '' )
							{
								blnHasError = true;
								this.Error.setError( true, arrElError[0] );
							}
							else if( !blnCurrentError )
							{
								this.Error.setError(false, arrElError[0] );
							}
						
						}
					}.bind(this));
				}

				if( arrElSelect.length > 0 )
				{
					var blnCurrentError = false;
					var blnNeedOne = false;
					
					arrElSelect.each(function(elNode, intI)
					{
						if( elNode.value == '' )
						{
							blnHasError = true;
							this.Error.setError( true, arrElError[0] );
						}
						else if( !blnCurrentError )
						{
							this.Error.setError(false, arrElError[0] );
						}	
					}.bind(this));
				}

				if( arrElTextarea.length > 0 )
				{
					if( arrElTextarea[0].value == '' )
					{
						blnHasError = true;
						this.Error.setError( true, arrElError[0] );
					}
					else
					{
						this.Error.setError( false, arrElError[0] );
					}
				}
			}
		}.bind(this));
		
		return blnHasError;
	}

});


Monktools.Html = {};
Monktools.Html.Overlayer = new Class({
	initialize:function(el,elButton,elButtonClose){
		
		this.elBody = $$('body')[0];

		this.elOverlayer = new Element('div',{
			'events':{
				click:function(){
					this.Close();
				}.bind(this)
			},
			'styles' : {
				'position':'absolute',
				'background-color': '#000000',
				'position': 'absolute',
				'z-index': '900',
				'opacity': '0.8',
				'left': 0,
				'top': 0,
				'display':'block'
			}
		});
		
		this.elOverlayer.oDrag = new Drag(elButtonClose, {
			snap: 0,
			onSnap: function(el){
				el.addClass('dragging');
			},
			onComplete: function(el){
				el.removeClass('dragging');
			}
		});
		
		this.elOverlayer.addEvents({
			mouseover:function()
			{
				//this.oDrag.element.setStyle('position','absolute');
				this.oDrag.attach();
			}.bind(this.elOverlayer),
			
			mouseout:function()
			{
				//this.oDrag.element.setStyle('position','relative');
				this.oDrag.detach();
			}.bind(this.elOverlayer)
		});
		
		this.elBody.appendChild( this.elOverlayer );
		
		this.elCenter = $(el);

		this.elBody.oSize = this.elBody.getSize();
		
		elButton.addEvent('click',function(event){
			event.stop();
			this.Show();
		}.bind(this));
		
		elButtonClose.addEvent('click',function(){
			this.Close();
		}.bind(this));
	},
	
	Show:function()
	{
		this.elBody.oSize = this.elBody.getScrollSize();
		this.elBody.oScroll = this.elBody.getScroll();
		
		this.elOverlayer.setStyles({
			'display':'block',
			'width': this.elBody.oSize.x,
			'height': this.elBody.oSize.y ,
			'top': 0
		});
				
		this.elCenter.setStyle('display','block');
		if( !this.elCenter.oSize ) this.elCenter.oSize = this.elCenter.getSize();
		
		var intScrollTop = getScrollTop();
		var intHeight = getHeight();
		var intTop = intScrollTop + 100;
		//var intTop = 100;
		
		this.elCenter.setStyles({
			'display':'block',
			'position':'absolute',
			'left': (( this.elBody.oSize.x-this.elCenter.oSize.x )/2)+ 'px',
			'top': intTop + 'px'
		});
		
	},	
	
	Close:function()
	{
		this.elOverlayer.setStyle('display','none');
		this.elCenter.setStyle('display','none');
	}
	
});

Monktools.Html.Tabber = new Class({
	initialize:function(tabs,content,activate){
		this.tabs = $A(tabs);
		this.content = $A(content);
		
		this.tabs.each(function(el,i){
			el._parent = this;
			el.i = i;
			el.addEvent('click',function(){
				this._parent.activate(this.i);
				return false;
			});
		}.bind(this));
		
		var cookie_activate = Cookie.read('Monktools.Html.Tabber.Index');
		
		if( cookie_activate != null )
		{
			this.activate( cookie_activate );
		}
		else if(activate != undefined)
		{
			this.activate( activate );
		}
	},
	
	activate:function(argI){
		Cookie.write('Monktools.Html.Tabber.Index', argI, {duration: 1});
		
		this.tabs.each(function(el,i){
			el.removeClass('active');
			
			if(i == argI)
			{
				el.addClass('active');
			}
		}.bind(this));
		
		this.content.each(function(el,i){
			el.setStyle('display','none');
			
			if(i == argI)
			{
				el.setStyle('display','block');
			}
		}.bind(this));
	}
});