/**
 * Scripts which are commonly used in application
 */

var aj = {}; // define global namespace for application

(function ($) {

$(function () {
	/**
	 * Initialize goto pagination events inside specified container
	 * 
	 * @param {HTMLElement} el Element inside wich pagination elements should be initialized, document body is used by default
	 * @param {function} callback Function to call with composed url (location.href is modified by default)
	 * @return bool
	 */
	aj.initPagination = function (el, callback) {
		var $el = $(el || document.body);
		var callback = callback || function (url) {location.href = url;};
		if ($el.filter('.go_fs').length || ($el = $el.find('.go_fs')).length) {
			$el.filter('.go_fs').each(function () {
				var $this = $(this);
				$this.find('input[name="go_btn"]').click(function () {
					var gotoMeta = eval("(" + $this.find('input[name="go_meta"]').val() + ")");
					var gotoVal = parseInt($this.find('input[name="go_pn"]').val());
					if (gotoVal > 0) {
						gotoVal < gotoMeta.max_page || (gotoVal = gotoMeta.max_page);
						// transform page number to pagination offset
						gotoVal = (gotoVal - 1) * gotoMeta.per_page;
						
						var url = gotoMeta.url;
						if (/%p/.test(url)) {
							url = url.replace(/%p/, gotoVal);
						} else {
							url = url.replace(/\/?$/, gotoVal && '/' + gotoVal || '');
						}
						callback(url);
					} else {
						$this.find('input[name="go_pn"]').select().focus();
					}
				});
			})
		}
		return false;
	};
	// initialize pagination if some is already present on the page
	aj.initPagination();
	
	/**
	 * Initialize links meant for table data to be sorted
	 * 
	 * @param {HTMLElement} el Element inside wich pagination elements should be initialized, document body is used by default
	 * @return bool;
	 */
	aj.initSortLinks = function (el) {
		var $el = $(el || document.body);
		// initialze all sorting readers
		$el.find('a.sort').click(function () {
			var $targetForm = $('form[name="' + $(this).attr('rev') + '"]');
			$targetForm.find('input[name="orderby"]').val($(this).attr('rel'));
			$targetForm.find('input[name="sort"]').val($(this).attr('href').replace(/^[^#]*#/, ''));
			$targetForm.submit();
			return false;
		});
		return false;
	};
	// initialize sorting links if already present on the page
	aj.initSortLinks();
	
	// initialize autoredirect behavior
	$('a[rel="auto"]').each(function () {
		var $this = $(this);
		var countdown = parseInt($this.attr('rev')) || 0; // number of seconds to wait before redirect
		setTimeout(function () {
			location.href = $this.attr('href');
		}, countdown * 1000);
		if (countdown) { // a countdown notification is required
			var interval;
			var $countTag = $('<div class="countdown" />').appendTo($this.parent());
			var countFunc = function () {
				$countTag.html('\n(Redirecting in ' + countdown + ' seconds...)\n');
				countdown-- > 0 || clearInterval(interval);
			};
			countFunc();
			interval = setInterval(countFunc, 1000);
		}
	});
});

})(jQuery);