var counter = {
  timer: null,
  eday: new Date("September 16, 2010 09:30:00"),
  edayEnd: new Date("September 16, 2010 23:59:59"),
  eday2011: new Date("September 15, 2011 00:00:00"),
  msPerDay: 24 * 60 * 60 * 1000,
  init: function() {
    counter.draw();

    counter.timer = setInterval(function(){
      counter.draw();
    }, 1000);
  },
  draw: function() {

    var time = new Date();
    var timeLeft = counter.eday.getTime() - time.getTime();
    var counting = false;
    if(timeLeft < 0){
      counting = true;
      timeLeft = time.getTime() - counter.eday.getTime();
    }
    if(timeLeft < 0){
      timeLeft = counter.eday2011.getTime() - time.getTime();
    }

    var tempDaysLeft = timeLeft / counter.msPerDay;
    var daysLeft = Math.floor(tempDaysLeft);

    var tempHoursLeft = (tempDaysLeft - daysLeft)*24;
    var hoursLeft = Math.floor(tempHoursLeft);
    if(hoursLeft < 10) hoursLeft = '0' + hoursLeft;

    var tempMinutesLeft = (tempHoursLeft - hoursLeft)*60;
    var minutesLeft = Math.floor(tempMinutesLeft);
    if(minutesLeft < 10) minutesLeft = '0' + minutesLeft;

    var tempSecondsLeft = (tempMinutesLeft - minutesLeft)*60;
    var secondsLeft = Math.floor(tempSecondsLeft);
    if(secondsLeft < 10) secondsLeft = '0' + secondsLeft;

    var text = (daysLeft > 0 ? daysLeft + ' <span>dag' + (daysLeft != 1 ? 'en' : '') + '</span> ' : '') + (hoursLeft > 0 ? hoursLeft + ' <span>uur</span> ' : '') + (minutesLeft > 0 ? minutesLeft + ' <span>' + (minutesLeft == 1 ? 'minuut' : 'minuten') + '</span> ' : '') + secondsLeft + ' <span>seconden</span> ' + (counting ? ' and counting' : 'te gaan');

    $('#counter').html(text);
  }
}

var spotlight = {
  current: 0,
  interval: null,

  init: function(){
    $('a', '#spotlight-pagination').live('click', function(e){
      e.preventDefault();
      clearInterval(spotlight.interval);
      $('#spotlight-holder').load($(this).attr('href'));
    });

    spotlight.interval = setInterval(function(){
      spotlight.current++;
      if(spotlight.current > $('ul a', '#spotlight-holder').length - 1){
        spotlight.current = 0;
      }
      $('#spotlight-holder').load($('a:eq(' + spotlight.current + ')', '#spotlight-pagination').attr('href'));
    }, 5000);
  }
}

var speakers = {
  init: function(){
    $('a', '#speakers-pagination').live('click', function(e){
      e.preventDefault();
      $('#speakers-holder').load($(this).attr('href'), function(){
        if($.fn.fhEllipses){
          $('#speakers-list .description').fhEllipses({
            lines: 3
          });
        }
        if($.fn.fhHover){
          $('#speakers-list li').fhHover();
        }
        $('a[rel*=external]', '#speakers-list').live('click', function(){
          $(this).attr('target', '_blank');
        });
      });
    });
  }
}

var getYourTickets = {
  info: null,
  speed: 500,

  init: function() {
    getYourTickets.info = $('#get-your-tickets-drag');
    getYourTickets.setDraggable();
    $('#get-your-tickets a').live('click', function(e){
      window.location = $(this).attr('href');
      e.preventDefault();
    });
  },

  setDraggable: function() {
    $('#get-your-tickets')
      .hover(function(){
        getYourTickets.info.stop().fadeTo('fast', 1);
      }, function(){
        getYourTickets.info.stop().fadeTo('fast', 0);
      })
      .draggable({
        axis: 'y',
        containment: [300, -215, 300, -145],
        start: function(){
          $(this).stop();
        },
        stop: function(event, ui){
          if($(this).position().top < -145){
            $(this).animate({
              'top': -215
            }, 300);
          }else{
            getYourTickets.info.stop().fadeTo('fast', 0);
            $(this).draggable('destroy');

            var ticket = $(this).find('a:last-child');
            var self = $(this);

            self
              .find('a:first-child')
              .after(ticket.clone())
              .end()
              .css({
                'top': self.position().top - 135
              })
              .animate({
                'top': -215
              }, getYourTickets.speed);

            ticket
              .animate({
                'top': 500,
                'opacity': 0
              }, getYourTickets.speed * 1.5, function(){
                window.location = $(this).attr('href');

                $(this).remove();
                getYourTickets.setDraggable();
              });
          }
        }
      });
  }
};

