﻿var SessionCounter = 0;
$(document).ready(
	function()
	{
		window.setInterval("SessionTicker()", 10000);
//		var jScriptParams = $("#jScriptParams").text();

		// MinorWarning
		if ($("#MinorWarning").text().length > 0)
		{
			alert($("#MinorWarning").text());
		}
		// IE compatilbility, ræður ekki við CSS:empty
		var _majorWarning = $("#MajorWarning");
		if (_majorWarning.text().length > 0)
		{
			_majorWarning.show();
		}

		// HelperModule
		var _helperModule = $("#HelperModule");
		if (_helperModule.text().length > 0)
		{
			var _imgHelperModule = $("#imgHelperModule");
			_imgHelperModule.fadeIn(2000);
			_imgHelperModule.bind("mouseover",
			function(ev)
			{
				_helperModule.slideDown();
				// og auto hide á glugga
				_helperModule.bind("mouseout",
				function(ev)
				{
					$(this).slideUp();
				});
			});
		}

		//EditorForGroups menu
		if ($("#EditorForGroups").length > 0)
		{
			$("#Center #ImgEditor").show();
			$("#Center #ImgEditor").bind("click", EFG_ToggleMenu);
		}

		// UsersInList hack
		$(".UserBasedList .Outer a:nth-child(2)").css("font-size", "125%");

		//Search
		$("#Right #Base_Query").bind("mouseover", FocusInput);
		$("#Right #Base_Query").bind("focus", ToggleInputBackground);
		$("#Right #Base_Query").bind('keyup',
			function(ev)
			{
				var code = (ev.keyCode ? ev.keyCode : ev.which);
				if (code == 13)
				{
					if ($(this).val().length >= 3)
					{
						$(this).css("cursor", "progress");
						$("#Center").css("cursor", "progress");
						window.location.href = GetBaseUrl() + "Search.aspx?isquery=true&q=" + $(this).val();
					}
					else
					{
						if ($(this).val().length == 0)
						{
							window.location.href = GetBaseUrl() + "Search.aspx";
						}
						else
						{
							alert("Leitarorð lágmark 3 stafir, tómur leitarstrengur færir á efnisyfirlit");
						}
					}
				}
			}
		);

		// Sýna CurrentGroup linka
		$("#Left .CurrentGroup p.CG1").mouseover(
			function(ev)
			{
				$("#Left .CurrentGroup p.LinksUnder").fadeIn();
			});

		$("#Center .Leyniord").bind('keyup',
			function(ev)
			{
				var code = (ev.keyCode ? ev.keyCode : ev.which);
				if (code == 13)
				{
					$("#imgLogin").click();
				}
			});


		// LOGIN reitir
		$("#Center .Username").bind("mouseover", FocusInput);
		$("#Center .Leyniord").bind("mouseover", FocusInput);
		$("#Center .Username").bind("focus", ToggleInputBackground);
		$("#Center .Leyniord").bind("focus", ToggleInputBackground);

		// USERBAR try login
		$("#imgLogin").click(
			function(ev)
			{
				$(this).css("cursor", "progress");
				$("#Center").css("cursor", "progress");
				var _username = $("#Center .Username").val()
				var _leyniord = $("#Center .Leyniord").val()
				if (_username.length == 10 && _leyniord.length >= 3)
				{
					$.ajax({
						type: "POST",
						contentType: "application/json; charset=utf-8",
						url: GetBaseUrl() + "Base.asmx/TryLogin",
						data: $.toJSON({ "Username": _username, "Password": _leyniord }),
						dataType: "json",
						success: function(iUsersID)
						{
							if (iUsersID.d > 0)
							{
								//smá fiff til að laga % í lok á url eins og getur gerst á leitarsíðunum
								var _location = decodeURIComponent(window.location);
								_location = _location.lastIndexOf("%") == _location.length - 1 ? _location += "25" : _location;
								window.location.href = GetBaseUrl() + "Login.aspx?ws=" + iUsersID.d + "&remember=" + $("#cbMuna").is(":checked") + "&goto=" + encodeURIComponent(_location);
								return false;
							}
							else
							{
								alert("Innskráning tókst ekki, röng kennitala og/eða leyniorð!");
							}
						},
						error: function()
						{
							alert("Villa við innskráningu, vinsamlegast hafðu samband við skrifstofu LÍ");
						}
					});
				}
				else
				{
					alert("Innskráning með 10 stafa kennitölu og leyniorði, hafðu samband við LÍ 564-4100 ef týnt leyniorð");
				}
			}
		);

		// USERBAR group select
		$("#Left .LILogo").mouseenter(
			function()
			{
				$("#Left .GroupMenu").show("fast");
			}
		);
		$("#Left .GroupMenu").mouseleave(
			function()
			{
				$(this).stop().removeAttr("style").hide();
				$(this).siblings(".Logo").show();
			}
		);

		// init Menu
		$('ul.sf-menu').superfish();

		// init jqTabs
		$("#tabs").tabs();

		// datepicker
		$(".DatePicker").datepicker({
			changeYear: true,
			constrainInput: true,
			dateFormat: 'dd.mm.yy'
		});

		// init HtmlAreas
		$(".jHtmlArea_Min").htmlarea({
			css: "../ui/Base.css",
			toolbar: ["html", "bold", "italic", "underline", "|", "link", "unlink", "image"]
		});
		$(".jHtmlArea_Max").htmlarea({
			css: "../ui/Base.css"
		});
	}
);
//======================
function EFG_ToggleMenu(ev)
{
	$("#EditorForGroups").css("left", ev.pageX + 6); 	//+6 svo það sé alltaf smá pláss til að klikka aftur á img
	$("#EditorForGroups").css("top", ev.pageY - $("#EditorForGroups").height());
	$("#EditorForGroups").toggle("fast");
	//$("#EditorForGroups").css("display","block");
}

function ToggleInputBackground()
{
	$(this).css("background-image", "none");
}
function FocusInput()
{
	$(this).focus();
}
function GetBaseUrl()
{
	var strBaseLocation;
	if (window.location.host.indexOf("localhost") >= 0)
	{
		strBaseLocation = "http://" + window.location.host + "/lis.is/";
	}
	else
	{
		if (window.location.host.indexOf("www.lis.is") >= 0)
		{
			strBaseLocation = "http://www.lis.is/";
		}
		else
		{
			strBaseLocation = "http://lis.is/";
		}
	}
	return strBaseLocation;
}

function FU_Selected()
{
	if ($("#FindUser select option:selected").val() == -1)
	{
		alert("Veldu notanda úr listanum hér að neðan");
		return false;
	}
	return true;
}

String.prototype.bool = function()
{
	return (/^true$/i).test(this);
};


//http://stackoverflow.com/questions/901115/get-querystring-with-jquery
function jRQ(name)
{
	name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	var regexS = "[\\?&]" + name + "=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(window.location.href);
	if (results == null)
		return "";
	else
		return results[1];
}

function SessionTicker()
{
	var SessionCounter = parseInt($("#SessionCounter").val());
	SessionCounter++;
	$("#SessionCounter").val(SessionCounter);
	if (SessionCounter > 180)	// tikkar hverjar 10sek og login er 30mín => 180 skipti
	{
		if (parseInt($("#UsersID").val()) > 0)
		{
			alert("Þú ert ekki innskráður lengur!");
			$("#Center").css("display","none");
			$("#UsersID").val("-1");
		}
	}
}