var directoryVisible = true;
function toggleDirectory()
{
	var explorerText = document.getElementById('explorerText');
	if (explorerText)
	{	
		if (directoryVisible)
		{
			explorerText.innerHTML = 'Show explorer';
			var directory = document.getElementById('directory');
			var source = document.getElementById('source');			
			source.style.left = 0;
			directory.style.display = 'none';
			directoryVisible = false;
		} else
		{
			explorerText.innerHTML = 'Hide explorer';
			var directory = document.getElementById('directory');
			var source = document.getElementById('source');
			source.style.left = '20%';
			directory.style.display = 'block';
			directoryVisible = true;
		}
		layoutDivsIE();
		//xmlhttpPost('/preferences.do?directoryVisible=' + directoryVisible,ignoreCallback);
	}
}

var outlineVisible = true;
function toggleOutline()
{
	var explorerText = document.getElementById('outlineText');
	if (explorerText)
	{	
		if (outlineVisible)
		{
			explorerText.innerHTML = 'Show outline';
			var outline = document.getElementById('outline');
			outline.style.display = 'none';
			var source = document.getElementById('source');
			source.style.right = 0;
			outlineVisible = false;
		} else
		{
			explorerText.innerHTML = 'Hide outline';
			var outline = document.getElementById('outline');
			var source = document.getElementById('source');
			source.style.right = '20%';
			outline.style.display = 'block';
			outlineVisible = true;
		}
		layoutDivsIE();
		//xmlhttpPost('/preferences.do?outlineVisible=' + outlineVisible,ignoreCallback);
	}
}

function addKnownSuperClassSection(name)
{
	addSectionHeader(name,name + '-sc','Known subtypes (max 10 will be shown)');
}

function addSectionHeader(name,id,label)
{
	var enclosingSpam = document.getElementById(name);
	if (enclosingSpam)
	{
		enclosingSpam.className = 'javadoc';
		parentDiv = document.createElement("DIV");
		parentDiv.id = id;
		sectionHeading = document.createElement("SPAN");
		sectionHeading.innerHTML = label;
		sectionHeading.className = 'jdst';
		parentDiv.appendChild(sectionHeading);
		enclosingSpam.appendChild(parentDiv);
	}
}

function addKnownImplementationSection(name)
{
	addSectionHeader(name,name + '-ic','Known implementations (max 10 will be shown)');
}

function addNothingKnown(name)
{
	addSectionHeader(name,name + '-nc','No known subtypes');
}

function addKnownSuperClass(parentName,name,href)
{
	addSection(parentName + '-sc',name,href);
}

function addSection(parentId,name,href)
{
	var enclosingElement = document.getElementById(parentId);
	if (enclosingElement)
	{
		sectionMember = document.createElement("DIV");
		sectionMember.className = 'sectionMember';
		sectionMember.innerHTML = '<a href="' + href + '#' + name.replace( new RegExp( "\\$", "g" ), "." ) + '">'+name+'</a>';
		enclosingElement.appendChild(sectionMember);
	}
}

function addKnownImplementationClass(parentName,name,href)
{
	addSection(parentName + '-ic',name,href);
}

function ignoreCallback(a){}

function xmlhttpPost(strURL,callback) {
    var xmlHttpReq = false;
    var self = this;
	self.cb = callback;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('GET', strURL, true);
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            self.cb(self.xmlHttpReq);
        }
    }
    self.xmlHttpReq.send(strURL);
}

var hrefsMap = new Array();

function elementObject(element,anchor)
{
	this.element = element;
	this.anchor = anchor;
	this.originalBackGround = null;
}

