﻿if (window.EmuLibraryNET == null || window.EmuLibraryNET == undefined) {
	window.EmuLibraryNET = {};
}

if (window.EmuLibraryNET.Standalone == null || window.EmuLibraryNET.Standalone == undefined) {
	window.EmuLibraryNET.Standalone = {};
}

if (window.EmuLibraryNET.Standalone.PagedRepeater == null || window.EmuLibraryNET.Standalone.PagedRepeater == undefined) {  
	window.EmuLibraryNET.Standalone.PagedRepeater = {};

	window.EmuLibraryNET.Standalone.PagedRepeater.HelloWorld = function() {
		alert("Hello World!");
	}

	window.EmuLibraryNET.Standalone.PagedRepeater.Sort = function(repeaterID, sort, dir) {
		var sortBy = document.getElementById('__' + repeaterID + '_SORTBY');
		var sortDir = document.getElementById('__' + repeaterID + '_DIRECTION');
		var nextPage = document.getElementById('__' + repeaterID + '_PAGE');

		if (sortBy && sortDir && nextPage) {
			nextPage.value = 1;
			sortBy.value = sort;
			sortDir.value = dir;
		}

		__doPostBack(repeaterID, '');
		return false;
	}

	window.EmuLibraryNET.Standalone.PagedRepeater.Paginate = function(repeaterID, page) {
		var nextPage = document.getElementById('__' + repeaterID + '_PAGE');

		if (nextPage) {
			nextPage.value = page;
			__doPostBack(repeaterID, '');
		}

		return false;
	}

	window.EmuLibraryNET.Standalone.PagedRepeater.FilterInit = function(repeaterID, id) {
		var filter = document.getElementById(id);

		if (filter.Init != true) {
			window.EmuLibraryNET.Standalone.PagedRepeater.AddEvent(filter, "keypress", function(args) { window.EmuLibraryNET.Standalone.PagedRepeater.FilterKeyPress(repeaterID, args); }, false);
			filter.Init = true;
		}
	}

	window.EmuLibraryNET.Standalone.PagedRepeater.FilterKeyPress = function(repeaterID, args) {
		if (window.EmuLibraryNET.Standalone.PagedRepeater.GetEventKeyCode(args) == 13) {
			var nextPage = document.getElementById('__' + repeaterID + '_PAGE');
			if (nextPage) { nextPage.value = 1; }

			__doPostBack(repeaterID, '');
		}

		return false;
	}

	window.EmuLibraryNET.Standalone.PagedRepeater.FilterReset = function() {
		window.location = window.location.href;
	}

	window.EmuLibraryNET.Standalone.PagedRepeater.AddEvent = function(obj, eventType, func, useCapture) {
		if (obj.addEventListener) {
			obj.addEventListener(eventType, func, useCapture);
			return true;

		} else if (obj.attachEvent) {
			return obj.attachEvent("on" + eventType, func);

		} else {

		}
	}

	window.EmuLibraryNET.Standalone.PagedRepeater.GetEventKeyCode = function(e) {
		if (window.event) {
			return e.keyCode;
		} else if (e.which) {
			return e.which;
		}
	}

	window.EmuLibraryNET.Standalone.PagedRepeater.GetEventCaller = function(e) {
		if (window.event) {
			return e.srcElement;
		} else if (e.which) {
			return e.target;
		}
	}
}