
$.ajaxSetup({cache: false});
function searchCat()
{
	var data = '';
	var finals = [];
	var url_finals = [];
	var query = $("[name=quick_cat_search]").attr('value');
	if(query.length > 1)
	{
		for(i=0; i<categories.length; i++)
		{
			res = categories[i].match(query);
			if(res)
			{
				finals.push(categories[i]);
				url_finals.push(url_categories[i]);
			}
		}
	}
	else
	{
		finals = categories;
		url_finals = url_categories;
	}

	var url_template = 'http://www.disputi.com/listing/category_#name#/1.html';
	var first_letter = '';
	for(b=0; b<finals.length; b++)
	{
		if(first_letter != finals[b].substr(0, 1))
		{
			if(first_letter != '')
				data += '</span>';
			first_letter = finals[b].substr(0, 1);
			data += '<div>' + first_letter + '</div><span>';
		}
		this_url = url_template.replace(/#name#/, url_finals[b]);
		data += '<a href="' + this_url + '">' + finals[b] + '</a>';
	}
	data += '</span>';
	$(".list_categories").html(data);
	stretch();
	$(".searching_cats").hide();
}

function add_tag()
{
	var dup = false;
	var tag = $('[name=tag_search]').attr('value');
	var chosen_tags = [];
	chosen_tags = $("[name=chosen_tags]").attr('value').split('@');
	for(i = 0; i < chosen_tags.length; i++)
	{
		if(chosen_tags[i] == tag)
			dup = true;
	}
	if(dup == false)
	{
		chosen_tags.push(tag);
		$("[name=chosen_tags]").attr('value', chosen_tags.join('@'));
		$(".tag_btn").after( '<div onclick="remove_tag(this);">' + tag + '</div>' );
	}

	$('[name=tag_search]').attr('value', '');
	$('[name=tag_search]').focus();
}

function remove_tag(obj)
{

	var new_tags = [];
	var tag = $(obj).html();
	var chosen_tags = $("[name=chosen_tags]").attr('value').split('@');
	for(i = 0; i < chosen_tags.length; i++)
	{
		if(chosen_tags[i] != tag)
			new_tags.push(chosen_tags[i]);
	}
	$("[name=chosen_tags]").attr('value', new_tags.join('@'));
	$(obj).remove();
}

function checkForm()
{

	var error = [];
	if($('[name=title]').attr('value').length < 5)
		error.push('заглавие на диспута');
	//if($('[name=post]').attr('value').length < 2)
	//	error.push('описание на диспута');
	//if($("[name=chosen_tags]").attr('value').split('@').length < 1)
	//	error.push('поне един таг');
	var selected = false
	$("[name=answer_type]").each(function(){
		if( $(this).attr('checked') == true )
			selected = true;
	});
	if(selected == false)
		error.push('възможност за отговор');

	if($("[name=*color_answer]").length > 10)
		error.push('Маскималния брой на отговорите е 10');
	else if(error.length > 0)
	{
		alert('Моля попълнете: ' + error.join(', '));
		return false;
	}
	else
	{
		you_sure();
		$('.publish_form').submit();
	}
}

function countRemain(obj)
{
	var length = $(obj).attr('value').length;
	if(length > 22)
	{
		$(obj).attr('value', $(obj).attr('value').substr(0, 22));
	}
}

function checkReply()
{
	var error = [];
	//$(".reply_to_form > [name=author_name]").attr('value');
	if( $(".reply_to_form > [name=comment]").attr('value').length < 3 )
		error.push('Моля попълнете полето за отговор!');

	if(error.length > 0)
	{
		alert(error);
		return false;
	}
	else
		$(".reply_to_form").submit();
}

function reply_to(comment_id, main_id)
{
	if($("#reply"))
	{
		var equal = $("[name=reply_to]").attr('value') == comment_id;
		$("#reply").remove();
		$("[name=reply_to]").remove();

		if(equal == true)
			return true;
	}

	var form = '<span id="reply">' + $('#submit').html() + '</span>';
	form = form.replace('emot_expand', 'emot_expand_reply');
	form = form.replace('emot_holder', 'emot_holder_reply');
	form += '<input type="hidden" name="reply_to" value="' + comment_id + '" />';

	if($("#" + main_id + " .date").html())
	{
		$("#" + main_id + " .date").before(form);
		$('#reply').css('width', '600px');
		$('#emot_holder_reply').css('width', '588px');
	}
	else
	{
		$("#" + main_id + " .message").after(form);
		$('#reply').css('width', '510px');
		$('#emot_holder_reply').css('width', '498px');
	}
	$(".ajax_message").html('');
	$("#comments .buton").unbind();
	$("#emot_expand_reply").unbind();
	$("#emot_expand_reply").click(function() {showEmots($("#emot_holder_reply"));});
	$("#comments .buton").click(function() { makeComment(2); });
	$("#reply").show();
}


function loadReply(comment_id, all, main_id)
{
	var tag_id = all ? '#r_' : '#c_';
	var box_id = all ? 'b_' : 'bi_';
	tag_id += main_id;
	if($(tag_id + " > .chron > .replies_container").html().length > 10)
	{
		$(tag_id + " > .chron > .replies_container").hide();
		$(tag_id + " > .chron > .replies_container").empty();
	}
	else
	{
		if(all)
			var mode = 'load_all_replies';
		else
			var mode = 'load_reply'
		$.post('http://www.disputi.com/ajax.html?r=' + Math.random(), {mode: mode, cid: comment_id, main_id: main_id }, function(data) {
			//$(tag_id + " > .chron > .replies_container").html(data);
			document.getElementById(box_id + main_id).innerHTML = data;
			$(tag_id + " > .chron > .replies_container").show();

		});
	}
}

function expandReply(reply_id)
{
	if($("#c_" + reply_id + " > .chron > .uparrow").css('float'))
	{
		$("#c_" + reply_id + " > .chron > .uparrow").attr('class', 'downarrow');
		$("#c_" + reply_id + " > .chron > .replies_container").show(400);
	}
	else
	{
		$("#c_" + reply_id + " > .chron > .downarrow").attr('class', 'uparrow');
		$("#c_" + reply_id + " > .chron > .replies_container").hide(200);
	}
}

function showEmots(obj)
{
	if($(obj).css('display') != 'inline' && $(obj).css('display') != 'block')
		$(obj).show();
	else
		$(obj).hide();
}

function insertEmot(emot)
{
	if($("[name=comment]").length > 1) // in case of open reply form
		var obj = $("[name=comment]")[1];
	else
		var obj = $("[name=comment]");

	$(obj).attr('value', $(obj).attr('value') + ' ' + emot + ' ');
	$(obj).focus();
	$("[id*=emot_holder]").hide(500);
}

function you_sure()
{
	$('[name=captcha_2]').attr('value', 'mr_roboto');
}

function you_post()
{
	$('[name=captcha_2]').attr('value', 'mr_posto_roboto');
}

var current_palette = 0;
function showPalette(i)
{
	i--;
	var dsp = $("#colors").css('display');
	if(dsp != 'none' && dsp != '' && current_palette - 1 == i)
		$("#colors").hide();
	else
		$("#colors").show();

	current_palette = i + 1;
	$("#colors").css('top', (i * 26) + 2 + 'px' );
}

function selectColor(color)
{
	$("#view_color_" + current_palette).css('background-color', color);
	$('[name=color_answer' + current_palette + ']').attr('value', color);
	$('[name=answer' + current_palette + ']').css('color', color);
	$('#colors').hide();
}

function loadSection(selected)
{
	var txt = '';
	$(".other_disputi").html('');
	$(".other_disputi").css('height', '50px');
	$(".other_disputi").css('background', 'url(\'http://www.disputi.com/images/ajax_loader_2.gif\') center center no-repeat');
	$.post('http://www.disputi.com/ajax.html', {mode: 'load_section', selected: selected}, function(data) {
		$('.other_disputi').html(data);
		$(".other_disputi").css('background', '');
		$(".other_disputi").css('height', '');
		$("#right > #buttons > a").each(function(i){
			txt = String($(this).attr('onclick'));
			if( txt.match(selected) )
				$(this).attr('class', 'active');
			else
				$(this).removeAttr('class');
		});
	});
	return false;
}

function addAnswer()
{
	var biggest = 0;
	$("[name*=answer]").each(function(){
		var name = parseInt( $(this).attr('name').replace('answer', '') );
		if(name > 0 && name > biggest && $(this).attr('type') == 'text' )
			biggest = name;
	});
	biggest++;
	if(biggest < 11)
	{
		var txt = '<span class="line">';
		txt += '<span class="colorpick" id="view_color_' + biggest + '" style="background-color: grey;" onclick="showPalette(' + biggest + ');"></span>';
		txt += '<input type="text" class="custom" name="answer' + biggest + '" value="опция за гласуване" />';
		txt += '<input type="hidden" name="color_answer' + biggest + '" value="grey" />';
		txt += '</span>';
		$("#add_another_answ").before(txt);
		if(!$("#remove_another_answ").html() && biggest >= 3)
			$("#add_another_answ").after("<span onclick=\"deleteAnswer();\" id=\"remove_another_answ\" class=\"btn\">-</span>");
		bindElements();
	}
}

function deleteAnswer()
{
	var total = $(".custom > .line").length - 1;
	$(".custom > .line").each(function(i){
		if(i == total)
			$(this).remove();
	});
	if(total == 2)
		$("#remove_another_answ").remove();
}

function bindElements()
{
	$(".line > .custom").focus( function(){ $(this).css('border', '1px solid #666666'); $(this).select(); });
	$(".line > .custom").blur( function(){ $(this).css('border', 'none'); });
}

function stretch()
{
	var right = $("#right").height() && $("#right > #buttons").height();
	if(right)
		return true;
	var height = $("#container").height();

	var footer = $("#footer").height() + 50;
	var header = $("#header").height();
	var wheight = $.browser.opera ? window.innerHeight : $(window).height();
	if( (height + footer + header) < wheight )
		$("#container").height( wheight - (header + footer) );
}


