sliderLength = 720;
sliderOffset = 15;
currentStep = null;
rtTimer = null;
t = null; 	//time
rt = null; 	//richtime
step = null;
// console.log(rt);


// Offset correction for mootols
Slider.implement({
	toPosition: function(step){
		return (this.max-this.options.offset) * step / this.options.steps;
	}
});
	
window.addEvent('domready', function(){
	rtClock();
	tClock();
	
	/* Slider 1 */
	// console.log("HI");
	
	rtSlider = new Slider($('slider'), $('handle'), {
		steps: sliderLength,
		offset: sliderOffset, 
		onChange: function(step) {
			
			currentStep = step;
			percent = Math.floor((sliderLength - step) / sliderLength * 100);
			if (percent < 75) {
				percent = 75;
			}
			$('value').setHTML(percent);
			$clear(rtTimer);
			new Fx.Style($('content'), 'opacity', {duration: 100}).set(percent / 100);

			dayofyear = step / 500 * 365;
			month = Math.ceil(dayofyear / 12);
			day = dayofyear % 12;
			rt = stepToTime(step); //new Date(2008, month, dayofyear, rt.getHours(), rt.getMonth(), rt.getSeconds())
			rtClock();
			
			// console.log(step);
		},
		onComplete: function() {
			rtReset();
		}
	}).set(timeToStep(rt) - sliderOffset);
	
	// console.log(t);
	

	tSlider = new Slider($('slider'), $('thandle'), {
		steps: sliderLength,
		offset: sliderOffset,
		onChange: function() {
			// console.log(this);	
		}
	}).set(timeToStep(t) - sliderOffset);
	
	// alert(tSlider);		
	
	step = timeToStep(rt);
	tTick.periodical(1000);
	rtTick.periodical(rtSec);

});

function stepToTime(step) {

	time = step / 720 * (dp(et) - dp(bt)) + dp(bt);

	time = new Date(time);
	return new Date(time.getFullYear(), time.getMonth(), time.getDate(), rt.getHours(), rt.getMinutes(), rt.getSeconds());
}

function dp(d) {
	return Date.parse(d);
}

function timeToStep(time) {
	step = (dp(time) - dp(bt)) / (dp(et) - dp(bt)) * 720;

	step = (time - bt) / (et - bt) * 720;
	
	if (step > 720) {
		step = 720;	
	} else if (step < 0) {
		step = 0;	
	}
	return Math.floor(step);
	// console.log(step);
}

function rtTick() {
	rt = new Date(Date.parse(rt) + 1000);
	rtClock();	
}

function tTick() {
	t = new Date(Date.parse(t) + 1000);
	tClock();	
}

function rtReset() {
	rtTimer = rtResetLoop.periodical(100);
}

function rtResetLoop() {
	if (currentStep != step) {
		diff = Math.ceil((step - currentStep) / 10);
		if (diff == 0) { diff = -1; };
		currentStep += diff;
		rtSlider.set(currentStep);
	} else {
		$clear(rtTimer);
	}	
}

tLastMinutes = 0;
function tClock() {
	if (t.getMinutes() != tLastMinutes) {
		onTMinute();
		tLastMinutes = t.getMinutes();
	}
	$('t-display').setHTML(lz(t.getDate()) + '.' + lz(t.getMonth() + 1) + '.' + (t.getYear() + 1900) + ' ' + lz(t.getHours()) + ':' + lz(t.getMinutes()) + ':' + lz(t.getSeconds()));
}

rtLastMinutes = 0;
function rtClock() {
	//console.log(rt);
	if (rt.getMinutes() != rtLastMinutes) {
		onRtMinute();
		rtLastMinutes = rt.getMinutes();
	}
	$('rt-display').setHTML(lz(rt.getDate()) + '.' + lz(rt.getMonth() + 1) + '.' + (rt.getYear() + 1900) + ' ' + lz(rt.getHours()) + ':' + lz(rt.getMinutes()) + ':' + lz(rt.getSeconds()));
}

/*
= Time Events
-------- -------- -------- -------- -------- -------- -------- */

function onTMinute() {
	
}

function onRtMinute() {
	
}

function flash(color) {
	if(!color) {
		color = '#FF0';
	}
	new Fx.Style($('all'), 'background-color', {duration: 500}).start(color, '#FFF');
}

/*
= Helpers
-------- -------- -------- -------- -------- -------- -------- */
// lz() : leadingZeros
function lz(str) {
	return leadWithChars(str, 2, 0);	
}


// fills a string to a specific length with chars starting at left
function leadWithChars(str, forcedLength, fill) {
	str = str.toString();
	while (str.length < forcedLength) {
		str = fill + str;	
	}
	return str;
}
