// JavaScript Document

function l(tx){

	$("#log").html($("#log").html()+""+tx)

}

function callback(_id, fn){
	
	this.id =_id;
	MANAGER.prepareCallback(this.id, fn);
	var context = this;
	this.invoke = function(){
		MANAGER.service(context.id);
	}
}

function manager(){
	var id = 0;
	var disabled = new Array();
	
	this.getNextID = function(){
		return id++;	
	}
	
	this.service = function(id){
		if(!disabled[id]){
			this[id](id);
		}
	}
	
	this.prepareCallback = function(id, fn){
		var old = this[ id ];
		disabled[id] = false;
		this[ id ] = function(){
			if ( fn.length == arguments.length )
				return fn.apply( this, arguments );
			else if ( typeof old == 'function' )
				return old.apply( this, arguments );
		};
	}
	
	this.disableCallback= function(id){
		disabled[id] = true;
	}
}




var MANAGER = new manager();
var menuBoxHeight = 30;
var lock = false
var curTop = 0;
var disableCallback = false;
var direction = 0; //0: going down, 1:going up
var currentItemId


function selectMenuItem(itemId){
	currentItemId = itemId
	$('#roller'+itemId).css('opacity','1.0');
	if(jQuery.browser.msie){
		$('#roller'+itemId).css('filter:',"alpha(opacity='100')");
		
		$('#roller2').css('top','-200px');
		$('#roller3').css('top','-260px');
		$('#roller4').css('top','-200px');
		$('#roller5').css('top','-100px');		
						  
	}
	
}

function showRoller(id){
	if(! jQuery.browser.msie){
		var CB = new callback(id, function(id) {
									animateRoller(id);
								})
		$('#roller'+id).animate( { opacity: "1.0",filter:"alpha(opacity='100')" }, 250, CB.invoke);
	} else {
		$('#roller'+id).css('filter','"alpha(opacity=\'100\')"');
	}
}

function hideRoller(id){
	MANAGER.disableCallback(id);
	if(currentItemId!=id){
		$('#roller'+id).animate( { opacity: "0.4",filter:"alpha(opacity='40')" },500);	
	}
}

function animateRoller(id){
	var img = $('#roller'+id);
	var h = img.height()
	var top = getTop(img)
	
	var topTarget
	var speed
	
	var CB = new callback(id, function(id) {
								animateRoller(id);
							})
	
	if((direction & id*2) == 0){	
		var distanceLeft = -h + menuBoxHeight - top;
		if(distanceLeft>-100){
			topTarget = -h + menuBoxHeight;
			speed = 10 * (-distanceLeft);
			direction = direction ^ 2*id;
		} else {
			topTarget = top-100;
			speed = 1000;
		}
		
	}else { //going up
		var distanceLeft = -top;
		if(distanceLeft<100){
			topTarget = 0;
			speed = 10 * (-distanceLeft);
			direction = direction ^ 2*id;
		} else {
			topTarget = top+100;
			speed = 1000;
		}
	} 
	img.animate( { top:  topTarget } , speed, CB.invoke );
}

function getTop(img){
	return parseInt(img.css('top').replace(/px,*\)*/g,""));
}

var TIME_BASE = 50

function IE_FadeOut(element, startValue, endValue, time){
	var numIterations = time/TIME_BASE;
	var step = (endValue-startValue) / numIterations;
	IE_Fader(element,startValue, step, numIterations)
}

function IE_Fader(element,startValue, fadeValue, count){
	l(startValue+' | '+count);
	var newVal = startValue+fadeValue;
	element.css('filter','"alpha(opacity=\''+newVal+'\')"');
	l('"alpha(opacity=\''+newVal+'\')"')
	if(count>0){
		setTimeout(function(){IE_Fader(element,newVal, fadeValue, --count)},TIME_BASE)	
	}
}