/*
#############################################
	Client: EMBRAER
	Project: E FOR EFFICIENCY
	AGENCIA GINGA
	HOTWORDS WINDOW & SHADOW
#############################################
*/

var gradientshadow={}
gradientshadow.depth=5 //Depth of shadow in pixels
gradientshadow.containers=[]

gradientshadow.create=function(){
	var a = document.all ? document.all : document.getElementsByTagName('*')
	for (var i = 0;i < a.length;i++) {
		if (a[i].className == "hotWindow") {
			for (var x=0; x<gradientshadow.depth; x++){
				var newSd = document.createElement("DIV")
				newSd.className = "shadow_inner"
				newSd.id="hotWindow"+gradientshadow.containers.length+"_"+x //Each shadow DIV has an id of "shadowL_X" (L=index of target element, X=index of shadow (depth) 
				if (a[i].getAttribute("rel"))
					newSd.style.background = a[i].getAttribute("rel")
				else
					newSd.style.background = "black" //default shadow color if none specified
				document.body.appendChild(newSd)
			}
		gradientshadow.containers[gradientshadow.containers.length]=a[i]
		}
	}
	gradientshadow.position()
	window.onresize=function(){
		gradientshadow.position()
	}
}

gradientshadow.position=function(){
	if (gradientshadow.containers.length>0){
		for (var i=0; i<gradientshadow.containers.length; i++){
			for (var x=0; x<gradientshadow.depth; x++){
			var shadowdiv=document.getElementById("hotWindow"+i+"_"+x)
				shadowdiv.style.width = gradientshadow.containers[i].offsetWidth + "px"
				shadowdiv.style.height = gradientshadow.containers[i].offsetHeight + "px"
				shadowdiv.style.left = gradientshadow.containers[i].offsetLeft + x + "px"
				shadowdiv.style.top = gradientshadow.containers[i].offsetTop + x + "px"
			}
		}
	}
}

if (window.addEventListener)
window.addEventListener("load", gradientshadow.create, false)
else if (window.attachEvent)
window.attachEvent("onload", gradientshadow.create)
else if (document.getElementById)
window.onload=gradientshadow.create

var lastbt;
function showHotWindow(bt, alvo){
	alvo = document.getElementById(alvo);
	alvo.style.display = "";
	if(document.all){
		alvo.offsetWidth > "318" ? alvo.style.width = "318px" : null;
	}
	curtop = findPosY(bt);
	curleft = findPosX(bt);
	windowTop = curtop + (bt.offsetHeight);
	windowLeft = curleft + (bt.offsetWidth / 2) - 30;
	alvo.style.top = windowTop + "px";
	if((windowLeft + alvo.offsetWidth + 5) > document.body.offsetWidth){
		windowLeft = (windowLeft - alvo.offsetWidth) + 60;
		alvo.style.left = windowLeft + "px";
		document.getElementById('hotWinArrow').className = "hotWindowArrowR";
	} else {
		alvo.style.left = windowLeft + "px";
		document.getElementById('hotWinArrow').className = "hotWindowArrowL";
	}
	lastbt = bt.id;
}

function posHotWindow(){
	alvo = document.getElementById('hotWindow');
	if(alvo.style.display != "none"){
		bt = document.getElementById(lastbt);
		nbtleft = findPosX(bt);
		windowLeft = nbtleft + (bt.offsetWidth / 2) - 30;
		if((windowLeft + alvo.offsetWidth + 5) > document.body.offsetWidth){
			windowLeft = (windowLeft - alvo.offsetWidth) + 60;
			alvo.style.left = windowLeft + "px";
			document.getElementById('hotWinArrow').className = "hotWindowArrowR";
		} else {
			alvo.style.left = windowLeft + "px";
			document.getElementById('hotWinArrow').className = "hotWindowArrowL";
		}
	}
}

jQuery.noConflict();
jQuery(document).ready(function($){
	$(".hw_close").click(function (event){
		alvo = this.id.replace(/.{6}$/,"");
		document.getElementById(alvo).style.display = "none";
		gradientshadow.position();
	});
	$(window).bind("resize", function(){
		if(document.getElementById('hotWindow')){posHotWindow();}
	});
	$(".hotWords").click(function (event){
		showHotWindow(this, 'hotWindow')
		gradientshadow.position();
	});
});