$(document).ready(function(){
  // Lightbox
  // ---------------------------------------------------------------------------
  
  $('ul.thumbnails a').click(function() {
    var app_name, platform, image_count, images_path;
    var horizontal_images = new Array();
    
    // Determine which app has just been clicked
    app_name = $(this).parents('div.app').attr("id");
    
    // Determine platform
    platform = $(this).find('div').attr("class").split(" ")[0];
    
    // Determine how many images we have for this app and platform
    image_count = $(this).parent().attr("data-count");
    
    // Determine whether there are horizontal images
    var s = $(this).parent().attr("data-horizontal-images");
    if (s) {
      horizontal_images = s.split(',');
    }
    
    // Determine language
    var is_fr = document.location.pathname.substr(1,2) == 'fr' ? true : false;
    
    // Build path to images
    images_path = (is_fr ? '/fr' : '') + '/images/apps/' + app_name + '/' + platform + '/';
    
    // Remove previous lightbox wrapper if there was one
    $('.lightbox_wrapper').remove();
    
    // Build new lightbox
    $('body').append('<div class="lightbox_wrapper"><div class="lightbox ' + platform + '"><div class="close"></div><div class="current"></div><ul class="thumbnails"></ul></div>');
    $('.lightbox div.current').append('<img src="' + images_path + '1.jpg" alt="Screenshot" />');
    for(var i = 1; i <= image_count; i++) {
      $('.lightbox .thumbnails').append('<li' + ($.inArray(i + '', horizontal_images) !== -1 ? ' class="horizontal"' : '') + '><img src="' + images_path + i + '.jpg" alt="Screenshot" /></li>');
    }
    $('.lightbox .thumbnails li').first().addClass("current");
    
    // Attach actions to thumbnails
    $('.lightbox .thumbnails li').click(function() {
      var image_path = $(this).find('img').attr('src');
      $('.lightbox div.current img').attr('src', image_path);
      $('.lightbox .thumbnails li').removeClass("current");
      $(this).addClass("current");
      
      if ($.inArray($(this).index() + 1 + '', horizontal_images) !== -1) {
        $('.lightbox div.current').addClass('horizontal');
      } else {
        $('.lightbox div.current').removeClass('horizontal');
      }
    })
    
    // Attach actions to prev/next buttons
    $('.lightbox div.current').append('<div class="nav prev">Previous</div><div class="nav next">Next</div>');
    $('.lightbox div.current div.nav').disableTextSelect();
    $('.lightbox div.prev').click(function() {
      var previous_item = $('.lightbox .thumbnails li.current').prev();
      if (previous_item.length) {
        previous_item.click();
      } else {
        $('.lightbox .thumbnails li:last-child').click();
      }
    })
    $('.lightbox div.next').click(function() {
      var next_item = $('.lightbox .thumbnails li.current').next();
      if (next_item.length) {
        next_item.click();
      } else {
        $('.lightbox .thumbnails li:first-child').click();
      }
    })

    // Attach action to close button
    $('.lightbox div.close').click(function() {
      $('.lightbox_wrapper').hide();
    })
    
    // Send click event to first item
    $('.lightbox ul.thumbnails li.current').click();
    
    // Lightbox wrapper is hidden via CSS, so we display it now
    $('.lightbox_wrapper').show();
    
    // Position lightbox
    $('.lightbox').css('margin-top', '-' + Math.round($('.lightbox').height() / 2) + 'px');
    return false;
  })
  
  // Twitter feed
  // ---------------------------------------------------------------------------
  
  if ($(".tweets").length > 0) {
    $(".tweets").getTwitter({
  		userName: "wherecloud",
  		numTweets: 3,
  		loaderText: "Loading tweets...",
  		slideIn: false,
  		showHeading: false,
  		showProfileLink: false
  	});
  }

  // Email obfuscating
  // ---------------------------------------------------------------------------

  $("span.mailto").wrap(function() {
    var email = $(this).text().replace(' (at) ', '@');
    $(this).text(email);
    return '<a href="mailto:' + email + '" />';
  })
  
  // Form processing
  // ---------------------------------------------------------------------------

  var $_GET = {};
  document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
    function decode(s) {
      return decodeURIComponent(s.split("+").join(" "));
    }
    $_GET[decode(arguments[1])] = decode(arguments[2]);
  });
  
  var question;
  if (question = $_GET["question"]) {
    $('form.ask_full .question input').val(question);
  }
});
