// ütf-marker
var base = {
  basePath:  lp.substr(0, lp.lastIndexOf('/')),
  mediaPath: lp.substr(0, lp.lastIndexOf('/')) + '/media',
  userPath:  lp.substr(0, lp.lastIndexOf('/')) + '/user',
  locale:    lp.substr(lp.lastIndexOf('/') + 1, lp.length),
  ajaxSetup: {
    url: lp + '/ajax/',
    type: 'GET',
    dataType: 'html'
  },
  currCnt: 0,
  img: new Image(),
  timeout : null          
};


var drawVisualization = function() {
  if(typeof(answer_data) == 'undefined'){
    return false;
  }
  // Create and populate the data table.
  var rowsArr = [];
  for(answer in answer_data)  {
    rowsArr[answer] = [answer_data[answer].title,  {v:parseInt(answer_data[answer].value), f:answer_data[answer].title}];
  }

  var data = new google.visualization.DataTable();
  data.addColumn('string', 'Answers');
  data.addColumn('number', 'Results');
  data.addRows(rowsArr);

  // Create and draw the visualization.
  new google.visualization.PieChart(document.getElementById('votes-results')).draw(data, {is3D:false, width:360, colors:charts_color, legendFontSize:9, tooltipFontSize:9});

}

if(typeof(google) != 'undefined'){
  google.load('visualization', '1', {packages: ['piechart']});
  google.setOnLoadCallback(drawVisualization);
}


var buildCharts = function(boxElementList){

  if(jQuery(boxElementList).find('.histogram-horizontal').length > 0)
  { 
    var totalAnswers = parseInt(jQuery(boxElementList).find('.total-answers').text());

    jQuery(boxElementList + ' .answer-row').each(function(){
      var pourcentage = (parseInt(jQuery(this).find('.result').text()) / totalAnswers * 100);

      jQuery(this).append('<div class="drawbar-wrapper"></div>');
      jQuery(this).children('.drawbar-wrapper').append('<div class="drawbar"></div>').append(jQuery(this).find('.result'));
      jQuery(this).find('.drawbar').animate({width:pourcentage+'%'},800);

    })
  }
 
}

  //votes
  buildCharts('.votes-box');
  
  
$(window).ready(function() {
  $.ajaxSetup(base.ajaxSetup);
  
  $('span.email').defuscate();
  $('#search').hint();
  
  $('#link-list li, .right-col #event-list li').bigFatLink();
  
  $('.listing .more,.listing .detail-view').click(function() {
    var currLi = $(this).parent('li')
    if(currLi.hasClass('active')) {
      return false;
    }
    $('.listing .message').slideUp(500);
    $('.listing li').removeClass('active');
    currLi.addClass('active').find('div.message').slideDown(500);
    return false;
  })

  
  var jVote = {
    button : $('.vote-sender'),
    input : $('.votes-form input')
  }
  jVote.button.attr('disabled', 'disabled').addClass('disabled');
  jVote.input.click(function() {
    jVote.button.attr('disabled', 'disabled').addClass('disabled');
    jVote.input.each(function() {
      if(this.checked) {
        jVote.button.removeAttr('disabled', 'disabled').removeClass('disabled');
        return;
      }
    })
  });
  
  // slide show  
  base.preloadNextPicture();
  
  // hide archive if empty
  $('body[id!=bd-liens] #second-navi>li, #main-navi ul li').each(function() {
    if(this.className.indexOf('items') == -1) {
      //this.style.display = 'none';
    }
  })


  if(window.location.hash){
    $('#' + window.location.hash.substr(1)).find('.more').trigger('click');
  }
  
});


base.preloadNextPicture = function() {
  if(typeof(slideshowPics) == 'undefined' || slideshowPics.length == 1) {
    return false;
  }
  window.clearTimeout(base.timeout);
  var picPath    = base.userPath + '/topic_pictures/';
  base.img.src   = picPath + slideshowPics[base.currCnt];
  base.currCnt   = base.currCnt == slideshowPics.length -1
                 ? 0
                 : base.currCnt + 1;
  base.img.onload = function() {    
    jQuery('#slide-show').css('background-image','url(' + base.img.src + ')') 
    base.togglePicture();
  }
}

base.togglePicture = function() {
  base.timeout = window.setTimeout (function(){   
    jQuery('#slide-img').fadeOut(1000, function() {
      this.src = base.img.src;
      jQuery('#slide-img').fadeIn(2000, function() {
        base.preloadNextPicture();
      })           
    }); 
  }, 4000);
}
