
function on_resize()
{
	 //draw_count is a hack for pie charts. To disable them to be able to open pies
	draw_count = 1
	chart.redraw();

}

function hideLabels(labels)
{
	for(var i=0;i<labels.length;i++)
    {
		labels[i].style.display = "none";
    }
}

function hideAllLabels()
{
	 if(!labels || !labels[0])
		  return;
	 if(labels[0][0])
	 {
		  for (var labelIndex in labels)
		  {
			 if(labelIndex!='indexOf')
			 {
			   hideLabels(labels[labelIndex])

			 }
		  }
	 }

	 else
	 {
		  for (i=0;i<labels.length;i++)
		  {
			   labels[i].style.display = "none";
		  }

	 }

}



function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function onNeedsTicks_dateGraph( min , max , chart ) {
		if( allYears )
		{

			tickArr = [];
			var tickEvery;
			for(tickEvery=1;dateGraph_data_list.length/tickEvery>12;tickEvery++){}
			for(var i=0;i<dateGraph_data_list.length;i++)
			{
				if(i%tickEvery==0)
				{
					tickArr[tickArr.length] = [dateGraph_data_list[i][0],null];
				}
				else
				{
					tickArr[tickArr.length] = [null];
				}

			}

			return tickArr
	        }
		else
		{
			return null;

		}
	}

var first_series_on = false
var no_series = false
function showDataSeries(changed_data_series,is_show)
{

	 chart.clearSelectedPoint()

	 if (!enable_initialize_pusedo_bar_filtering)
     {
        if(is_show)
            changed_data_series.show();
        else
            changed_data_series.hide();
     }

	 if (pseudo_bar_all_data_series[0].getPoints()[0].start_y == undefined)
	 {
		  populate_start_y(pseudo_bar_all_data_series)
	 }

	 changePoints = changed_data_series.getPoints();


	 for(seriesIndex=0;seriesIndex<pseudo_bar_all_data_series.length;seriesIndex++)
	 {
		  comparisonPoints = pseudo_bar_all_data_series[seriesIndex].getPoints()

		  if (changed_data_series==pseudo_bar_all_data_series[seriesIndex])
		  {
			   is_changed_data_series = true
		  }
		  else
		  {
			   is_changed_data_series = false
		  }

		  for(dataIndex=0;dataIndex<comparisonPoints.length;dataIndex++)
		  {


			   if (!is_changed_data_series && comparisonPoints[dataIndex].start_y>=changePoints[dataIndex].start_y)
			   {
					if(is_show)
						 comparisonPoints[dataIndex].y = comparisonPoints[dataIndex].y + changePoints[dataIndex].userdata
					else
						 comparisonPoints[dataIndex].y = comparisonPoints[dataIndex].y - changePoints[dataIndex].userdata

			    }

		  }
	 }


	 if(is_show)
		  changed_data_series.show();
	 else
		  changed_data_series.hide();

	 set_extremes(pseudo_bar_all_data_series)
}


function set_extremes(data_series)
{
	 highest_y = 0

	 for(seriesIndex=0;seriesIndex<data_series.length;seriesIndex++)
	 {
		  if(data_series[seriesIndex].getVisibility() )
		  {
			   points = data_series[seriesIndex].getPoints()
			   for(dataIndex=0;dataIndex<points.length;dataIndex++)
			   {

					if (points[dataIndex].y > highest_y )
					{

							  highest_y = points[dataIndex].y

					}


			   }
		  }
	 }

	 if(highest_y<1 && highest_y>0)
	 {
		  highest_y = 1
	 }
	 if( highest_y >= 1 )
	 {
		 chart.axis_left.setExtremes( 0, Math.round(highest_y + (highest_y*.1)) );
	     chart.redraw(false)
	 }



}


//function is used for testing
function display_all_series_data()
{
	 for(seriesIndex=0;seriesIndex<pseudo_bar_all_data_series.length;seriesIndex++)
	 {
		  comparisonPoints = pseudo_bar_all_data_series[seriesIndex].getPoints()

		  for(dataIndex=0;dataIndex<comparisonPoints.length;dataIndex++)
		  {
			   alert(comparisonPoints[dataIndex].y)
			   alert(comparisonPoints[dataIndex].userdata)
		  }
	 }

}

