/* Funções diversas */
var isIE =(/\bmsie\b/i.test(navigator.userAgent)&& document.all&&!(/\bopera\b/i.test(navigator.userAgent)));
$ = function (a) {
	return document.getElementById(a);
};

// Aplica um valor alpha de 0 a 100
setAlpha = function (e, a) {
	a = Math.round(a);
	if (typeof e == 'string') e = $(e);
	with (e.style) {
		if (isIE) {
			filter = 'alpha(opacity='+a+')';
		} else {
			opacity = a/100;
		}
	}
};
// Aplica a posição x e y a um objeto
setPosition = function (obj, x, y) {
	with (obj.style) {
		top = x+'px';
		left = y+'px';
	}
};
// Retorna a posição de um objeto
getPos = function (e) {
	if (typeof e == 'string') e = $(e);
	var left = 0;
	var top = 0;
	while (e.offsetParent) {
		left += e.offsetLeft;
		top += e.offsetTop;
		e = e.offsetParent;
	}
	left += e.offsetLeft;
	top += e.offsetTop;
	return {x:left, y:top};
};
// Retorna o tamanho de um objeto
getSize = function (e) {
	if (typeof e == 'string') e = $(e);
	return {x:e.offsetWidth, y:e.offsetHeight};
};
// Retorna o tamanho total do documento
getDocSize = function () {
	return {x:document.body.offsetWidth, y:document.body.offsetHeight};
};
// Retorna o tamanho da área visivel
getDocVisibleSize = function () {
	var _x, _y;
	if (window.innerWidth) {
		_x = window.innerWidth;
	} else if (document.documentElement && document.documentElement.clientWidth) {
		_x = document.documentElement.clientWidth;
	} else if (document.body) {
		_x = document.body.clientWidth;
	}
	if (window.innerHeight) {
		_y = window.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		_y = document.documentElement.clientHeight;
	} else if (document.body) {
		_y = document.body.clientHeight;
	}
	return {x:_x, y:_y};
};
// Retorna caracter pressionado em um evento do teclado
getKey = function (e) {
	try {
		return event ? (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode)) : null;
	} catch (f) {
		return e.keyCode;
	}
};
// Retorna o scroll da página
getScroll = function () {
	if (self.pageXOffset) {
		sX = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollLeft) {
		sX = document.documentElement.scrollLeft;
	} else if (document.body) {
		sX = document.body.scrollLeft;
	}
	if (self.pageYOffset) {
		sY = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		sY = document.documentElement.scrollTop;
	} else if (document.body) {
		sY = document.body.scrollTop;
	}
	return {x:sX, y:sY};
};
// Retorna a posição do mouse no documento
getMousePos = function (ev) {
	if (ev.pageX || ev.pageY) return {x:ev.pageX, y:ev.pageY};
	var SC = getScroll();
	return {x:ev.clientX+SC.x-document.body.clientLeft, y:ev.clientY+SC.y-document.body.clientTop};
};
// Mostra um div na tela
mostraDIV = function (e) {
	if (typeof e == 'string') e = $(e);
	e.style.display = 'block';
};
// Oculta um div da tela
ocultaDIV = function (e) {
	if (typeof e == 'string') e = $(e);
	e.style.display = 'none';
};

/* Evento window.onstopscroll */
window.intervalstopscroll = null;
window.onscroll = function() {
	clearTimeout(window.intervalstopscroll);
	window.intervalstopscroll = setTimeout('window.onstopscroll();window.onstopscroll2();', 50);
};
window.onstopscroll2 = function() {
	PALPITE.alinha();
};

/* Fundo escuro do site */
var bgSite = {
	'show': function() {
		setAlpha('bgSite', 50);
		$('bgSite').style.height = getDocSize().y+340+'px';
		$('bgSite').style.display = 'block';
	},
	'hide': function() {
		$('bgSite').style.display = 'none';
	},
	'alinha': function(){
		$('bgSite').style.width = getDocSize().x+'px';
		$('bgSite').style.height = getDocSize().y+340+'px';
	}
};


