function Funds_class() {

	this.definitionPopUp = null;
}

Funds_class.prototype.init = function() {
	
	Events.delegate(document.body, "click", {
		 selector:"a.prospectus"
		,handler:this.openProspectusLink
		,context:this	
	});
	
	var ETFHubLink = Element.parseSelector("a.hublink", document.body);
	
	if(ETFHubLink.length) {
		
		Events.add({
			 element:ETFHubLink
			,type:"click"
			,handler:this.openHUB
			,context:this	
		});	
	}
}

Funds_class.prototype.openPrintWindow = function(url) {
	if(browserProps.isIE) { /* 680 and 800 was scrunching the page too much and cutting of the chart. */
		 var w = 900;
	 } else {
		 var w = 900;
	 }
	window.open(url,'print','width='+w+',resize=no,scrollbars=yes,toolbar=yes,menubar=yes');
}

Funds_class.prototype.openHUB = function(e, el) {
	
	e.cancel();
	el.blur();
	
	try {
		var win = window.open(buildAbsolutePath("/etfs/hub/index.asp"), "ETFHUB", "width=800,height=600,scrollbars=yes,resizable=yes,0");
			win.focus();
			
	} catch(e) { 
		alert("Please allow popups for ETF Hub Access.");
	}
}

Funds_class.prototype.switchRankings = function(rankingType) {

	Element.removeClass("a-rankings-active","active");
	Element.removeClass("a-rankings-gainers","active");
	Element.addClass("a-rankings-"+rankingType,"active");
	Element.get("a-rankings-"+rankingType).blur();

	Common.contentBufferAbortRequests();
	Common.loadContentBuffer({
		page:"rankingsBuffer.asp",
		context:this,
		contentType:"text/html",
		data:{
			rankingType:rankingType
		},
		onload:function(cb) {
			Element.setHTML("div-rankings",cb.getResult());
			try { initMore(); } catch(e) {}
		},
		onerror:null
	});

}

Funds_class.prototype.submitSymbolSelections = function(args) {
	
	if (!args.target) { args.target = "compare"; }
	if (!args.type) { args.type = "checkbox"; }
	
	var input = Element.get("categoriesSymbolsInput");
	if (args.type == "input") {
		if (input.value == "" || !/\w/.test(input.value)) {
			var msg = "Please enter up to five symbols before submitting the form."
			Common.drawInlineMsg("errStandard", msg);
			return false;
		}
	}

	var compareSymbols = [];
	if (args.type == "checkbox") {
		var checkBoxes = Element.parseSelector("input.chkCompareSymbol");
		for (var i = 0; i < checkBoxes.length; i++) {
			if (checkBoxes[i].checked == true) { compareSymbols.push(checkBoxes[i].value); }
		}
	} else {
		//var input = Element.get("categoriesSymbolsInput");
		//compareSymbols = input.value.replace(/ /g, "").split(",");
		compareSymbols = Common.stringToArray(input.value, true);
	}

	if (!compareSymbols.length) {
		var msg = "Please select up to five symbols before submitting the form."
		Common.drawInlineMsg("errStandard", msg);
		return false;
	}
	

	var formAction, inputName;
	if (args.target == "compare") {
		var values = {compareType:"specific", symbolMultiple:compareSymbols.join(",")};
		location.href = "../compare/compareResults.asp?data=" + DataFunctions.serializer.serialize(values);
		return;
	} else {
		formAction = gSessionSeedDomain + "/cgi-bin/apps/u/WatchListCreate";
		inputName = "symbols";
	}

	if (compareSymbols.length > 0) {
		var form = Element.create("form", {action: formAction, method: "get"}, null, document.body);
		var input = Element.create("input", {type: "hidden", name: inputName, value: compareSymbols.join(",")}, null, form);

		form.submit();
	}
}



Funds_class.prototype.initCategoriesPage = function(args) {

	if (!args) { args = {}; }
	Element.setDisplay("div-delayedkey","none")
	var selectCategory = Element.get("selectCategory");
	var selectFamily = Element.get("selectFamily");
	var selectView = Element.get("selectView");

	var selectCategoryOptions = Element.parseSelector("option", selectCategory);
	var selectFamilyOptions = Element.parseSelector("option", selectFamily);
	var selectViewOptions = Element.parseSelector("option", selectView);

	for (var i = 0; i < selectCategoryOptions.length; i++) {
		if (selectCategoryOptions[i].value == startCategory) { selectCategoryOptions[i].selected = true }
	}
	for (var i = 0; i < selectFamilyOptions.length; i++) {
		if (selectFamilyOptions[i].value == startFamily) { selectFamilyOptions[i].selected = true }
	}
	for (var i = 0; i < selectViewOptions.length; i++) {
		if (selectViewOptions[i].value == startView) { selectViewOptions[i].selected = true }
	}

	this.loadCategoriesContent({type:args.type||"ETF",view: startView, category: startCategory, family: startFamily, numRows: startNumRows});
}

