﻿var jAllForums;
var jTabPopularThemes;
var jTabNewThemes;
var jPanel;
var mainPage;

myBbcodeSettings = {
  nameSpace:          "bbcode", // Useful to prevent multi-instances CSS conflict
  markupSet: [
      {name:'B', key:'B', openWith:'[b]', closeWith:'[/b]'}, 
      {name:'I', key:'I', openWith:'[i]', closeWith:'[/i]'}, 
      {name:'U', key:'U', openWith:'[u]', closeWith:'[/u]'}, 
      {separator:'---------------' },
      {name:'Цитата', openWith:'[quote]', closeWith:'[/quote]'}
//      {name:'Code', openWith:'[code]', closeWith:'[/code]'}, 
   ]
}




$(function () {
	if(0 && mainPage){
		jAllForums = $('#all_forums');
		var jPopularThemes = $('#popular_themes');
		var jNewThemes = $('#new_themes');
	
//		$(jPopularThemes).hide();
//		$(jNewThemes).hide();
//		$('.forum_title:first', jPopularThemes).remove();
//		$('.forum_title:first', jNewThemes).remove();
	
		jPanel = $(jAllForums).prev();/*prepend('<div class="forum_title">\
											<h2>Все форумы</h2>\
										</div>').*/
//			before(	'<div class="main_page_top_panel">\
//							<a href="#" onclick="return tabShow(\'all_forums\', this)" class="current">Все форумы</a>\
//							<a href="#" onclick="return tabShow(\'popular_themes\', this)">Обсуждаемые темы</a>\
//							<a href="#" onclick="return tabShow(\'new_themes\', this)">Новые темы</a>\
//						</div>').
//			prev();
//		$(jPanel).prepend('<a href="#" onclick="return tabShow(\'all_forums\', this)" class="current">Все форумы</a>');
		$(jPanel).html('\
							<a href="#" onclick="return tabShow(\'all_forums\', this)" class="current">Все форумы</a>\
							<a href="#" onclick="return tabShow(\'tab_popular_themes\', this)">Обсуждаемые темы</a>\
							<a href="#" onclick="return tabShow(\'tab_new_themes\', this)">Новые темы</a>\
							');
		$(jAllForums).after('<div id="tab_popular_themes" style="display:none"></div><div id="tab_new_themes" style="display:none"></div>');
		jTabPopularThemes = $('#tab_popular_themes').html($(jPopularThemes).html());
		jTabNewThemes = $('#tab_new_themes').html($(jNewThemes).html());
		$('.forum_title:first', jTabPopularThemes).remove();
		$('.forum_title:first', jTabNewThemes).remove();
		$('tr:hidden', jTabPopularThemes).show();
		$('tr:hidden', jTabNewThemes).show();

		$('tr:hidden', jPopularThemes).remove();
		$('tr:hidden', jNewThemes).remove();

		var jNewPost = $('#new_post');
		jNewPost.css('overflow', 'hidden');
		function scrollTop() {
			var jFirstPost = jNewPost.children('div.post:first');
			var iHeight = jFirstPost.height() + 7;
			jFirstPost.animate({'marginTop': -iHeight}, iHeight*20, 'swing', function () {
					$(this).css('marginTop', '0').appendTo(jNewPost);
					setTimeout(scrollTop, 3000);
				});
		}
	
		setTimeout(scrollTop, 3000);
		
	}
	
	$('#form_post_text').markItUp(myBbcodeSettings);
	
	$('#js_search_form a.to_form').click(function () {
		$('div#forum_search').toggle();
		return false;
	});
})

function tabShow(sId, jsA){
	$(jAllForums).hide();
	$(jTabPopularThemes).hide();
	$(jTabNewThemes).hide();
	
	$('#'+sId).show();
	
	$('a', jPanel).removeClass('current');
	$(jsA).toggleClass('current');
	
	return false;
}

function quotePost(postId){
	var sAuthor = $('#post_text_'+postId).attr('author');
	sAuthor = sAuthor.replace(/"/g, '&quot;');
	var sText = '';
	
	var cN = $('#post_text_'+postId)[0].childNodes;
	for (var i=0, l=cN&&cN.length||0; i<l; i++) {
		if (cN[i].nodeType == 3 && String(cN[i].nodeValue).replace(/\s*/, '').length != 0) {
			sText += cN[i].nodeValue.replace(/(^\s+)|(\s+$)/g, '');
			if (i != l-1){
				sText += '\n';
			}
		}
	}

	$('#form_post_text').val($('#form_post_text').val() + '[quote="'+ sAuthor +'"]' + sText + '[/quote]\n');
}

//Работа с полями ввода файлов для галерей
$(function () {
	var jInputsGallery = $('input.inputgalleryphoto');
	if (jInputsGallery.length == 0) {
		return;
	}
	
	jInputsGallery.after('<img class="gallerylink_remove" src="/img/delete.png" onclick="removeInputPhotoGallery(this)" />');
	
	jInputsGallery.filter(':last').next('img.gallerylink_remove').next('br').after('<img class="gallerylink_add" src="/img/add.png" onclick="addInputPhotoGallery(this)" />');
})

function removeInputPhotoGallery (thisLink) {
	$(thisLink).prev('input.inputgalleryphoto').remove();
	$(thisLink).next('br').remove();
	$(thisLink).remove();
};

function addInputPhotoGallery (thisLink) {
	$(thisLink).before('<input type="file" name="photogallery" class="inputgalleryphoto" /><img class="gallerylink_remove" src="/img/delete.png" onclick="removeInputPhotoGallery(this)" /><br>');
};

