$(document).ready(function(){
	$(".captcha-a").click(ReloadCaptcha);

	$("#orphus-container").html('<script type="text/javascript" src="/orphus/orphus.js"></script><a href="http://orphus.ru" id="orphus" target="_blank"><img alt="Система Orphus" src="/orphus/orphus.gif" border="0" width="257" height="48" /></a>');

	$("a[rel='print']").click(function(){
		print();

		return false;
	});

	$("a[rel='del-link']").click(function(){
		return confirm('Вы действительно хотите удалить?');
	});

	$(".invite-friend").click(function(){
		var rlink = $(this).attr('href');
		openGWindow('', 20);

		$("#group-window-content .accept").click(function(){
			location.href = rlink;
			closeSafeGWindow();
			return false;
		});

		$("#group-window-content .decline").click(function(){
			closeSafeGWindow();
			return false;
		});

		return false;
	});

	$(".quick-tour a").click(function(){
		$.get(this.href, function(data){
			$("#quick-tour-fade").css({width:$(document).width() + 'px', height:$(document).height() + 'px'}).show().fadeTo(300, 0.5).click(function(){
				closeGenWindow('qt');
				$(this).hide();
			});
			openGenWindow('qt', data, [-150, -150]);

			var qt_obj = $("#qt-window-content .qt-page");
			var qt_links = $("#qt-window-content .qt-pages a");

			qt_links.click(function(){
				this.blur();

				var lnk = $(this).attr("href");
				var curr_page = parseInt(lnk.substring(lnk.length-1, lnk.length))-1;

				qt_links.removeClass("sel").eq(curr_page).addClass("sel");
				qt_obj.hide().eq(curr_page).fadeIn('slow');

				return false;
			}).eq(0).click();
		});

		return false;
	});

	$(".profile-topmenu li.disabled a").attr("href", "#").click(function(){
		this.blur();
		return false;
	});

	var pa_text = 'название новинки, продукта или технологии';
	var pa_comment = 'опишите, по каким причинам вы добавляете ваш выбор к рейтингу (опционально)';

	$("#pa_name, #epa_name").val(pa_text).focus(function(){
		var obj = $(this);
		if(obj.val() == pa_text){
			obj.val('');
		}
	}).blur(function(){
		var obj = $(this);
		if(!$.trim(obj.val())){
			obj.val(pa_text);
		}
	});

	$("#pa_comment, #epa_comment").val(pa_comment).focus(function(){
		var obj = $(this);
		if(obj.val() == pa_comment){
			obj.val('');
		}
	}).blur(function(){
		var obj = $(this);
		if(!$.trim(obj.val())){
			obj.val(pa_comment);
		}
	});

	$(".extanswers .other").click(function(){
		$("." + (this.id == 'pa0' ? 'pa-ui' : 'epa-ui')).toggleClass("hidden")
	});

	$(".extanswers :checkbox").click(function(){
		if($(".extanswers input:checked").length)
			$("#extsubm").show();
		else
			$("#extsubm").hide();
	});

	$(".open a").click(function(){
		$(this).hide();
		$(".opened").show();

		return false;
	});
});

var ReloadCaptcha = function(){
	$(".captcha").attr('src', $(".captcha").attr('src') + '1');
	return false;
}

var showArticleImage = function(ID, TYPE){
	window.open('/showArticleImage.php?id=' + ID + '&type=' + TYPE, 'wn1', 'menubar=no,location=no,resizable=no,scrollbars=no,status=no');

	return false;
}

var showNewsImage = function(ID, TYPE){
	window.open('/showNewsImage.php?id=' + ID + '&type=' + TYPE, 'wn1', 'menubar=no,location=no,resizable=no,scrollbars=no,status=no');

	return false;
}

var showTooltipPanel = function(offset, data){
	$("#tooltip").css({top:offset.top+22+'px', left:offset.left+'px', width:(offset.width-10 || 200)+'px'}).html(data).show();
}
var closeTooltipPanel = function(){
	$("#tooltip").hide();
}

function searchhide(ID){
	with(document.getElementById(ID))
		if(value == 'Поиск')
			value='';
}
function searchshow(ID){
	with(document.getElementById(ID))
		if(value == '')
			value='Поиск';
}

function answerhide(ID){
	with(document.getElementById(ID))
		if(value == 'Вопрос')
			value='';
}
function answershow(ID){
	with(document.getElementById(ID))
		if(value == '')
			value='Вопрос';
}
function deschide(ID){
	with(document.getElementById(ID))
		if(value == 'Описание')
			value='';
}
function descshow(ID){
	with(document.getElementById(ID))
		if(value == '')
			value='Описание';
}

