var ContentHeight = 140;
var t;

function togleLoginDiv(){
	var container = document.getElementById("LoginContainer");
	var isIE6 = false;
	if((navigator.userAgent.indexOf("MSIE 6") > -1)) {
		isIE6 = true;
	}

	if(container.style.visibility == "visible") {
		if(isIE6) {
			displayDropDown();
		}
		hideLoginDiv();
	} else {
		if(isIE6) {
			hideDropDown();
		}
		displayLoginDiv();
	}
}

function setCloseTimer() {
	t=setTimeout("togleLoginDiv()",1000);
}

function clearCloseTimer() {
	clearTimeout(t);
}

function displayLoginDiv(){
	var container = document.getElementById("LoginContainer");
		container.style.visibility = "visible";
	var userNameInput = document.getElementById("userNameField");
		userNameInput.focus();
	var content = document.getElementById("LoginContent");
	var contentSlideIn = new Animation(content, "top", -(ContentHeight), 0, "px");
		contentSlideIn.init();
}

function hideLoginDiv(){	
	var content = document.getElementById("LoginContent");
	var contentSlideOut = new Animation(content, "top", 0, -(ContentHeight), "px");
		contentSlideOut.onComplete = function(){
			var container = document.getElementById("LoginContainer");
			container.style.visibility = "hidden";	
		}
		contentSlideOut.init();
}

function displayDropDown() {
	var dropDown = document.getElementById("marketSelect");
	if(dropDown != null) {
		dropDown.style.visibility = "visible";
	}
}

function hideDropDown() {
	var dropDown = document.getElementById("marketSelect");
	if(dropDown != null) {
		dropDown.style.visibility = "hidden";
	}
}

function Animation(elmnt, propName, initValue, targetValue, valueExt){
	var ths = this;
	this.elmnt = elmnt;
	this.propName = propName;
	this.initValue = initValue;
	this.targetValue = targetValue;
	this.valueExt = valueExt;
	this.increment = 0.2;

	this.key = null;
	this.initValue = initValue;
	this.currentValue = null;

	//for color animations
	this.red = 0;
	this.green = 0;
	this.blue = 0;

	this.targetRed = 0;
	this.targetGreen = 0;
	this.targetBlue = 0;

	this.init = function(){
		//this.key = elmnt.nodeName+propName+initValue+targetValue+valueExt+(Math.random());
		if(this.elmnt){
            this.key = this.elmnt.id+this.elmnt.className+this.elmnt.nodeName+this.propName+this.initValue+this.targetValue+this.valueExt;
            if(this.currentValue == null){
                this.currentValue = this.elmnt.style[this.propName];
                if(this.currentValue == undefined || this.currentValue == null || this.currentValue == ""){
                    this.currentValue = this.initValue;
                }else{
                    this.currentValue = parseFloat(this.currentValue);
                }
                if(this.currentValue == undefined || this.currentValue == null || this.currentValue == ""){
                    this.currentValue = 0;
                }
            }
            if(this.propName == "borderColor"
                        ||	this.propName == "backgroundColor"
                        ||	this.propName == "color"){
                /*
                this.red = parseInt(this.currentValue.substr(0,2), 16);
                this.green = parseInt(this.currentValue.substr(2,2), 16);
                this.blue = parseInt(this.currentValue.substr(4,2), 16);
                this.targetRed = parseInt(this.targetValue.substr(0,2), 16);
                this.targetGreen = parseInt(this.targetValue.substr(2,2), 16);
                this.targetBlue = parseInt(this.targetValue.substr(4,2), 16);
                this.pushCurrentColor();
                Animator.addEvent(this.onUpdateColor, this.key);
                */
                return false;
            }else{
                this.pushCurrentValue();
                Animator.addEvent(this.onUpdate, this.key);
            }

            Animator.play();
		}
		return true;
	};
	this.pushCurrentValue = function(){
		if(this.propName == "opacity"){
			setOpacity(this.elmnt, this.currentValue);
		}else{
			this.elmnt.style[this.propName] = this.currentValue + ((this.valueExt)?this.valueExt:"");//temp code
		}
	};
	this.pushCurrentColor = function(){
		this.elmnt.style[this.propName] = "rgb(" + this.red + "," +this.green + "," + this.blue + ")";
	};
	this.onUpdateColor = function(){
		//not currently implemented
		ths.finalizeAnimation();
		return;
	};
	this.onUpdate = function(){
		var distance = parseFloat(ths.targetValue - ths.currentValue);
		var nextStride;
		if(distance > 1){
			nextStride = parseFloat(distance)*parseFloat(ths.increment);
				nextStride = Math.ceil(nextStride);
			ths.currentValue = ths.currentValue + nextStride;
			ths.pushCurrentValue();
		}else if(distance < -1){
			nextStride = parseFloat(distance)*parseFloat(ths.increment);
				nextStride = Math.floor(nextStride);
			ths.currentValue = ths.currentValue + nextStride;
			ths.pushCurrentValue();
		}else{
			ths.currentValue = ths.targetValue;
			ths.pushCurrentValue();
			ths.finalizeAnimation();
		}
	};
	this.finalizeAnimation = function(){
		Animator.removeEvent(this.key);
		this.onComplete();
	};
	this.onComplete = function(){
		return;
	};
}