/* Tween */
tween = function () {
	var obj = $(arguments[0]);
	obj.numStepTween = (isIE) ? 10 : 10;
	obj.thisIntervalTween = null;
	obj.posXTween = 100;
	obj.posYTween = 100;
	obj.onEnterFrameTween = function() {
		obj.numTimeTween += obj.numStepTween;
		var posX = findTweenValue(obj.numInicioXTween, obj.numFinalXTween, 0, obj.numTimeTween, obj.numDurationTween, obj.strEasingTween);
		var posY = findTweenValue(obj.numInicioYTween, obj.numFinalYTween, 0, obj.numTimeTween, obj.numDurationTween, obj.strEasingTween);
		setPosition(obj, posX, posY);
		if (obj.numTimeTween>=obj.numDurationTween) {
			setPosition(obj, obj.numFinalXTween, obj.numFinalYTween);
			clearInterval(obj.thisIntervalTween);
			obj.callbackTween();
		}
	};
	obj.tweenTo = function() {
		var pos = getPos(obj);
		this.numTimeTween = 0;
		this.numInicioXTween = pos.y;
		this.numInicioYTween = pos.x;
		this.numFinalXTween = arguments[0];
		this.numFinalYTween = arguments[1];
		this.strEasingTween = (arguments[2]) ? arguments[2] : 'linear';
		this.numDurationTween = (arguments[3]) ? arguments[3]*1000 : 1000;
		this.callbackTween = arguments[4] || function () {
		};
		clearInterval(obj.thisIntervalTween);
		obj.thisIntervalTween = setInterval(obj.onEnterFrameTween, obj.numStepTween);
	};
	obj.stopTween = function() {
		clearInterval(obj.thisIntervalTween);
	};
};
/* Shake */
shake = function () {
	var obj = $(arguments[0]);
	var dist = 4;
	var moving = false;
	obj.numStepShake = 25;
	obj.thisIntervalShake = null;
	obj.onEnterFrameShake = function() {
		obj.numTimeShake += obj.numStepShake;
		var posX = Math.round((Math.random()*dist*2-dist)+obj.numInicioXShake);
		var posY = Math.round((Math.random()*dist*2-dist)+obj.numInicioYShake);
		setPosition(obj, posX, posY);
		if (obj.numTimeShake>=obj.numDurationShake) {
			setPosition(obj, obj.numInicioXShake, obj.numInicioYShake);
			clearInterval(obj.thisIntervalShake);
			obj.callbackShake();
			moving = false;
		}
	};
	obj.shakeNow = function() {
		if (moving == true) {
			obj.stopShake();
		}
		var pos = getPos(obj);
		this.numTimeShake = 0;
		this.numInicioXShake = pos.y;
		this.numInicioYShake = pos.x;
		this.numDurationShake = (arguments[0]) ? arguments[0]*1000 : 1000;
		this.callbackShake = arguments[1] || function () {
		};
		obj.thisIntervalShake = setInterval(obj.onEnterFrameShake, obj.numStepShake);
		moving = true;
	};
	obj.stopShake = function() {
		clearInterval(obj.thisIntervalShake);
		setPosition(obj, obj.numInicioXShake, obj.numInicioYShake);
	};
};
/* Alpha */
alpha = function () {
	var obj = $(arguments[0]);
	obj.numStepAlpha = (isIE) ? 45 : 40;
	obj.thisIntervalAlpha = null;
	obj.onEnterFrameAlpha = function() {
		obj.numTimeAlpha += obj.numStepAlpha;
		var newAlpha = findTweenValue(obj.numInicioAlpha, obj.numFinalAlpha, 0, obj.numTimeAlpha, obj.numDurationAlpha, obj.strEasingAlpha);
		setAlpha(obj, newAlpha);
		if (obj.numTimeAlpha>=obj.numDurationAlpha) {
			setAlpha(obj, obj.numFinalAlpha);
			clearInterval(obj.thisIntervalAlpha);
			obj.callbackAlpha();
		}
	};
	obj.alphaTo = function() {
		this.numTimeAlpha = 0;
		this.numInicioAlpha = arguments[0];
		this.numFinalAlpha = arguments[1];
		this.strEasingAlpha = (arguments[2]) ? arguments[2] : 'linear';
		this.numDurationAlpha = (arguments[3]) ? arguments[3]*1000 : 1000;
		this.callbackAlpha = arguments[4] || function () {
		};
		clearInterval(obj.thisIntervalAlpha);
		obj.thisIntervalAlpha = setInterval(obj.onEnterFrameAlpha, obj.numStepAlpha);
	};
	obj.stopAlpha = function() {
		clearInterval(obj.thisIntervalAlpha);
	};
};
// Controla a posição do scroll
var SC = {
	'interval': null,
	'initPos': 0,
	'finalPos': 0,
	'timeNow': 0,
	'timeTotal': 0,
	'effect': '',
	'step': 20,
	'fn': 20,
	'scroll': function(pos,effect,time,fn) {
		var sc = getScroll();
		SC.fn = fn || function(){};
		SC.timeTotal = (time)?time*1000:1000;
		SC.timeNow = 0;
		SC.initPos = sc.y;
		SC.finalPos = pos;
		SC.effect = effect;
		clearInterval(SC.interval);
		SC.interval = setInterval(SC.enterFrame,SC.step);
	},
	'enterFrame': function() {
		var sc = findTweenValue(SC.initPos, SC.finalPos, 0, SC.timeNow, SC.timeTotal, SC.effect);
		window.scrollTo(0,sc);
		SC.timeNow += SC.step;
		if (SC.timeNow >= SC.timeTotal) {
			clearInterval(SC.interval);
			SC.fn();
		}
	}
};

