////////////////////////////////////////////////////////
// Constants and vars
var IMG_DIR = "img";
var IMG_EXT = "png";
var CN_SYNOPSIS = "arrowsynopsis";
var CN_MAP_TEXT_OBJECTS = "maptextobject";


var gSynopsises = Array();
var gMapTextObjects = Array();
var gMapDefault = 0;

window.onload = Body_OnLoad;
window.document.onclick = Body_OnClick;

var isIE = document.all ? true : false;

////////////////////////////////////////////////////////
// Events handlers
/*
* Body OnLoad handler
*/
function Body_OnLoad()
{
	var oHtmlSynopsises = GetAllElementsByClass(document, CN_SYNOPSIS);
	var length = oHtmlSynopsises.length;
	for (var i = 0; i < length; i++) {
		gSynopsises[i] = new Synopsis(oHtmlSynopsises[i]);
		gSynopsises[i].Close();
	}
	document.getElementById("CloseAllSynopsisLink").style.display = "none";
	if (length == 0) {
		document.getElementById("OpenAllSynopsisLink").style.display = "none";
	}
	OpenSynopsisWithNeedAnchor(document.location.href);
}

/*
* Body OnClick handler
*/
function Body_OnClick(e)
{
	var oSource;
	if (isIE) {
		oSource = window.event.srcElement;
	} else {
		oSource = e.target;
	}
	if (oSource != null && (oSource.tagName.toUpperCase() == "A" || 
		oSource.tagName.toUpperCase() == "AREA")) {
		OpenSynopsisWithNeedAnchor(oSource.href);
	}
}

/*
* Synopsis OnClick handler
*/
function Synopsis_OnClick(oHtmlLink)
{
	var length = gSynopsises.length;
	for (var i = 0; i < length; i++) {
		if (gSynopsises[i].oHtmlLink == oHtmlLink) {
			if (gSynopsises[i].isOpen) {
				gSynopsises[i].Close();
			} else {
				gSynopsises[i].Open();
			}
		}
	}
}

/*
* Close all synopsises link OnClick handler
*/
function CloseAllSynopsisLink_OnClick()
{
	var length = gSynopsises.length;
	for (var i = 0; i < length; i++) {
		gSynopsises[i].Close();
	}
	document.getElementById("OpenAllSynopsisLink").style.display = "inline";
	document.getElementById("CloseAllSynopsisLink").style.display = "none";
}

/*
* Open all synopsises link OnClick handler
*/
function OpenAllSynopsisLink_OnClick()
{
	var length = gSynopsises.length;
	for (var i = 0; i < length; i++) {
		gSynopsises[i].Open();
	}
	document.getElementById("OpenAllSynopsisLink").style.display = "none";	
	document.getElementById("CloseAllSynopsisLink").style.display = "inline";
}




////////////////////////////////////////////////////////
// Synopsis class
/*
* Constructor
* @Param: oHtml - parent html-element
*/
function Synopsis(oHtml)
{
	//Methods
	this.Open = Synopsis_Open;
	this.Close = Synopsis_Close;
	//Fields
	this.oHtml = oHtml;
	this.oHtmlLink = this.oHtml.getElementsByTagName("a")[0];
	this.oHtmlContent = this.oHtml.getElementsByTagName("div")[0];
}

/*
* Open Synopsis
*/
function Synopsis_Open()
{
	this.oHtmlContent.style.display = "block";
	this.oHtmlLink.getElementsByTagName("img")[0].src = IMG_DIR + "/down.png";
	this.isOpen = true;
}

/*
* Close Synopsis
*/
function Synopsis_Close()
{
	this.oHtmlContent.style.display = "none";
	this.oHtmlLink.getElementsByTagName("img")[0].src = IMG_DIR + "/right.png";
	this.isOpen = false;
}

////////////////////////////////////////////////////////
// Common function
/*
* Open synopsis with anchor that specify in url
*/
function OpenSynopsisWithNeedAnchor(url)
{
	var startPos = url.lastIndexOf("#");
	if (startPos == -1) {
		return;
	}
	var anchorName = url.substring(startPos + 1, url.length);
	if (anchorName == "") {
		return;
	}
	var iLength = gSynopsises.length;
	for (i = 0; i < iLength; i++) {
		var tags = gSynopsises[i].oHtmlContent.getElementsByTagName("a");
		var jLength = tags.length;
		for (var j = 0; j < jLength; j++) {
			if (tags[j].name == anchorName) {
				gSynopsises[i].Open();
			}
		}
	}
}


/*
* Return first descendant which has attribute class=className
* @Param: htmlObject oParent
* @Param: string className
* @Return: htmlObgect
*/
function GetElementByClass(oParent, className)
{
	var childNodes = GetAllDescendants(oParent);
	var iLength = childNodes.length;
	for (var i = 0; i < iLength; i++) {
		if (childNodes[i].className == className) {
			return childNodes[i];
		}
	}
	return null;
}


/*
* Return all descendants which have attribute class=className
* @Param: htmlObject oParent
* @Param: string className
* @Return: array
*/
function GetAllElementsByClass(oParent, className)
{
	var result = new Array();
	var childNodes = GetAllDescendants(oParent);
	var iLength = childNodes.length;
	for (var i = 0; i < iLength; i++) {
		if (childNodes[i].className == className) {
			result[result.length] = childNodes[i];
		}
	}
	return result;
}

function GetAllDescendants(oParent, items)
{
	if (isIE) {
		return oParent.all;
	}
	var result;
	if (items == null) {
		result = new Array();
	} else {
		result = items;
	}
	var tmp = oParent.childNodes;
	var iLength = tmp.length;
	for (var i = 0; i < iLength; i++) {
		result.push(tmp[i]);
		result = GetAllDescendants(tmp[i], result);
	}
	return result;
}