/**
 * FusionCharts: Flash Player detection and Chart embed 
 * 
 * Morphed from SWFObject (http://blog.deconcept.com/swfobject/) under MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 */

if(typeof infosoftglobal == "undefined") var infosoftglobal = new Object();
if(typeof infosoftglobal.FusionChartsUtil == "undefined") infosoftglobal.FusionChartsUtil = new Object();
infosoftglobal.FusionCharts = function(swf, id, w, h, debugMode, registerWithJS, c, scaleMode, lang){
	if (!document.getElementById) { return; }
	
	//Flag to see whether data has been set initially
	this.initialDataSet = false;
	
	//Create container objects
	this.params = new Object();
	this.variables = new Object();
	this.attributes = new Array();
	
	//Set attributes for the SWF
	if(swf) { this.setAttribute('swf', swf); }
	if(id) { this.setAttribute('id', id); }
	if(w) { this.setAttribute('width', w); }
	if(h) { this.setAttribute('height', h); }
	
	//Set background color
	if(c) { this.addParam('bgcolor', c); }
	
	//Set Quality	
	this.addParam('quality', 'high');
	
	//Add scripting access parameter
	this.addParam('allowScriptAccess', 'always');

	//Add transparent
	this.addParam('wmode', 'transparent');

	
	//Pass width and height to be appended as chartWidth and chartHeight
	this.addVariable('chartWidth', w);
	this.addVariable('chartHeight', h);

	//Whether in debug mode
	debugMode = debugMode ? debugMode : 0;
	this.addVariable('debugMode', debugMode);
	//Pass DOM ID to Chart
	this.addVariable('DOMId', id);
	//Whether to registed with JavaScript
	registerWithJS = registerWithJS ? registerWithJS : 0;
	this.addVariable('registerWithJS', registerWithJS);
	
	//Scale Mode of chart
	scaleMode = scaleMode ? scaleMode : 'noScale';
	this.addVariable('scaleMode', scaleMode);
	//Application Message Language
	lang = lang ? lang : 'EN';
	this.addVariable('lang', lang);
}

infosoftglobal.FusionCharts.prototype = {
	setAttribute: function(name, value){
		this.attributes[name] = value;
	},
	getAttribute: function(name){
		return this.attributes[name];
	},
	addParam: function(name, value){
		this.params[name] = value;
	},
	getParams: function(){
		return this.params;
	},
	addVariable: function(name, value){
		this.variables[name] = value;
	},
	getVariable: function(name){
		return this.variables[name];
	},
	getVariables: function(){
		return this.variables;
	},
	getVariablePairs: function(){
		var variablePairs = new Array();
		var key;
		var variables = this.getVariables();
		for(key in variables){
			variablePairs.push(key +"="+ variables[key]);
		}
		return variablePairs;
	},
	getSWFHTML: function() {
		var swfNode = "";
		if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) { 
			// netscape plugin architecture			
			swfNode = '<embed type="application/x-shockwave-flash" src="'+ this.getAttribute('swf') +'" width="'+ this.getAttribute('width') +'" height="'+ this.getAttribute('height') +'"  ';
			swfNode += ' id="'+ this.getAttribute('id') +'" name="'+ this.getAttribute('id') +'" ';
			var params = this.getParams();
			 for(var key in params){ swfNode += [key] +'="'+ params[key] +'" '; }
			var pairs = this.getVariablePairs().join("&");
			 if (pairs.length > 0){ swfNode += 'flashvars="'+ pairs +'"'; }
			swfNode += '/>';
		} else { // PC IE			
			swfNode = '<object id="'+ this.getAttribute('id') +'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'+ this.getAttribute('width') +'" height="'+ this.getAttribute('height') +'">';
			swfNode += '<param name="movie" value="'+ this.getAttribute('swf') +'" />';
			var params = this.getParams();
			for(var key in params) {
			 swfNode += '<param name="'+ key +'" value="'+ params[key] +'" />';
			}
			var pairs = this.getVariablePairs().join("&");			
			if(pairs.length > 0) {swfNode += '<param name="flashvars" value="'+ pairs +'" />';}
			swfNode += "</object>";
		}
		return swfNode;
	},
	setDataURL: function(strDataURL){
		//This method sets the data URL for the chart.
		//If being set initially
		if (this.initialDataSet==false){
			this.addVariable('dataURL',strDataURL);
			//Update flag
			this.initialDataSet = true;
		}else{
			//Else, we update the chart data using External Interface
			//Get reference to chart object
			var chartObj = infosoftglobal.FusionChartsUtil.getChartObject(this.getAttribute('id'));
			chartObj.setDataURL(strDataURL);
		}
	},
	setDataXML: function(strDataXML){
		//If being set initially
		if (this.initialDataSet==false){
			//This method sets the data XML for the chart INITIALLY.
			this.addVariable('dataXML',strDataXML);
			//Update flag
			this.initialDataSet = true;
		}else{
			//Else, we update the chart data using External Interface
			//Get reference to chart object
			var chartObj = infosoftglobal.FusionChartsUtil.getChartObject(this.getAttribute('id'));
			chartObj.setDataXML(strDataXML);
		}
	},
	render: function(elementId){
		var n = (typeof elementId == 'string') ? document.getElementById(elementId) : elementId;
		n.innerHTML = this.getSWFHTML();
		return true;		
	}
}

