/*
---

script: NiceButton.js

requires:

- more:1.2.4.4:Fx.Elements

provides: [NiceButton]

...
*/

var RadioAr = new Array();

var NiceButton = new Class({
	
	
	Implements: [Options, Events],
		
		options: {
			delay: 7000,
			transition: 'crossFade',
			duration: '50',
		},
	
	high: function() {
		//this.status = 'high';
		this.show(1);
	},

	mouseenter: function() {
		this.show(1);
	},
	normal: function() {
		this.status = 'normal';
		this.show(0, {duration: '300'});
	},

	mouseleave: function() {
		if (this.status != 'clicked') {this.normal()};
	},

	
	show: function(slide, options){
		slide = (typeof slide == 'number') ? this.slides[slide] : slide;
		if (slide != this.current) {
			var			
				previous = this.current.setStyle('z-index', 1),
				next = this.reset(slide);
			var transition = (options && options.transition) ? options.transition: this.getTransition(slide),
				duration = (options && options.duration) ? options.duration: this.getDuration(slide);
				
			if (this.transitioning){
				this.reset(slide);
				timer = $clear(timer);
			}; 
			
			
			this.transitioning = true;
			
				//previous = this.current.setStyle('z-index', 1),
				//next = this.reset(slide);
			var slideData = {
				previous: {
					element: previous,
					index: this.slides.indexOf(previous)
				}, 
				next: {
					element: next,
					index: this.slides.indexOf(next)
				}
			};
			this.fireEvent('show', slideData);
			this.transitions[transition](previous, next, duration, this);
			var timer = (function() { 
				//previous.setStyle('display','none');
				this.fireEvent('showComplete', slideData);
				this.transitioning = false;
			}).bind(this).delay(duration);
			this.current = next;
		}; 
					
		
		return this;
	},
	
	initialize: function(element, options){
		this.setOptions(options);
		this.element = document.id(element);
		this.slides = this.element.getChildren();
		this.current = this.slides[0];
		this.transitioning = false;
		this.setup();

		var status = 'normal';
		var RadioButton = '';
		var no_high_pic = false;
		var no_click_pic = false;		
		if (typeof(this.slides[1]) == 'undefined') {this.no_high_pic = true};
		if (typeof(this.slides[2]) == 'undefined') {this.no_click_pic = true};
		if (this.RadioButton = this.element.getProperty('radio')) {
			if (RadioAr.length == 0) {this.clicks()};
			RadioAr.push(this);
		};
		

	},
	
	setup: function(){
	    this.setupElement().setupSlides(true);

		this.element.addEvent('mouseenter', this.mouseenter.bind(this));
		this.element.addEvent('mouseleave', this.mouseleave.bind(this));
		this.element.addEvent('click', this.clicks.bind(this));
		this.element.setStyle('cursor','pointer');
/*
		this.element.addEvents({
			mouseenter: function () {if  (this.status !='clicked')  {this.high.bind(this)}},
			mouseleave: function () {if (this.status != 'clicked') {this.normal.bind(this)}},
			click: function () {this.clicks.bind(this)} 
		});
*/
		return this;
	},
	
	setupElement: function(){
		var el = this.element;
		if (el.getStyle('position') != 'absolute' && el != document.body) el.setStyle('position','relative');
		return this;
	},
	
	setupSlides: function(hideFirst){
		this.slides.each(function(slide, index){
			this.storeTransition(slide).reset(slide);
			if (hideFirst && index != 0) slide.setStyle('display','none');
		}, this);
		return this;
	},
	
	storeTransition: function(slide){
		var classes = slide.get('class');
		var transitionRegex = /transition:[a-zA-Z]+/;
		var durationRegex = /duration:[0-9]+/;
		var transition = (classes.match(transitionRegex)) ? classes.match(transitionRegex)[0].split(':')[1] : this.options.transition;
		var duration = (classes.match(durationRegex)) ? classes.match(durationRegex)[0].split(':')[1] : this.options.duration;
		slide.store('ssTransition', transition).store('ssDuration', duration);
		return this;
	},
	
	resetOptions: function(options){
		this.options = $merge(this.options, options);
		this.setupSlides(false);
		return this;
	},
	
	getTransition: function(slide){
		return slide.retrieve('ssTransition');
	},
	
	getDuration: function(slide){
		return slide.retrieve('ssDuration');
	},
	

	

		
	clicks: function() {
		if (this.RadioButton != null) {
			this.status='clicked';
			for (var i = 0; i < RadioAr.length; i++) {
			  if (RadioAr[i] != this) {RadioAr[i].normal()}
			}
		}
		if (!this.no_click_pic) {this.show(2)} else {this.show(1)};
	},
	
	

	
	reset: function(slide){
		return slide.setStyles({
			'position': 'absolute',
			'z-index': 0,
			'display': 'block',
			'left': 0,
			'top': 0
		}).fade('show');

		this.element.addEvents({
			mouseenter: function () {if  (this.status !='clicked') {this.reset()}},
			mouseleave: function () {if (this.status != 'clicked') {this.normal()}},
			click: function () {this.clicks()} 
		});
	},
	
	nextSlide: function(){
		var next = this.current.getNext();
		return (next) ? next : this.slides[0];
	},

	previousSlide: function(){
		var previous = this.current.getPrevious();
		return (previous) ? previous : this.slides.getLast();
	},
	
	toElement: function(){
		return this.element;
	}

});



