﻿/// <reference path="../../../js/jquery-1.3.2-vsdoc.js" />
var Helper = {
	re: {
		Guid: /(^[\da-f]{8}\-[\da-f]{4}\-[\da-f]{4}\-[\da-f]{4}\-[\da-f]{12}$)/i
	},
	IsGuid: function(s) {
		return this.re.Guid.test(s);
	}
}
jQuery(function() {
	var $ = jQuery;
	$(".sport-types-list").ApplyPages({ pageSize: 38 });
	$("a.popup").popup();
	$("input.suggestion").suggestion();
	$(".form-extended-search fieldset.radio>input.ui-autocomplete-input").eq(1).hide();
	$(".form-extended-search fieldset.radio input[type=radio]").change(function() {
		$(".form-extended-search fieldset.radio>input.ui-autocomplete-input").hide();
		$(".form-extended-search fieldset.radio>input.ui-autocomplete-input").eq($(this).index(".form-extended-search fieldset.radio input[type=radio]")).show();
	});
	
	$(".form-toggler").click(function() {
		$('.form-toggle').toggle();
		$(this).html($(this).html() == 'Простой поиск' ? 'Расширенный поиск' : 'Простой поиск');
		return false;
	});
	$("ul.box.main p:not(.image-container)").each(function() {
		$(this).css('margin-top', ($(this).closest("li").height() - $(this).outerHeight()) / 2 + "px");
		$(this).closest("li").height('auto');
	});
	$("form").completeReset().setDatePicker("mm/dd/yy");

	$(".tabs").each(function() {
		$('ul.tabnav li', this).last().addClass("last");
		$('ul.tabnav li', this).first().addClass("first");
	});
	$('div.content-item-wrap', this)
		.append('<div class="corner-tr"></div> \
				<div class="corner-bl"></div> \
				<div class="corner-br"></div>');
	$(".tabs").tabs();

	$('.rounded').each(function(i) {
		$(this)
			.wrap("<div class='t'></div>")
			.wrap("<div class='b'></div>")
			.wrap("<div class='l'></div>")
			.wrap("<div class='r'></div>")
			.wrap("<div class='r'></div>")
			.wrap("<div class='bl'></div>")
			.wrap("<div class='br'></div>")
			.wrap("<div class='tl'></div>")
			.wrap("<div class='tr'></div>");
	});

});
(function($) {
	$.metadata.setType("attr", "data");
	function setMinDate(selectedDate) {
		var instance = $(this).data("datepicker");
		var date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
		$(this).parent().find("input.date[name=to]").datepicker("option", "minDate", date);
	};
	function setMaxDate(selectedDate) {
		var instance = $(this).data("datepicker");
		var date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
		$(this).parent().find("input.date[name=from]").datepicker("option", "maxDate", date);
	};
	$.fn.setDatePicker = function(dateFormat) {
		this.filter("form").find("input.date").each(function() {
			var el = $(this), name = el.attr("name");
			el.datepicker({
				dateFormat: dateFormat,
				onSelect: (name == 'from')
					? setMinDate
					: (name == 'to')
					? setMaxDate : null,
				showOn: 'button',
				buttonImage: '/content/Mossport/calendar.gif',
				buttonImageOnly: true
			});
		});
		return this;
	}
	function createControl(self, service, value) {
		var input = $("<input>")
					.val(value || "")
					.insertAfter(self.element)
					.focus(function() { var t = this; setTimeout(function() { t.select(); }); }, 350)
					.autocomplete({
						source: function(request, response) {
							$.getJSON(service, { input: request.term }, function(data) {
								if (data != null && jQuery.isArray(data)) {
									var re = new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + $.ui.autocomplete.escapeRegex(request.term) + ")(?![^<>]*>)(?![^&;]+;)", "gi");
									response($.map(data, function(d) {
										return {
											id: d.ID,
											label: d.DisplayName.replace(re, "<strong>$1</strong>"),
											value: d.DisplayName
										};
									}));
									return;
								}
								response([]);
							});
						},
						delay: 500,
						change: function(event, ui) {
							Sys.Debug.traceDump(ui.item, "change");
							if (!ui.item) {
								// remove invalid value, as it didn't match anything
								$(this).val("");
								self.element.val("");
								return false;
							}
							self.element.val(ui.item.id);
							self._trigger("change", event, {
								item: ui.item
							});

						},
						select: function(event, ui) {
							self.element.val((ui.item) ? ui.item.id : "");
						}
					});

	};
	$.widget("ui.suggestion", {
		_create: function() {
			var self = this;
			var hidden = this.element.addClass("value");
			var m = $(this.element).metadata(), sPath = m.path, iPath = m.item;
			if (sPath == null)
				return;
			if (Helper.IsGuid(this.element.val())) {
				$.getJSON(sPath, { itemId: this.element.val() }, function(data) {
					createControl(self, sPath, (data || {}).DisplayName);
				});
			}
			else
				createControl(self, sPath);
		}
	});

})(jQuery);

