var TimeSpinner = new Class({
	Implements: [Events, Options],

	version: 0.4,

	options : {
		increment: 15,
		separator: ':',
		range: {
			low: -1,
			high: -1
		},
		delay: 200,
		alarm: [],
		'doAlarm': Class.empty
	},

	initialize: function(obj, options, date) {
		if (!obj) { return false; }

		if( $defined( options ) ) {
			this.setOptions(options);
		}

		this.timespinner = obj;
		//this.timespinner.disabled = true;
		this.timespinner.addClass('timespinner');

		this.date = new Date();
		if( $defined( date ) ) {
			if( date !== date.toInt() ) {
				this.date.setHours( date.substring(0,2).toInt() );
				this.date.setMinutes( date.substring(3,5).toInt() );
			} else {
				var m = date % 60;
				this.date.setMinutes( m );
				this.date.setHours( (date - m)/60 );
			}
		} else {
            this.readTime();
        }
		this.doaction = '';

        var container = new Element('div').addClass('timespinner_buttons').injectAfter(obj);

		var bp = new Element('input', { 'type': 'button' }).addClass('timespinner_plus').injectInside(container);
		bp.addEvent('mousedown', function() {this.doStart('inc'); }.pass('inc', this) );
		bp.addEvent('mouseup', function() {this.doStop(); }.pass('', this) );

		bp = new Element('input', { 'type': 'button' }).addClass('timespinner_minus').injectInside(container);
		bp.addEvent('mousedown', function() {this.doStart('dec'); }.pass('dec', this));
		bp.addEvent('mouseup', function() {this.doStop(); }.pass('', this) );

		this.fixTime();

		this.addEvent(
			'doclick', function() {
				switch( this.doaction ) {
					case 'inc' :
						this.incTime();
						break;
					case 'dec' :
						this.decTime();
						break;
					case 'cancel' :
						this.doaction = '';
						return;
					default:
						return;
				}

				this.fireEvent( 'doclick', '', this.options.delay );
			}
		);

//		this.addEvent( 'doChange', this.options.doChange);
		this.addEvent( 'doAlarm', this.options.doAlarm);
	},
    explode: function( item, delimiter) {
        tempArray = new Array(1);
        var Count = 0;
        var tempString = new String(item);

        while ( tempString.indexOf( delimiter ) > 0 ) {
            tempArray[Count] = tempString.substr( 0, tempString.indexOf( delimiter ) );
            tempString = tempString.substr( tempString.indexOf( delimiter )+1, tempString.length - tempString.indexOf( delimiter ) + 1 );
            Count = Count+1;
        }

        tempArray[Count]=tempString;
        return tempArray;
    },
    timeToSec: function( str ) {
        //alert(parseInt(str));
        var step, sec = 0;
        var unit = this.explode( str, ':' );
        for( var i=0; i < unit.length; i++ ) {
            step = (i) ? step / 60 : 3600;
            unit[i] = unit[i].toInt() == unit[i] ? unit[i].toInt() : 0;
            sec +=  unit[i] * step;
        }
        return sec;
    },
	readTime: function() {
        //alert(this.timespinner.value);
        var sec = this.timeToSec( this.timespinner.value );
        //this.date.setTime( sec );
        this.date.setHours( ( sec / 3600 ).toInt() );
        this.date.setMinutes( ( ( sec % 3600 ) / 60 ).toInt() );
        //alert( this.timeToSec( this.timespinner.value ) );
	},
	doStart: function(todo) {
		this.doaction = todo;
		this.fireEvent( 'doclick', "", 0 );
	},
	doStop: function() {
		this.doaction = 'cancel';
	},

	setTime: function( val ) {
		if( val instanceof Date ) {
			this.date = val;
		} else {
			this.date.setTime( val );
		}
		this.fixTime();
	},
	getTime: function()  {
		return( this.date );
	},

	setInc: function( val ) {
		this.options.increment = val;
		this.fixTime();
	},
	getInc: function( val ) {
		return(this.options.increment);
	},

	fixTime: function() {
		var m = this.date.getMinutes();
		this.date.setMinutes( m - (m % this.options.increment) );

		if( this.options.range.high > -1 ) {
			var t = this.date.getHours()*60 + this.date.getMinutes();
			if( t >= this.options.range.high ) {
				var n  = this.options.range.high % 60;
				this.date.setMinutes( n );
				this.date.setHours( (this.options.range.high - n)/60 );
			}
		}

		if( this.options.range.low > -1 ) {
			var t = this.date.getHours()*60 + this.date.getMinutes();
			if( t <= this.options.range.low ) {
				var n  = this.options.range.low % 60;
				this.date.setMinutes( n );
				this.date.setHours( (this.options.range.low - n)/60 );
			}
		}

		var h = this.date.getHours().toInt();
		m = this.date.getMinutes().toInt();
		this.timespinner.value =  (h<10 ? "0" : "") + h + this.options.separator + (m<10 ? "0" : "") + m;
/*
		var t = this.date.getHours()*60 + this.date.getMinutes();
		this.options.alarm.each( function(val) {
			if( val == t ) {
				this.fireEvent('doAlarm', {who:this.timespinner.id,time:val}, 0 );
			}
		}, this);
*/
	},

	incTime: function( val ) {
        this.readTime();
		if( $defined( val ) == false  ) {
			val = this.options.increment;
		}
		this.date.setTime( this.date.getTime() + (val*60000) );
		this.fixTime();
	},
	decTime: function( val ) {
        this.readTime();
		if( $defined( val ) == false  ) {
			val = this.options.increment;
		}
		this.date.setTime( this.date.getTime() - (val*60000) );
		this.fixTime();
	}
});

TimeSpinner.implement(new Events, new Options);

