var icon = 0;

$(function(){
  
  $("#icon_open").click(function(){
    
    if (icon == 0){       
      
      $(this).html("<span style='font-size: 22px;'><i class='fa fa-close fa-lg'></i></span>");
      
      icon = 1;
    
    }else{
      
      $(this).html("<i class='fa fa-bars fa-lg'></i>");
      
      icon = 0;    
    
    }
    
    return true;
  
  });

});

var Sidebar = {
  
  toggle: function(event){
    
    event.preventDefault();
    
    var sidebar_wrap = document.getElementById("sidebar_wrap");
    
    if (sidebar_wrap.style.width == "280px"){
      
      Sidebar.openFunction("left");
    
    }else{
      
      Sidebar.openFunction("right");
    
    }
  
  },toggleSwipe: function(direction){
    
    if (direction == 'right'){
      
      Sidebar.openFunction("right");
    
    }else{
      
      Sidebar.openFunction("left");
    
    }
  
  },openFunction: function(direction){
    
    var sidebar_wrap = document.getElementById("sidebar_wrap");

    if (direction == 'right'){
      
      sidebar_wrap.style.width = "280px";
      sidebar_wrap.style.height = "auto";
      document.body.style.overflow = "hidden";
      Sidebar.addClass(document.body,"openSidebar");
    
    }else{
      
      sidebar_wrap.style.width = "";
      sidebar_wrap.style.height = "";
      document.body.style.overflow = "";
      Sidebar.removeClass(document.body,"openSidebar");
      
    }
  
  },addClass: function(o, c){
    
    var re = new RegExp("(^|\\s)" + c + "(\\s|$)", "g")
    
    if (re.test(o.className)) return
    
    o.className = (o.className + " " + c).replace(/\s+/g, "").replace(/(^ | $)/g, "")
  
  },removeClass: function(o, c){
    
    var re = new RegExp("(^|\\s)" + c + "(\\s|$)", "g")
    
    o.className = o.className.replace(re, "$1").replace(/\s+/g, " ").replace(/(^ | $)/g, "")
  
  }
  
}