Element.Properties.nicebutton = {

	set: function(options){
		var nicebutton = this.retrieve('nicebutton');
		return this.eliminate('nicebutton').store('nicebutton:options', options);
	},

	get: function(options){
		if (options || !this.retrieve('nicebutton')){
			if (options || !this.retrieve('nicebutton:options')) this.set('nicebutton', options);
			this.store('nicebutton', new NiceButton(this, this.retrieve('nicebutton:options')));
		}
		return this.retrieve('nicebutton');
	}

};


NiceButton.adders = {
	
	transitions:{},
	
	add: function(className, fn){
		this.transitions[className] = fn;
		this.implement({
			transitions: this.transitions
		});
	},
	
	addAllThese : function(transitions){
		$A(transitions).each(function(transition){
			this.add(transition[0], transition[1]);
		}, this);
	}
}

$extend(NiceButton, NiceButton.adders);
NiceButton.implement(NiceButton.adders);

NiceButton.add('fade', function(previous, next, duration, instance){
	previous.set('tween',{duration: duration}).fade('out');
	return this;
});

NiceButton.addAllThese([

	['none', function(previous, next, duration, instance){
		previous.setStyle('display','none');
		return this;
	}],

	['crossFade', function(previous, next, duration, instance){
		previous.set('tween',{duration: duration}).fade('out');
		next.set('tween',{duration: duration}).fade('in');
		return this;
	}],

	['fadeThroughBackground', function(previous, next, duration, instance){
		var half = duration/2;
		next.set('tween',{ duration: half	}).fade('hide');
		previous.set('tween',{
			duration: half,
			onComplete: function(){
				next.fade('in');
			}
		}).fade('out');
	}],
	
	['pushLeft', function(previous, next, duration, instance){
		var distance = instance.element.getSize().x;
		next.setStyle('left', distance);
		new Fx.Elements([previous,next],{duration: duration}).start({
			0: { left: [-distance] },
			1: { left: [0] }
		});
		return this;
	}],
	

	
	['pushRight', function(p,n,d,i){
		var distance = i.element.getSize().x;
		n.setStyle('left', -distance);
		new Fx.Elements([p,n],{duration: d}).start({
			0: { left: [distance] },
			1: { left: [0] }
		});
		return this;
	}],
	

	['pushUp', function(p,n,d,i){
		var distance = i.element.getSize().y;
		n.setStyle('top', distance);
		new Fx.Elements([p,n],{duration: d}).start({
			0: { top: [-distance] },
			1: { top: [0] }
		});
		return this;
	}],

	['pushDown', function(p,n,d,i){
		var distance = i.element.getSize().y;
		n.setStyle('top', -distance);
		new Fx.Elements([p,n],{duration: d}).start({
			0: { top: [distance] },
			1: { top: [0] }
		});
		return this;
	}],



]);

/*
*/