(function($) {
	var $ = jQuery,
		$d = function(obj, method) { return function() { method.apply(obj, arguments); } };
	window.OpenPopup = function(url) {
		window.open(url, "_blank", "height=400,width=600,left=200,top=200,directories=0,location=0,menubar=0,status=0,toolbar=0,scrollbars=1,resizable=1");
	};
	$.fn.popup = function() {
		this.filter("a").click(function(e) {
			e.preventDefault();
			window.OpenPopup($(this).attr("href"));
		});
	};
	$.fn.completeReset = function() {
		this.filter("form").find("input:reset").click(function(e) {
			e.preventDefault();
			var f = $(this).parents("form:first");
			f.find("input:text, input:hidden.value, textarea, select").val("");
			f.find("input:checkbox, input:radio").attr("checked", false);
		});
		return this;
	};
	$.fn.ApplyPages = function(options) {
		options = $.extend({}, { separator: '' }, options);

		if (options.pageSize && options.pageSize > 0) {
			this.each(function() {
				var t = { el: [], cp: 0, p: [], incr: incrPage, decr: decrPage },
					d = $('<div class="m-pager"></div>');


				$('<a href="#"></a>').html(options.linkL || '«').appendTo(d).bind("click.plugin-listMenu", $d(t, t.decr));
				$(this).children("li").not(":has(> a.show-fav)").not(":has(> a.show-all)").each(function(i) {
					var pi = Math.floor(i / options.pageSize),
						area = t.p[pi] || (t.p[pi] = []);
					area.push(this);
					if ($(this).hide().hasClass("selected"))
						t.cp = pi;
				});

				$(t.p).each(function(i) {
					$('<a class="num" href="#">' + (i + 1) + '</a>')
						.appendTo(d).bind("click.plugin-listMenu", function(e) {
							e.preventDefault(); e.stopPropagation(); setPage(i, t);
						});
				});
				$('<a href="#"></a>').html(options.linkR || '»').appendTo(d).bind("click.plugin-listMenu", $d(t, t.incr));
				t.el[0] = d.insertBefore(this);
				t.el[1] = d.clone(true).addClass('pager-bottom').insertAfter(this);

				setPage(t.cp, t);
			});
		}
		return this;
	};
	function setPage(n, o) {
		var t = o || this;
		n = (0 + n) || 0;
		if (n < 0) n = 0;
		if (n >= t.p.length) n = t.p.length - 1;
		if (!t.p[n])
			return;
		if (t.cp != undefined && t.cp != n) {
			$.each(t.p[t.cp], function() { $(this).hide(); });
		}
		t.cp = n;
		$.each(t.p[t.cp], function() { $(this).show(); });
		$(t.p[t.cp]).last().addClass("last");
		$(t.el).each(function() {
			$("a.num", this).removeClass("selected").eq(n).addClass("selected");
		});
	}
	function incrPage(e) {
		e.preventDefault();
		e.stopPropagation();
		setPage(this.cp + 1, this);
	};
	function decrPage(e) {
		e.preventDefault();
		e.stopPropagation();
		setPage(this.cp - 1, this);
	};
})(jQuery);