var flipShown = false;

var animation = {duration:0, starttime:0, to:1.0, now:0.0, from:0.0, element:null, timer:null};

function mouseDown(event, id) {
if( navigator.appName != "Microsoft Internet Explorer" )
	{
	event.target.src = "images/"+id+"_down.png";
	}else{
	event.srcElement.src = "images/"+id+"_down.gif";
	}
//	event.stopPropagation();
//	event.preventDefault();
}

function mouseUp (event, id) {
//	if (hiliteOp)
//		setOpHilite();
//	else
if( navigator.appName != "Microsoft Internet Explorer" )
	{
		event.target.src = "images/"+id+".png";
	}else{
		event.srcElement.src = "images/"+id+".gif";
	}
}

function mouseOut (event, id) {
//	if (!hiliteOp)
//		event.target.src = "images/"+id+".png";
}

//  Returns val if min < val < max
//  Returns min if val <= min
//  Returns max if val >= max

function limit_3 (val, min, max)
{
    return val < min ? min : (val > max ? max : val);
}

function computeNextFloat (from, to, ease)
{
    return from + (to - from) * ease;
}


function animate()
{
	var T;
	var ease;
	var time = (new Date).getTime();
		
	
	T = limit_3(time-animation.starttime, 0, animation.duration);
	
	if (T >= animation.duration)
	{
		clearInterval (animation.timer);
		animation.timer = null;
		animation.now = animation.to;
	}
	else
	{
		ease = 0.5 - (0.5 * Math.cos(Math.PI * T / animation.duration));
		animation.now = computeNextFloat (animation.from, animation.to, ease);
	}
	
	animation.element.style.opacity = animation.now;
}

function mousemove (event)
{
	if (!flipShown)
	{
		// fade in the flip widget
		if (animation.timer != null)
		{
			clearInterval (animation.timer);
			animation.timer  = null;
		}
		
		var starttime = (new Date).getTime() - 13; // set it back one frame
		
		animation.duration = 500;
		animation.starttime = starttime;
		animation.element = document.getElementById ('flip');
		animation.timer = setInterval ("animate();", 13);
		animation.from = animation.now;
		animation.to = 1.0;
		animate();
		flipShown = true;
	}
}

function mouseexit (event)
{
	if (flipShown)
	{
		// fade in the flip widget
		if (animation.timer != null)
		{
			clearInterval (animation.timer);
			animation.timer  = null;
		}
		
		var starttime = (new Date).getTime() - 13; // set it back one frame
		
		animation.duration = 500;
		animation.starttime = starttime;
		animation.element = document.getElementById ('flip');
		animation.timer = setInterval ("animate();", 13);
		animation.from = animation.now;
		animation.to = 0.0;
		animate();
		flipShown = false;
	}
}
function onshow () {
//	if (timerInterval == null) {startSecondHandTimer();}
//	updateTime(false);
//flipShown=true;mouseexit
//document.getElementById("flip").style.display="none";
}

function onhide () {
exitflip();
//	if (timerInterval != null) { we were hidden clear the hands
//		clearHands();
		
		// clear the timer
//		clearInterval(timerInterval);
//		timerInterval = null;
//	}
}
function showbackside(event)
{
	var front = document.getElementById("front");
	var back = document.getElementById("behind");
	
	if (window.widget)
		widget.prepareForTransition("ToBack");
	onhide();
	
	front.style.display="none";
	back.style.display="block";
	
	if (window.widget)
		setTimeout ('widget.performTransition();', 0);	


	document.getElementById('fliprollie').style.display = 'none';
}


function doneClicked() {
        var front = document.getElementById("front");
        var back = document.getElementById("behind");

        if (window.widget)
                widget.prepareForTransition("ToFront");


        front.style.display="block";
        back.style.display="none";

        setTimeout ('flipitback();', 0);
        onshow();

}


function flipitback()
{
//	updateTime(false);
	if (window.widget)
		setTimeout("widget.performTransition();", 0);
}


if (window.widget) {
	data = widget.preferenceForKey("locale");
	widget.onhide = onhide;
	widget.onshow = onshow;
}


function enterflip(event)
{
//	document.getElementById('fliprollie').style.display = 'block';
	document.getElementById('flip').style.background = 'url(images/flip.gif) no-repeat top left';
}

function exitflip(event)
{
//	document.getElementById('fliprollie').style.display = 'none';
	document.getElementById('flip').style.background = 'url(images/white_i.gif) no-repeat top left';
}
