function eventBufferObject() {
	this.buffer = [];
	this.add = function(func) {
		this.buffer[this.buffer.length] = func;
	}
	this.load = function() {
		for (var x = 0; x < this.buffer.length; x++) {
			if (typeof(this.buffer[x]) == "string") {
				eval(this.buffer[x])
			} else {
				this.buffer[x]();
			}
		}
	}
}

// Initializes the Event On Load Buffer
var loadBuffer = new eventBufferObject();

// defines basic variables and setup
common_js = function() {
	// common buffer
	this.commonGen = document.getElementById("commonGen");
	// container for common inpage elements
	this.commonContainer = document.getElementById("commonContainer");
	this.symbolEnter = document.getElementById("symbolEnter");
	this.initLookup = '';
	this.menustate = 'off';
}

// Selects and Focus the Symbol Entry Value on Click
common_js.prototype.checkSymbolEntry = function() {
	if (this.symbolEnter.value) {
		this.symbolEnter.select();
		this.symbolEnter.focus();
	}	
}

common_js.prototype.changeParent = function(url){	
	if(url){	
			try {
			window.opener.location.href = url;
			window.opener.focus();
			}
			catch(e){
			window.location.href = url;
			}
		}		
}

// couple chart items
charts_js = function() {
	this.chart = document.getElementById("chart");
	this.srcChart = "";	
}

charts_js.prototype.getSnapChart = function(duration,symbol,companyname,width,volume,ytd,iType) {

	if (iType == 'OF' || iType == 'MF') {
		document.getElementById("cht_90").className = "chtOff";
		document.getElementById("cht_180").className = "chtOff";
		document.getElementById("cht_365").className = "chtOff";
		document.getElementById("cht_1095").className = "chtOff";
	} else if (iType == 'ETF') {
		document.getElementById("cht_1").className = "chtOff";
		document.getElementById("cht_5").className = "chtOff";
		document.getElementById("cht_30").className = "chtOff";
		document.getElementById("cht_"+ytd).className = "chtOff";
		document.getElementById("cht_365").className = "chtOff";
		document.getElementById("cht_1095").className = "chtOff";
	} else {
		document.getElementById("cht_1").className = "chtOff";
		document.getElementById("cht_5").className = "chtOff";
		document.getElementById("cht_30").className = "chtOff";
		document.getElementById("cht_"+ytd).className = "chtOff";
		document.getElementById("cht_365").className = "chtOff";
		document.getElementById("cht_1095").className = "chtOff";
	}
	
	document.getElementById("cht_"+duration).className = "chtOn";
	this.srcChart = '../../common/charts/chartEngine.asp?symbol='+symbol+'&companyname='+companyname+'&duration='+duration+'&width='+width+'&volume='+volume;
	try {commonGen.document.location.replace(this.srcChart);} catch(e) {}
}


// Load Chart
charts_js.prototype.loadCht = function(who,symbol,type) {
	this.eleOffset = who;
	this.symbolCht = symbol;
	this.chttype = type;
	this.chtID = window.setTimeout("charts.loadChtTime();",600);
}

charts_js.prototype.loadChtTime = function() {
	if (this.symbolCht) {
	var x = 0;var y = 0;
	while (this.eleOffset.offsetParent != null) {
		x += this.eleOffset.offsetLeft;
		y += this.eleOffset.offsetTop;
		this.eleOffset = this.eleOffset.offsetParent;
	}

	x += this.eleOffset.offsetLeft + 50;
	y += this.eleOffset.offsetTop;
	
	if (x > 500) { x -= 275; }
		
	var isharesLyr = document.getElementById("isharesData");
	if (isharesLyr && y > 620) { y = 500 }		
	
	if(document.body.clientHeight) { winHeight = document.body.clientHeight }
	if(window.outerHeight) { winHeight = window.outerHeight }	

	var vDistance = winHeight - y;	
	if (vDistance < 200 && document.body.clientHeight) { y -= 150; }
	if (vDistance < 370 && window.outerHeight) { y -= 200; }
	
	
	common.commonContainer.style.display = 'block';
	common.commonContainer.style.top = y+'px';
	common.commonContainer.style.left = x+'px';
	var symHL = document.getElementById(this.symbolCht);
	if (symHL) {
		symHL.style.display = 'block';
	} else {
		commonGen.document.location.replace('../../common/charts/popupChart.asp?symbol='+this.symbolCht+'&type='+this.chttype);
	}
	}
}

// close Chart
charts_js.prototype.closeCht = function(symbol) {
	window.clearTimeout(this.chtID);
	var t_id = common.commonContainer.getElementsByTagName("div");
	for (var c = 0;c<t_id.length;c++) {
		document.getElementById(t_id[c].id+'Load').style.display = 'none';
		document.getElementById(t_id[c].id).style.display = 'none';
	}
}

common_js.prototype.switchMenu = function(ele,what) {
		var eleDisplay 	= document.getElementById('navmenupad');
		var selectors 	= document.getElementsByTagName('SELECT');
						
		if (what && this.menustate == 'off') {
			var x = 0;var y = 0;			
			for (var i=0; i<selectors.length; i++){
				selectors[i].style.visibility = 'hidden';
			}			
			while (ele.offsetParent != null) {
				x += ele.offsetLeft - 190;
				y += ele.offsetTop + 18;
				ele = ele.offsetParent;
			}			
			eleDisplay.style.display = 'block';
			eleDisplay.style.left = x+'px';
			eleDisplay.style.top = y+'px';		
			this.menustate = 'on';			
		} 
		else {
			for (var i=0; i<selectors.length; i++){
				selectors[i].style.visibility = 'visible';
			}
			eleDisplay.style.display = 'none';
			this.menustate = 'off';
		}		
	}
	
common_js.prototype.containsDOM = function(container, containee) {
  var isParent = false;
  do {
    if ((isParent = container == containee))
      break;
    containee = containee.parentNode;
  }
  while (containee != null);
  return isParent;
}
	
common_js.prototype.checkMouseOut = function(element, evt) {
  if (element.contains && evt.toElement) {
	return !element.contains(evt.toElement);
  }
  else if (evt.relatedTarget) {
   	return !this.containsDOM(element, evt.relatedTarget);
  }
}

common_js.prototype.hideMenu = function() {
  this.timeout = setTimeout("common.switchMenu('lnk_prefs',0)", 450);	
}

common_js.prototype.layerOff = function() {
	var navmenu = document.getElementById('navmenupad');
	navmenu.style.display = 'none';
}

// Adds the common js object
loadBuffer.add("common = new common_js()");

// adds chart js object
loadBuffer.add("charts = new charts_js()");

// Initializes all onload event
onload = function() {
	loadBuffer.load();
	document.title='iShares: Quotes & Research';		
}