function searchhide1(ID){
	with(document.getElementById(ID))
		if(value == 'Поиск по вопросам и ответам')
			value='';
}
function searchshow1(ID){
	with(document.getElementById(ID))
		if(value == '')
			value='Поиск по вопросам и ответам';
}

function searchhide_blog(ID){
	with(document.getElementById(ID))
		if(value == 'Поиск по блогам')
			value='';
}
function searchshow_blog(ID){
	with(document.getElementById(ID))
		if(value == '')
			value='Поиск по блогам';
}

function searchhide_community(ID){
	with(document.getElementById(ID))
		if(value == 'Поиск по сообществам')
			value='';
}
function searchshow_community(ID){
	with(document.getElementById(ID))
		if(value == '')
			value='Поиск по сообществам';
}
function searchhide_pres(ID){
	with(document.getElementById(ID))
		if(value == 'Поиск по презентациям')
			value='';
}
function searchshow_pres(ID){
	with(document.getElementById(ID))
		if(value == '')
			value='Поиск по презентациям';
}


var openGenWindow = function(ident, data, offsetY){
	if(data){
		$("#" + ident + "-window-content").html(data);
	}

	if(typeof(offsetY) == 'object'){
		offsetX = offsetY[0];
		offsetY = offsetY[1];
	} else {
		offsetX = 0;
	}
	
	var sY = window.scrollY ? window.scrollY : (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);

	$("#" + ident + "-window").css({top:sY + offsetY + $(window).height()/3+'px', left:$(window).width()/2-150+offsetX+'px'}).fadeIn("normal");
}

var closeGenWindow = function(ident){
	$("#" + ident + "-window").fadeOut("normal", function(){
		$("#" + ident + "-window-content").html("");
	});
}

var closeSafeGenWindow = function(ident){
	$("#" + ident + "-window").fadeOut("normal");
}

var closeSafeGenWindowFunction = function(ident, func){
	$("#" + ident + "-window").fadeOut("normal", func);
}

var openGWindow = function(data, offsetY){
	openGenWindow("group", data, offsetY);
}

var closeGWindow = function(){
	closeGenWindow("group");
}

var closeSafeGWindow = function(){
	closeSafeGenWindow("group");
}

var closeSafeGWindowFunction = function(func){
	closeSafeGenWindowFunction("group", func);
}

var registerTooltip = function(sId, nId, sScript, pre_params, sDefMess){
	$(sId).keyup(function(e){
		var c = e.which;

		var obj = $(this);

		if(this.value.length > 2)
		{
			if(c != 12 && c != 13 && !(c > 32 && c < 41)){
				var offset = $(this).offset();

				params = {name: this.value};
				
				for (var key in pre_params)
				{
					params[key] = $(pre_params[key]).val();
				}

				$.get(sScript, params, function(data){
					if(data){
						offset.width = $(sId).width();

						showTooltipPanel(offset, data);
						$("#tooltip a").click(function(){
							var link = $(this).attr('href');
							link = link.substring(link.indexOf("#"), link.length);

							$(nId).val(link.substring(1, link.length));
							
							obj.val($(this).text());

							return false;
						});
					}
				});
			} else if(c == 40){
				SelectFromTooltip(nId, this, 1);
			} else if(c == 38){
				SelectFromTooltip(nId, this, -1);
			}
		}
	}).keydown(function(e){
		if(e.which == 13){
			closeTooltipPanel();
			$(this).blur();
			return false;
		}
	}).blur(function(){
		if(!$.trim(this.value)){
			this.value = sDefMess;
		}

		setTimeout('closeTooltipPanel()', 500);
	}).focus(function(){
		if($.trim(this.value) == sDefMess){
			this.value = '';
		}
	}).blur();
}

var SelectFromTooltip = function(id, obj, act){
	var gallery = $("#tooltip a");
	var totalLinks = gallery.size();
	var iteration = gallery.index(gallery.filter(".curr").get(0));

	if(act > 0){
		iteration = iteration < totalLinks-1 ? iteration + 1 : 0;
	} else {
		iteration = iteration > 0 ? iteration - 1 : totalLinks - 1;
	}

	gallery
		.removeClass("curr").parent("li").removeClass("hover").end()
		.eq(iteration).addClass("curr").parent("li").addClass("hover");
	
	obj.value = gallery.eq(iteration).text();
	
	var link = gallery.eq(iteration).attr('href');
	link = link.substring(link.indexOf("#"), link.length);
	
	$(id).val(link.substring(1, link.length));
}