FB.init('933c10c2c4e056055f79ad9acaaba9ac', '/xd_receiver.htm');

var photos;

function showGallery(photos, which) {
  jQuery.slimbox(photos || window.photos || [], which || 0, {
    loop: true,
    resizeDuration: 1, 
    imageFadeDuration: 200, 
    captionAnimationDuration: 200
  });
};

$(document).ready(function () {
  $('#pri-nav li').bind({
    mouseenter: function () {
      var self = $(this),
          div = self.children('div.sub-nav:first');
      
      if (div.children('ul:first').children('li').size()) {
         div.css('top', self.offset().top + self.outerHeight() - 2).stop().fadeTo(250, 1);
      };
    }, 
    mouseleave: function () {
      var self = $(this);
      
      self.children('div.sub-nav:first').stop().fadeTo(250, 0, function () {
        $(this).hide();
      });
    }    
  });

  if (!/android|iphone|ipod|series60|symbian|windows ce|blackberry/i.test(navigator.userAgent)) {
    $('ul.image-wall a').bind('click', function () {
      var chosen = $(this).attr('href');    
      
      if (photos) {
        showGallery(photos, photos.findOne(function (a) {
          return a[0] == chosen;
        }));
      } else {
        jQuery.ajax({
          url: '/photos/photos/index/',
          method: 'get',
          dataType: 'json',
          success: function (response) {
            if (response.success) {
              photos = response.photos;
              
              showGallery(photos, photos.findOne(function (a) {
                return a[0] == chosen;
              }));
            };
          }
        });
      };
      
      return false;
    });
    
		$("a[rel^='lightbox']").slimbox({
      loop: true
    }, null, function(el) {
			return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
		});
  };
  
  $('#flash').css('left', ($(window).width() / 2) - ($('#flash').outerWidth() / 2)).delay(1000).fadeTo(1000, 1).delay(5000).fadeTo(1000, 0);
  $('#flash').ajaxComplete(function (event, xml, settings) {
    
  });
  
  // Comment magic.
  $('ul.comments').each(function () {
    var self = $(this),
        extra = self.children('li.comment:gt(3)').hide();
    
    if (extra.size()) {
      self.children('li.comments-expand').show().find('a span:first').html(extra.size());
    };
  });
  
  $('ul.comments li.comments-expand a').bind('click', function () {
    var self = $(this);
    
    self.closest('li').hide();
    self.closest('ul').children('li.comment:hidden').slideDown();
    
    return false;
  });
  
  $('form.require-login').live('submit', function () {
    var self = $(this),
        reenable = function () {
          return self.find('input:submit:first').attr('disabled', false);
        };
    
    if (!isLoggedIn) {
      try {
        FB.Connect.requireSession(function() {
          jQuery.ajax({
            url: '/user/auth/login',
            dataType: 'json',
            complete: function () {
              isLoggedIn = true;
              
              reenable().trigger('click');            
            }
          });
        }, function () {
          reenable();
        });
      } catch (e) {
        window.location.href = "/user/auth/login/?redirect=" + window.location.href;
      };
      
      return false;
    };
  });

  $('a.require-login').live('click', function () {
    var target = $(this).attr('href') || window.location.href;
    
    if (!isLoggedIn) {
      if (FB.Connect) {
        FB.Connect.requireSession(function () {
          window.location.href = "/user/auth/login/?redirect=" + target;
        });
      } else {
        window.location.href = "/user/auth/login/?redirect=" + target;
      };
      
      return false;
    };
  });

  $('form.ajax').live('submit', function () {
    var self = $(this);
    
    jQuery.ajax({
      dataType: 'json',
      type: self.attr('method'),
      url: self.attr('action'),
      data: self.serialize(),
      complete: function () {
        self.find('input:submit:first').attr('disabled', false);
      },
      success: function (response) {
        if (response.success && response.content) {
          $(response.selector || self).replaceWith(response.content);
        };
      }
    });
    
    return false;
  });  
  
  $('form').bind('submit', function () {
    $(this).find('input:submit:first').attr('disabled', true);
  });
  
  (function () {
  function getSampleDiv() {
    var div = $('#merch-sample'),
        sample,
        p;
    
    if (!div.size()) {
      div = $('<div />').addClass('color-hover').attr('id', 'merch-sample').appendTo('body');
      sample = $('<div />').addClass('sample');
      p = $('<p />');
      
      $('<div />').addClass('inner').append(sample).append(p).appendTo(div);
    };
    
    return div;
  };  
  
  $('ul.merch ul.images').hide();
  $('ul.merch ul.colors li.color').bind({
    mouseenter: function () {
      var self = $(this),
          position = self.position(),
          bottom = position.top + self.outerHeight(),
          center = position.left + self.outerWidth() * 0.5,
          sample = getSampleDiv();

      sample.find('.sample:first').css('backgroundColor', self.css('backgroundColor'));
      sample.find('p:first').html(self.attr('class').split(" ")[1].replace(/-/g,' ').capitalizeWords());
      sample.css({
        top: bottom -5 ,
        left: center - (0.5  * sample.outerWidth())
      });
      sample.show();
    },
    mouseleave: function () {
      getSampleDiv().hide();
    }
  });
  
  }())
});

Array.prototype.findOne = function (func, val) {
  for (var i = this.length; i--;) {
    if (func(this[i])) {
      return val ? this[i] : i;
    };
  };
};

String.prototype.capitalizeWords = function () {
  var str = this.split(" "),
      i = str.length;
  
  for (;i--;) {
    str[i] = str[i].charAt(0).toUpperCase() + str[i].substring(1);
  };
  
  return str.join(" ");
};