﻿$(document).ready(function() {
    if ($('#ehwmVotingPuckbox').length > 0 && $('#ehwmVotingPuckbox').is(':visible')) {
        initPuckBox();
    }
  if ($('div.delete-upload').length > 0) {
          $('div.delete-upload').find('.delete-upload').click(function(e) {
              var parent = $(this).parent();
              var input = $(this).siblings('input[type=hidden]').clone();
              var inputName = input.attr('id').replace(/_uploaded/, '');
              input.val('').attr({
                  'type': 'file',
                  'name': inputName
              }).removeAttr('id').removeAttr('value');
              parent.replaceWith(input);
              e.preventDefault();
              return false;
          })
  } 
  
  $('.blink').click(blinkClick);
})

function blinkClick() {
  childId = $(this).attr('rel');
    $.get("/rund-um-persil/eishockey-wm-voting/eishockey-wm-voting-galerie/vote/" + childId, function(data){
      if(data == 'wait') {
        tb_show("", "/rund-um-persil/eishockey-wm-voting/eishockey-wm-voting-popup-warten?KeepThis=true&amp;TB_iframe=true&amp;height=570&amp;width=692&amp;allowtransparency=true&amp;background=transparent&amp;modal=true", false);
      }
      else if(data == 'thanks') {
        removePuk(childId);
        tb_show("", "/rund-um-persil/eishockey-wm-voting/eishockey-wm-voting-popup-danke?KeepThis=true&amp;TB_iframe=true&amp;height=570&amp;width=692&amp;allowtransparency=true&amp;background=transparent&amp;modal=true", false);
      }
      else if(parseInt(data) != null) {
        removePuk(childId);
      }
     });
    return false;
}

function removePuk (childId) {
  var zahlenarr = $('#ehwmVotingPuckbox #rest').text().split(' ');
  var zahl = parseInt(zahlenarr[0]);
  if(zahl <= 0)
    return;
  zahl--; 
  zahlenarr[0] = zahl;
  if(zahl == 1) {
    zahlenarr[1] = 'Puck';
  } else {
    zahlenarr[1] = 'Pucks';
  }
  $('#ehwmVotingPuckbox #rest').text(zahlenarr.join(' '));  
  
  $('#ehwmVotingPuckbox ul li.active:last').removeClass('active');
  
  addPoint(childId);
}

function addPoint(childId) {
  $('.clink').css('display', 'none');
  $('.blink').css({
    'display': 'block',
    'height': 25,
    'marginTop': 4,
    'paddingTop': 4
  });
  
    
    boxSelector = '#blink' + childId;
    var punktearr = $(boxSelector).text().split(' ');
    var punkte = parseInt(punktearr[0]);
    punkte++; 
    punktearr[0] = punkte;
    if(punkte == 1) {
      punktearr[1] = 'Puck';
    } else {
      punktearr[1] = 'Pucks';
    }
    
    $(boxSelector).text(punktearr.join(' '));
 
}

function showBox(numberOfActive) {
  var zahlenarr = $('#ehwmVotingPuckbox #rest').text().split(' ');
  zahlenarr[0] = numberOfActive;
  $('#ehwmVotingPuckbox #rest').text(zahlenarr.join(' '));
  $('#ehwmVotingPuckbox').css('display', 'block');
  $('#ehwmVotingPuckbox ul li').each(function() {
    if(numberOfActive > 0) {
      $(this).addClass('active');  
    }      
    numberOfActive--;
  })
  initPuckBox();
}

var puckBox = {
    $e: null,
    maxTop: 0,
    _o: 0
}
function initPuckBox() {
    puckBox.$e = $('#ehwmVotingPuckbox');
    puckBox.maxTop = puckBox.$e.offset().top;
    puckBox._o = puckBox.maxTop + puckBox.$e.height() + 70 - $(window).height();
    if (puckBox._o < 0) puckBox._o = 0;
    movePuckBox();
    $(window).bind('scroll', movePuckBox).bind('resize', function(){
        puckBox._o = puckBox.maxTop + puckBox.$e.height() + 70 - $(window).height();
        if (puckBox._o < 0) puckBox._o = 0;
        movePuckBox();
    });
}
function movePuckBox() {
    var _h = puckBox.maxTop + $(window).scrollTop() - puckBox._o;
    if (puckBox.maxTop + $(window).scrollTop() - puckBox._o < puckBox.maxTop)
        _h = puckBox.maxTop;
    puckBox.$e.css({
        top: _h
    });
}
