﻿/// <reference path="jquery-1.3.2-vsdoc.js" />
jQuery(function() {
	var $ = jQuery, Trim = function(s) {
		s = String(s).replace(/^\s+/, "");
		s = s.replace(/\s+$/, "");
		return s;
	};
	$("div#libInfoShort > h4 > a").click(function(e) {
		e.preventDefault();
		$(this).parents("div:first").toggleClass("expanded"); //.children(".info").toggle();
	});
	$("select.IssueYearSelect").change(function() 
	{
		var t = $(this), a = t.data("IssueYearSelect"), y = $(this).attr("value"), s = $("#imagedropdownlist");
		
		if (!a) 
		{
			a = $("<select/>");
			s.find("option").clone().appendTo(a);
			t.data("IssueYearSelect", a);
		}
		s.empty();
		a.children().eq(0).clone().appendTo(s);
		if (!y || y == '') {
			a.children().clone().appendTo(s);
		}
		else
		{
			a.find("option." + y).clone().appendTo(s);	
		}
		document.getElementById("imagedropdownlist").refresh();
		
		if ($($('#imagedropdownlist_child a').get(0)).html() == $($('#imagedropdownlist_child a').get(1)).html()) {
			$('#imagedropdownlist_child a').get(0).remove();
			$("#imagedropdownlist_titletext").html($('#imagedropdownlist_child a.selected').html());
		}
	})
	.each(function() 
	{
		var t = $(this), a = t.data("IssueYearSelect"), y = t.prev(".InitIssueYearSelect").attr("value"), s = $("#imagedropdownlist");
		if (!a) 
		{
			a = $("<select/>");
			s.find("option").clone().appendTo(a);
			t.data("IssueYearSelect", a);
		};

		if (!y || y == '' || !s.find("option." + y).length)
			return;
		t.attr("value", y);
		s.children(":not(." + y + ")").remove();
		
	});
	$.fn.favoriteCheck = function(options) {
		options = $.extend({ defaultPath: "/fav/doc/" }, options);
		this.click(function(e) {
			var t = this, s = this.checked;
			e.preventDefault();
			$.getJSON(options.defaultPath + (t.checked ? "add" : "remove") + "/" + t.value,
				function(data) {
					if (data)
						t.checked = s;
				});

		});
		return this;
	};
	$.fn.commentRemover = function() {
		//options = $.extend({  }, options);
		this.click(function(e) {
			var t = $(this);
			e.preventDefault();
			if (confirm("Удалить комментарий?")) {
				$.getJSON(t.attr("href"),
				function(data) {
					if (data)
						t.parent().remove();
				});
			}

		});
		return this;
	};
	$.fn.imageUpdater = function() {
		//c-img-fld
		this.find("a").bind("click", function(e) {
			var i = $(this).nextAll("img:first"), s = i.attr("src");
			e.preventDefault();
			i.attr("src", "");
			if (String(s).indexOf("?") == -1)
				s = s + "?";
			s = s + Math.random();
			i.attr("src", s);
		});
	};
	$.fn.commentEditor = function() {
		var startEdit = function(i) {
			var url = $(i).attr("href"), title = $("> h6", i.parentElement).hide(), txt = $("> p:last", i.parentElement).hide();
			$("> a.command", i.parentElement).hide();
			$('<h6 class="edit-comment-title"><input type="text" name="commentTitle" maxlength="64" size="50"/></h6>')
			.insertBefore(title).find("> input").attr("value", title.text());
			$('<p class="edit-comment-text"><textarea></textarea><br/><input type="button" name="send" value="Изменить комментарий"/><input type="button" name="cancel" value="Отмена"/></p>')
			.insertBefore(txt).find("> textarea").attr("value", txt.text()).end()
			.find("> input").bind('click', i.parentElement, function(e) {
				var P = $(e.data), B = $(this);
				if (B.attr("name") != 'cancel') {
					var D = {
						title: P.find(".edit-comment-title > input").attr("value"),
						text: Trim(P.find("textarea").attr("value"))
					};
					if (!D.text || D.text == "")
						return alert('Заполните пожалуйста текст коментария.');
					$.getJSON(url, D,
						function(data) {
							if (data)
								title.text(D.title); txt.text(D.text);
						});
				}
				B.parent().remove(); P.find(".edit-comment-title").remove();
				P.find("> h6").show().end().find("> p:last").show().end().find("> a.command").show();
			});
		}
		this.click(function(e) {
			e.preventDefault();
			startEdit(this);
		});
		return this;
	};
});
jQuery(function() {
	var $ = jQuery, ie6 = $.browser.msie && (jQuery.browser.version < 7);
	$.fn.sportListMenu = function(options) {
		options = options || {};
		this.each(function() {
			var q = $(this), wrap = null;
			if (ie6) {
				q.css("position", "static");
				wrap = $("<div/>").attr("class", q.attr("class")).addClass("hidden-content").css("position","static").appendTo(document.body);//
			}
			else if (q.css("position") != "absolute")
				q.css("position", "relative");
				
			q.children("li").each(function() {
				new menuItem(this, null, options, wrap);
			});
		});

		if (options.pageSize && options.pageSize > 0) {
			var t = { cp: 0, p: [], incr: incrPage, decr: decrPage },
            d = $('<div class="m-pager"></div>').html(options.separator || ' | '),
            leftLink = $('<a href="#"></a>').html(options.linkL || '&lt;&lt;&lt;').prependTo(d).bind("click.plugin-listMenu", $d(t, t.decr)),
            rightLink = $('<a href="#"></a>').html(options.linkR || '&gt;&gt;&gt;').appendTo(d).bind("click.plugin-listMenu", $d(t, t.incr));
			d.insertBefore(this).clone(true).insertAfter(this);
			$(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;
			});

			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(); });
	}
	function incrPage(e) {
		e.preventDefault();
		e.stopPropagation();
		setPage(this.cp + 1, this);
	};
	function decrPage(e) {
		e.preventDefault();
		e.stopPropagation();
		setPage(this.cp - 1, this);
	};
	function menuItem(el, parent, options,wrap) {
		var t = this, x, y, pos; //, off = t.offset();
		t.options = options || {};
		t.options.shiftX = (t.options.shiftX || 0); // - $(el).outerWidth(false);
		t.options.shiftY = (t.options.shiftY || 0); // -$(el).children(":first").outerHeight(false);

		t.parent = null;
		t.o = false;
		t.over = itemOver;
		t.out = itemOut;
		t.toggleWiew = toggleWiew;

		t.el = el;
		t.sub = $(t.el).children("ul")[0];
		$(t.el)
			.bind("mouseover.plugin-listMenu", $d(t, t.over))
			.bind("mouseout.plugin-listMenu", $d(t, t.out));
		if (t.sub) {
			if (ie6) {
				t.sub = $(t.sub).hide().appendTo(wrap)[0];
			}

			$(t.sub)
		    .css("position", "absolute")
		    .bind("mouseover.plugin-listMenu", $d(t, t.over))
			.bind("mouseout.plugin-listMenu", $d(t, t.out));
			if ($.browser.msie) {
				t.ifrBg = $('<iframe frameborder="0" scrolling="no" style="display:none;position:absolute;"/>').insertBefore(t.sub).hide();
			}
		}
		if (t.options.deep) {
			t.o = true;
			t.toggleWiew();
			t.sub.children("li").each(function() {
				new menuItem(this, t);
			});
			t.o = false;
			t.toggleWiew();
		}
	};
	function toggleWiewAll() {
		var t = this;
		if (t.o) {
			var e = $(t.el).addClass("m-over");
			if (!t.sub)
				return;
			var pos = t.elP || (t.elP = e.position())
				, elOffTop = t.elOT || (t.elOT = e.offset().top + t.options.shiftY)
				, y = t.subY || (t.subY = pos.top + t.options.shiftY)
				, x = t.subX || (t.subX = pos.left + e.outerWidth(false) + t.options.shiftX)
				, s = $(t.sub).css("left", x).css("top", y).show()
				, h = t.subH || (t.subH = s.outerHeight(false))
				, b = $(window).height() + $(window).scrollTop()
			;
			if (t.ifrBg)
				$(t.ifrBg).css("left", x).css("top", y).css("height", h).css("width", s.outerWidth(false)).show();
			if (b < (h + elOffTop)) {
				var ys = y + (b - (h + elOffTop));
				s.css("top", ys);
				if (t.ifrBg)
					$(t.ifrBg).css("top", ys);
			}
		} else {
			$(t.el).removeClass("m-over");
			if (!t.sub)
				return;
			$(t.sub).hide();
			if (t.ifrBg)
				$(t.ifrBg).hide();
		}
	};
	function toggleWiewIe6() {
		var t = this;
		if (t.o) {
			var e = $(t.el).addClass("m-over");
			if (!t.sub)
				return;
			var s = $(t.sub), h = t.subH || (t.subH = s.outerHeight(false)),
			 bpo = { target: t.el, targetPos: 1, hideAfterPosition: false,
			 	elementPos: 0, sy: t.options.shiftY, sx: t.options.shiftX
			 };
			s.positionBy(bpo); //.show();
			if (t.ifrBg)
				$(t.ifrBg).css({ height: h, width: s.outerWidth(false) }).positionBy(bpo); //.show();
		} else {
			$(t.el).removeClass("m-over");
			if (!t.sub)
				return;
			$(t.sub).hide();
			if (t.ifrBg)
				$(t.ifrBg).hide();
		}
	};
	function toggleWiew() {
		if (ie6)
			toggleWiewIe6.call(this);
		else
			toggleWiewAll.call(this);
	};
	function itemOver() {
		this.o = true;
		setTimeout($d(this, toggleWiew), 100);
	};
	function itemOut() {
		this.o = false;
		setTimeout($d(this, toggleWiew), 100);
	};
	function $d(obj, method) {
		return function() {
			method.apply(obj, arguments);
		}
	};
});