/**
 * @author Greg Robbins
 */

function setupAnchors()
{
	
	
	trajUlObj = document.getElementById("trajectoria");
	anchorsArr = trajUlObj.getElementsByTagName("A");
	//alert(anchorsArr);
	for(i = 0; i < anchorsArr.length; i++)
	{
		addEvent(anchorsArr[i], "click", clickArtist, false);
		//addEvent(anchorsArr[i], "mouseover", clickArtist, false);
		//addEvent(anchorsArr[i], "mouseout", popOut, false);
		//alert("Setting attNode to " + i);
		att = document.createAttribute("isActivePopup");
		att.nodeValue = 0;
		anchorsArr[i].setAttributeNode(att);
	}
}

function clickArtist(e)
{
	target = fetchTarget(e);
	idArtist = target.id.match(/idArtist_([0-9]+)/)[1];
	//target.setAttribute("isActivePopup", 1);
	//alert("idPerformance: " + idPerformance);
	ajaxObj.call("action=ajax_luzDeGasTrajectoria&idArtist=" + idArtist + "&lang=" + pageLang, clickArtistResp);
}

function clickArtistResp(resp)
{
	if(!resp) return;
	
	title = resp["title"];
	idArtist = resp["idArtist"];
	dateString = resp["dateString"];
	mediaFiles = resp["mediaFiles"];
	
	//get position of target
	target = document.getElementById("idArtist_" + idArtist);
	posArr = findPos(target);
	//alert(posArr);
	targetTop = posArr[1] - 134;
	//targetLeft = posArr[0] + target.offsetWidth;
	targetLeft = posArr[0];
	//alert(targetTop + ", " + targetLeft);
	
	//stop here if the popup is already active
	if(target.getAttribute("isActivePopup") == 1) return;
	
	//create popup
	popupDivObj = document.createElement("DIV");
	popupDivObj.id = "popup_" + idArtist;
	popupDivObj.className = "trajPopup";
	popupDivObj.style.top = targetTop + "px";
	popupDivObj.style.left = targetLeft + "px";
	addEvent(popupDivObj, "click", popOut, false);
	
	//create popup content container
	pdContainer = document.createElement("DIV");
	pdContainer.className = "trajPopupContentContainer";
	
	
	//add content
	if(mediaFiles.length > 0)
	{
		//just 1 image for now
		imageURL = mediaFiles[0]["filename"];
		//create element
		imgObj = document.createElement("IMG");
		imgObj.src = "img_thumbs/" + imageURL;
		imgObj.className = "trajPopupImg";
		pdContainer.appendChild(imgObj);
	}
	
	titleObj = document.createElement("STRONG");
	titleObj.innerHTML = title;
	
	pdContainer.appendChild(titleObj);
	if (!dateString.match(/^0/))
	{
		pdContainer.innerHTML += "<br />" + dateString;
	}
	popupDivObj.appendChild(pdContainer);
	document.body.appendChild(popupDivObj);
	
	//this window is now open
	target.setAttribute("isActivePopup", 1);
}

function popOut(e)
{
	target = fetchTarget(e);
	
	while(!target.id.match(/popup_([0-9]+)/))
	{
		target = target.parentNode;
	}
	
	idArtist = target.id.match(/popup_([0-9]+)/)[1];
	textLink = document.getElementById("idArtist_" + idArtist);
	
	document.body.removeChild(target);
	
	textLink.setAttribute("isActivePopup", 0);
}

function rollout_popOut(e)
{
	target = fetchTarget(e);
	target.setAttribute("isActivePopup", 0);
	
	//Get idPerformance
	idPerformance = target.id.match(/idPerf_([0-9]+)/)[1];
	popupObj = document.getElementById("popup_" + idPerformance);
	if(popupObj) document.body.removeChild(popupObj);
}

function addListeners(e)
{
	if (!document.getElementsByTagName) return;
	setupAnchors();
}

addEvent(window, 'load', addListeners, false);