/**
* librarious
*/

var lib = {};

lib.Behaviours = {
    first : function() {
      //call purr method on flash messages
      $('.flash').each(function(){
        if($(this).hasClass('error')){
          var stickiness = true;
        }else{
          var stickiness = false;
        }
        $(this).purr({
          isSticky: stickiness,
          insertInto: '.gamma',
          prepend: true
        });
      });
      
      var $fixed_sides = $('.gamma');
      var fix_at = $fixed_sides.offset().top - 15;

      $(window).scroll(function(){
        if( $(window).scrollTop() > fix_at ){
          $fixed_sides.css({position: 'fixed', top: 15 });
        }else{
          $fixed_sides.css({position: 'absolute', top:''});
        }
      });
      
      //set body to height of sidebar if shorter
      
      
      
      if($('.alpha').height() > $('.content').height())
        $('.content').height($('.alpha').height());
      if($('.gamma').height() > $('.content').height())
        $('.content').height($('.gamma').height());
        
      //adds class name to main menu items on hover
      $('#header dd').hover(
        function(){ $(this).addClass('current'); },
        function(){ $(this).removeClass('current'); }
      );
      
      //add events to menu items with submenus
      $('#header dd ul.submenu').each(function(){
        var submenu = this;
        $(this).parent().hover(
          function(event){
            //mouseenter
            if(this.hideTimer != null) clearTimeout(this.hideTimer);
            this.displayTimer= setTimeout(function(){lib.displaySub(submenu);}, 150);
          },
          function(event){
            //mouseleave
            if(this.displayTimer != null) clearTimeout(this.displayTimer);
            this.hideTimer= setTimeout(function(){lib.hideSub(submenu);}, 300);
          }
        );
      });
    },
    
    last : function() {},
    pages : {
        'media' : {
          'add' : function() {
            
          }
        },
        'auth' : {
          'register' : function(){
            if($('#screenname_example').text() == '') $('#screenname_example').text('jimeh');
            
            $('#register_screenname').keyup(function(){
              
              var screennameInput = $('#register_screenname');
              if( this.checkUserTimer != null) clearTimeout(this.checkUserTimer);
              if($(screennameInput).val().length > 2){
                this.checkUserTimer = setTimeout(function(){lib.checkUser(screennameInput);},500);
              }
              
              $('#screenname_example').text(screennameInput.val());
              if($('#screenname_example').text() == '') $('#screenname_example').text('jimeh');
            });
            
            $('#register_password2, #register_password').keyup(function(){
              if(typeof(this.checkPasswordsTimer) == 'number') clearTimeout(this.checkPasswordsTimer);
              
              if($('#register_password2').val().length > 0){
                if($('#register_password2').val() != $('#register_password').val()){
                  this.checkPasswordsTimer = setTimeout(function(){
                    $('#password_error_message').fadeOut('fast',function(){
                      $('#password_error_message').remove();
                    });
                    var password_error_message = '<div id="password_error_message" class="flash error"><p>Those passwords don\'t match. Try again?</p></div>';

                    $(password_error_message).purr({
                      isSticky: true,
                      insertInto: '.gamma',
                      prepend: true,
                      isPersistent: true
                    });
                  },1000);

                }else{
                  if(typeof(this.checkPasswordsTimer) == 'number') clearTimeout(this.checkPasswordsTimer);
                  $('#password_error_message').fadeOut('fast',function(){
                    $('#password_error_message').remove();
                  });
                }
              }
              if($('#register_password2').val().length == 0 && $('#register_password').val().length == 0){
                $('#password_error_message').fadeOut('fast',function(){
                  $('#password_error_message').remove();
                });
              }
            });
          }
        }
    },
    
    run : function(cntl, actn){
      lib.Behaviours.pages[cntl][actn].apply();
    }
};

$(document).ready(function() {
  
  var cntl = $('#cntl').attr('class');
  var actn = $('#actn').attr('class').split(" ")[0];

  lib.Behaviours.first.call(this, cntl, actn);
  if(lib.Behaviours.pages[cntl] && lib.Behaviours.pages[cntl][actn])
      lib.Behaviours.pages[cntl][actn].apply();

  lib.Behaviours.last.call(this, cntl, actn);
});  

lib.checkUser = function (element){
    $.ajax({
        url       : '/api/user/show',
        data      : {'screenname': $(element).val()},
        success   : function(data){
                      console.log(data);
                      $('#screenname_error_message').remove();
                      var screenname_error_message = '<div id="screenname_error_message" class="flash error"><p>That screenname is already taken</p></div>';
        
                      $(screenname_error_message).purr({
                        isSticky: true,
                        insertInto: '.gamma',
                        prepend: true,
                        isPersistent: true
                      });
                    },
        error     : function(XMLHttpRequest, textStatus, errorThrown){
                      $('#screenname_error_message').fadeOut('fast',function(){
                        $('#screenname_error_message').remove();
                      });
                    },
        dataType  : 'json'
    });

};

$('a.remove_item').bind('click',function(){
  var removeItemId = this.id.split(':')[1];
  var mediaParent = $(this).parents('li.thing');
  // var confirmDialog = '<div class="confirm delete" style="margin:-100px auto 0 auto;width:100%;text-align:center;height:100px;><p>Are you sure you want to do that?</p><p><a href="#">Delete</a> <a href="#">Cancel</a></p></div>';

  $.post('/api/item/delete',{'id': removeItemId },
    function(data){
      if(data) 
        $(mediaParent).slideUp();
    }, 'json');
  
  return false;
});

lib.displaySub = function (element){
  $(element).slideDown(50);
};

lib.hideSub = function (element){
  $(element).slideUp(150);
};

