var ocn = {};

function ocn_Ocena(_element, _obrazekOcena, _obrazekTlo, _ocena, _urlZapisz)
{
   var remoting = new Remoting();
   var _numer = null;
   
   function pobierzOcene(element, ocena)
   {
      var wartosc = null;
      if (isNaN(wartosc = parseFloat(element)) && isNaN(wartosc = parseFloat(element.getAttribute('alt'))) && isNaN(wartosc = parseFloat(element.getAttribute('value'))))
      {
         if (typeof ocena == 'number')
         {
            wartosc = ocena;
         }
         else
         {
            with (_element.getElementsByTagName(_element.nodeName.toLowerCase() == 'select' ? 'option' : 'img'))
            {
               for (var i = 0; i < length; i++)
               {
                  if (item(i) == element)
                  {
                     wartosc = i + 1;
                  }
               }
            }
         } 
      }
      return wartosc;
   }
   
   this.zaznacz = function(element)
   {
      if (!this.disabled)
      {
         if (typeof element == 'undefined')
         {
            element = _ocena;
         }
         else if (typeof element != 'number')
         {
            element = pobierzOcene(element);
         }
         if (element != null || _element.nodeName.toLowerCase() != 'select')
         {
            with (_element.getElementsByTagName(_element.nodeName.toLowerCase() == 'select' ? 'option' : 'img'))
            {
               if (length)
               {
                  var zakres = pobierzOcene(item(length - 1), length) - pobierzOcene(item(0), 1);
                  if (zakres)
                  {
                     if (element != null)
                     {
                        var w = (element - pobierzOcene(item(0), 1)) / zakres;
                     }
                     for (var i = 0; i < length; i++)
                     {
                        if (item(i).nodeName.toLowerCase() == 'option')
                        {
                           item(i).selected = w >= (i - 0.5) / (length - 1);
                        }
                        else
                        {
                           var o = pobierzOcene(item(i), i + 1);
                           if (element == null)
                           {
                              var obr = false;
                           }
                           else
                           {
                              if (element <= -0.5)
                              {
                                 var obr = w <= (i + 0.5) / (length - 1) && o < 0.5;
                              }
                              else
                              {
                                 var obr = w >= (i - 0.5) / (length - 1) && o > -0.5;
                              }
                           }
                           if (obr)
                           {
                              item(i).className += ' ocn__ ocn__' + o;
                              
                              obr = _obrazekOcena;
                           }
                           else
                           {
                              item(i).className = item(i).className.replace(/(^|\s+)ocn__(\s+|$)/g, ' ').replace(/(^|\s+)ocn__\d+(\s+|$)/g, ' ');
                              obr = _obrazekTlo;
                           }
                           item(i).src = typeof obr == 'object' ? obr[i] : obr;
                        }
                     }
                  }
               }
               if (typeof this.onselect == 'function')
               {
                  this.onselect(this, element);
               }
            }
         }
      }
   }
   
   this.ustaw = function(ocena)
   {
      if (!this.disabled)
      {
         if (typeof ocena == 'number')
         {
            var wartosc = ocena;
            if (isNaN(wartosc = parseFloat(wartosc)))
            {
               return;
            }
            _ocena = wartosc;
            _numer = null;
         }
         else
         {
            var wartosc = pobierzOcene(ocena);
            if (wartosc != null)
            {
               _ocena = wartosc;
               _numer = null;
            }
            else
            {
               with (_element.getElementsByTagName(_element.nodeName.toLowerCase() == 'select' ? 'option' : 'img'))
               {
                  for (var i = 0, img = _obrazekOcena; i < length; i++)
                  {
                     if (item(i) == ocena)
                     {
                        _ocena = i + 1;
                        _numer = null;
                        break;
                     }
                  }
                  if (i >= length)
                  {
                     return;
                  }
               }
            }
         }
         this.zaznacz();
         if (typeof this.onchange == 'function')
         {
            this.onchange(this);
         }
      }
   }
   
   this.pobierz = function()
   {
      return _ocena;
   }
   
   this.pobierzNumer = function(ocena)
   {
      if (typeof ocena == 'undefined')
      {
         if (_numer != null)
         {
            return _numer;
         }
         var wartosc = _ocena;
      }
      else
      {
         var wartosc = ocena;
      }
      if (typeof wartosc == 'number')
      {
         with (_element.getElementsByTagName(_element.nodeName.toLowerCase() == 'select' ? 'option' : 'img'))
         {
            var zakres = pobierzOcene(item(length - 1), length) - pobierzOcene(item(0), 1);
            if (zakres)
            {
               var w = (wartosc - pobierzOcene(item(0), 1)) / zakres;
               for (var i = 0; i < length; i++)
               {
                  if (w <= i / (length - 1))
                  {
                     if (typeof ocena == 'undefined')
                     {
                        _numer = i;
                     }
                     return i;
                     break;
                  }
               }
            }
         }
      }
      return null;
   }
   
   this.pobierzElement = function()
   {
      return _element;
   }
   
   this.odczytaj = function(a, t, r, url)
   {
      if (url && typeof this.onload == 'function' && typeof ocn_Ocena.dane == 'undefined')
      {
         ocn_Ocena.dane = new ocn_Dane(
            url,
            function(ocena, parametry)
            {
               parametry['self'].onload(parametry['self'], ocena);
            },
            1,
            true
         );
      }
      ocn_Ocena.dane.odczytaj(a, t, r, {'self': this});
   }
   
   this.zapisz = function(url)
   {
      if (!this.disabled && !isNaN(parseFloat(_ocena)))
      {
         this.disabled = true;
         if (typeof url == 'undefined')
         {
            url = _urlZapisz;
         }
         if (url)
         {
            remoting.send([url, {'ocena': _ocena}]);
         }
      }
   }
   
   this.disabled = false;
   this.onselect = null;
   this.onchange = this.zapisz;
   this.onload = function(ocn_ocena, ocena)
   {
      ocn_ocena.ustaw(ocena);
      ocn_ocena.disabled = true;
   }
}

