﻿Type.registerNamespace("Finatica");
Type.registerNamespace("Finatica.Controls");
Type.registerNamespace("Finatica.Controls.SliderPager");

Finatica.Controls.SliderPager = function (element) {
	Finatica.Controls.SliderPager.initializeBase(this, [element]);
	this.controlID = element.id;
	this.ulID = null;
	this.currentPage = 1;
	this.pagesCount = 1;
	this.parentClientID = '';
	this.navigateUrl = '';
	this.renderOnServer = false;
	this.sliderID = '';
	this.uniqueID = '';
	this.usePostback = true;
}

Finatica.Controls.SliderPager.prototype =
{
	initialize: function () {
		Finatica.Controls.SliderPager.callBaseMethod(this, "initialize");
		if (this.renderOnServer != true)
			this.createPager();
		else this.createPagerClient();
	},

	dispose: function () {
		Finatica.Controls.SliderPager.callBaseMethod(this, 'dispose');
	},

	createPagerClient: function () {
		if (this.usePostback == true)
			for (var i = 1; i <= this.get_buttonsCount(); i++) {
				$("#" + this.ulID + " > li > a").bind('click', this, this.pageClick);
			}
	},

	createPager: function () {
		if (this.navigateUrl == null || this.navigateUrl == '')
			this.navigateUrl = '/';
		else if (this.navigateUrl[this.navigateUrl.length - 1] != '/')
			this.navigateUrl += '/';

		var ulElement = $("#" + this.ulID);
		ulElement.html('');
		if (this.get_pagesCount() > 1) {
			var offset = this.get_initialOffset();
			for (var i = offset; i < offset + this.get_buttonsCount(); i++) {
				var li = "<li><a href='" + this.navigateUrl + "page/" + i + "/' class='pagerButton ";
				if (i == this.get_currentPage())
					li += " active";
				li += "'"
				if (this.get_events().getHandler('pageChanged') != null) {
					li += "onclick='return false;'";
				}
				li += ">";
				li += i;
				li += "</a></li>";
				ulElement.append(li);
			}

			ulElement.append("<li class='total'>Страниц: " + this.get_pagesCount() + "</li>");

			if (this.get_events().getHandler('pageChanged') != null) {
				$("#" + this.ulID + " > li > a").bind("click", { eventSender: this }, function (event) {
					$("#" + $(this).parent().parent().attr("id") + " > li > a").removeClass("active");
					event.data.eventSender.currentPage = Number($(this).text());
					if (event.data.eventSender.get_pagesCount() > event.data.eventSender.get_buttonsCount())
						event.data.eventSender.redrawPager();
					else
						$(this).addClass("active");
					event.data.eventSender.get_events().getHandler('pageChanged')(event);
				});
			}
		}
	},

	redrawPager: function () {
		this.redrawPagerOffset(this.get_initialOffset());
	},

	redrawPagerOffset: function (offset) {
		$("#" + this.ulID + "> li").find("a.active").removeClass("active");
		for (var i = 1; i <= this.get_buttonsCount(); i++) {
			var pageNumber = (i + offset - 1).toString();
			$("#" + this.ulID + " > li:nth-child(" + i + ") > a").text(pageNumber);
			$("#" + this.ulID + " > li:nth-child(" + i + ") > a").attr('href', this.navigateUrl + "page/" + pageNumber + "/");
			if (i + offset - 1 == this.currentPage)
				$("#" + this.ulID + "  > li:nth-child(" + i + ") > a").addClass("active");

		}
	},

	hide: function () {
		$('#' + this.controlID).hide();
	},

	show: function () {
		$('#' + this.controlID).show();
	},

	toggle: function () {
		$('#' + this.controlID).toggle();
	},

	hideSlider: function () {
		$('#' + this.sliderID).hide();
	},

	showSlider: function () {
		$('#' + this.sliderID).show();
		var slider = $find(this.sliderID);
		slider.repaint();
	},

	get_currentPage: function () {
		if (this.currentPage != null) {
			this.currentPage = this.currentPage < 1 ? 1 : (this.currentPage > this.get_pagesCount() ? this.get_pagesCount() : this.currentPage);
		}
		return this.currentPage;
	},

	set_currentPage: function (value) {
		this.currentPage = value;
	},

	get_buttonsCount: function () {
		var pagesCount = this.get_pagesCount();
		return 10 < pagesCount ? 10 : pagesCount;
	},

	get_pagesCount: function () {
		return this.pagesCount;
	},

	set_pagesCount: function (value) {
		this.pagesCount = value < 1 ? 1 : value;
		if (this.pagesCount > 10 && this.sliderID != '') {
			$find(this.sliderID).set_maximumValue(this.pagesCount - this.get_buttonsCount());
			this.showSlider();
		}
	},

	get_initialOffset: function () {
		if (this.get_pagesCount() > this.get_buttonsCount()) {
			if (this.get_currentPage() + this.get_buttonsCount() - 1 > this.get_pagesCount())
				return this.get_pagesCount() - this.get_buttonsCount() + 1;
			if (this.get_currentPage() > 1)
				return this.get_currentPage() - 1;
			return this.get_currentPage();
		}
		return 1;
	},

	add_pageChanged: function (handler) {
		this.get_events().addHandler('pageChanged', handler);
	},

	remove_pageChanged: function (handler) {
		this.get_events().removeHandler('pageChanged', handler);
	},

	clear: function () { $("#" + this.ulID).html(''); },

	sliderChange: function (arg, sender) {
		$(".test").text(sender.get_value());
	},

	pageClick: function (handler) {
		__doPostBack(handler.data.uniqueID, $(handler.target).text())
		return false;
	}
}

Finatica.Controls.SliderPager.descriptor =
{
	properties: [{ name: 'ulID', type: String },
				 { name: 'parentClientID', type: String },
				 { name: 'pagesCount', type: Number },
				 { name: 'currentPage', type: Number },
				 { name: 'navigateUrl', type: String },
				 { name: 'sliderID', type: String },
				 { name: 'renderOnServer', type: Boolean },
				 { name: 'uniqueID', type: String },
				 { name: 'usePostback', type: Boolean }
				 ],
	events: [{ name: 'pageChanged'}]
}

Finatica.Controls.SliderPager.registerClass("Finatica.Controls.SliderPager", Sys.UI.Control);

Finatica.Controls.SliderPager.sliderChange = function (sender, arg) {
	$find($(sender.get_element()).parent().attr('id')).redrawPagerOffset(sender.get_value());
}


Finatica.Controls.SliderPager.sliderLoad = function (sender, arg) {
	sender.repaint();
}