// ------------ Fix for Out of Memory Bug in IE in FP9 ---------------//
/* Fix for video streaming bug */
infosoftglobal.FusionChartsUtil.cleanupSWFs = function() {
	if (window.opera || !document.all) return;
	var objects = document.getElementsByTagName("OBJECT");
	for (var i=0; i < objects.length; i++) {
		objects[i].style.display = 'none';
		for (var x in objects[i]) {
			if (typeof objects[i][x] == 'function') {
				objects[i][x] = function(){};
			}
		}
	}
}
// Fixes bug in fp9
infosoftglobal.FusionChartsUtil.prepUnload = function() {
	__flash_unloadHandler = function(){};
	__flash_savedUnloadHandler = function(){};
	if (typeof window.onunload == 'function') {
		var oldUnload = window.onunload;
		window.onunload = function() {
			infosoftglobal.FusionChartsUtil.cleanupSWFs();
			oldUnload();
		}
	} else {
		window.onunload = infosoftglobal.FusionChartsUtil.cleanupSWFs;
	}
}
if (typeof window.onbeforeunload == 'function') {
	var oldBeforeUnload = window.onbeforeunload;
	window.onbeforeunload = function() {
		infosoftglobal.FusionChartsUtil.prepUnload();
		oldBeforeUnload();
	}
} else {
	window.onbeforeunload = infosoftglobal.FusionChartsUtil.prepUnload;
}

/* Add Array.push if needed (ie5) */
if (Array.prototype.push == null) { Array.prototype.push = function(item) { this[this.length] = item; return this.length; }}

/* Function to return Flash Object from ID */
infosoftglobal.FusionChartsUtil.getChartObject = function(id)
{
  if (window.document[id]) {
      return window.document[id];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1) {
    if (document.embeds && document.embeds[id])
      return document.embeds[id]; 
  } else {
    return document.getElementById(id);
  }
}
/* Aliases for easy usage */
var getChartFromId = infosoftglobal.FusionChartsUtil.getChartObject;
var FusionCharts = infosoftglobal.FusionCharts;


function getCommStyle() {
	var res=[];
	res.push("<styles>");
	res.push("	<definition>");
	res.push("		<style name='CaptionFont' type='font' font='dotum' size='11' color='767A9D' />");
	res.push("		<style name='ToolTipFont' type='font' font='tahoma' size='11' color='777777' isHTML='1' />");
	res.push("		<style name='LegendFont' type='font' font='dotum' size='11' color='565B86' />");
	res.push("		<style name='YValueFont' type='font' font='Verdana' size='9' color='767A9D' bold='1' />");
	res.push("		<style name='YAxisFont' type='font' font='dotum' size='11' color='767A9D' bold='0'/>");
	res.push("		<style name='LabelFont' type='font' font='tahoma' size='10' color='767A9D' />");
	res.push("	</definition>");
	res.push("	<application>");
	res.push("		<apply toObject='CAPTION' styles='CaptionFont' />");
	res.push("		<apply toObject='TOOLTIP' styles='ToolTipFont' />");
	res.push("		<apply toObject='LEGEND' styles='LegendFont' />");
	res.push("		<apply toObject='YAXISVALUES' styles='YValueFont' />");
	res.push("		<apply toObject='TRENDVALUES' styles='YValueFont' />");
	res.push("		<apply toObject='YAXISNAME' styles='YAxisFont' />");
	res.push("		<apply toObject='DataLabels' styles='LabelFont' />");
	res.push("	</application>");
	res.push("</styles>");
	return res.join("");
}

