

<!--

var ix = 1;
quotationSources = {
 STB: {
  name: 'Stawki bazowe',
  groups: {
   LIB: {
    name: 'LIBOR',
    items: [
     ['CHF 1M','/_xml/CHF1m.xml'],
     ['CHF 2M','/_xml/CHF2m.xml'],
     ['CHF 3M','/_xml/CHF3m.xml'],
     ['CHF 6M','/_xml/CHF5m.xml'],
     ['CHF 12M','/_xml/CHF12m.xml'],
     ['EUR 1M','/_xml/EUR1m.xml'],
     ['EUR 2M','/_xml/EUR2m.xml'],
     ['EUR 3M','/_xml/EUR3m.xml'],
     ['EUR 6M','/_xml/EUR5m.xml'],
     ['EUR 12M','/_xml/EUR12m.xml'],
     ['GBP 1M','/_xml/GBP1m.xml'],
     ['GBP 2M','/_xml/GBP2m.xml'],
     ['GBP 3M','/_xml/GBP3m.xml'],
     ['GBP 6M','/_xml/GBP5m.xml'],
     ['GBP 12M','/_xml/GBP12m.xml'],
     ['JPY 1M','/_xml/JPY1m.xml'],
     ['JPY 2M','/_xml/JPY2m.xml'],
     ['JPY 3M','/_xml/JPY3m.xml'],
     ['JPY 6M','/_xml/JPY5m.xml'],
     ['JPY 12M','/_xml/JPY12m.xml'],
     ['USD 1M','/_xml/USD1m.xml'],
     ['USD 2M','/_xml/USD2m.xml'],
     ['USD 3M','/_xml/USD3m.xml'],
     ['USD 6M','/_xml/USD5m.xml'],
     ['USD 12M','/_xml/USD12m.xml']
    ]
   },
   WIB: {
    name: 'WIBOR',
    items: [
     ['PLN ON','/_xml/PLNON.xml'],
     ['PLN TN','/_xml/PLNTN.xml'],
     ['PLN 1W','/_xml/PLN1W.xml'],
     ['PLN 2W','/_xml/PLN2W.xml'],
     ['PLN 1M','/_xml/PLN1M.xml'],
     ['PLN 3M','/_xml/PLN3M.xml'],
     ['PLN 6M','/_xml/PLN6M.xml'],
     ['PLN 9M','/_xml/PLN9M.xml'],
     ['PLN 12M','/_xml/PLN12M.xml']
    ]
   }
  }
 },
 WAL: {
  name: 'Waluty',
  items: [
    ['CHF','/_xml/20_notowania.xml'],
    ['EUR','/_xml/2_notowania.xml'],
    ['GBP','/_xml/23_notowania.xml'],
    ['JPY','/_xml/15_notowania.xml'],
    ['USD','/_xml/1_notowania.xml']
  ]
 },
 IND: {
  name: 'Indeksy',
  items: [
    ['WIG','/_xml/WIG.xml'],
    ['WIG20','/_xml/WIG20.xml'],
    ['sWIG80','/_xml/sWIG80.xml'],
    ['mWIG40','/_xml/mWIG40.xml']
  ]
 },
 FUN: {
  name: 'Fundusze',
  groups: {
   AKP: {
    name: 'Akcji polskich',
    items: []
   },
   AKZ: {
    name: 'Akcji zagranicznych',
    items: []
   },
   MIP: {
    name: 'Mieszane (Zrównoważone) polskie',
    items: []
   },
   MIZW: {
    name: 'Mieszane (Zrównoważone) zagraniczne',
    items: []
   },
   SWP: {
    name: 'Stabilnego wzrostu polskie',
    items: []
   },
   PDP: {
    name: 'Polskich papierów dłużnych',
    items: []
   },
   PDU: {
    name: 'Dolarowych papierów dłużnych',
    items: []
   },
   PDE: {
    name: 'Euro papierów dłużnych',
    items: []
   },
   PDUW: {
    name: 'Dolarowych papierów dłużnych (USD)',
    items: []
   },
   PDEW:{
    name: 'Euro papierów dłużnych (EUR)',
    items: []
   },
   RPP:{
    name: 'Polskie, rynku pieniężnego',
    items: []
   },
   XXX:{
    name: 'Pozostałe',
    items: []
   },
   EMP:{
    name: 'Emerytalne',
    items: []
   }
  }
 }
}
comparisionChart = {
/* properties */
 now: null,
 flash: null,
 chartType: null,
 chartPrecision: null,
 dataItems: [],
 timeScope: {
  from: null,
  to: null
 },
 predefinedTimeScope: [
  ['3D', 3],
  ['1M', 30],
  ['3M', 90],
  ['6M', 180],
  ['1R', 365],
  ['2L', 730]
 ],
 predefinedTimeScopeTarget: null,
 dataSourcePickerTarget: null,
 dataSourcePickersCount: 5,
 summaryTarget: null,
 summaryItemsColours: ['#ff0808','#0012ff','#64ad10','#bb07c2','#ff9833'],
 updateButton: null,
 datePicker: {},
 datePickerTarget: {from: null,to: null},
/* methods */
 update: function () {
  this.updateFlash();
 },
 updateFlash: function () {
  var files = '';
  for (var i = 0;this.dataItems.length>i ;i++ )
  {
   if (typeof this.dataItems[i] != "undefined" && this.dataItems[i] != null)
   {
    files = files + this.dataItems[i][1] + ',';
   }
  }
  files = files.substr(0,files.lastIndexOf(','));
  this.flash.k2Multichart_sendProperties(this.timeScope.from, this.timeScope.to, files, this.chartType, this.chartPrecision)
 },
 updateDatePicker: function () {
  $(this.datePickerTarget.from).datepicker('setDate',new Date(this.timeScope.from));
  $(this.datePickerTarget.to).datepicker('setDate',new Date(this.timeScope.to));
 },
 getLastWorkingDay: function (d) {
  var d = new Date(d);
  if (d.getDay() == 6) {
   d.setDate(d.getDate()-1);
  } else if (d.getDay() == 0) {
   d.setDate(d.getDate()-2);
  } else if (d.getDay() == 1) {
   d.setDate(d.getDate()-3);
  } else {
   d.setDate(d.getDate()-1)
  }
  return d;
 },
 parseCalendarDate: function (s) {
  var result;
  var d = new Date(s);
  var day   = (d.getDate() <10)? "0" + d.getDate() : d.getDate();
  var month = ((d.getMonth()+1) <10)? "0" + (d.getMonth()+1) : d.getMonth()+1;
  var year = d.getFullYear();
  result = day + '/' + month + '/' + year;
  return result;
 },
 getPredefinedTimeScope: function (days) {
  var start = new Date(this.lastWorkingDay);
  var end = new Date(this.lastWorkingDay);
  start.setDate(end.getDate()-days);
  return {from:start,to:end}
 },
 setTimeScope: function (dates) {
  if (typeof dates.from != "undefined" && typeof dates.to != "undefined") {
    if (new Date(dates.from) >= new Date(dates.to)) {
    alert('Proszę wybrać poprawny zakres dat.');
    return false;
    } else {
    this.timeScope.from = new Date(dates.from);
    this.timeScope.to = new Date(dates.to);
    }
  } else if (typeof dates.from != "undefined") {
   if (new Date(dates.from) >= new Date(this.timeScope.to))
   {
    alert('Proszę wybrać poprawny zakres dat.');
    return false;
   } else {
    this.timeScope.from = new Date(dates.from);
   }
  } else if (typeof dates.to != "undefined") {
   if (new Date(dates.to) <= new Date(this.timeScope.from))
   {
    alert('Proszę wybrać poprawny zakres dat.');
    return false;
   } else {
    this.timeScope.to = new Date(dates.to);
   }
  }
  this.renderTimeScope();
  return true;
 },
 getAjaxFundItems: function (groupId, groupName) {
  $('html').css({cursor:'progress'})
  $.getJSON('/notowania/'+groupName+'/fundusze.php',function (items) {
   var result = [];
   for (var i = 0;items.length > i;i++ )
   {
    result[i] = [items[i]['f_nazwa'], '/_xml/'+items[i]['f_id']+'_fundusze.xml'];
   }
   quotationSources['FUN'].groups[groupName].items = result;
   var selection = document.getElementById(groupId).getElementsByTagName('select')[1];
   comparisionChart.renderDataSourcePickerLevel3(selection)
   $('html').css({cursor:'default'})
  })
 },
 setDataItem: function (i, item) {
  this.dataItems[i] = item;
  this.renderQuotationSummary();
 },
 removeDataItem: function (i) {
  if (typeof this.dataItems[i] != "undefined")
  {
   this.dataItems[i] = null;
  }
  this.renderQuotationSummary();
 },
 clearPredefinedTimeScopeSelection: function() {
  $(this.predefinedTimeScopeTarget).find('input[type=radio]').removeAttr('checked');
 },
 bindUpdateButton: function () {
  $(this.updateButton).click(function () {
   comparisionChart.update();
   return false;
  })
 },
 bindDatePickers: function () {
  var minDate = new Date();
   minDate.setFullYear(minDate.getFullYear()-20);
  var datePickerSettings = options.datePicker;
  datePickerSettings.dateFormat = 'yy/m/d';
  datePickerSettings.mandatory = true;

$(this.datePicker.from).datepicker(datePickerSettings);
  $(this.datePicker.to).datepicker(datePickerSettings);
  $(this.datePicker.from).datepicker("change",{
   onSelect: function (date,input) {
    comparisionChart.clearPredefinedTimeScopeSelection();
    return comparisionChart.setTimeScope({from:date});
   },
    beforeShow: function (input) {
  $(input).datepicker('setDate',comparisionChart.timeScope.from)
   }
  });
  $(this.datePicker.to).datepicker("change",{
   onSelect: function (date) {
    comparisionChart.clearPredefinedTimeScopeSelection();
    return comparisionChart.setTimeScope({to:date});
   },
    beforeShow: function (input) {
  $(input).datepicker('setDate',comparisionChart.timeScope.to)
   }
  });
 },
/* render stuff */
 ini: function(options) {
  this.lastWorkingDay = this.getLastWorkingDay(new Date());
  
  if (typeof options.timeScope != "undefined") {
   this.timeScope.from = options.timeScope.from;
   this.timeScope.to = options.timeScope.to;
  } else {
   this.timeScope.from = new Date(this.lastWorkingDay);
   this.timeScope.from.setMonth(this.timeScope.from.getMonth()-1)
   this.timeScope.to = new Date(this.lastWorkingDay);
  }
  this.updateButton = options.updateButton;
  this.datePicker.from = options.datePicker.from;
  this.datePicker.to = options.datePicker.to;
  this.datePickerTarget.from = options.datePickerTarget.from;
  this.datePickerTarget.to = options.datePickerTarget.to;
  this.dataSourcePickerTarget = options.dataSourcePickerTarget;
  this.predefinedTimeScopeTarget = options.predefinedTimeScopeTarget;
  this.summaryTarget = options.summaryTarget;
  this.chartType = options.chartType;
  this.chartPrecision = options.chartPrecision;
  this.renderDataSourcePickerLevel1();
  this.renderPredefinedTimeScope();
  this.renderTimeScope();
  this.bindUpdateButton();
  this.bindDatePickers();
  if(ix == 1){
 reBindSelect('oh');
 ix++;
  }
  $('#quotation-source-pickers').find('.selectGroup:not(:first)').hide();
 },
 renderDataSourcePickerLevel1: function() {
  for (var i = 0;this.dataSourcePickersCount > i ;i++ )
  {
   var g = document.createElement('div');
   g.className = 'selectGroup';
   g.id = 'sg'+i;
   var s = document.createElement('select');
   s.name = 'quotation_group_'+i;
   s.id = s.name;
   s.onchange = function () {
    comparisionChart.removeDataItem(this.parentNode.id.substr(2));
    comparisionChart.renderDataSourcePickerLevel2(this);
   }
   var o = new Option('--wybierz--','-1');
   try {
    s.add(o,null);
   } catch (ex) {
    s.add(o);
   }
   for (group in quotationSources)
   {
    var o = new Option(quotationSources[group].name,group)
    try {
     s.add(o,null);
    } catch (ex) {
     s.add(o);
    }
   }
   var c = '<span style="background-color:'+this.summaryItemsColours[i]+'">&nbsp;</span>';
   g.innerHTML = c;
   g.appendChild(s);
   this.dataSourcePickerTarget.append(g)
  }
 },
 renderDataSourcePickerLevel2: function (selection) {
  var group = $(selection.parentNode);
  group.find('select + select').remove();
  var groupAbbr = selection[selection.selectedIndex].value;
  if (groupAbbr == "-1") return;
  if (typeof quotationSources[groupAbbr].items != "undefined")
  {
   group.append(this.renderDataSourcePickerLevel2Items(quotationSources[groupAbbr].items))
  } else if (typeof quotationSources[groupAbbr].groups != "undefined")
  {
   group.append(this.renderDataSourcePickerLevel2Groups(quotationSources[groupAbbr].groups))
  } else {
   // bug?
  }
 },
 renderDataSourcePickerLevel2Groups: function (source) {
  var s = document.createElement('select');
  s.name = 'quotation_subgroup_'+String(Math.random()).substr(2);
  s.id = s.name;
  s.onchange = function () {
   comparisionChart.removeDataItem(this.parentNode.id.substr(2));
   comparisionChart.renderDataSourcePickerLevel3(this);
  }
  var o = new Option('--wybierz--','-1');
  try {
   s.add(o,null);
  } catch (ex) {
   s.add(o);
  }
  for (group in source)
  {
   var o = new Option(source[group].name,group)
   try {
    s.add(o,null);
   } catch (ex) {
    s.add(o);
   }
  }
  return s;
 },
 renderDataSourcePickerLevel2Items: function (source) {
  var s = document.createElement('select');
  s.name = 'quotation_subgroup_'+String(Math.random()).substr(2);
  s.id = s.name;
  s.onchange = function () {
   var sg = this.parentNode.id;
   sg = sg.substr(2);
   if (this[this.selectedIndex].value == "-1")
   {
    comparisionChart.removeDataItem(sg)
   } else {
    comparisionChart.setDataItem(sg,[this[this.selectedIndex].text,this[this.selectedIndex].value])
   }
  }
  var o = new Option('--wybierz--','-1');
  try {
   s.add(o,null);
  } catch (ex) {
   s.add(o);
  }
  for (var i = 0;source.length > i; i++)
  {
   var o = new Option(source[i][0],source[i][1])
   try {
    s.add(o,null);
   } catch (ex) {
    s.add(o);
   }
  }
  return s;
 },
 renderDataSourcePickerLevel3: function (selection) {
  var group = $(selection.parentNode);
  group.find('select:eq(2)').remove();
  var groupAbbr = selection[selection.selectedIndex].value;
  
  if (groupAbbr == "-1") return;
  for (parentGroup in quotationSources)
  {
   if (parentGroup == "FUN" && typeof quotationSources[parentGroup].groups != "undefined")
   {
    if (quotationSources[parentGroup].groups[groupAbbr].items.length == 0)
    {
     this.getAjaxFundItems(group.attr('id'),groupAbbr);
    } else {
     group.append(this.renderDataSourcePickerLevel3Items(quotationSources[parentGroup].groups[groupAbbr].items));
    }
   } else if (typeof quotationSources[parentGroup].groups != "undefined" && 
    typeof quotationSources[parentGroup].groups[groupAbbr] != "undefined" && 
    quotationSources[parentGroup].groups[groupAbbr].items.length > 0)
   {
    group.append(this.renderDataSourcePickerLevel3Items(quotationSources[parentGroup].groups[groupAbbr].items))
    return
   }
  }
 },
 renderDataSourcePickerLevel3Items: function (source) {
  return this.renderDataSourcePickerLevel2Items(source);
 },
 renderPredefinedTimeScope: function() {
  for (var i = 0;this.predefinedTimeScope.length>i;i++ )
  {
   // IE vff
   if (navigator.appName.match(/Microsoft.*Internet.*Explorer/))
   {
    var ch = '';
    if (this.predefinedTimeScope[i][0] == '1M') ch = 'checked="checked"';
    var r = document.createElement('<input type="radio" name="predefinedTimeScope" value="'+this.predefinedTimeScope[i][1]+'" id="pts_'+this.predefinedTimeScope[i][0]+'" '+ch+'/>');
   } else {
    var r = document.createElement('input');
    r.type = 'radio';
    r.name = 'predefinedTimeScope';
    r.value = this.predefinedTimeScope[i][1];
    r.id = 'pts_' + this.predefinedTimeScope[i][0];
    if (this.predefinedTimeScope[i][0] == '1M') r.checked = 'checked';
   }
   r.onchange = function () {
    var ts = comparisionChart.getPredefinedTimeScope(this.value)
    comparisionChart.setTimeScope(ts);
   }
   var l = document.createElement('label');
   l.htmlFor = r.id;
   l.innerHTML = this.predefinedTimeScope[i][0];
   this.predefinedTimeScopeTarget.append(r)
   this.predefinedTimeScopeTarget.append(l)
  }
 },
 renderQuotationSummary: function () {
  $(this.summaryTarget).empty();
  for (var i = 0;this.dataItems.length>i ;i++ )
  {
   if (typeof this.dataItems[i] != "undefined" && this.dataItems[i] != null)
   {
    var si = '<div class="summaryItem"><p>'
    si = si + '<span style="background-color:'+this.summaryItemsColours[i]+'">&nbsp;</span>';
    si = si + this.dataItems[i][0] + '</p></div>';
    $(this.summaryTarget).append(si);
   }
  }
 },
 renderTimeScope: function () {
  var from = $(this.datePickerTarget.from);
  var to = $(this.datePickerTarget.to);
  from.html(this.parseCalendarDate(this.timeScope.from))
  to.html(this.parseCalendarDate(this.timeScope.to))
   
  this.updateDatePicker();
 }
}
function k2Multichart_chartIsReady(name) {
    if(typeof(initQuotationComparision) != 'undefined') {
         comparisionChart.flash = document.getElementById(name);
         var s = window.setTimeout("initQuotationComparision()",100);
         return true;
    }
}
function k2Multichart_sendProperties(firstDate, secondDate, charts , type , coma) {
 comparisionChart.flash.k2Multichart_sendProperties(firstDate, secondDate, charts , type , coma);
}
function k2Multichart_newDates(firstDate, secondDate) {
 first = new Date(firstDate);
 second = new Date(secondDate);
}
function reBindSelect(activatedObject){
 var test = activatedObject;
 fixInterval = setInterval(function(){ 
  clearInterval(fixInterval); 
   if(typeof test == 'object'){
    $(test).siblings('select').change(function(){
     if($(this).next('select').size() > 0) {
      var obj = $(this);
      reBindSelect(obj);
     }
     else {
      $(this).parents('.selectGroup:first').next().show();
      return false;
     }
    });
   } else {
    $('#quotation-source-pickers select').change(function(){
     if($(this).next('select').size() > 0) {
      var obj = $(this);
      reBindSelect(obj);
     }
     else {
      $(this).parents('.selectGroup:first').next().show();
      return false;
     }
    });
   }
 },201);
}





$(document).ready(function () {
$('div.content.tabs ul.navi a').each(function (i) {
 var i = i;
 $(this).click(function () {
  if ($(this).parent().hasClass('active')) return false
  $(this).parent().siblings().removeClass('active')
  $(this).parent().addClass('active');
  switch (i) {
   case 0:
    comparisionChart.chartPrecision = 1;
    comparisionChart.chartType = 'returnRate';
    break;
   case 1:
    comparisionChart.chartPrecision = 2;
    comparisionChart.chartType = 'value';
    break;
   default:
    break;
  }
  if (comparisionChart.dataItems.length > 0)
    comparisionChart.update();
  return false
 })
})
})
//-->

