$(document).ready(function() {
	$('redirect-form').submit();
  change_plan_submit_buttons($('body'));

  $('.content_units .content_unit:first-child').each(function() {
    var jq_obj = $(this);
    jq_obj.parents('.content_units').eq(0).height(jq_obj[0].offsetHeight);
  });

  $('a.next_content_unit').click(function() {
    var jq_obj = $(this);
    var parent_obj = jq_obj.parents('div.content_units').eq(0);
    var content_unit;
    if (jq_obj.is('.content_unit')) {
      content_unit = jq_obj;
    } else {
      content_unit = jq_obj.parents('.content_unit').eq(0);
    }
    var next_content_unit = content_unit.next('.content_unit');

    if (content_unit.find('div.list_of_plans').length > 0) {
      var li_obj = jq_obj.parents('li,p');
      if (li_obj.length > 0) {
        next_content_unit.find('.plan_form').css({'display': 'none'});
        next_content_unit.find('li.' + li_obj.attr('class')).css({'display': 'block'});
      }
    }

    var new_left_margin = parseInt(parent_obj.css('marginLeft')) - 950;
    new_left_margin = new_left_margin - ( new_left_margin % 950 );
    new_left_margin = "" + new_left_margin + "px";
    var new_height = "" + next_content_unit[0].offsetHeight + "px";
    parent_obj.animate({'marginLeft': new_left_margin, 'height': new_height }, 500);
    $.scrollTo(parent_obj, 500);
    return false;
  });

  $('a.prev_content_unit').click(function() {
    var jq_obj = $(this);
    var parent_obj = jq_obj.parents('div.content_units').eq(0);
    var content_unit;
    if (jq_obj.is('.content_unit')) {
      content_unit = jq_obj;
    } else {
      content_unit = jq_obj.parents('.content_unit').eq(0);
    }

    var new_left_margin = parseInt(parent_obj.css('marginLeft')) + 950;
    new_left_margin = new_left_margin - ( new_left_margin % 950 );
    new_left_margin = "" + new_left_margin + "px";
    var new_height = "" + content_unit.prev('.content_unit')[0].offsetHeight + "px";
    parent_obj.animate({'marginLeft': new_left_margin, 'height': new_height }, 500);
    return false;
  });

  $('div#Screencast a').click(function() {
    $('div#Screencast a').hide();
    $('div#Screencast div.player').show();
  });

  $('div#Features ul li img.thumbnail').click(function() {
    if ($('div#Features ul li img.displayed').length > 0) {
      return;
    }
    var jq_obj = $(this);
    var img_offset = jq_obj.offset();
    var ul_offset = $('div#Features').offset();
    var full_image = jq_obj.siblings('img.fullsize');
    var left = "" + (img_offset.left - ul_offset.left) + "px";
    var top = "" + (img_offset.top - ul_offset.top) + "px";

    full_image.width(jq_obj.width());
    full_image.height(jq_obj.height());
    full_image.css({'left': left, 'top': top, 'display': 'block'});
    jq_obj.css({'visibility': 'hidden'});

    full_image.animate({'left': '63px', 'top': '51px', 'width': '824px', 'height': '512px'}, 500, "swing", function() {
      reveal_frame();
    });
    full_image.addClass('displayed');
  });

  $('div#Features a.frame').click(function() {
    $(this).fadeOut(50, function() {
      close_frame();
    });
    return false;
  });

  $('#BlogFeed').load('/blogpipe');
});

function reveal_frame() {
  var jq_obj = $('div#Features a.frame');
  jq_obj.fadeIn(50);
}

function close_frame() {
  var displayed = $('div#Features ul li img.displayed');
  var jq_obj = displayed.siblings('img.thumbnail');
  var img_offset = jq_obj.offset();
  var ul_offset = $('div#Features').offset();
  var left = "" + (img_offset.left - ul_offset.left) + "px";
  var top = "" + (img_offset.top - ul_offset.top) + "px";

  displayed.animate({'left': left, 'top': top, 'width': '309px', 'height': '192px'}, 500, "swing", function() {
    $('div#Features ul li img.thumbnail').css({'visibility': 'visible'});
    $('div#Features ul li img.displayed').css({'display': 'none'});
    $('div#Features ul li img.displayed').removeClass('displayed');
  });
}

function change_plan_submit_buttons(change_scope) {
  change_scope.find('input[type=submit].replace').each(function(i) {
    $(this).replaceWith("<a href=\"#" + $(this).attr('name') + "\" class=\"button submit_form\">" + $(this).val() + "</a>");
  });

  change_scope.find('a.submit_form').click(function(e) {
    e.preventDefault();
    e.stopPropagation();
    var jq_obj = $(this);
    if (jq_obj.attr('href') != '#') {
      var name = jq_obj.attr('href');
      name = name.replace(/\#/, '');
      jq_obj.after('<input type="hidden" name="' + name + '" value="' + jq_obj.html() + '" />');
    }
    var parent_form = jq_obj.parents('form').eq(0);
    var serialization = parent_form.serialize();
    $.post('/signups', serialization, function(data) {
      if (data.errors.length == 0)
        window.location.href = data.redirect_path;
      else {
        var errors = $('<div>').addClass('plan_errors');
        $.each(data.errors, function() {
          errors.append(this + "<br/>");
        });
        $.each(data.error_fields, function() {
          $('input#user_' + this).css({'border-color': '#f00'});
          $('label[for=user_' + this + ']').css({'color': '#f00'});
        });
        $('.notes_or_errors').html(errors);
      }
    }, "json");
    return false;
  });
}