var user_idlanguageinterface = '';
var user_idlanguagecontent = '';
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./) || window.navigator.userAgent.indexOf("Edge/18") > -1 )  // If Internet Explorer or old edge
{ 
	$('.fa-globe-americas-scale').css('margin-left',0);
}

function checkUnsavedData(){
	var doAction = true;
	if (typeof existUnsavedData !== 'undefined' && existUnsavedData && typeof gConfirmsavelesValue !== 'undefined' && gConfirmsavelesValue !== null){
		doAction = confirm(gConfirmsavelesValue);
	}
	return doAction;
}

function getIsMenuWide(){
	var liTopDiff = true;
	var pT = -999999;
	$('nav#menu>ul>li').each(function( index ) {
		if (pT !== -999999) {
			liTopDiff = (liTopDiff && pT == $( this ).position().top);  
		}
		pT = $( this ).position().top;
	});
	return !liTopDiff;
}

function init() {
	user_idlanguageinterface = $('select[name=user_idlanguageinterface]').val();
	user_idlanguagecontent = $('select[name=user_idlanguagecontent]').val();
}

$(document).ready(function(){
	init(); 

	$('#languagesBox').css('visibility','visible');
	
	$("#languagesBox").dialog({ 
		autoOpen:	false
	});

	$('#languagesbox-button-cancel').click(function(){
		$("#languagesBox").dialog('close'); 
	});

	$('select[name=user_idlanguageinterface]').change(function() { 
		ShowModalWindow(true);
		$('select[name=user_idlanguagecontent]').load(_GLangsSelectUrl + '?user_idlanguageinterface=' + this.value + '&user_idlanguagecontent=' + $("#user_idlanguagecontent").val());
		ShowModalWindow(false);	
		user_idlanguageinterface = $(this).val(); 
	});

	$('select[name=user_idlanguagecontent]').change(function() { 
		user_idlanguagecontent = $(this).val(); 
	});
	
	$('#languagesbox-button-save').unbind();
	$('#languagesbox-button-save').click(function(){
		var url = _GLangSetUrl + "&user_idlanguageinterface=" + user_idlanguageinterface + "&user_idlanguagecontent=" + user_idlanguagecontent;
		$.post(url, function( data ) {
		    var currentlocation = location.href;//.toLowerCase();
			var newlocation = currentlocation.replace(/&?lang=([^&]$|[^&]*)/i, "");
			if (typeof ShowModalWindow === "function") ShowModalWindow(true);
			$('.ui-widget-overlay').unbind('click');
			if (newlocation != currentlocation) {
				window.location = newlocation; 
			} else {
				location.reload();
			}
		});
	});

	$('#btn-languagesBox').unbind();
	$('#btn-languagesBox').click(function(){
		if (checkUnsavedData()) {
			var p = $(this).position();
			var h = $(this).height();
			// Task 59610 set visible to titlebar
			//$(".ui-dialog-titlebar").hide();
			$("#languagesBox").dialog({ 
				title:		gLanguagesBoxTitle
			,	width:		320
			,	height:		_GLangBoxHeight
			,	autoOpen:	false
			,	closeOnEsc:	true
			,	modal:		true
			,   resizable: false
			,	draggable: false
			, 	position: [p.left - 320 + h + 15, 40]
			,	open: function(event, ui) 
				{ 
					$('.ui-widget-overlay').bind('click', function()
					{ 
						$("#languagesBox").dialog('close'); 
					}); 
					setTimeout(function(){ $('.ui-dialog').resize(); $(window).resize(); }, 500);
				}
			}).dialog("open");
		}
	});

	function rebuildMenu(){
		if (!getIsMenuWide()) {
			$('#menu>ul>li.active>a>span').removeClass('li_active_a_span_wide').addClass('li_active_a_span');
			$('#menu').removeClass('mune_wide_height_wide').addClass('mune_wide_height');
			if ($('#box-poll-bread-crumbs').length) {
				$('#box-menu').removeClass('box-mune_wide_height_wide-admin').addClass('box-mune_wide_height-admin');
				$('#btn-tree-open').removeClass('btn-tree-open-widemenu-poll').addClass('btn-tree-open-poll');		
				$('.btn-rigth-first').removeClass('btn-review-widemenu-poll').addClass('btn-review-poll');		
				$('.btn-rigth-second').removeClass('btn-discussion-widemenu-poll').addClass('btn-discussion-poll');		
			} else {
				if ($('#box-tabs').length) {
					$('#box-menu').removeClass('box-mune_wide_height_wide-admin').addClass('box-mune_wide_height-admin');
				} else {
					$('#box-menu').removeClass('box-mune_wide_height_wide').addClass('box-mune_wide_height');
				}
				$('#btn-tree-open').removeClass('btn-tree-open-widemenu-admin').addClass('btn-tree-open-usual');			
				$('.btn-rigth-first').removeClass('btn-review-widemenu').addClass('btn-review-usual');
				$('.btn-rigth-second').removeClass('btn-discussion-widemenu').addClass('btn-discussion-usual');
			}
		} else {
			$('#menu>ul>li.active>a>span').removeClass('li_active_a_span').addClass('li_active_a_span_wide');
			$('#menu').removeClass('mune_wide_height').addClass('mune_wide_height_wide');
			if ($('#box-poll-bread-crumbs').length) {
				$('#box-menu').removeClass('box-mune_wide_height-admin').addClass('box-mune_wide_height_wide-admin');
				$('#btn-tree-open').removeClass('btn-tree-open-poll').addClass('btn-tree-open-widemenu-poll');
				$('.btn-rigth-first').removeClass('btn-review-poll').addClass('btn-review-widemenu-poll');
				$('.btn-rigth-second').removeClass('btn-discussion-poll').addClass('btn-discussion-widemenu-poll');
			} else {
				if ($('#box-tabs').length) {
					$('#box-menu').removeClass('box-mune_wide_height-admin').addClass('box-mune_wide_height_wide-admin');
				} else {
					$('#box-menu').removeClass('box-mune_wide_height').addClass('box-mune_wide_height_wide');
				}					
				$('#btn-tree-open').removeClass('btn-tree-open-usual').addClass('btn-tree-open-widemenu');
				$('.btn-rigth-first').removeClass('btn-review-usual').addClass('btn-review-widemenu');
				$('.btn-rigth-second').removeClass('btn-discussion-usual').addClass('btn-discussion-widemenu');
			}
		}
	}

	(function ($) {
		$.fn.sizeChanged = function (handleFunction) {
			var element = this;
			var lastWidth = element.width();
			var lastHeight = element.height();
		
			setInterval(function () {
				if (lastWidth === element.width()&&lastHeight === element.height())
					return;
				if (typeof (handleFunction) == 'function') {
					handleFunction({ width: lastWidth, height: lastHeight },
								   { width: element.width(), height: element.height() });
					lastWidth = element.width();
					lastHeight = element.height();
				}
			}, 100);
			return element;
		};
	}(jQuery));

	$( '#box-menu').sizeChanged(function() {
		rebuildMenu();
		if ($('#container.layout-resizing-hook').length) {
			$('#container').layout().resizeAll();
		}
	});

	rebuildMenu();
	refreshMainMenuPollsCount();
	refreshMainMenuNewMessagesCount();
});

$("#search-link").unbind('click');
$("#search-link").bind('click',function (){
	$("#hidepanel").attr("value","0");
	$("#searchform").submit();
});

$("#searchform").submit(function(e){
	if (checkUnsavedData()) {
		ShowModalWindow(true);
		return true;
	}else {
		e.preventDefault();
		return false;
	}
});