Imports Telerik.Web.UI

Namespace PCSolution

	''' <summary>
	''' Umoznuje na strance presunout zaznamy v tabulce na nove umisteni mysi
	''' </summary>
	Public Class SortItems

		Dim pPage As Page
		Dim ajax As Telerik.Web.UI.RadAjaxPanel
		Dim ph As PlaceHolder
		Public _Table As String

		Public Sub New()

			pPage = HttpContext.Current.Handler
			ajax = pPage.FindControl("ajax")
			ph = pPage.FindControl("ph")

		End Sub

		Enum eRazeni
			Tabulka = 0
			Seznam = 1
		End Enum

		Sub InsertSorting(sTable As String, Optional cilRazeni As eRazeni = 0)

			_Table = sTable

			Dim hSort As New HiddenField : hSort.ID = "hSort"
			hSort.Value = ""
			ph.Controls.Add(hSort)

			Dim ajaxSort As New RadAjaxPanel : ajaxSort.ID = "ajaxSort"
			AddHandler ajaxSort.AjaxRequest, AddressOf ajaxSort_Start
			ajaxSort.LoadingPanelID = "ajaxLoading"
			ajaxSort.Width = Unit.Pixel(16) : ajaxSort.Height = Unit.Pixel(11)
			ph.Controls.Add(ajaxSort)

			Dim ajaxLoading As New RadAjaxLoadingPanel : ajaxLoading.ID = "ajaxLoading"
			ajaxLoading.Skin = ""
			ajaxLoading.Controls.Add(New LiteralControl("<img src=""/Img/progress.gif"" alt=""Data..."" />"))
			ph.Controls.Add(ajaxLoading)

			If cilRazeni = eRazeni.Tabulka Then
				ph.Controls.Add(New LiteralControl("<script type=""text/javascript"">SortTable();</script>"))
			ElseIf cilRazeni = eRazeni.Seznam Then
				ph.Controls.Add(New LiteralControl("<script type=""text/javascript"">SortUL();</script>"))
			End If

		End Sub

		Private Sub ajaxSort_Start(sender As Object, e As AjaxRequestEventArgs)

			ajax.ResponseScripts.Add("HidePopInfo();")

			Dim hSort As HiddenField = pPage.FindControl("hSort")

			Dim s() As String = hSort.Value.Split(",")
			Dim n As Int16 = 1

			For Each sID As String In s
				If sID = "" Then Continue For
				DB.ExecuteCommand("UPDATE " & _Table & " SET sort = " & n & " WHERE id = " & sID.Replace("sort", ""))
				n += 1
			Next

		End Sub

	End Class

End Namespace