function registerHrefHighlightHandlers()
{
	var source = document.getElementById('source');
	if (source)
	{
		var hrefs = source.getElementsByTagName('a');
		for (var i=0;i<hrefs.length;i++)
		{
			var key = getKey(hrefs[i]);
			if (key)
			{
				var hrefList = hrefsMap[key];
				if (!hrefList)
				{
					hrefList = new Array();
					hrefsMap[key] = hrefList;
				}
				hrefList[hrefList.length] = new elementObject(hrefs[i],hrefs[i].name != null && hrefs[i].name.length > 0);
				addEvent(hrefs[i],'mouseover',highlightHrefs);
				addEvent(hrefs[i],'mouseout',unhighlightHrefs);
			}
		}
	}
	setInterval('highlighHashElement()',200); 
}


function layoutDivsIE()
{
  if( -1 != navigator.userAgent.
      indexOf ("MSIE") )
  {
	  var headerHight = 47;
	  var width = document.body.offsetWidth;
	  var height = document.body.offsetHeight - headerHight;
	  var currentLeftOffset = 0;
	  var directory = document.getElementById('directory');
	  if (directory && directory.style.display != 'none')
	  {
		 directory.style.position = 'absolute';
		 directory.style.height = height;
		 directory.style.left = 0;
		 directory.style.top = headerHight;
		 directory.style.bottom = 0;
		 directory.style.width = width / 5;
		 currentLeftOffset = width / 5;
	  }
	  var currentRightOffset = 0;
	  var outline = document.getElementById('outline');
	  if (outline && outline.style.display != 'none')
	  {
		 outline.style.position = 'absolute';
		 outline.style.height = height;
		 outline.style.left = (width / 5) * 4;
		 outline.style.top = headerHight;
		 outline.style.bottom = 0;
		 outline.style.width = width / 5;
		 currentRightOffset = width / 5;
	  }
	  var source = document.getElementById('source');
	  if (source && source.style.display != 'none')
	  {
		 source.style.position = 'absolute';
		 source.style.height = height;
		 source.style.left = currentLeftOffset;
		 source.style.top = headerHight;
		 source.style.bottom = 0;
		 source.style.width = width - currentLeftOffset - currentRightOffset;
	  }
	  var bottomGoogleLinks = document.getElementById('bottomGoogleLinks');
	  if (bottomGoogleLinks)
	  {
		bottomGoogleLinks.style.position = 'absolute';  
	  }
	  var rightBar = document.getElementById('rightBar');
	  if (rightBar && rightBar.style.display != 'none')
	  {
		 rightBar.style.position = 'absolute';
		 rightBar.style.height = height;
		 rightBar.style.left = width - 165;
		 rightBar.style.top = headerHight + 5;
		 rightBar.style.bottom = 0;
		 rightBar.style.width = 160;
		 currentRightOffset = 170;
	  }
	  var textContent = document.getElementById('textContent');
	  if (textContent)
	  {
		textContent.style.position = 'absolute';
		textContent.style.height = height;
		textContent.style.left = currentLeftOffset;
		textContent.style.top = headerHight;
		textContent.style.bottom = 0;
		textContent.style.width = width - currentLeftOffset - currentRightOffset;
	  }
	  var googleResultContent = document.getElementById('googleResultContent');
	  if (googleResultContent)
	  {
		googleResultContent.style.position = 'absolute';
		googleResultContent.style.height = height;
		googleResultContent.style.left = currentLeftOffset;
		googleResultContent.style.top = headerHight;
		googleResultContent.style.bottom = 0;
		googleResultContent.style.width = width - currentLeftOffset - currentRightOffset;
	  }	  
	  document.body.scrollTop = 0;
	}
}

function addResizeEvent()
{
  if( -1 != navigator.userAgent.
      indexOf ("MSIE") )
  {
	addEvent(window,'resize',onResize);
  }  
}

function onResize(e)
{
	layoutDivsIE();
}
	
var documentPos = null;

function highlighHashElement()
{
	if (document.location.hash.length > 0)
	{
		var currentDocumenPos = document.location.hash.substr(1,document.location.hash.length);
		if (currentDocumenPos != documentPos)
		{
			if (documentPos)
			{
				highlightKey(documentPos, '#FFFFFF','#FFFFFF');
			}
			highlightKey(currentDocumenPos, '#a6e6ff','#cee3ff');
			documentPos = currentDocumenPos;
		}
	}
}