function setTrandLine(valueArr) {
	var res=[];
	res.push("<trendLines >");
	for(var i=0,cnt=valueArr.length;i<cnt;++i) res.push("	<line startValue='"+valueArr[i]+"' displayvalue='"+addComma(valueArr[i])+"' color='767A9D' alpha='0' /> ");
	res.push("</trendLines>");
	return res.join("");
}

function setCategoty(dataset, paddingCnt) {
	var label_cnt = 0;

	var res=[];
	res.push("<categories>");
	for(var i=0;i<paddingCnt;++i) res.push("<category />");	

	if (dataset.length < 40) {
		for(var i=0;i<(40-dataset.length);++i) res.push("<category />");
	}

	for(var i=0,j=0,cnt=dataset.length;i<cnt;++i) {
		if(dataset[i][1] == 1) {
			label_cnt = label_cnt + 1;
			res.push("<category label='"+ makeDotYYMMDD(dataset[i][0])+"' />");	
			res.push("<vLine color='F1F1F1' alpha='100'/>"); //vertical line 추가
		}
		else { 
			if (i==(cnt-1) && label_cnt == 0) {
				res.push("<category label='"+ makeDotYYMMDD(dataset[i][0])+"' />");	
				res.push("<vLine color='F1F1F1' alpha='100'/>"); //vertical line 추가
			} else {
				res.push("<category />");
			}
		}
	}

	for(var i=0;i<paddingCnt;++i) res.push("<category />");	
	res.push("</categories>");

	return res.join("");
}


function setValue(dataset, idx, isToolTip, isUpDn) {
	var res=[];

	if (dataset.length < 40) {
		for(var i=0;i<(40-dataset.length);++i) res.push("<set />");	
	}
	
	for(var i=0,cnt=dataset.length;i<cnt;++i) {
		res.push("<set value='"+dataset[i][idx]+"'");
		if(isToolTip) res.push(" tooltext=' "+makeDotYYMMDD(dataset[i][0])+" | "+addComma(dataset[i][idx])+" '");
		if(isUpDn) {
			if(dataset[i][idx]>=0) res.push(" color='F20021'");
			else res.push(" color='3A8BFF'");
		}
		res.push(" />");
	}
	return res.join("");
}

function setValuePer(dataset, idx, isToolTip, isUpDn) {
	var res=[];

	if (dataset.length < 40) {
		for(var i=0;i<(40-dataset.length);++i) res.push("<set />");	
	}
	
	for(var i=0,cnt=dataset.length;i<cnt;++i) {
		res.push("<set value='"+dataset[i][idx]+"'");
		if(isToolTip) res.push(" tooltext=' "+makeDotYYMMDD(dataset[i][0])+" | "+dataset[i][idx]+"% '");
		if(isUpDn) {
			if(dataset[i][idx]>=0) res.push(" color='F20021'");
			else res.push(" color='3A8BFF'");
		}
		res.push(" />");
	}
	return res.join("");
}

function setPadding(valueSet, paddingCnt) {
	var res=[];
	for(var i=0;i<paddingCnt;++i) res.push("<set />");	
	res.push(valueSet);	
	for(var i=0;i<paddingCnt;++i) res.push("<set />");	
	return res.join("");
}

function setDataset(valueSet, chartOption) {
	var res=[];
	res.push("<dataset "+chartOption+">");
	res.push(valueSet);	
	res.push("</dataset>");
	return res.join("");
}

