(function ($) {
  var contentBody, preLoader, firstAccess = true;
  var titleSuffix = "| Birds Commercial";
  var app = $.sammy('#wrapper', function () {
    this.use(Sammy.Title);
    this.use(Sammy.GoogleAnalytics, '#!');

    //would be traditionally placing updateTitle in this.bind('event-context-after', function() { }); but due to jQuery loading in content the context-after function doesn't always fire
    this.helpers({
      updateTitle: function (newPageTitle) {
        var pageTitle = "Birds Commercial - Interior Fit-Out Specialists";
        if (newPageTitle.length > 0) {
          pageTitle = newPageTitle + (newPageTitle.indexOf(titleSuffix) == -1 ? titleSuffix : "");
        }
        this.title(pageTitle);
      },
      loadAjaxPage: function (ctx) {
        var p = ctx.path.replace('/#!', ''),
        self = this, args = arguments;
        var loadResult = $.get(p, function (response, status, xhr) {
          if (status == "error") {
            var msg = "Sorry but there was an error: ";
            contentBody.html(msg + xhr.status + " " + xhr.statusText);
          } else {
            slideCouchOut(function () {
              slidePanelOut($('#nav-menu'), 300);
              slidePanelOut(contentBody, 300, function () {
                slidePanelOut($('.footer'), 350, function () {
                  $('.footer').css("left", 1100);
                  contentBody.css("left", 1100).hide();
                  $('#nav-menu').css("left", 1100).show();
                  contentBody.show(); // if this is moved javascript below may break

                  var pageHtml = $(response);
                  var pageTitle = (/<title>(.*?)<\/title>/m).exec(response)[1];
                  var pageContent = pageHtml.find('#content').html();
                  contentBody.html(pageContent);
                  ctx.updateTitle(pageTitle);
                  if (p.indexOf("gallery") != -1) {
                    InitialiseGallery();
                  }
                  else if (p.indexOf("services") != -1) {
                    $('.service-navigation a').each(function () {
                      var urlParts = /^(https?:\/\/.+?)?(\/.+?)(\?.*?)?$/.exec($(this).attr("href"));
                      var hashBangLink = "#!" + urlParts[2];
                      $(this).attr("href", hashBangLink);
                    });
                    InitialiseServices();
                  }
                  else if (p.indexOf("about") != -1) {
                    InitialiseAbout();
                  }
                  else if (p.indexOf("free-quote") != -1) {
                    InitialiseFreeQuote();
                  }
                  slidePanelIn($('#nav-menu'), 0, 300, function () {
                    $('.footer').show();
                    slidePanelIn($('.footer'), 0, 200);
                    slidePanelIn(contentBody, 280, 500);
                  });
                  slideCouchIn(1000);
                });
              });
            });
          }
        });
      }
    });

    this.get('/#!/', function (ctx) {
      this.updateTitle('');
      $('.section').fadeIn(1500);
    });

    this.get('^(\/#!\/services/(.+))$', function (ctx) {
      var serviceBody = $('.inner-body');
      if (serviceBody.length == 0) {
        this.loadAjaxPage(ctx);
      }
      var p = ctx.path.replace('/#!', '');
      var loadResult = $.get(p, function (response, status, xhr) {
        if (status == "error") {
          var msg = "Sorry but there was an error: ";
          serviceBody.html(msg + xhr.status + " " + xhr.statusText);
        } else {
          $('.service-navigation').hide();
          $(serviceBody).hide();
          $('.client-gallery').hide();

          var pageHtml = $(response);
          var pageTitle = (/<title>(.*?)<\/title>/m).exec(response)[1];
          var pageContent = pageHtml.find('.inner-body').html();
          var originalWidth = $('#services .inner-content').css("width");
          var rightValue = 20;
          var duration = 200;

          $('#services .inner-content').animate({
            width: '-=400',
            right: '+=200'
          }, duration);
          serviceBody.html(pageContent);
          $('#services .inner-content').animate({
            width: originalWidth,
            right: rightValue
          }, duration, function () {
            $(serviceBody).fadeIn(500);
            $('.service-navigation').fadeIn(500);
            InitialiseServices();
          });
        }
      });
      return;
    });

    this.get('^(\/#!\/(.*))$', function (ctx) {
      this.loadAjaxPage(ctx);
    });
  });

  $(function () {
    $('ul#nav-menu li a').each(function () {
      var hashBangLink = "#!" + $(this).attr("href");
      $(this).attr("href", hashBangLink);
    });
    contentBody = $('#content');
    $('.section').hide();
    app.run('/#!/');
  })
})(jQuery);

