$(document).ready(function () {

  // --------------------------------------------------
  // SCROLLABLE
  // --------------------------------------------------

  $('#testimonials .scrollable').each(function (i,o) {
    var api = $(o).scrollable({
      size: 1,
      onBeforeSeek: function(e, index){
        this.getRoot().animate({
          height: $(this.getItems()[index]).height()
        });
        window.console.log(index);
      },
      api:true
    }).begin(0);
    // fix initial height for new scrollable behavior...:
    api.getRoot().height(
      api.getItems().eq(0).height()
    );
    // hide next/prev on one item...:
    if (api.getSize()==1) {
      api.getNaviButtons().hide();
    };
  });

  // --------------------------------------------------
  // DIALOGS
  // --------------------------------------------------

  $('a[rel="dialog"]')
    .each(function(i,o){
      // options
      var options = {
        newsletter: {
          onClose: function(){
            var content = this.getContent();
            content.find('.response').hide();
            content.find('form').resetForm();
          }
        },
        affiliate: {
          onClose: function(){
            var content = this.getContent();
            content.find('.response').hide();
          }
        }
      };
      // get dialog
      var name = $(this).attr('href').substr(1);
      var dialog = $('#dialog_' + name);
      // get API
      var api = dialog.overlay($.extend({
        expose: '#f2f0e4',
        api: true
      }, options[name]));
      // bind
      $(o).click(function(e){
        api.load();
        e.preventDefault();
        return false;
      });
    })

  // --------------------------------------------------
  // NEWSLETTER
  // --------------------------------------------------

  $('#newsletter_form').ajaxForm({
    dataType: 'script'
  });

  // --------------------------------------------------
  // AFFILIATE
  // --------------------------------------------------

  $('#affiliate_form').ajaxForm({
    dataType: 'script'
  });

  // --------------------------------------------------
  // BUTTONS
  // --------------------------------------------------

  $('a.button').add('.submit input[type="submit"]').each(function(i,o){
    var self = $(this);

    var light = self.css('border-top-color');
    var dark = self.css('border-bottom-color');

    self.hover(
      function (e) {
        $(this).addClass('hover');
      },
      function (e) {
        $(this).removeClass('hover');
      }
    );
    self.mousedown(function(e){
      self.addClass('pressed');
      self.css('border-color', dark);
      self.css('border-bottom-color', light);
      self.css('border-right-color', light);
    });
    self.mouseup(function(e){
      self.removeClass('pressed');
      self.css('border-color', light);
      self.css('border-bottom-color', dark);
      self.css('border-right-color', dark);
    });
    self.mouseout(function(e){
      self.trigger('mouseup');
    });
  });

  // --------------------------------------------------
  // "ACTIVATE NOW!" TAB
  // --------------------------------------------------

  // $('#nav a[href$="activate.php"]')
  $('#nav a:contains("Activate"):first')
    .attr('target', '_blank')
    .parents('li').eq(0)
      .addClass('orange');

  // --------------------------------------------------
  // FLOWPLAYER
  // --------------------------------------------------

  if (typeof flowplayer=='function') {
    $('#content a.flowplayer').text('').flowplayer("/flow-player/flowplayer-3.1.5.swf", {
      clip: {
        autoPlay: false,
        autoBuffering: true
      },
      plugins: {
        controls: {
          backgroundColor: '#ffffff',
          sliderColor: '#444341',
          progressColor: '#d4d0c8',
          bufferColor: '#aeaca6'
        }
      },
      canvas: {
        backgroundColor: '#d4d0c8'
      }
    });
  };

  // --------------------------------------------------
  // AUDIOPLAYER
  // --------------------------------------------------

  if (typeof AudioPlayer=='object') {
    var audioplayer_id = 1;
    $('#content a.audioplayer').each(function (i,o) {
      // pieces
      var link = $(o);
      var p = link.parent('p');

      // audioplayer
      var id = 'dynamic_audioplayer_'+audioplayer_id;
      audioplayer_id++;
      $('<p class="audioplayer_link clearfix"></p>')
        .html('<span class="label">'+link.html()+':</span> <span id="'+id+'" class="object"></span><div class="clear"/>')
        .insertAfter(p);

      // body
      link.remove();
      if (p.html().trim()!='') {
        p.addClass('audioplayer_body');
      } else {
        p.remove();
      };

      AudioPlayer.embed(id, {
        soundFile: link.attr('href'),
        titles: ' '
      });
    });
  };

});

// --------------------------------------------------
// CONSOLE FIX
// --------------------------------------------------

if (console === undefined) {
  var console = new function() {
    return {
      log: function() {},
      warning: function() {},
      error: function() {}
    }
  };
}

// --------------------------------------------------
// PROTOTYPES
// --------------------------------------------------

Array.prototype.indexAt = function(){
  for(var i = 0; i < this.length; i++){
    if(this[i] === arguments[0])
      return i;
  };
  return -1;
};
String.prototype.trim = function() {
  return this.ltrim().rtrim();
};
String.prototype.ltrim = function() {
  return this.replace(/^\s+/, '');
};
String.prototype.rtrim = function() {
  return this.replace(/\s+$/, '');
};

