$(function()
{
  $('a[rel*=external]').attr('target', '_blank');

  var
    devices = [
      ["Sony Reader", 70],
      ["iPhone <span>and</span> iPod", 70],
      ["Blackberry", 74],
      ["Symbian S60", 70],
      ["Windows Mobile", 70],
      ["Nokia", 70],
      ["Mac <span>and</span> PC", 145]
    ],
    device = 1,
    listWidth = 572,
    list = $('div.device-list'),
    description = $('div.devices div.description'),
    setDescription = function(animate)
    {
      var text = devices[device][0];

      if (animate)
      {
        var newDescription = $('<div class="description"/>').html(text);

        description.
          before(newDescription).
          fadeOut('normal', function()
          {
            description.remove();
            description = newDescription;
          });
      }
      else
      {
        description.html(text);
      }
    },
    getListMargin = function()
    {
      var margin = list.css('margin-left');
      if (margin == undefined || margin == '') return 0;
      else return parseInt(margin.replace(/^([-0-9]+)px$/, '$1'));
    },
    moving = false,
    moveList = function(offset)
    {
      moving = true;

      list.animate(
        {marginLeft: (getListMargin() + offset) + 'px'},
        'normal', 'swing', function() { moving = false });

      setDescription(true);
    };

  setDescription(false);

  $('.devices .button-left').click(function()
  {
    if (moving) return false;

    if (device-- == 0) device = 6;
    if (device == 3)
    {
      list.css({
        'width': (list.width() * 2) + 'px',
        'margin-left': getListMargin() - listWidth
      });
    }

    var offset = devices[device][1];
    if (device == 4) offset += 70;
    else if (device == 5) offset += 10;

    moveList(offset);
  });

  $('.devices .button-right').click(function()
  {
    if (moving) return false;

    if (device++ == 6) device = 0;
    if (device == 3) list.css('width', (list.width() * 2) + 'px');

    var offset = -devices[device][1];
    if (device == 0) offset -= 80;

    moveList(offset);
  });

  var
    brandsMoving = false,
    brands = $('.partners .brands'),
    brandsGradientLeft = $('.partners .gradient-left'),
    brandsGradientRight = $('.partners .gradient-right');

  $('.partners .button-left').click(function()
  {
    if (!brandsMoving && brandsGradientLeft.css('display') != 'none')
    {
      brandsMoving = true;
      if ($.browser.msie)
      {
        brandsGradientLeft.hide();
        brandsGradientRight.show();
      }
      else
      {
        brandsGradientLeft.fadeOut();
        brandsGradientRight.fadeIn();
      }
      brands.animate(
        {marginLeft: 0},
        'normal', 'swing', function() { brandsMoving = false });
    }
  });

  $('.partners .button-right').click(function()
  {
    if (!brandsMoving && brandsGradientRight.css('display') != 'none')
    {
      brandsMoving = true;
      if ($.browser.msie)
      {
        brandsGradientLeft.show();
        brandsGradientRight.hide();
      }
      else
      {
        brandsGradientLeft.fadeIn();
        brandsGradientRight.fadeOut();
      }
      brands.animate(
        {marginLeft: '-589px'},
        'normal', 'swing', function() { brandsMoving = false });
    }
  });
});
