// scripting here
(
	function ($) {
		$.fn.serializeObject = function () {

			var o = {};
			var a = this.serializeArray();
			$.each(a, function () {
				if (o[this.name]) {
					if (!o[this.name].push) {
						o[this.name] = [o[this.name]];
					}
					o[this.name].push(this.value || '');
				} else {
					o[this.name] = this.value || '';
				}
			});
			return o;
		};
	}
)(jQuery);

function squareClass () {
	$('.square').each(
		function (i, obj) {
			$(obj).height($(obj).width());
		}
	);
}

function halfsquareClass () {
	$('.half-square').each(
		function (i, obj) {
			$(obj).height(($(obj).width()/2) - 15);
		}
	);
}

function getTimeRemaining(endtime) {
  var t = Date.parse(endtime) - Date.parse(new Date());
  var seconds = Math.floor((t / 1000) % 60);
  var minutes = Math.floor((t / 1000 / 60) % 60);
  var hours = Math.floor((t / (1000 * 60 * 60)) % 24);
  var days = Math.floor(t / (1000 * 60 * 60 * 24));
  return {
    'total': t,
    'days': days,
    'hours': hours,
    'minutes': minutes,
    'seconds': seconds
  };
}

function initializeClock(id, endtime) {
  var clock = document.getElementById(id);
  var daysSpan = clock.querySelector('.days');
  var hoursSpan = clock.querySelector('.hours');
  var minutesSpan = clock.querySelector('.minutes');
  var secondsSpan = clock.querySelector('.seconds');

  function updateClock() {
    var t = getTimeRemaining(endtime);

    daysSpan.innerHTML = t.days;
    hoursSpan.innerHTML = ('0' + t.hours).slice(-2);
    minutesSpan.innerHTML = ('0' + t.minutes).slice(-2);
    secondsSpan.innerHTML = ('0' + t.seconds).slice(-2);

    if (t.total <= 0) {
      clearInterval(timeinterval);
    }
  }

  updateClock();
  var timeinterval = setInterval(updateClock, 1000);
}

function responsiveSponsors() {
	if ($(window).width() <= 400) {
		$('.mod-sponsors .platinium .logo a img').addClass('img-responsive').css({'margin': 'auto', 'max-width': '270px'});
		$('.mod-sponsors .gold .logo a img').addClass('img-responsive').css({'margin': 'auto', 'max-width': '170px'});
		$('.mod-sponsors .silver .logo a img').addClass('img-responsive').css({'margin': 'auto', 'max-width': '100px'});
	} else {
		$('.mod-sponsors .platinium .logo a img').removeClass('img-responsive').css({'margin': 'inherit', 'max-width': '350px'});
		$('.mod-sponsors .gold .logo a img').removeClass('img-responsive').css({'margin': 'inherit', 'max-width': '250px'});
		$('.mod-sponsors .silver .logo a img').removeClass('img-responsive').css({'margin': 'inherit', 'max-width': '180px'});
	}
}

$(document).ready(function(){
	squareClass();
	$(window).resize(function() {squareClass();});

	halfsquareClass();
	$(window).resize(function() {halfsquareClass();});

	if ($(".mod-banner").length > 0) {
		var deadline = new Date(Date.parse("Apr 14 2018 8:00:00 GMT") - 60*60*1000);
		initializeClock('clockdiv', deadline);
	}

	// CONTACTS FORM
	$("body").on(
		"click",
		".contacts form button[type=submit]",
		function () {
			$.post(
				$(".contacts form").attr("action"),
				$(".contacts form").serializeObject(),
				function (data) {
					data = $.parseJSON(data);

					$(".contacts .form-result").html(data.message);

					if (data.status == true) {
						$(".contacts form")[0].reset();
					}

					setTimeout(function () {$(".contacts .form-result").html("");}, 2500);
				}
			);

			return false;
		}
	);
/*
	$('body').scrollspy({target: 'navBar', offset: 50});
		$('a').on('click', function(event) {
			if (this.hash !== "") {
				event.preventDefault();

				var hash = this.hash;

				$('html, body').animate({
					scrollTop: $(hash).offset().top
				}, 1200, function() {

				window.location.hash = hash;
			});
		}
	});
*/
	if ($(".fancybox").length > 0) {
		$(".fancybox").fancybox();
	}

	responsiveSponsors();
	$(window).resize(function() {
		responsiveSponsors();
	});

	var menuButton = document.getElementById('menuButton');
	menuButton.addEventListener('click', function (e) {
	    menuButton.classList.toggle('is-active');
	    e.preventDefault();
	});

});