Funds_class.prototype.loadCategoriesContent = function(args) {

	args.type = args.type || "ETF";
	args.view = args.view || Element.get("selectView").value;
	args.category = args.category || Element.get("selectCategory").value;
	args.family = args.family || Element.get("selectFamily").value;
	args.startRow = args.startRow || 1;

	var tableCont = Element.get("tableCont");
	var table = (Element.parseSelector("table", tableCont).length > 0) ? Element.parseSelector("table", tableCont)[0] : null;

	if (table) {
		var size = Element.getSize(tableCont);
		Element.removeChildNodes(tableCont);
		var placeholder = Element.create("div", null, null, null);
		Element.setSize(placeholder, size.width, size.height);
		tableCont.appendChild(placeholder);
	}

	Common.contentBufferAbortRequests();
	Common.loadContentBuffer({
		page:"categoriesBuffer.asp",
		contentType:"text/html",
		data:{
			args:args
		},
		onload:function(cb) {
			var result = cb.getResult();
		
			Element.setDisplay("div-delayedkey",result.match("delayed")?"block":"none")
			Common.handleContentBufferHTML(tableCont,result);
		},
		onerror:null
	});

}

Funds_class.prototype.countCheck = function(obj) {
	var checkBoxes = Element.parseSelector("input.chkCompareSymbol");
	for (var i = 0, count = 0; i < checkBoxes.length; i++) {
		if (checkBoxes[i].checked == true) { count++; }
	}

	if (count > 4) {
		for (var i = 0, count = 0; i < checkBoxes.length; i++) {
			if (checkBoxes[i].checked == false) { checkBoxes[i].disabled = true }
		}
	} else {
		for (var i = 0, count = 0; i < checkBoxes.length; i++) {
			if (checkBoxes[i].disabled == true) { checkBoxes[i].disabled = false }
		}
	}
}

Funds_class.prototype.clearSymbolsList = function() {

	var categoriesSymbolsInput = Element.get("categoriesSymbolsInput");
	categoriesSymbolsInput.value = "";
}



Funds_class.prototype.initComparePage = function() {

	Element.setDisplay("div-delayedkey","none")
	var selectView = Element.get("selectView");
	var selectCompare = Element.get("selectCompare");

	var selectViewOptions = Element.parseSelector("option", selectView);
	var selectCompareOptions = Element.parseSelector("option", selectCompare);

	for (var i = 0; i < selectViewOptions.length; i++) {
		if (selectViewOptions[i].value == startView) { selectViewOptions[i].selected = true }
	}
	for (var i = 0; i < selectCompareOptions.length; i++) {
		if (selectCompareOptions[i].value == startComparison) { selectCompareOptions[i].selected = true }
	}

	this.loadCompareContent(startView, startComparison);
}

Funds_class.prototype.loadCompareContent = function(view, comparison) {
	if (!view) { view = Element.get("selectView").value };
	if (!comparison) { comparison = Element.get("selectCompare").value };
	var symbols = currentSymbols;

	var tableCont = Element.get("tableCont");
	var table = (Element.parseSelector("table", tableCont).length > 0) ? Element.parseSelector("table", tableCont)[0] : null;

	if (table) {
		var size = Element.getSize(table);
		Element.removeChildNodes(tableCont);
		var placeholder = Element.create("div", null, null, null);
		Element.setSize(placeholder, size.width, size.height);
		tableCont.appendChild(placeholder);
	}

	Common.contentBufferAbortRequests();
	Common.loadContentBuffer({
		page:"compareBuffer.asp",
		contentType:"text/html",
		data:{
			symbols: symbols,
			view: view,
			comparison: comparison
		},
		onload:function(cb) {
			var result = cb.getResult();
			Element.setDisplay("div-delayedkey",result.match("delayed")?"block":"none")
			Common.handleContentBufferHTML(tableCont,result);
		},
		onerror:null
	});

}

Funds_class.prototype.updateComparisonSymbols = function() {
	var form = Element.get("formCompare");
	var comparisonSymbols = Element.get("comparisonSymbols");
	var comparisonSymbolsInput = Element.get("comparisonSymbolsInput");

	comparisonSymbols.value = comparisonSymbolsInput.value;
	form.submit();
}

Funds_class.prototype.submitCompareFunds = function() {
	var form = Element.get("formCompare");

	form.submit();
}

Funds_class.prototype.windowProps = 'width=680,height=480,scrollbars=1,resizable=yes,menubar=yes,titlebar=0,toolbar=yes,directories=1,location=no';

Funds_class.prototype.openProspectusLink = function(e, el) {
	
	e.cancel();
	
	var prosWindow = window.open(el.href,'prospectus',this.windowProps);
		prosWindow.focus();
}

Funds_class.prototype.openProspectus = function(id) {
	var page = "http://prospectus-express.newriver.com/pnet/get_template.asp?clientid=amtd&fundid=" + id;
	prosWindow = window.open(page,'mutualpdf',this.windowProps);
	prosWindow.focus(); // Make sure it's on top of everything else on spawning.
}

Funds_class.prototype.openProspectusStreetSymbol = function(symbol) {
	var page = buildAbsolutePath("/common/getProspectus.asp?symbol=" + symbol);
	prosWindow = window.open(page,'mutualpdf',this.windowProps);
	prosWindow.focus(); // Make sure it's on top of everything else on spawning.
}

Funds = new Funds_class();

loadBuffer.add(function(){
	
	Funds.init();	
});