

$(document).ready(function()
{
	$('a').attr('onfocus', 'blur()');
	
	$('#login > input').eq(1).keypress(function(e)
	{
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13))
			$('#login').submit();
	});
	
	searchControl();
	
	if($('img').length > 0)
	{
		for(var i = 0; i < $('img').size(); i++)
		{
			var src =  $('img').eq(i).attr('src').replace('../img', 'img');
			$('img').eq(i).attr('src', src);
		}
	}
});

function delConfirm(url)
{
	if(confirm('Na pewno usun±æ ?')) document.location = url;
	
	return;
}

function initEditor()
{
	tinyMCE.init({
		theme : "advanced",
		language : "pl",
		mode : "textareas",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "center",
		plugins : "paste,media,fullscreen,table,emotions,layer,insertdatetime,print,searchreplace,emotions,insertdate,inserttime",
		theme_advanced_buttons1 : "bold,italic,underline,undo,redo,link,unlink,image,forecolorpicker,backcolorpicker,removeformat,cleanup,search,replace,print",
		theme_advanced_buttons2 : "formatselect,fontsizeselect,pastetext,pasteword,selectall,media,fullscreen,bullist,numlist,justifyleft,justifycenter,justifyright,justifyfull",
		theme_advanced_buttons3 : "",
		//entity_encoding : "raw",
		//entities: 'lt,gt',
		add_unload_trigger : false,
		remove_linebreaks : false,
		inline_styles : false,
		convert_fonts_to_spans : false,
		forced_root_block : false,
        force_p_newlines : false,
	});
	
}


function getComments(id)
{
	$.get('./getComment' + id, function(d){$('#comm').html(d); $('#comments').val(' ');});
}

function addComments(id)
{
	var url = './addComment' + id;
	$('#clicked').css('display', 'none');
	
	$.post(url, {'body':$('#comments').val()}, function(d)
	{
		if(d != 'OK') alert(d);
		else getComments(id);
	});
	
	$('#clicked').css('display', '');
}

function editComments(id)
{
	$.get('./getOne' + id, function(d)
	{
		if(d != 0) $('#comm' + id).html(d);
	});
}

function save(id)
{
	$.post('./getOne' + id, {'body':$('#edit' + id).val()}, function(d)
	{getComments(d)});
}


var STIME;

function searchSugestion()
{
	clearTimeout(STIME);
	$('#searchBox').css('display', 'none');

	STIME = setTimeout(function()
	{
		$.post('./search', {'search' : $('#search').val()}, function(d)
		{
			if(d != 0 && d != undefined)
			{
				var top  = ($('#search').position().top + $('#logo').height()) - $('#search').height() - 1;
				var left = ($('#search').position().left + $('#orangeLine').position().left) - $('#search').width() - 11;
								
				$('#searchBox').css({
					'width'    :  $('#search').width() + 'px',
					'top'      :  top + 'px',
					'left'     :  left + 'px',
					'position' : 'absolute',
					'display'  : 'block',
					'zIndex'   : '50'
				});
				
				$('#searchBox').html(d);
			}
		});
	}, 500);
}


function searchControl()
{
	$('#search').attr('disable', true);
	$('#search').attr('onclick', 'if(this.value == "Szukaj") this.value = "";');
	$('#search').keypress(function(e)
	{
		if((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) $('#searchForm').submit();
		else searchSugestion();
	});
}


function getLicence()
{
	$.post('./licence', {}, function(d) {
		
		$('#searchBox').html(d);
		$('#searchBox').css({
			'width': '500px',
			'top': ($(window).scrollTop() + 10) + 'px',
			'left': $(window).width() / 4,
			'position': 'absolute',
			'display': 'block',
			'zIndex': '50',
			'opacity': '0'
		});
		$('#searchBox').animate({'opacity':1}, 300);
	});
}

function runSearch(word)
{
	$('#search').val(word);
	$('#searchForm').submit();
}

function searchPage(page)
{
	$('#page').val(page);
	$('#searchForm').submit();
}
