function log() {
	WSDOM.Console.log.apply(WSDOM.Console, arguments);
}

function chartPopup_class() {
	this._timeoutIds = [];
	this._dialog;
	this._loadDiv;
	this._chartHolder = "popup-chart-holder";
}

chartPopup_class.prototype.setMenuTimeouts = function (val) {
	this._timeoutIds.push(val);
}

chartPopup_class.prototype.getMenuTimeouts = function () {
	var timeouts = this._timeoutIds;
	return timeouts;
}

chartPopup_class.prototype.clearMenuTimeouts = function () {
	this._timeoutIds = [];
}

chartPopup_class.prototype.focusPopup = function() {
	var timeoutIds = this.getMenuTimeouts();
	for ( var i=0; i<timeoutIds.length; i++ ) {
		clearTimeout(timeoutIds[i]);
	}
	
	return;
}

chartPopup_class.prototype.delayRemove = function() {
	var timeoutId = setTimeout(("chartPopup.remove()"), 100);
	this.setMenuTimeouts(timeoutId);
}

chartPopup_class.prototype.init = function(el) {
	this._element = el;
	this._symbol = this._element.getAttribute("symbol");
	this._fund = this._element.getAttribute("fund");
	
	this.clearMenuTimeouts();
	
	if (!this._dialog) {
		var args = {
			content: this.createContent()
			,width: 290
			//,height: 220
			,xOffset: -40
			,yOffset: -125
		}
		
		this.createPopup(args);

		this._dialog.AddClass("priceChart");

		this.request();
	}
}

chartPopup_class.prototype.remove = function() {
	if (this._dialog) {
		this._dialog.Remove();
		this._dialog = null;
		
		this.clearMenuTimeouts();
	}
}

chartPopup_class.prototype.createContent = function(args) {
	var content = Element.create("div", {id:"div-chart-popup"}, [
		Element.create("div", {className:"popup-header"}, [
			Element.create("span", {className:"fundname"}, this._fund)
			,Element.create("span", {className:"symbol"}, this._symbol)
		])
		,Element.create("div", {id: this._chartHolder}, this.loadingDiv())
		,Element.create("div", {className:"popup-footer"}, [
			Element.create("a", {href:Links.Summary + "?Symbol=" + this._symbol + "&SymbolSet=Street", className:"detail-link"}, "View Fund Details")
		])
	]);
	
	return content;
}

chartPopup_class.prototype.createPopup = function(args) {
	this._dialog = new Popup_class(args.content);
	this._dialog.SetSize({width: args.width, height: args.height});

	// dynamically position popup 
	var xy = this.getPosition();
	var xOffset = args.xOffset || 0;
	var yOffset = args.yOffset || 0;
	this._dialog.SetPosition({top: xy.y + yOffset, left: xy.x - xOffset});
	
	this._dialog.Show();
	
	if (this._dialog.container && this._dialog.container[0]) {
		Events.add({element:this._dialog.container[0], type:"mouseover", handler:chartPopup.focusPopup, context:chartPopup});
		Events.add({element:this._dialog.container[0], type:"mouseout", handler:chartPopup.delayRemove, context:chartPopup});
	}
}

chartPopup_class.prototype.getPosition = function() {
	var xy = Element.getXY(this._element);
		
	return xy;
}

chartPopup_class.prototype.loadingDiv = function() {
	if (!this._loadDiv && Element.get(this._chartHolder)) {
		this._loadDiv = Element.create("div", {className:"load"}, null, Element.get(this._chartHolder));
		return this._loadDiv;
	}
}

chartPopup_class.prototype.removeLoadingDiv = function() {
	if (this._loadDiv) {
		Element.remove(this._loadDiv);
		this._loadDiv = null;
	} 
}
chartPopup_class.prototype.request = function(el) {
	this._buffer = FundRemoting.PriceChart({
		onload: this.load
		,onerror: this.error
		,context: this
		,arguments: {symbol: this._symbol}
	});
}

chartPopup_class.prototype.load = function(buffer) {
	var result = buffer.getResult();
	
	if (result.status && Element.get(this._chartHolder)) {
		this.removeLoadingDiv();
		var img = Element.get(this._chartHolder).getElementsByTagName("img");
		
		if (img[0]) {
			img[0].src = result.path;
		} else {
			Element.create("img", {src: result.path}, null, Element.get(this._chartHolder));
		}
	} else {
		this.error();
	}
}

chartPopup_class.prototype.error = function() {
	if (Element.get(this._chartHolder)) {
		this.removeLoadingDiv();
		Element.create("div", {className:"error"}, "Chart unavailable", Element.get(this._chartHolder));
	}
}

var chartPopup = new chartPopup_class;

