var modal;

var shareUploadPhoto = function() 
{
  FB.ui({
    method: 'feed',
    link:   uploadImageUrl,
    picture: uploadImage,
    name:   "I've just uploaded a hat tag photo for the innocent Big Knit",
    description: 'I just played the innocent Big Knit hat tag game, and in doing so contributed 10p to Age UK - vote for my masterpiece to help donate another 5p, or upload your own hat for a chance to win.',
    actions: { name: 'Vote for my masterpiece', link: uploadImageUrl }
  });
};

var sharePhoto = function(shareUrl, shareImage)
{
  FB.ui({
    method: 'feed',
    link:   shareUrl,
    picture: shareImage,
    name:   "Hat's awesome",
    description: 'Does it get any better than this Big Knit hat? Come vote for it, and innocent will donate 5p to Age UK on your behalf every day that you vote.',
    actions: { name: 'Vote for this masterpiece', link: shareUrl }
  });
};

var shareOwnPhoto = function(shareUrl, shareImage)
{
  FB.ui({
    method: 'feed',
    link:   shareUrl,
    picture: shareImage,
    name:   "Help me out in the innocent Big Knit hat tag game",
    description: "I've got the best photo - wouldn't you agree? Vote for my masterpiece to help donate 5p for every day you vote to Age UK, or upload your own hat for a chance to win.",
    actions: { name: 'Vote for my masterpiece', link: shareUrl }
  });
};

var shareVote5 = function() 
{
  FB.ui({
    method: 'feed',
    link:   uploadImageUrl,
    picture: uploadImage,
    name:   "I've just voted for my favourite innocent Big Knit hat tag",
    description: 'innocent drinks have just donated 5p to Age UK because I clicked on a funny picture. Come and do the same to help raise even more money, or upload your own image and be in with a chance to win &pound;500 for your Christmas shopping.',
    actions: { name: 'Vote for this masterpiece', link: uploadImageUrl }
  });
};

var shareVote = function()
{
  FB.ui({
    method: 'feed',
    link:   uploadImageUrl,
    picture: uploadImage,
    name:   "I've just voted for my favourite innocent Big Knit hat tag",
    description: 'innocent drinks have donated 5p to Age UK today because I clicked on a funny picture. Come and do the same to help raise even more money, or upload your own image and be in with a chance to win &pound;500 for your Christmas shopping.',
    actions: { name: 'Vote for this masterpiece', link: uploadImageUrl }
  });
};

var addShareEvents = function()
{
  $$('a.share').each(function(el)
  {
    el.addEvent('click', function(e) {
      e.stop();
      sharePhoto(this.href, this.rel);
    });
  });
  
  $$('a.share_mine').each(function(el)
  {
    el.addEvent('click', function(e) {
      e.stop();
      shareOwnPhoto(this.href, this.rel);
    });
  });
};

// Close popup without having to reload the page
var destroyPopup = function(e)
{
  e.stop();
  var popup = this.getParent();
  popup.dispose();
};

window.addEvent('domready', function(){
  // animate upload button
  if ($('upload_btn'))
  {
    $('upload_btn').addEvents({
      'mouseover' : function() {
        $('upload_btn').set('src', '/images/fb.animated.upload.red.gif');
      },
      'mouseout'  : function() {
        $('upload_btn').set('src', '/images/fb.animated.upload.blue.gif');
      }
    });
  }
  
  // banner
  if ($('upload_banner'))
  {
    $('upload_banner').addEvents({
      'mouseover' : function() {
        $('upload_banner').set('src', '/images/fb.animated.upload.red2.gif');
      },
      'mouseout'  : function() {
        $('upload_banner').set('src', '/images/fb.animated.upload.green.gif');
      }
    });
  }
  
  if ($('close_popup'))
  {
    $('close_popup').addEvent('click', destroyPopup);    
  }

  // lighbox images
  modal = new LightFace({
    width: 460,
    buttons: [ { title: 'Close', event: function() { this.close(); }, color: 'green' } ]
  });
  
  $$('div[rel="lightface"]').addEvent('click', function(e) 
  {
    e.stop();
    modal.load(this.getElement('.lightface-content').get('html'));
    modal.open();
    addShareEvents();
  });
  
  // var share dialogs
  if ($('upload_share')) $('upload_share').addEvent('click', shareUploadPhoto);
  if ($('vote_share0')) $('vote_share0').addEvent('click', shareVote);
  if ($('vote_share5')) $('vote_share5').addEvent('click', shareVote5);
  
  addShareEvents();
  
  if (Browser.ie7) $('wrapper').addClass('ie7');  
  if (Browser.ie8) $('wrapper').addClass('ie8');  
  
  fireOnLoadEvents();
  
  // Try and hide web debug to keep out of the way on dev
  try { sfWebDebugToggleMenu(); } catch(e) { }
  
  // IE fix for errant console.log
  if (typeof console === "undefined") {
    console = { log: function() { } };
  }
});
