function switchTabs(tabName) {
	tabs = ['news', 'articles', 'blogs'];
	for (i=0; i<tabs.length; i++) {
		display = tabs[i] == tabName ? 'block' : 'none';
		$('#' + tabs[i] + '_active').css('display', display);
		$('#' + tabs[i] + '_tab_content').css('display', display);
	}
}

function switchAdd(obj, act, parent) {
	tabs = ['news', 'blogs'];
	for (i=0; i<tabs.length; i++) {
//		$('#add_' + tabs[i]).css('display', 'none');
		$('#add_' + tabs[i]).fadeOut('10');
		$('#add_' + tabs[i]).stop(true, true);
	}

	if (act) {
		oWin = $('#add_' + obj);
		oParent = $('#'+parent);


		oWin.css('left', oParent.position().left - oWin.width() + oParent.width() + 4);
		oWin.css('top', oParent.position().top - 6);
//		oWin.show('fast');
		oWin.fadeIn('20');
		oWin.bind("mouseleave",function(){
			switchAdd(obj, false);
		});
	}
}

function switchAddObjType(parent) {
	act = $('#' + parent).attr('checked');
	clss = act ? 'source_block_dis' : '';
	focus1 = act ? 'head11' : 'source_author';

	$('#source_author').attr('disabled', act);
	$('#source_url').attr('disabled', act);
	if (act) {
//		$('#source_author').attr('value', '')
//		$('#source_url').attr('value', '')
		$('#source_block').attr('class', 'source_block_dis');
		$('#head11').focus();
	}
	else {
		$('#source_block').attr('class', '');
		$('#source_author').focus();
	}
}

function switchWin(winId, parentId, hideWin, fl) {
//	switchLoad();
	$('#' + winId).stop(true, true);
	
	if (hideWin) {
//		$('#' + winId).hide('10');
		$('#' + winId).fadeOut('10');
	}
	else {
		leftPos = $('#' + parentId).position().left;
		docWidth = $('body').width();
//		contentWidth = $('.main').width();
//		if (docWidth < contentWidth) {
//			docWidth = contentWidth;
//		}
		winWidth = $('#' + winId).width();
//		sideWidth = (docWidth - contentWidth)/2;

		fitX = docWidth - leftPos - winWidth - 10/* - sideWidth*/;
		if (docWidth < (winWidth + 20)) {
			leftPos = 10;
		}
		else if (fitX < 0) {
			leftPos += fitX;
		}

		$('#' + winId).css('left', leftPos);
		$('#' + winId).css('top', $('#' + parentId).position().top + (fl ? 0 : ($('#' + parentId).height() + 10)));
//		$('#' + winId).show(300);
		$('#' + winId).fadeIn('100');
	}
}

function switchLoad(parentId) {
	$('#en_load').stop(true, true);
	if (parentId == undefined) {
		$('#en_load').fadeOut('10');
	}
	else {
		$('#en_load').width($('#'+parentId).width());
		$('#en_load').height($('#'+parentId).height());
		$('#en_load').css('left', $('#' + parentId).position().left);
		$('#en_load').css('top', $('#' + parentId).position().top);
//		$('#en_load').fadeTo('fast', 0.66);
		$('#en_load').fadeIn('fast');
	}
}


function switchFormHelpWin(parentId, htmlData) {
	winObj = $('#form_help_win');
	winObj.stop(true, true);
	if (parentId == undefined || !parentId) {
//		winObj.html('');
		winObj.fadeOut('2');
	}
	else {
		parentObj = $('#'+parentId);
		winObj.html(htmlData);
		winObj.css('left', parentObj.position().left + parentObj.width() + 15);
		winObj.css('top', parentObj.position().top - 3);
		winObj.fadeIn('10');
	}
}

function setHelp(parentId, htmlData) {
	if (parentId == undefined || !parentId) {
		return;
	}
	parentObj = $('#'+parentId);
	parentObj.bind("mouseleave", function() {
		switchFormHelpWin();
	});
	parentObj.bind("mouseenter", function() {
		switchFormHelpWin(parentId, htmlData);
	});
}


function ajaxErrHandler(x, e, parId, atTop) {
	if (x.status == 0) {
		err = 'Проверьте Ваше подключение к интернету.';
	}
	else if (x.status == 404) {
		err = 'Запрашиваемая страница не найдена (404).';
	}
	else if(x.status == 500) {
		err = 'Внутренняя ошибка сервера (500).';
	}
	else if (e == 'parsererror') {
		err = 'Ошика обработки JSON запроса.';
	}
	else if (e == 'timeout') {
		err = 'Сервер не отвечает.';
	}
	else {
		err = 'Неизвестная ошибка.\n'+x.responseText;
	}
	switchLoad();
	$('#msg_err_content').html(err);
	if (atTop) {
		switchWin('msg_err_win', parId, false, true);
	}
	else {
		switchWin('msg_err_win', parId);
	}
}


function switchUserMenu(act, parent) {

	$('#user_menu').fadeOut('10');
	$('#user_menu').stop(true, true);

	if (act) {
		oWin = $('#user_menu');
		oParent = $('#'+parent);


		oWin.css('left', oParent.position().left - oWin.width() + oParent.width() + 0);
		oWin.css('top', oParent.position().top + 2);
		oWin.fadeIn('10');
		oWin.bind("mouseleave",function(){
			switchUserMenu(false);
		});
	}
}
