function createBarChart(parentElementID) {
  Silverlight.createObjectEx( {
    source: "/Pages/Business/Xaml/BarChart.xaml",
    parentElement: document.getElementById(parentElementID),
    id: "silverlightControl",
    properties: {
      width: "400", height: "160", version: "1.0", background: document.body.style.backgroundColor, inplaceInstallPrompt: true
    },
    events: { onLoad: LoadBars }
  });
}

function LoadBars(control, context, rootElement) {

  var y = 5;

  var Height = (Values.length * 35) + 5;

  ConfigureAxis(rootElement, Height);

  for (i=0; i < Values.length ; i++ ) {
    new Bar("Bar" + i, rootElement, 50, y, Values[i], Percentages[i], Titles[i]);
    y += 35;
  }
}

function ConfigureAxis(rootElement, Height) {

  var axisWidth = rootElement.FindName("BarChart_Canvas").Width - 150;

  rootElement.FindName('Bottom_Axis_Canvas')["Canvas.Top"] = Height + 5;
  rootElement.FindName('VerticalAxis_Line')["Y2"] = Height;

  var horizontalAxis_Line = rootElement.FindName('HorizontalAxis_Line');

  horizontalAxis_Line["X2"] = axisWidth + 51;
  horizontalAxis_Line["Y1"] = Height;
  horizontalAxis_Line["Y2"] = Height;

  MoveLine(rootElement.FindName('TwentyFivePercent_Line'), rootElement.FindName('TwentyFivePercent_TextBlock'), Height, (axisWidth / 4));
  MoveLine(rootElement.FindName('FiftyPercent_Line'), rootElement.FindName('FiftyPercent_TextBlock'), Height, (axisWidth /2));
  MoveLine(rootElement.FindName('SeventyFivePercent_Line'), rootElement.FindName('SeventyFivePercent_TextBlock'), Height, (axisWidth / 4) * 3);
  MoveLine(rootElement.FindName('OneHundredPercent_Line'), rootElement.FindName('OneHundredPercent_TextBlock'), Height, axisWidth) 

  rootElement.FindName('BarChart_Canvas').Height = Height + 20;

  var Percent_TextBlock = rootElement.FindName('Percentage_TextBlock');
  Percent_TextBlock["Canvas.Left"] = ((axisWidth - Percent_TextBlock.ActualWidth) / 2) + 50;
}

Silverlight.createDelegate = function(instance, method) {
  return function() { return method.apply(instance, arguments); }
}

function MoveLine(passedLine, passedTextBlock, Height, Left) {
  passedLine["Y2"] = Height;
  passedLine["X1"] = Left + 50;
  passedLine["X2"] = Left + 50;

  passedTextBlock["Canvas.Left"] = (Left + 50) - (passedTextBlock.ActualWidth / 2);
}
