$(document).ready(function() {	
	$('.countdown-timer').countdown(countdown.toDate(), function(event) {
	    var $this = $(this).html(event.strftime(''
				+ '<ul class="countdown flex-c-c">'
	      + '<li><span class="weeks">%w</span><p class="weeks_ref">weeks</p></li>'
	      + '<li><span class="days">%d</span><p class="days_ref">days</p></li>'
				+ '<li><span class="hours">%H</span><p class="hours_ref">hours</p></li>'
				+ '<li><span class="minutes">%M</span><p class="minutes_ref">minutes</p></li>'
				+ '<li><span class="seconds">%S</span><p class="seconds_ref">seconds</p></li>'
				+ '</ul>'
			 ));
	});
	
	function ScrolClass() {
		if($(this).scrollTop() >= 50) {
			$('.topMenu').addClass('topMenu-fixed');
		} else {
			$('.topMenu').removeClass('topMenu-fixed');
		}
  }
  $(window).scroll(function() {
    ScrolClass();
  });
  $(document).ready(function() {
    ScrolClass();
  });
	
	$(".buttonDrop").click(function(){
		$("."+$(this).attr("data-class")).toggleClass("active");
		$(this).toggleClass("active");
	});

	$('.tab-button').click(function(){
		$('.tab-button').removeClass('active');
		$(this).addClass('active');
		$('.tab-block').removeClass('active');
		$('#'+$(this).attr('data-tab')).addClass('active');
	});
	$('.tab-button-media').click(function(){
		$('.tab-button-media').removeClass('active');
		$(this).addClass('active');
		$('.tab-block-media').removeClass('active');
		$('#'+$(this).attr('data-tab')).addClass('active');
	});
	
	$('.toTop').click(function() {
      $('body,html').animate({scrollTop:0},800);
  });
	
	$('.rang-button').click(function(){
		$('.rang-button').removeClass('active');
		$(this).addClass('active');
		$('.rangBlock').removeClass('active');
		$('#'+$(this).attr('data-tab')).addClass('active');
	})

	var res = $(".dropDown-menu");
	$('[data-class^="m"]').on("click", funk);
	$(document).click(function(e) {
		if ($(e.target).closest(res).length || $(e.target).closest('.menu-a').length) return;
		res.fadeOut(100);
	});
	function funk(){
		$(this).toggleClass("show");
		var link = $(this).attr('data-class'),
				el = $('.dropDown-menu.'+link);
		if(el.css("display") == "none"){
			res.hide();
			el.slideToggle(400);
		}
		else{
			el.slideToggle(400);
		}
	}
	
	var overlay = $('#overlay2'); 
  var open_modal = $('.open_modal'); 
  var close = $('.modal_close, #overlay'); 
  var modal = $('.modal_div'); 
	open_modal.click( function(event){ 
		 event.preventDefault(); 
		 var div = $(this).attr('href'); 
		 overlay.fadeIn(400, 
				 function(){ 
						 $(div) 
								 .css('display', 'block') 
								 .animate({opacity: 1, top: '15%'}, 200); 
		 });
	});

	close.click( function(){ 
				modal 
				 .animate({opacity: 0, top: '15%'}, 200, 
						 function(){ 
								 $(this).css('display', 'none');
								 overlay.fadeOut(400); 
						 }
				 );
	});
	 
	$('#submit_payment_method').on('submit', function(e){
		e.preventDefault();
		var method = $('#payment_method option:selected').val();
		var pack = $('#pack').val();
		window.location.href = base_url + 'donate/'+encodeURIComponent(method)+'/'+encodeURIComponent(pack);
	});
	
	var swiper = new Swiper('.swiper-slider', {
		navigation: {
			nextEl: '.swiper-button-next',
			prevEl: '.swiper-button-prev',
		},
		pagination: {
			el: '.swiper-pagination',
			clickable: 'true',
		},
		speed: 1000,
		autoplay: true,
	});
	
	if (typeof server_events !== 'undefined') { 
		var initEvents = function() {
			var eventBody = $('.eventBody');
			$.each(server_events, function (eventName, event) {
				var eventClass = eventName.replace(/\s/g,'')+'-Event';
				eventBody.append("<div class='tableBlock-tr "+eventClass+"'><div class='tableBlock-td td-50'><span class='top-event-name'>"+eventName+"</span></div><div class='tableBlock-td td-50'><span class='top-event-time eventTime'>-</span></div></div>")
			});
		}

		var eventUpdate = function() {
			moment.tz.setDefault("Europe/Riga");

			var st = moment();
			var et = moment().endOf('day')

			var todayWeekday = st.day();

			var secondsUntilEndOfDay = moment.duration(et.diff(st)).asSeconds();

			$.each(server_events, function (eventName, event) {

				var eventClass = eventName.replace(/\s/g,'')+'-Event';
				var closestEvent = false;
				var eventDuration = false;
				var firstEvent = false;
				var dayIsSet = false;

				// search for closest time for each event
				$.each(event.times, function(index, time){
					var t = moment(time, 'hh:mm');
					var duration = moment.duration(st.diff(t));
					var diff = duration.asSeconds();

					if((closestEvent == false && diff < 0) || (diff > closestEvent && diff < 0)){
						closestEvent = diff;
						eventDuration = duration;
					}
				});

				// if no event left for today, set first event for next day
				if(eventDuration == false){

					$.each(event.times, function(index, time){
						var h = time.substr(0, 2);
						var m = time.substr(3, 2);
						var t = moment().add(secondsUntilEndOfDay, 'seconds').add({hours: h, minutes: m});
						var duration = moment.duration(st.diff(t));

						eventDuration = duration;

						return false;

					});

				}

				var hours = Math.abs(eventDuration.hours());
				var minutes = Math.abs(eventDuration.minutes());
				var seconds = Math.abs(eventDuration.seconds());

				if(event.days != 0){
					$.each(event.days, function(index, day){

						if(todayWeekday < day){

							var wd = moment().weekday(day-1).add({hours: hours, minutes: minutes, seconds: seconds});

							var duration = moment.duration(st.diff(wd));

							eventDuration = duration;

							dayIsSet = true;

							return false;
						}

					});

					if(dayIsSet == false){
						$.each(event.days, function(index, day){
								var wd = moment().day(7+day).add({hours: hours, minutes: minutes, seconds: seconds});
								var duration = moment.duration(st.diff(wd));
								eventDuration = duration;
								return false;
						});
					}

				}

				var days = Math.abs(eventDuration.days());

				var eventTime = '';

				if(days > 0)
					eventTime = eventTime + days + 'd ';
				if(hours > 0)
					eventTime = eventTime + hours + 'h ';

				eventTime = eventTime + minutes + 'm ' + seconds + 's';

				var eventTimeString = eventTime;

				var openText = event.openText == true ? '(Opened) ' : '';

				if(days == 0 && hours == 0 && 60 > minutes)
					eventTimeString = "<span style='color:#ffb86b;'>"+eventTime+"</span>";

				if(days == 0 && hours == 0 && 30 > minutes)
					eventTimeString = "<span style='color:#fff548;'>"+eventTime+"</span>";

				if(days == 0 && hours == 0 && event.toOpen > minutes)
					eventTimeString = "<span style='color:#abff29;'>"+openText+eventTime+"</span>";

				$('.'+eventClass).find('span.eventTime').html(eventTimeString);

			});

		};
		initEvents();
		setInterval(eventUpdate, 1000);
	}
});

