/**
 * General js functionality used by all pages on the app
 */

// GLOBAL vars
var debug = true;
// /GLOBAL vars

var UTIL = (function() {
  var app = {};

  app.debug = function(str) {
    debug && window.console && console.log(str); // prevents error in IE
  };

  app.ie_version = function() {
    if (navigator.appVersion.indexOf("MSIE") != -1) {
      version = parseFloat(navigator.appVersion.split("MSIE")[1]);
      return version;
    }
    return 9999; // will return 9999 for all other browsers
  };

  return app;
}(jQuery));


var FORMS = (function($) {
  var app = {};

  // replace submit buttons with the 'waiting' progress bar when clicked
  app.init = function() {
    $('<div class="submit wait">&nbsp;</div>').insertBefore($('input[type=submit]')).hide();
    $('input[type=submit]').click(function() {
      $(this).hide();
      $(this).prev().show();
    });

    $('.form-fields .error input, .form-fields .error textarea, .form-fields .error select').focus(function() {
      $(this).parents('.error').first().removeClass('error');
    });
    $('fieldset.toggle legend').click(function() {
      if ($(this).parent().hasClass('open')) {
        $(this).parent().removeClass('open').addClass('closed');
      } else if ($(this).parent().hasClass('closed')) {
        $(this).parent().removeClass('closed').addClass('open');
      }
    });
  };

  return app;
}(jQuery));


var MESSAGES = (function($) {
  var app = {};
  app.init = function() {
    if ($('#message').length) {
      var h = $('#message').height();
      $('#message').delay(2500).animate({
        marginTop: '-'+h+'px'
      }, 1000, function() {
        $(this).remove();
      });
    }
  };
  return app;
}(jQuery));


var APP = (function($) {
  var app = {};

  app.init = function() {
    $(function() {
      if (!(UTIL.ie_version() < 9)) {
        if (window.location.search.indexOf('intro=true') != -1) {
          $.cookie('splash', 0);
        }
        if ($.cookie('splash') != 1) {
          $.cookie('splash', 1, { expires: 1 }); // create the cookie, set to expire in 1 day
          intro();
        } else {
          $('section').hide().fadeIn();
          scrollBars();
        }
      } else {
        scrollBars();
      }
    });
  };

  function scrollBars() {
    // scrollbars
    $('article').each(function() {
      // annoying, but seems like we need to do this to fix scrollpane bullshit
      if ($(this).innerHeight() <= parseInt($(this).css('maxHeight'), 10)) {
        $(this).css({
          paddingTop: 0,
          paddingBottom: 0
        });
        $(this).children().first().css('paddingTop', '20px');
        $(this).children().last().css('paddingBottom', '20px');
      }
    });
    $('article').jScrollPane();
  }

  function intro() {

    // hide everything else
    $('header nav, header address, section, header hr').hide();

    var $logo = $('#logo'),
        $footer = $('footer span'),
        $logo_clone,
        logo_h = 30,
        logo_clone_h = 13,
        logo_left = parseInt($logo.css('marginLeft')) + parseInt($('#container').css('paddingLeft')),
        logo_top = $logo.css('marginTop'),
        logo_orig_position = $logo.css('position'),
        footer_orig_position = $footer.css('position'),
        to_top = $(window).height() / 3 - logo_h / 2,
        to_left = $(window).width() / 2 - $logo.width() / 2;

    $footer.hide();

    $logo.css({
      opacity: 0,
      height: logo_h + 'px', // hide the top part
      position: 'absolute',
      bottom: 5,
      left: to_left
    });

    $logo_clone = $logo.clone();
    $logo_clone.css({
      opacity: 0,
      top: 0,
      height: logo_clone_h + 'px',
      backgroundPosition: 'left top' // show the top part
    });
    $('body').append($logo_clone);

    $logo_clone.animate({
      opacity: 1,
      top: to_top
    }, 1000, function() {
      $logo.animate({
        opacity: 1,
        bottom: $(window).height() - $logo_clone.offset().top - logo_h - logo_clone_h
      }, 750, function() {
        $logo_clone.remove();
        $logo.height(logo_h + logo_clone_h).css({
          top: $logo.offset().top - logo_clone_h,
          bottom: 'auto'
        });

        // do the footer
        console.log($(window).width(), $('#container').width())
        var offset = $(window).width() < $('#container').outerWidth(true) ? $('#container').outerWidth(true) - $(window).width() : 0;
        console.log(offset);
        $footer.css({
            position: 'absolute',
            bottom: $(window).height() - ($logo.offset().top + logo_h + logo_clone_h + $footer.height() + 5) + 'px',
            right: $(window).width() / 2 - $footer.outerWidth(true) / 2 + offset
          }).fadeIn(1000, function() {

            // shoot to the upper left / lower right
            $logo.delay(1000).animate({
                top: logo_top,
                left: logo_left
              }, 500, function() {
                $logo.css('position', logo_orig_position); // back to normal
                $('header nav, header address, section').fadeIn(); // fade everything else in
              });
            $footer.delay(1000).animate({
                bottom: 10,
                right: 20
              }, 500, function() {
                $footer.css('position', footer_orig_position);
                // and animate in the red lines
                $('header hr').width(1).show().animate({
                  width: '100%'
                }, 1000);
                scrollBars();
              });
          });
      });
    });

  };

  return app;
} (jQuery));

APP.init();

