var glossaryWords = new Array();
var softPopupId = "softPopup";
var softPopupClassName = "glossaryPopup";
var display = "rightOfLink";
var idAttribute = "rel";
var splitElement = "";
var splittedTitle = "";
var glossaryId = "";
var activeEle = "";
var mySoftPopup;

function addGlossaryWord(linkID, glossaryId, glossaryTitle, glossaryDescription) {
	glossaryWords[glossaryId] = new Array();
	glossaryWords[glossaryId]["title"] = glossaryTitle;
	glossaryWords[glossaryId]["description"] = glossaryDescription;
}

function openGlossary(event) {
	$ = jQuery;
	if(activeEle!=event.target){
		closeSoftPopup();
		activeEle = event.target;
		event.stopImmediatePropagation();
		glossaryId = $(activeEle).attr(idAttribute);
		if(splitElement!="") {
			var split = glossaryId.split(splitElement);
			glossaryId = split[(split.length-1)];
			split.pop();
			splittedTitle = split.join(splitElement);
			$(activeEle).attr(idAttribute,splittedTitle);
		}
		openSoftPopup(event,glossaryWords[glossaryId]["title"], glossaryWords[glossaryId]["description"],$);
	}
	else{
		closeSoftPopup();
	}
	return false;
}

function openSoftPopup(event, glossaryTitle, glossaryDescription) {
	mySoftPopup = $("#"+softPopupId);
	if (mySoftPopup.length==0) {
		$("body").append("<div id=\""+softPopupId+"\" style=\"display:none;\"></div>");
		mySoftPopup = $("#"+softPopupId);
	}
	
	if(splitElement!="") {
		$(activeEle).attr(idAttribute,splittedTitle);
	}
	if(glossaryTitle==''){
		mySoftPopup.html("<div class=\"borderTop\"></div><div class=\"glossaryPopupInner\"><div class=\"content\">"+glossaryDescription+"</div></div><div class=\"borderBottom\"></div>");
	} else {
		mySoftPopup.html("<div class=\"borderTop\"></div><div class=\"glossaryPopupInner\"><div class=\"head\"><h1>" + glossaryTitle + "</h1><a href=\"javascript:closeSoftPopup();\" class=\"close\"></a></div><div class=\"content\">"+glossaryDescription+"</div></div><div class=\"borderBottom\"></div>");
	}

	if(navigator.appVersion.indexOf("MSIE 6")>-1){
		mySoftPopup.bgiframe();
	}
	mySoftPopup.addClass(softPopupClassName);
	mySoftPopup.css("position","absolute");
	
	var outerHeightA = $(activeEle).outerHeight();
	var outerWidthA = $(activeEle).outerWidth();
	var posX = $(activeEle).offset().left;
	var posY = $(activeEle).offset().top;
	var mouseX = $(event).pageX;
	var mouseY = $(event).pageY;
	var outerHeightP = mySoftPopup.outerHeight();
	var outerWidthP = mySoftPopup.outerWidth();
	
	if(display == "topCenter"){
		mySoftPopup.css("left",(posX-outerWidthP/2+outerWidthA/2)+"px");
		mySoftPopup.css("top",(posY-outerHeightP)+"px");
	}
	else if (display == "rightOfLink"){
		mySoftPopup.css("left",(posX+outerWidthA)+"px");
		mySoftPopup.css("top",(posY+outerHeightA-outerHeightP)+"px");
	}
	
	mySoftPopup.fadeIn(100);//css("display","block");
	mySoftPopup.bind("click",closeSoftPopup);
	$(document).bind("click",closeSoftPopup);
}

function closeSoftPopup() {
	$ = jQuery;
	if ($("#"+softPopupId).length>0) {
		$("#"+softPopupId).fadeOut(100);
		$("#"+softPopupId).html("");
		$("#"+softPopupId).removeClass(softPopupClassName);
		if(splitElement!="") {
			$(activeEle).attr(idAttribute,splittedTitle + splitElement + glossaryId);
		}
		mySoftPopup.unbind("click",closeSoftPopup);
		$(document).unbind("click",closeSoftPopup);
		activeEle = "";
	}
}