var myEday = {
  init: function(){
    $('#my-eday-preview li a.remove').live('click', function(e){
      e.preventDefault();

      var answer = confirm('Weet u zeker dat u dit programma uit uw My Eday wilt verwijderen?');

      if(answer){
        var link = $(this);
        var textHolder = link.next();
        var text = textHolder.text();
        var newText = '';
        var i = 0;

        $.ajax({
          url: link.attr('href')
        });

        link.data('interval', setInterval(function(){
          if(i < text.length){
            newText+= text.substr(i, 2);
            i+= 2;
            textHolder.html('<span>' + newText + '</span>' + text.substr(i, text.length - 1));
          }else{
            clearInterval(link.data('interval'));
            link.parent().slideUp();
          };
        }, 50));
      };
    });
  }
};

var program = {
  init: function(){

    $('[rel=ajax].remove-program, [rel=ajax].add-program')
      .live('click', function(e){
        var page = window.location;
        e.preventDefault();

        $.ajax({
          url: $(this).attr('href'),
          success: function(response){
            $('#timetable-holder').html(response);
            $('#timetable .presentation').fhHover();
            $('#popup-message').achtung({timeout:4});
          }
        });
      });

    $('#more-sessions, #second-outdoor-display').click(function(e){
      $('#outdoor-display').trigger('click');
      $('#more-sessions, #second-outdoor-display').fadeOut('slow', function(e){
        $(this).remove();
      });
    });

    $('#timetable-navigation a').bind('click', function(e){
      e.preventDefault();

      $('#timetable-navigation a').each(function()
      {
        var title = $(this).attr('title');

        $(this)
          .removeClass('active')
          .attr('title', $(this).text())
          .html(title);
      });
      $(this).addClass('active');

      var room = $(this).attr('id').substr(0, $(this).attr('id').length - 8);

      $('#rooms .scrollable')
        .fadeOut()
        .filter(function(){
          return ($(this).attr('id') == room + '-rooms');
        })
        .fadeIn();
    });

    this.setTips();
  },

  setTips: function(){
    $('.presentation', '#timetable').each(function(index){
      var link = $(this).find('[rel=fhhLocation]');
      if(link.length){
        $(this).qtip({
          content: {
            url: link.attr('href')
          },
          show: {
            delay: 500
          },
          hide: {
            fixed: true,
            delay: 300
          },
          style: {
            width: 300,
            padding: 5,
            border: {
              width: 7,
              radius: 5
            },
            tip: 'leftMiddle',
            name: 'light' // Inherit from preset style
          },
          position: {
            corner: {
              target: 'rightMiddle',
              tooltip: 'leftMiddle'
            }
          }
        });
      };
    });
  }
};

$(function(){
  counter.init();
  spotlight.init();
  getYourTickets.init();
  myEday.init();
  program.init();
  speakers.init();

  if($.fn.fhEllipses){
    $('#speakers-list .description').fhEllipses({
      lines: 3
    });
    $('#news-list .description').fhEllipses({
      lines: 3,
      readMoreElement: {
        className: '.read-more'
      }
    });
  }

  if($.fn.fhHover){
    $('#timetable .presentation, #spotlight, #news-list li, #speakers-list li').fhHover();
  }

  if($.fn.gzGallery){
    $('#partners, #playgrounders, #silver-sponsors, #bronze-sponsors').gzGallery({
      method: 'slide',
      counter: false
    });
  }

  $('input, select, textarea').gzPrettyForms();
  $('a[rel*=external]').live('click', function(){
    $(this).attr('target', '_blank');
  });

});