/*--------------------------------------------------------------
# Copyright (C) joomla-monster.com
# License: http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
# Website: http://www.joomla-monster.com
# Support: info@joomla-monster.com
---------------------------------------------------------------*/

// Sticky Bar
var JMstickyTimer;
var JMbody;
var JMallpage;
var JMbar;
var JMbarHeight;
var JMbarOffset;
var JMscrollOffset;
var JMwindowHeight;
var JMbarOffsetAndBarHeight;

function JMresizeFunction() {
	if(JMbody.hasClass('sticky-bar') && !JMbody.hasClass('off-canvas')) {
		if (JMbar.length) {
			if(JMbarOffsetAndBarHeight >= (JMwindowHeight + JMscrollOffset)) {
				JMbody.addClass('make-sticky-bottom');
				JMallpage.css('padding-top', '0px');
				//console.log('Make menu sticky bottom');
			} else {
				if( JMbarOffset.top == 0 || JMscrollOffset > JMbarOffset.top) {
					JMbody.addClass('make-sticky-top');
					JMallpage.css('padding-top', (JMbarHeight) + 'px');
					//console.log('Make menu sticky top');

				} else {
					JMbody.removeClass('make-sticky-top make-sticky-bottom');
					JMallpage.css('padding-top', '0px');
					//console.log('Make menu static');
				}
			}
		}
	}
};

jQuery(window).load(function(){
	JMbody = jQuery('body');
	JMallpage = jQuery('#jm-allpage');
	JMbar = JMallpage.find('#jm-logo-nav');
	JMbarHeight = JMbar.outerHeight();
	JMbarOffset = JMbar.offset();
	JMwindowHeight = jQuery(window).height();
	JMbarOffsetAndBarHeight =  JMbarOffset.top + JMbarHeight;
	
	//console.log('Start window height ' + JMwindowHeight);
	//console.log('Start barOffset ' + JMbarOffset.top);
	
	if (JMstickyTimer != 'undefined') {
		clearTimeout(JMstickyTimer);
	}
	
	JMstickyTimer = setTimeout(function(){
		
		JMbarOffset = JMbar.offset();
		JMscrollOffset = jQuery(window).scrollTop();
		JMresizeFunction();
		
	}, 50); 
	
	
	jQuery(window).scroll(function () {
		if (JMstickyTimer != 'undefined') {
			clearTimeout(JMstickyTimer);
		}
		JMstickyTimer = setTimeout(function(){
			
			JMscrollOffset = jQuery(window).scrollTop();
			//console.log('Scroll scrollOffset ' + JMscrollOffset);
			
			JMresizeFunction();
			
		}, 10); 
	
	});
	
	jQuery(window).resize(function() {
		if (JMstickyTimer != 'undefined') {
			clearTimeout(JMstickyTimer);
		}
		JMstickyTimer = setTimeout(function(){
			
			JMbarOffset = JMbar.offset();
			JMscrollOffset = jQuery(window).scrollTop();
			JMwindowHeight = jQuery(window).height();
			JMresizeFunction();
			
		}, 50); 
		
	});
	
});