var Animator = {
	TIME: 30, //miliseconds.
	timer: -1,
	queue: new Array(),
	init: function(){
		if(this.timer == -1){
			this.timer = window.setInterval(this.animate, this.TIME);
		}
		this.queue = new Array();
	},
	play: function(){
		if(this.timer == -1){
			this.timer = window.setInterval(this.animate, this.TIME);
		}
	},
	pause: function(){
		if(this.timer != -1){
			window.clearInterval(this.timer);
			this.timer = -1;
		}
	},
	animate: function(){
		if(!Animator.queue){
			return;
		}
		var nullPointers = 0;
		var queueSize = 0;
		var i;
		for(i in Animator.queue){
			queueSize++;
			if(Animator.queue[i] && (typeof Animator.queue[i]).toLowerCase() == "function"){
				try{
				  Animator.queue[i]();
				}catch(excpt){}
			}else{
				nullPointers++;
			}
		}
		if(nullPointers == queueSize){
			Animator.pause();
		}
		return;
	},
	addEvent: function(functionObj, functionId){
		if((this.queue) && (typeof functionObj == "function")){
			this.queue[functionId] = functionObj;
			return true;
		}else{
			return false;
		}
	},
	removeEvent: function(functionId){
		if((this.queue) && (this.queue[functionId])){
			this.queue[functionId] = null;
			return true;
		}else{
			return false;
		}
	}
};

function checkInitAlpha(elmnt){
	if(elmnt.filters["alpha"] == undefined){
		elmnt.style.filter = "Alpha(enabled=1)";
	}
}

function getOpacity(elmnt){
	var opacity = -1;
	if(elmnt.filters){
		checkInitAlpha(elmnt);
		opacity = parseFloat((elmnt.filters["alpha"])?elmnt.filters["alpha"].opacity:100);
	}else if(elmnt.style.opacity != undefined){
		if(elmnt.style.opacity == null || elmnt.style.opacity == ""){
			opacity = 100;
		}else{
			opacity = 100*parseFloat(elmnt.style.opacity);
		}
	}
	return opacity;
}

function setOpacity(elmnt, opac){
	if(elmnt.filters){
		checkInitAlpha(elmnt);
		if(elmnt.filters["alpha"]){
			elmnt.filters["alpha"].opacity = opac;
		}
	}else if(elmnt.style.opacity != undefined){
		elmnt.style.opacity = parseFloat(opac)/100;
	}
	if(opac == 0){
	   elmnt.style.visibility = "hidden";
	}else{
       elmnt.style.visibility = "visible";
	}
}