function populate_start_y(all_series)
{
	 for(seriesIndex=0;seriesIndex<all_series.length;seriesIndex++)
	 {
		  comparisonPoints = all_series[seriesIndex].getPoints()

		  for(dataIndex=0;dataIndex<comparisonPoints.length;dataIndex++)
		  {
			   comparisonPoints[dataIndex].start_y = comparisonPoints[dataIndex].y
		  }
	 }

}



var heightOffset = 0;
var fontRatioCharLength = 6;
var labels = [] ;

function draw_vertical_bar_labels(series_list,associatedBarLabelList,prefix,suffix)
{
     prefix = prefix || null;
     suffix = suffix || null;
	 var additiveBarSeriesValueList = [];
	 for(var i=0;i<series_list[0].getPoints().length;i++)
	 {
		 additiveBarSeriesValueList[i]=0;
	 }

	 for(var dataSeriesIndex=0;dataSeriesIndex<series_list.length;dataSeriesIndex++)
	 {

		 dataPoints = series_list[dataSeriesIndex].getPoints();

		 if(!labels[dataSeriesIndex])
			 labels[dataSeriesIndex] = []
		 if(series_list[dataSeriesIndex]&&!series_list[dataSeriesIndex].visible)
		 {
			 hideLabels(labels[dataSeriesIndex]);
			 continue;
		 }

		 for(var dataIndex=0;dataIndex<dataPoints.length;dataIndex++)
		 {
			 //If series is invisible hide points
			 dataPoint = dataPoints[dataIndex];
			 displayValue = dataPoint.userdata;
			 value =dataPoint.y;   //this is due to the use of pusedo bars
			 binValue = dataPoint.x;

			 barEnd = {
				 top: chart.axis_left.pointToPixel(value),
				 left: chart.axis_bottom.pointToPixel(binValue)

			 };

			 var div;
			 var display = ""
			 if(prefix)
			    display += prefix
			 
			 display += addCommas(displayValue);
			 
			 if(suffix)
			    display += suffix
			 
			 if (!labels[dataSeriesIndex][dataIndex])
			 {
				 div = document.createElement('div');
				 div.className = associatedBarLabelList[dataSeriesIndex];
				 
				 div.innerHTML = display
				 labels[dataSeriesIndex][dataIndex] = div;
				 document.body.appendChild(div);
			 }
			 else
			 {
				 div = labels[dataSeriesIndex][dataIndex];
				 div.innerHTML = display
			 }

			 startPxl = chart.axis_left.pointToPixel(additiveBarSeriesValueList[dataIndex]);
			 barEnd.bottom = chart.axis_left.pointToPixel(additiveBarSeriesValueList[dataIndex]);
			 additiveBarSeriesValueList[dataIndex] = additiveBarSeriesValueList[dataIndex] + displayValue;

			 if (barEnd.top == undefined) { div.style.display = "none";continue; }
			 else if (barEnd.left == undefined) { div.style.display = "none";continue; }
			 else if (barEnd.bottom == undefined) { div.style.display = "none";continue; }

			 textLength = display.length;
			 pxlsLength = textLength * fontRatioCharLength;

			 divLeft = barEnd.left - (pxlsLength/2);
			 div.style.left = divLeft+"px";


			 //we want it right in the middle of the bar. So that is just the bar height /2 plus the zero
			 middleOfBar = ( (barEnd.top - startPxl) / 2) + startPxl
			 //know move it over so the center of the text is at the center of the bar
		     divTop = middleOfBar - fontRatioCharLength

		     if(!isNaN(divTop))
		     {
			   div.style.top = divTop+heightOffset+"px";
			   div.style.display = "block";
		     }

		 }
	 }

 }

var pseudo_bar_all_data_series = []
var enable_initialize_pusedo_bar_filtering = false

function initialize_pusedo_bar_filtering(all_data_series)
{
	 pseudo_bar_all_data_series = all_data_series
	 enable_initialize_pusedo_bar_filtering = true
}