/*--------------------------------------------------------------
# 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
---------------------------------------------------------------*/

//Set Module's Height script

function setModulesHeight() {
	var regexp = new RegExp("_mod([0-9]+)$");

	var jmmodules = jQuery(document).find('.jm-module') || [];
	if (jmmodules.length) {
		jmmodules.each(function(index,element){
			var match = regexp.exec(element.className) || [];
			if (match.length > 1) {
				var modHeight = parseInt(match[1]);
				jQuery(element).find('.jm-module-in').css('height', modHeight + 'px');
			}
		});
	}
}

jQuery(document).ready(function(){
	
	setModulesHeight();
	
  //jQuery 'Back to Top' script
  jQuery('#jm-back-top a').click(function () {
      jQuery('body,html').animate({
          scrollTop: 0
      }, 800);
      return false;
  });
	
});

//header height
var resizeCache;
function checkheight() {
	var headerbg = jQuery('#jm-header-bg');
	var headercontent = jQuery('#jm-header-content');
	
  if (headerbg.length > 0 && headercontent.length > 0) {
		var offset = headercontent.outerHeight();
		headerbg.css('min-height', (offset) + 'px');
	}
};

//counter
var counted = false;
function countnow() {
	
	var moduleTarget = jQuery('.jm-counter .jm-count');
  if (!moduleTarget.length) return;

	var objectPosition = moduleTarget.offset();
	var scrollWindow = jQuery(window).scrollTop();
	var screenHeight = jQuery(window).height();
	if (objectPosition.top < (scrollWindow + screenHeight)) {
		if(!counted) {
			moduleTarget.countTo();
			counted = true;
		}
	}
	
};

jQuery(window).load(function() {
	checkheight();
  countnow();
});

jQuery(window).resize(function() {
	clearTimeout(resizeCache);
  resizeCache = setTimeout(checkheight, 30);
});

jQuery(window).scroll(function() {
	countnow();
});