/* Função de easing */
findTweenValue = function (PS, PD, TS, TN, TD, AT, E1, E2) {
	var t = TN-TS, b = PS, c = PD-PS, d = TD-TS, a = E1, p = E2, s = E1;
	switch (AT.toLowerCase()) {
	case "linear" :
		return c*t/d+b;
	case "easeinexpo" :
		return (t == 0) ? b : c*Math.pow(2, 10*(t/d-1))+b;
	case "easeoutexpo" :
		return (t == d) ? b+c : c*(-Math.pow(2, -10*t/d)+1)+b;
	case "easeoutelastic" :
		if (t == 0) {
			return b;
		}
		if ((t /= d) == 1) {
			return b+c;
		}
		if (!p) {
			p = d*.3;
		}
		if (!a || a<Math.abs(c)) {
			a = c;
			var s = p/4;
		} else {
			var s = p/(2*Math.PI)*Math.asin(c/a);
		}
		return (a*Math.pow(2, -10*t)*Math.sin((t*d-s)*(2*Math.PI)/p)+c+b);
	}
};


stageTest = function(o) {
	var DSC = getScroll().y;
	var DVS = getDocVisibleSize().y;
	var OP = getPos(o).y;
	var OS = getSize(o).y;
	return (DSC < OP && OP+OS < DVS+DSC);
};

focusTo = function(o) {
	try {
		$(o).focus();
		$(o).select();
	} catch(e) {}
};


var PALPITE = {
	'tweencreate': 0,
	'aberto':0,
	'mostrar': function() {
			$('bts_palpite').style.display="inline";
	},
	'finalizar': function() {
			$('bts_palpite').style.display="none";
	},
	'show': function(id) {
		$('addpalpite').style.display = "block";
		document.form_busca.area.style.visibility = "hidden";
		
		this.mostrar();
		
		if(this.tweencreate == 0){
			new tween('addpalpite');
			this.tweencreate = 1;
		}
		this.aberto = 1;
		
		this.alinha();
		bgSite.show();	
	},
	'alinha': function(){
	
		if(this.aberto == 0)return;
	
		var SC = getScroll();
		var VS = getDocVisibleSize();
		var SZ = getSize('addpalpite');
		
		newX = (VS.x/2)-235;
		newY = (VS.y/2)-185;		

		$('addpalpite').tweenTo((newY+SC.y), (newX+SC.x), 'easeoutexpo', 0.3);		
	},
	'hide': function(){
		document.form_busca.area.style.visibility = "visible";
		this.aberto = 0;
		var SC = getScroll();
		var VS = getDocVisibleSize();
		var SZ = getSize('addpalpite');	
		
		newX = (VS.x/2)-235;
		newY = (VS.y-SZ.y)/2;
		
		$('addpalpite').tweenTo(-500, newX, 'easeoutexpo', 1,function(){	
			$('addpalpite').style.display = "none";	
		});
		bgSite.hide();
	},
	'redirect':function(){
		
		var VS = getDocVisibleSize();
		newX = (VS.x/2)-235;
		
		$('addpalpite').tweenTo(-500, newX, 'easeoutexpo', 1,function(){	
			$('addpalpite').style.display = "none";	
		});
	}
};


var browserEm = {
	'aberto':0,
	'btOK': '',
	'show': function(url) {
		$('browserEm').style.display = "block";
		this.aberto = 1;
		this.alinha();
		bgSite.show();
		$('browserEm_iframe').src = url;
	},
	'alinha': function() {
		if(this.aberto == 0)return;
		var SC = getScroll();
		var VS = getDocVisibleSize();
		var SZ = getSize('browserEm');
		newX = (VS.x-SZ.x)/2;
		newY = (VS.y-SZ.y)/2;
		$('browserEm').style.top = (newY+SC.y)+'px';
		$('browserEm').style.left = (newX+SC.x)+'px';		
	},
	'hide': function() {
		var SC = getScroll();
		var VS = getDocVisibleSize();
		var SZ = getSize('browserEm');
		newX = (VS.x-SZ.x)/2;
		newY = (VS.y-SZ.y)/2;
		this.aberto = 0;	
		$('browserEm').style.display = "none";
		bgSite.hide();	
	}
};

/* Eventos da página */
window.onstopscroll = function() {
	PALPITE.alinha();
};

window.onresize = function() {
	PALPITE.alinha();
};