/* ===========================================================
 * jquery-square_menu.js v1
 * ===========================================================
 * Copyright 2013 Pete Rojwongsuriya.
 * http://www.thepetedesign.com
 *
 * Recreate a full page animated menu like you see 
 * Square Website using jQuery and CSS3
 *
 * https://github.com/peachananr/square_menu
 *
 * ========================================================== */

!function(jQuery){
  
  var defaults = {
    flyDirection: "bottom",
    button: "Menu",
    animationStyle: "vertical",
    closeButton: "X"
	};
	
  
  jQuery.fn.square_menu = function(options){
    var settings = jQuery.extend({}, defaults, options),
        el = jQuery(this);  
    
    jQuery("body").addClass("fsm-body")
    
    el.addClass("sm-menu sm-"+settings.flyDirection+ " sm-" + settings.animationStyle)
    el.wrapInner("<div class='sm-wrapper'></div>")
    
    el.find(".menusection").addClass("sm-nav").wrapInner("<div class='nav-inner'></div>");
    
    jQuery.fn.closeMenu = function() {
      
      jQuery(".sm-menu").addClass("animated").removeClass("re-rotate")
      jQuery(".sm-menu .sm-close").remove()
      jQuery(".sm-menu .sm-nav:first-child .nav-inner").removeClass("animated flyInLeft")
      jQuery(".sm-menu .sm-nav:nth-child(2) .nav-inner").removeClass("animated flyInRight")
      
      if (jQuery("body").hasClass("sm-activate")) {
        jQuery(".sm-menu").find(".sm-nav").removeClass("animated")
        jQuery(".sm-menu").one("transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd", function(){
          jQuery("body").removeClass("sm-activate").find(".sm-menu").removeClass("ff-hack")
        });
      }
    }
    
    jQuery.fn.openMenu = function() {
      
      jQuery(".sm-menu").addClass("animated").addClass("ff-hack")
      
      if (!jQuery("body").hasClass("sm-activate")) {
        if (jQuery(".sm-overlay").length < 1) jQuery("<div class='sm-overlay'></div>").hide().prependTo("body")
        jQuery(".sm-overlay").fadeIn("fast", function() {
          jQuery("body").addClass("sm-activate")
          var fired = false;
          jQuery("body").one("transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd", function(e){
            if ( ! fired ) {
                fired = true;
                jQuery(".sm-menu .sm-nav").addClass("animated");
                jQuery(".sm-menu .sm-nav").one("transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd", function(e){
                  if (e.originalEvent.propertyName == '-webkit-transform' || e.originalEvent.propertyName == 'transform' || e.originalEvent.propertyName == '-o-transform' || e.originalEvent.propertyName == '-moz-transform') {
                    jQuery(".sm-menu").addClass("re-rotate")
                    if (settings.closeButton != false) {
                      el.prepend("<a href='#' class='sm-close'>" + settings.closeButton + "</a>")

                      jQuery(".sm-close").click(function() {
                        el.closeMenu();
                        return false;
                      });
                    }

                    jQuery(".sm-menu .sm-nav:first-child .nav-inner").addClass("animated flyInLeft")
                    jQuery(".sm-menu .sm-nav:nth-child(2) .nav-inner").addClass("animated flyInRight")
                    jQuery(".sm-overlay:not(.clicked)").addClass("clicked").click(function() {
                      el.closeMenu();
                    });
                  }
                });
            }
          });
        });
      } else {
        el.closeMenu();
      }
    }
    
    
    jQuery(".sm-button").click(function() {
      el.openMenu();
      return false;
    });
  }
}(window.jQuery);
jQuery(document).ready( function() {
	    jQuery(".sidemenu").square_menu();
	  });
jQuery(document).ready(function(){	  
var melobar = function() { 
	if (window.matchMedia('(max-width: 890px)').matches) {
		jQuery('.sm-menu .left .nav-inner, .sm-menu .right .widget').prependTo('#rightbar');
	}
};
  	jQuery(window).resize(melobar);
  	melobar();  
});