var ocn_Wynik = (function()
{
   var instance = null;
   function Singleton()
   {
      this.url = null;
      
      var zakresy = {};
      var opisy = {};
      var wyniki = null;
      
      this.odczytaj = function(id, a, t, r, rodzaj, minimum, maksimum, opis)
      {
         if (typeof opis == 'object')
         {
            if (typeof opisy[a] == 'undefined')
            {
               opisy[a] = opis;
            }
         }
         else if (typeof opisy[a] == 'object')
         {
            opis = opisy[a];
         }
         if (typeof minimum == 'number' && typeof maksimum == 'number')
         {
            zakresy[a] = [minimum, maksimum];
         }
         
         if (wyniki == null)
         {
            wyniki = new ocn_Dane(
               this.url,
               function(ocena, parametry)
               {
                  if (document.getElementById('ocn_glosy_' + parametry['id']))
                  {
                     document.getElementById('ocn_glosy_' + parametry['id']).innerHTML = ocena[0];
                  }
                  if (typeof zakresy[a] == 'object')
                  {
                     var wartosc = (zakresy[a][0] + ocena[parametry['rodzaj']]) / (zakresy[a][1] - zakresy[a][0]);
                     if (document.getElementById('ocn_' + parametry['id']))
                     {
                        with (document.getElementById('ocn_' + parametry['id']).getElementsByTagName('img'))
                        {
                           item(0).alt = Math.round(ocena[parametry['rodzaj']] * 100) / 100;
                           if (item(0).width > item(0).height)
                           {
                              item(1).width = Math.round(wartosc * item(0).width);
                           }
                           else
                           {
                              item(1).height = Math.round(wartosc * item(0).height);
                           }
                        }
                     }
                     if (document.getElementById('ocn_opis_' + parametry['id']) && typeof parametry['opis'] == 'object')
                     {
                        document.getElementById('ocn_opis_' + parametry['id']).innerHTML = parametry['opis'][Math.round(wartosc * (parametry['opis'].length - 1))];
                     }
                  }
               },
               0,
               300
            );
         }
         
         wyniki.odczytaj(a, t, r, {'id': id, 'rodzaj': rodzaj, 'opis': opis});
      }
   }
   
   return function()
   {
      if (!instance)
      {
         instance = new Singleton();
      }
      return instance;
   };
})();

function ocn_Dane(url, callback, typ, nocache)
{
   if (typeof typ != 'number')
   {
      typ = 0;
   }
   var wyniki = {};
   
   this.odczytaj = function(a, t, r, parametry)
   {
      ustaw(wyniki, a, t, r, []);
      wyniki[a][t][r][wyniki[a][t][r].length] = parametry;
      if (!istnieje(ocn, a, t, r))
      {
         ustaw(ocn, a, t, r, []);
      }
   }
   
   function istnieje()
   {
      for (var i = 1, obiekt = arguments[0]; i < arguments.length; i++)
      {
         if (typeof obiekt[arguments[i]] == 'undefined')
         {
            return false;
         }
         obiekt = obiekt[arguments[i]];
      }
      return true;
   }
   
   function ustaw()
   {
      for (var i = 1, obiekt = arguments[0]; i < arguments.length - 1; i++)
      {
         var value = i < arguments.length - 2 ? {} : arguments[arguments.length - 1];
         if (typeof obiekt[arguments[i]] == 'undefined')
         {
            obiekt[arguments[i]] = value;
         }
         obiekt = obiekt[arguments[i]];
      }
   }
   
   var loaded = false;
   if (!loaded)
   {
      var remoting = new Remoting();
      
      function load(a, t)
      {
         var csv = '';
         for (var r in wyniki[a][t])
         {
            if (typeof wyniki[a][t][r] != 'function')
            {
               if (typeof ocn[a][t][r][typ] == 'undefined')
               {
                  csv += (csv == '' ? '' : ',') + r;
               }
               else
               {
                  for (var i = 0; i < wyniki[a][t][r].length; i++)
                  {
                     callback(ocn[a][t][r][typ], wyniki[a][t][r][i]);
                  }
               }
            }
         }
         if (csv != '')
         {
            remoting.send(
               [url, {'a': a, 't': t, 'r': csv}],
               function(data)
               {
                  for (var r in data)
                  {
                     if (typeof data[r] != 'function' && typeof wyniki[a][t][r] != 'undefined')
                     {
                        for (var i = 0; i < wyniki[a][t][r].length; i++)
                        {
                           callback(data[r], wyniki[a][t][r][i]);
                        }
                     }
                  }
               },
               nocache
            );
         }
      }
      
      function onload()
      {
         if (typeof callback == 'function')
         {
            for (var a in wyniki)
            {
               if (typeof wyniki[a] != 'function')
               {
                  for (var t in wyniki[a])
                  {
                     if (typeof wyniki[a][t] != 'function')
                     {
                        load(a, t);
                     }
                  }
               }
            }
         }
      }
      
      Remoting.addDOMContentLoadedEventListener(onload);
      loaded = true;
   }
}