$.fn.extend({
    switchLanguage: function(lang){
			this.setCookie('dmn_language', lang, 10);
			window.location.reload();
		},
		setCookie: function (key, value, days) {
			var expires = new Date();
			expires.setTime(expires.getTime() + (days * 24 * 60 * 60 * 1000));
			document.cookie = key + '=' + encodeURIComponent(String(value)) + ';expires=' + expires.toUTCString();
		},
		readCookie: function (key){
			var result;
			return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? (result[1]) : null;
		},
		topPlayers: function(div, server, ml, res, gres, flag){
			$.ajax({
				type: 'POST',
				dataType: 'json',
				url: base_url + 'rankings/topPlayersWidget?server='+server,
				success: function (data) {
					if (typeof data !== 'undefined' && data != null) {
						if (data.error) {
							if ($.isArray(data.error)) {
								$.each(data.error, function (key, val) {
									console.log(val);
								});
							}
							else{
								console.log(data.error);
							}
						}
						else{
							if(data != false){
								html = '';
								if ($.isArray(data)) {
									i = 0;
									$.each(data, function (key, val) {
										i++;
										mlc = '';
										gresc = '';
										flags = '';
										if(ml == 1){
											mlc = '<sup>' + val.mlevel + '</sup>';
										}
										if(gres == 1){
											gresc = '<sup>' + val.gresets + '</sup>';
										}
										if(flag == 1){
											flags += '<span class="f16"><span class="flag ' +val.country[0]+ '"></span></span>';
										}
										html += '<li class="top-list">';
										html += '<span class="top-number">'+ i + '.</span><span class="top-flag">' +flags+ '</span><span class="top-name"><a href="' + base_url + 'info/player/' + val.name + '-id' + val.id +'/' + server + '">' + val.name + '</a></span>';
										html += '<span class="top-lvl">' + val.level + mlc +'</span>';
										if(res == 1){
											html += '<span class="top-lvl">' + val.resets + gresc +'</span>';
										}
										html += '</li>';
									});
								}
								$('#'+div).html(html);
							}
							else{
								$('#'+div).html('No Data');
							}
						}
					}
				}
			});
		},
		topGuilds: function(div, server, display_level){
			$.ajax({
				type: 'POST',
				dataType: 'json',
				url: base_url + 'rankings/topGuildsWidget?server='+server,
				success: function (data) {
					if (typeof data !== 'undefined' && data != null) {
						if (data.error) {
							if ($.isArray(data.error)) {
								$.each(data.error, function (key, val) {
									console.log(val);
								});
							}
							else{
								console.log(data.error);
							}

						}
						else{
							if(data != false){
								html = '';
								if ($.isArray(data)) {
									i = 0;
									$.each(data, function (key, val) {
										i++;
										html += '<li class="top-list">';
										html += '<span class="top-number">' +i+ '. </span><span class="top-name"><a href="' + base_url + 'info/guild/' + val.name + '-id' + val.gid + '/' + server + '">' + val.name + '</a></span>';
										if(display_level == 1){
											html += '<span class="score">' + val.level +'</span>';
										}
										html += '</li>';
									});
								}
								$('#'+div).html(html);
							}
							else{
								$('#'+div).html('No Data');
							}
						}
					}
				}
			});
		},
		initTippy: function(img = false){
			tippy('div.grid-item, div.grid-item-modern, .market_item, span[id^="market_item_"], #inventoryc div', {
        theme: 'item',
        delay: 150,
        interactive: true,
        duration: 500,
        animation: 'scale',
        inertia: true,
				arrow: true,
				hideOnClick: false,
        onShow(instance) {
					instance.setContent('<i class="fa fa-spinner fa-spin"></i> Loading');
					
					if(instance.state.ajax === undefined){
						instance.state.ajax = {
							isFetching: false,
							canFetch: true,
						}
					}
					
					if(instance.state.ajax.isFetching || !instance.state.ajax.canFetch){
						return;
					}
					
					var is_image = '&image=false';
					if(img == true){
						is_image = '&image=true';
					}
					
					fetch(base_url + 'warehouse/item?id=' + $(instance.reference).data('id') + '&server=' + $(instance.reference).data('server') + is_image)
					.then(function(response){
						return response.json();
					})
					.then(function(json){
						if('error' in json){
							instance.setContent("<span style='color:red;'>" + json.error + "</span>");
						} 
						else{
							instance.setContent(json.tooltip);
						}
					})
					.finally(() => {
						instance.state.ajax.isFetching = false;
					})
        },
				onHidden(instance) {
					instance.setContent('<i class="fa fa-spinner fa-spin"></i> Loading');
					instance.state.ajax.canFetch = true;
				},
			});
		}
});