function getKey(aElement)
{
	var href = aElement.getAttribute('href',2);
	var name = aElement.name;
	var key = name;
	if (!name && href.match(/^#.*$/))
	{
		key = href.substring(1);
	}
	return key;
}


function highlightHrefs(e)
{
	var target = getEventTarget(e);
	var key = getKey(target);
	highlightKey(key,'#aaaaaa','#dddddd');
}

function highlightKey(key, declarationStyle,usageStyle)
{
	if (key)
	{
		for (var i=0;i<hrefsMap[key].length;i++)
		{
			hrefsMap[key][i].originalBackGround = hrefsMap[key][i].element.style.backgroundColor;
			if (hrefsMap[key][i].anchor)
				hrefsMap[key][i].element.style.backgroundColor = declarationStyle;
			else
				hrefsMap[key][i].element.style.backgroundColor = usageStyle;
		}
	}
}

function unhighlightHrefs(e)
{
	var target = getEventTarget(e);
	var key = getKey(target);
	for (var i=0;i<hrefsMap[key].length;i++)
	{
		if (hrefsMap[key][i].originalBackGround)
		
			hrefsMap[key][i].element.style.backgroundColor = hrefsMap[key][i].originalBackGround;
		else
			hrefsMap[key][i].element.style.backgroundColor = '#FFFFFF';
	}
}

function getEvent(e)
{
  if(!e) return window.event;
  return e;
}
function getEventTarget(e)
{
  e = getEvent(e);
  if(e.target)
	return e.target;
  return e.srcElement;
}

function addEvent(element, eventName, callback)
{
  if(element == null) return;
  if(element.addEventListener)
	element.addEventListener(eventName, callback, false);
  else if(element.attachEvent)
	element.attachEvent("on" + eventName, callback);
}

var homeOptions = {
	script: "/findClass.do?limit=15&",
	varname: "prefix",
	shownoresults:true,		
	json: false,
	maxresults: 15,		
	callback: setValidClassHome	
};
var maoptions = {
	script: "/findMavenArtifact.do?limit=15&",
	varname: "prefix",
	shownoresults:false,		
	json: false,
	maxresults: 15,		
	callback: setValidMavenArtifact	
};

var options = {
	script: "/findClass.do?limit=15&",
	varname: "prefix",
	shownoresults:false,		
	json: false,
	maxresults: 15,		
	callback: setValidClass	
};
var homeMaoptions = {
	script: "/findMavenArtifact.do?limit=15&",
	varname: "prefix",
	shownoresults:true,		
	json: false,
	maxresults: 15,		
	callback: setValidHomeMavenArtifact	
};

function setValidClassHome(selectedObject, inputDom)
{
	document.getElementById('homeFindClassHref').value = selectedObject.id;
	gotoClass('homeFindClassHref');
}


function setValidHomeMavenArtifact(selectedObject, inputDom)
{
	document.getElementById('homeFindMavenArtifactHref').value = selectedObject.id;
	gotoMavenArtifact('homeFindMavenArtifactHref');
}
function setValidMavenArtifact(selectedObject, inputDom)
{
	document.getElementById('findMavenArtifactHref').value = selectedObject.id;
	gotoMavenArtifact('findMavenArtifactHref');
}
function gotoMavenArtifact(fieldId)
{
	var findHref = document.getElementById(fieldId);
	if (findHref.value == 'NOT_SET')
		alert('Please type first letters of the Maven artifact and than select it from presented options');
	else
		window.location = findHref.value;
}
function setValidClass(selectedObject, inputDom)
{
	document.getElementById('findClassHref').value = selectedObject.id;
	gotoClass('findClassHref');
}
function gotoClass(fieldId)
{
	var findClassHref = document.getElementById(fieldId);
	if (findClassHref.value == 'NOT_SET')
		alert('Please type first letters of the class and than select it from presented options');
	else
		window.location = findClassHref.value;
}
function isdefined( variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}

