

var ProfileChart = function() {
	
};

ProfileChart.prototype.BUFFER_PATH = "../../tools/charts/fundSmartChart.asp";

ProfileChart.prototype.init = function() {
	
	this.container = Element.get("SmartChartContainer");
	this.loader = Element.get("smartChartLoader");
	
	this.params = {
		 time:365
		,width:this.getWidth()
		,height:228
		,compare:[]
	};
	
	Events.add({
		 element:window
		,type:"resize"
		,handler:this.resizeChart
		,context:this
	});
	
	this.get();
};

ProfileChart.prototype.resizeChart = function () {

	this.get();	
};

ProfileChart.prototype.getWidth = function() {
		
	var width = Element.parseSelector(".chartSide")[0].offsetWidth;
	
	return width;
};

ProfileChart.prototype.get = function () {
	
	var width = this.getWidth();
	
	Element.setStyle(Element.parseSelector(".content", this.loader), "margin-left:"+ ((width/2) - 85)+"px")
	Element.removeClass(this.loader, "none");
	
	Common.loadContentBuffer({
		 page:this.BUFFER_PATH
		,prevenEval:true
		,onload:this.renderChart
		,onerror:this.renderError
		,context:this
		,data:this.params
	});
};

ProfileChart.prototype.renderChart = function ( data ) {
	
	data = DataFunctions.serializer.deserialize(data.getResult());
	
	if(!this.img) {
		var img = Element.create("img", {}, "", this.container);
		this.img = img
	}
	this.img.src = data.file;
	Element.addClass(this.loader, "none");
};

ProfileChart.prototype.switchChart = function( el, time ) {
	
	el.blur();
	
	var els = Element.parseSelector("a", "timeframe_controls");
			  Element.removeClass(els, "active")
			  Element.addClass(el, "active");
			  
			  
	var time = this.timeMap[time];
	
	this.params.time = time;
	this.get();
};

ProfileChart.prototype.addComparison = function( el ) {
	
	var p = this.params.compare;
	var attr = el.getAttribute("issue");
	var i = 0;
	var len = p.length;
	var hasCompare = false;
	
	if(el.checked) {
		
		for(; i < len; i++) {
			if(p[i].symbol == attr) {
				hasCompare = true;
			}
		}
		if(!hasCompare) {
			p.push(this.compareMap[attr]);	
		}
				
	} else {
		for(; i < len; i++) {
			if(p[i].symbol == attr) {
				p.splice(i,1);
				i--;
				len--;
			}
		}
	}
	this.params.compare = p;
	this.get();
};

ProfileChart.prototype.timeMap = {
	 "current":1
	,"1mo":30
	,"3mo":90
	,"6mo":180
	,"1yr":365
};

ProfileChart.prototype.compareMap = {
	"599362":{"symbol":"599362","params":{"color":"FFB754"}}
	,"579435":{"symbol":"579435","params":{"color":"1E5DCF"}}
	,"575769":{"symbol":"575769","params":{"color":"9633C1"}}
};