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

      var answer = confirm('Are you sure you wish do delete this program?');

      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){
        e.preventDefault();

        $.ajax({
          url: $(this).attr('href'),
          success: function(response){
            $('#popup-message').achtung({
              timeout: 4
            });
          }
        });
      });
    
    $('.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: 200,
            background: '#e5f4f9',
            padding: 10,
            border: {
              width: 0,
              radius: 2
            },
            tip: {
              corner: 'bottomMiddle',
              size: {
                x: 20, // Be careful that the x and y values refer to coordinates on screen, not height or width.
                y : 8 // Depending on which corner your tooltip is at, x and y could mean either height or width!
              }
            }
          },
          position: {
            corner: {
              target: 'topMiddle',
              tooltip: 'bottomMiddle'
            }
          }
        });
      };
    });
  }
};

var galleries = {
  init: function()
  {
    $('#spotlight').gzGallery(
    {
      counter: false,
      description: false,
      method: 'slide',
      interval: 6000
    });
    $('#sponsors-teaser #partners, #sponsors-teaser #mobile-expert-gallery, #sponsors-teaser #sponsors').gzGallery(
    {
      counter: false,
      description: false,
      method: 'slide',
      interval: 3000
    });
  }
};

$(function(){
  
  $('#news-list img, #speakers-list img, #advisors-list img').load(function()
  {
    grayscale(this, true);
  });

  myEday.init();
  program.init();
  galleries.init();

  if($.fn.fhEllipses){
    $('#speakers-list .description, #advisors-list .description').fhEllipses({
      lines: 3
    });
    $('#news-list .description').fhEllipses({
      lines: 8
    });
  }

  if($.fn.fhHover){
    $('#timetable .presentation, #spotlight, #news-list li, #speakers-list li, #advisors-list li, #hotels li').fhHover();
  }
  
  $('a[rel*=external]').live('click', function(){
    $(this).attr('target', '_blank');
  });
});

function grayscale(image, bPlaceImage)
{
  var myCanvas        = document.createElement('canvas');
  var myCanvasContext = myCanvas.getContext('2d');

  var imgWidth  = image.width;
  var imgHeight = image.height;
  
  myCanvas.width  = imgWidth;
  myCanvas.height = imgHeight;
  
  myCanvasContext.drawImage(image, 0, 0);

  var imgd = myCanvasContext.getImageData(0, 0, 500, 300);
  var pix  = imgd.data;
  
  for (var i = 0, n = pix.length; i <n; i += 4)
  {
    var grayscale = pix[i] * .3 + pix[i+1] * .59 + pix[i+2] * .11;
    pix[i]   = grayscale;
    pix[i+1] = grayscale;
    pix[i+2] = grayscale;
  }
  
  myCanvasContext.putImageData(imgd, 0, 0);

  if (bPlaceImage)
  {
    $(image).after(myCanvas);
	}
}
