
(function($){
  
  var $body = $(document.body),
  $loader = $('.ajax-loading'),
  $content = $('#content');


  $.Ajaxy.configure({
    'options':{
      track_all_internal_links: false,
      auto_ajaxify_documentReady: true,
      auto_sparkle_documentReady: true,
      debug: true,
      scrollto_content: false
    },
    'Controllers': {
      '_generic': {
        request: function(){
          // Prepare
          var Ajaxy = $.Ajaxy;
          // Log what is happening
          if ( Ajaxy.options.debug ) window.console.debug('$.Ajaxy.Controllers._generic.request', [this,arguments]);
          // Loading
          $loader = $('.ajax-loading'),
          $loader.fadeIn(300,function(){});
          // Done
          return true;
        },
        response: function(){
          // Prepare
          var Ajaxy = $.Ajaxy;
          var data = this.State.Response.data;
          var state = this.state||'unknown';
          // Log what is happening
          if ( Ajaxy.options.debug ) window.console.debug('$.Ajaxy.Controllers._generic.response', [this,arguments], data, state);
          // Title

          //var title = data.find("#title").css("border", "1px solid red");


          var title = data.title||false; // if we have a title in the response JSON
          if ( !title && this.state||false ) title = 'jQuery Ajaxy - '+this.state; // if not use the state as the title
          if ( title ) document.title = title; // if we have a new title use it
          // Loaded
          $(".ajax-loading").fadeOut(700,function(){}); 
          window.console.debug(data);
     
          // Display State
          return true;
        },
        error: function(){
          // Prepare
          var Ajaxy = $.Ajaxy;
          var data = this.State.Error.data||this.State.Response.data;
          var state = this.state||'unknown';
          // Error
          var error = data.error||data.responseText||'Unknown Error.';
          var error_message = data.content||error;
          // Log what is happening
          window.console.debug(data.responseText);
          // Loaded
          var Action = this;
           $responseContent =$(error.responseText);
           $content.html($responseContent).fadeIn(400,function(){
            Action.documentReady($responseContent);
            $.scrollTo($("#content"),700, {offset:-50} );
            shutterReloaded.init('sh');
          }
        );
          // Done
          return true;
        }
      },
      'page': {
        classname: 'ajaxy-page',
        matches:/.*/,
        request: function(){
          // Prepare
          var Ajaxy = $.Ajaxy;
          // Log what is happening
          if ( Ajaxy.options.debug ) window.console.debug('$.Ajaxy.Controllers.page.request', [this,arguments]);
          // Adjust Menu
          //$menu.find('.active').removeClass('active');
          // Hide Content
          //$content.stop(true,true).fadeOut(400);
          // Return true
          return true;
        },

        response: function(){
          $content = $('#content');
          // Prepare
          //var
          var Ajaxy = $.Ajaxy;
          var data = this.State.Response.data;
           //window.console.debug('asd',this.State.Response.data);
          var state = this.state;
          var State = this.State;
          // Log what is happening
          if ( Ajaxy.options.debug ) window.console.debug('$.Ajaxy.Controllers.page.response', [this,arguments], data, state);
          var Action = this;
           $responseContent =$(data.html);
           $content.html($responseContent).fadeIn(400,function(){
            Action.documentReady($responseContent);
            $.scrollTo($("#content"),700, {offset:-50} );
            shutterReloaded.init('sh');
          }
        );
          // Return true
          return true;
        }
      }
    }
  });

// All done
})(jQuery);
// Back to global scope
