var SlideForm = Class.create(Observable, {
	initialize: function($super, container){
		$super();
		
		//Collect the elements
		this.container = $(container);
		this.slides = this.container.select('fieldset');
		this.length = this.slides.length;
		
		this.slider = this.container.down('div');
		
		this.inputs = this.slides.collect(function(item, index){
		//	return item.select('input, textarea, select').invoke('disable');
		});
		
		
		//Set the width for the elements
		this.container.absolutize().makeClipping();
		this.slides.invoke('setStyle', {
			width: this.container.getStyle("width")
		});
		
		this.container.setStyle({
			height: this.slides.first().getHeight() + 'px'
		});
		
		this.slider.setStyle({width: (this.container.getWidth() * this.slides.length + (this.slides.length * 25)) + 'px'});	
		
		/*
		this.slider.select('img.next').invoke('observe', 'click', this.next.bindAsEventListener(this));
		this.slider.select('img.previous').invoke('observe', 'click', this.previous.bindAsEventListener(this));
		*/
		this.afterFinish = this.afterFinish.bind(this);
		this.beforeStart = this.beforeStart.bind(this);
		
		
		
		this.idx = 0;
		this.busy = false;	
	},
	next: function(e){
		if(e) e.stop();
		
		this.go(this.idx + 1);
	},
	previous: function(e){
		if(e) e.stop();
		
		this.go(this.idx - 1);		
	},
	first: function(e){
		if(e) e.stop();
		
		this.go(0);	
	},
	last: function(e){
		if(e) e.stop();
		
		this.go(this.length-1);	
	},
	go: function(idx){
		if(this.busy || idx == this.idx || idx >= this.slides.length) 
			return;
	
		var direction = idx > this.idx ? 'right' : 'left';
		var width = 0;
		
		
		for(var i=this.idx; 
			direction == 'left' ? i > idx : i < idx; 
			direction == 'left' ? i-- : i++){
			
			width += this.slides[i].getWidth();
		}
				
		this.effect = new Effect.Move(this.slider, {
			_idx: idx,
			x: direction == 'left' ? width : -width,
			duration: 0.6,
			beforeStart: this.beforeStart,
			afterFinish:this.afterFinish
		});			
	},
	beforeStart: function(effect){
		this.busy = true;
	},
	afterFinish: function(effect){		
		this.idx = parseInt(effect.options._idx);
		
		this.dispatch('slide', effect.options._dir, this.idx);

		/*if(this.inputs[this.idx].size() != 0){
			this.inputs[this.idx].invoke('enable').first().focus();
		}*/
		
		this.busy = false;
	}
});

(function(){
	
var org = [];

window.validate_support_form = function(form){
	var failed = false;
	
	$$("form[name=submitticket] input[type=text],textarea").each(function(input, i){
		var n = input.readAttribute("name");
		var v = input.getValue();
		var l = input.previous('label');
		
		if(!org[i]){
			org[i] = l.innerHTML;
		}
		
		if(n == "email"){
			if(!/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i.test( v ) ){
				l.update(org[i] + ': <span style="color:red">Ugyldig e-postadresse</span>');
				failed = true;
			}else{
				l.update(org[i]);
			}
		}
		
		if(n == "message"){
			
			l.update(v.blank() ? org[i] + ': <span style="color:red">Du må skrive inn en melding</span>' : org[i]);
			failed = true;
		}
		
		if(n == "fullname"){
			l.update(v.blank() ? org[i] + ': <span style="color:red">Du må skrive inn navnet ditt</span>' : org[i] );
			failed = true;			
		}
	});
	
	if(!failed){
		form.update("<b>Takk for din henvendelse.</b>");
	}
	
	return !failed;
}

})();