function anketa(anketa_id, enabled, flip)
{ 
  if (enabled == undefined) { enabled = true; }
  
  if (flip == undefined) { flip = false; }
  
  this.ajaxVoteURL = "http://www.kinobox.cz/anketa_ajax/vote";
  //this.ajaxVoteURL = "/return.xml";
  
  this.clickableClass = "clickable";
  
  this.optionClass = "option";
  this.resultValueClass = "result_value";
  this.totalVotesClass = "total_votes";
  this.flipperClass = "flipper";
  this.optionsSectionClass = "options";
  this.resultsSectionClass = "results";
  this.indicatorClass = "indicator";
  this.full_percent = 200;
  
  this.anketaElement = null;
  
  this.optionElements = [];
  
  this.totalVotesElement = null;
  
  this.resultValueElements = [];
  
  this.optionsSectionElement = null;
  
  this.resultsSectionElement = null;
  
  this.flip = flip;
  
  this.enabled = enabled;
  
  anketa.prototype.initialize = function() { 
    this.anketaElement = document.getElementById(anketa_id); 
    this.optionElements = this.getDescendantsByClass(this.optionClass); 
    this.initializeOptions();
    this.resultValueElements = this.getResultValueElements();
    this.indicatorElements = this.getIndicatorElements();
    var totalVotesElements = this.getDescendantsByClass(this.totalVotesClass);
    this.totalVotesElement = totalVotesElements[0];
    if(this.flip) {
      this.flipperElements = this.getDescendantsByClass(this.flipperClass);
      this.initializeFlippers();
      var optionsSectionElements = this.getDescendantsByClass(this.optionsSectionClass);
      this.optionsSectionElement = optionsSectionElements[0];
      var resultsSectionElements = this.getDescendantsByClass(this.resultsSectionClass);
      this.resultsSectionElement = resultsSectionElements[0];
    }
  }
  
  anketa.prototype.getDescendantsByClass = function(class_name) {
    if(this.anketaElement == null) return false;
    var elements = [];
    var reg = new RegExp('\\b'+class_name+'\\b');
    var all = this.anketaElement.getElementsByTagName('*');
    for (var i = 0; i < all.length; i++) {
      if (reg.test(all[i].className)) elements.push(all[i]);
    }
    return elements;
  }
  
  anketa.prototype.initializeOptions = function() { 
    for (var i = 0; i < this.optionElements.length; i++) { 
      var value = this.getOptionValue(this.optionElements[i]); 
      this.addEventListener(this.optionElements[i], "click", this.createVoteCallback(value), this);
    }
  }
  
  anketa.prototype.initializeFlippers = function() {
    for (var i = 0; i < this.flipperElements.length; i++) {
      this.addEventListener(this.flipperElements[i], "click", this.flipDisplay, this);
    }
  }
  
  anketa.prototype.getResultValueElements = function() {
    var resultValueElements = [];
    var r=new RegExp('\\b'+this.resultValueClass+'\\b');
    for(var i in this.optionElements) {
      if(this.optionElements.hasOwnProperty(i)) {
        var value = this.getOptionValue(this.optionElements[i]);
        var a=this.optionElements[i].getElementsByTagName('*');
        for(var i=0;i<a.length;i++){ 
          if(r.test(a[i].className)){
            resultValueElements[value] = a[i];
          }
        }
      }
    }
    return resultValueElements;
  }
  
  anketa.prototype.getIndicatorElements = function() {
    var indicatorElements = [];
    var r=new RegExp('\\b'+this.indicatorClass+'\\b');
    for(var i in this.optionElements) {
      if(this.optionElements.hasOwnProperty(i)) {
        var value = this.getOptionValue(this.optionElements[i]);
        var a=this.optionElements[i].getElementsByTagName('*');
        for(var i=0;i<a.length;i++){ 
          if(r.test(a[i].className)){
            indicatorElements[value] = a[i];
          }
        }
      }
    }
    return indicatorElements;
  }
  
  anketa.prototype.createVoteCallback = function(value) {
    var anketaObj = this;
    return function () {
      if(!anketaObj.enabled) return false;
      anketaObj.vote(value);
    };
  }

  anketa.prototype.getOptionValue = function(optionElement) {  
    if(optionElement.getAttribute('value')) { return optionElement.value; } 
    var all = optionElement.getElementsByTagName('*');
    for (var i = 0; i < all.length; i++) {
      if(all[i].getAttribute('value')) return all[i].value;
    }
    return null;
  }

  anketa.prototype.vote = function(value) { 
    var ajaxRequest = new ajaxObject(this.ajaxVoteURL);
    var anketaObj = this;
    ajaxRequest.callback = function(responseText, responseStatus, responseXML) {
      anketaObj.processResponse(responseText, responseStatus, responseXML);
    }
    ajaxRequest.update('option_id='+encodeURIComponent(value),'GET');
  }
  
  anketa.prototype.processResponse = function(responseText, responseStatus, responseXML) {
    if (responseStatus==200)
    {
      var xmldoc = responseXML;
      
      var root = xmldoc.getElementsByTagName("root").item(0);
      
      var results = root.getElementsByTagName('result');
      
      var total_votes = root.getElementsByTagName("total_votes").item(0).firstChild.nodeValue;
      
      this.updateTotalVotes(total_votes);
        
      var results_array = [];    
      for (var i = 0; i < results.length; i++) {
        results_array[results[i].getAttribute('option_id')] = results[i].firstChild.nodeValue;  
      }
      this.updateResultsFromArray(results_array, total_votes);
      
      var disable_voting = root.getElementsByTagName("disable_voting").item(0).firstChild.nodeValue;
      if(disable_voting == 1) {
        this.disableVoting();
      }
      if(this.flip) {
        this.flipDisplay();
      }
    }
  }

  anketa.prototype.flipDisplay = function() {
    this.optionsSectionElement.style.display = (this.optionsSectionElement.style.display != 'none' ? 'none' : '' );
    this.resultsSectionElement.style.display = (this.resultsSectionElement.style.display != 'none' ? 'none' : '' );
  }

  anketa.prototype.disableVoting = function() {
    this.enabled = false;
    for (var i = 0; i < this.optionElements.length; i++) {
      this.optionElements[i].className = this.optionElements[i].className.replace( new RegExp('(^|\\s)' + this.clickableClass + '(\\s|$)') ,' ');
    }
  }
  
  anketa.prototype.addEventListener = function(obj,evt,fnc,scope,useCapture) {
    if (!useCapture) useCapture=false;
    var scoped = scope ? function(e) { fnc.apply(scope, [e]); } : fnc;
  	if (obj.addEventListener){ 
  		obj.addEventListener(evt,scoped,useCapture);
  		return true;
  	} else if (obj.attachEvent) {
      return obj.attachEvent("on"+evt,scoped);
    }
  }
  
  anketa.prototype.set100Percent = function(value) {
    this.full_percent = value;
  }
  
  anketa.prototype.valueToIndicatorSize = function(value, full_value) {
    return parseInt(this.full_percent/(full_value/value));
  }
  
  anketa.prototype.updateResultsFromArray = function(array, total_votes) {
    for(var i in array) {
      if(array.hasOwnProperty(i)) {
        if(this.resultValueElements[i]) {
          while (this.resultValueElements[i].childNodes.length > 0 ) {
            this.resultValueElements[i].removeChild(this.resultValueElements[i].firstChild );       
          }
          this.resultValueElements[i].appendChild(document.createTextNode(array[i]==undefined?0:array[i])); 
        }
        var indicator_size = this.valueToIndicatorSize(array[i], total_votes);
        if(this.indicatorElements[i]) {
          this.indicatorElements[i].style.backgroundPosition = "-" + (this.full_percent-indicator_size) + "px top"; 
        }
      }
    }
  }
  
  anketa.prototype.updateTotalVotes = function(value) {
    if(this.totalVotesElement)
    {
      if(this.totalVotesElement.childNodes) {
        while (this.totalVotesElement.childNodes.length > 0 ) {
          this.totalVotesElement.removeChild(this.totalVotesElement.firstChild);       
        }
      }
      this.totalVotesElement.appendChild(document.createTextNode(value));
    }
  }
  
  this.initialize();
  
}