function getMinMax(dataset, sNum, eNum) {
	var n_max=dataset[0][sNum];
	var n_min=dataset[0][sNum];
	var n_point = 0;
	var return_maxmin = new Array();

	for(var i=0,cnt=dataset.length;i<cnt;++i) {
		for (var j=sNum;j<=eNum;++j) {
			if ( dataset[i][j] < n_min) {
				n_min = dataset[i][j];
			} else if (dataset[i][j] > n_max) {
				n_max = dataset[i][j];
			}
		}
	}
	

	n_point = Math.round(Math.abs(n_max - n_min)*0.2);
	return_maxmin[0] = Math.round(n_min - n_point);
	return_maxmin[1] = Math.round(n_max + n_point);

	return return_maxmin;
}

function getMax(dataset, sNum, eNum) {
	var n_max=dataset[0][sNum];
	var return_max = new Array();

	for(var i=0,cnt=dataset.length;i<cnt;++i) {
		for (var j=sNum;j<=eNum;++j) {
			 if (dataset[i][j] > n_max) {
				n_max = dataset[i][j];
			}
		}
	}


	return_max[0] = n_max + Math.round(n_max*0.2);
	return_max[1] = return_max[0] - (return_max[0]*2);

	return return_max;
}

function roundMath(num, upIndex) {
	var tmp = num;

	if (upIndex == "" || upIndex == undefined || upIndex < 1 || num < 100) {
		upIndex = 1;
	}

	for (i=1; i <= upIndex; i++) {
		tmp = tmp * 0.1;
	}

	tmp = Math.round(tmp);

	for (i=1; i <= upIndex; i++) {
		tmp = tmp * 10;
	}

	return tmp;
}

function YAxisValueRound(d_num) {
	var return_num;

	if ( String(d_num).indexOf("-") == 0) {
		if (String(d_num).length > 3) {
			return_num = String(d_num).substring(0,3);

			for (var i=0; i < String(d_num).length-3;++i )
			{
				return_num += "0";
			}

		} else {
			return_num = d_num;
		}

	} else {
		if (String(d_num).length > 2) {
			return_num = String(d_num).substring(0,2);

			for (var i=0; i < String(d_num).length-2;++i )
			{
				return_num += "0";
			}

		} else {
			return_num = d_num;
		}

	}

	return parseInt(return_num);
}

function addComma(s) {
	s+='';
	var re = new RegExp('(-?[0-9]+)([0-9]{3})'); 
	while(re.test(s)) s = s.replace(re, '$1,$2'); 
	return s;
};

function makeYYYYMMDD(dDate) {

	var str_date;

	str_date = new String(dDate.getFullYear());

	if ( dDate.getMonth()+1 <= 9) {
		str_date = str_date + "0" + new String(dDate.getMonth()+1);
	} else {
		str_date = str_date + new String(dDate.getMonth()+1);
	}

	if ( dDate.getDate() <= 9) {
		str_date = str_date + "0" + new String(dDate.getDate());
	} else {
		str_date = str_date + new String(dDate.getDate());
	}
	
	return str_date;
}

function makeDotYYMMDD(str) {
	var return_str;
	var str_len;

	if (str.length) {
		str_len = str.length;
	} else {
		str_len = 0;
		return_str = "";
	}

	if (str_len >= 6) {
		return_str =  str.substring(0,str_len-4) + "." + str.substring(str_len-4,str_len-2) + "." + str.substring(str_len-2,str_len);
	}

	return return_str;
}

var Ldate = "";
var Rdate = "";

function changeChartDate(otype) {
	var return_date = "";
	var t_date = new Date();
	
	// -60데이터 기준일
	if (otype == "0") {
		t_date.setFullYear(Ldate.substring(0,4));
		t_date.setMonth((Ldate.substring(4,6)-1));
		t_date.setDate(Ldate.substring(6,8));
		t_date.setDate(t_date.getDate()-1);

	// +60데이터 기준일
	} else if (otype == "1") {
		t_date.setFullYear(Rdate.substring(0,4));
		t_date.setMonth((Rdate.substring(4,6)-1));
		t_date.setDate(Rdate.substring(6,8));
		t_date.setDate(t_date.getDate()+1);	
	}

	return t_date;
}