/* LANGUAGE */

var messages = {
	comments_report_confirm : 'Na pewno chcesz raportować ten komentarz?',
	comments_edit_in_progress : 'Trwa edycja komentarza!',
	comments_editing_different : 'Inny komentarz w trakcie edycji!',
	comments_nothing_edited : 'Brak komentarza w trakcie edycji!',
comments_already_rated : 'Ten komentarz był już przez ciebie oceniany!',
comments_login_to_rate : 'Zaloguj się by móc oceniać komentarze!',	

drag_settings_unavailable : 'Ustawienia niedostępne',
	
	editor_help_info : '<span>[b]...[/b]</span> lub <span>*...*</span> - pogrubienie<br />' +
					   '<span>[i]...[/i]</span> lub <span>/.../</span> - kursywa<br />' +
					   '<span>[u]...[/u]</span> lub <span>_..._</span> - podkreślenie<br />' +
					   '<span>http://...</span> lub <span>[url]...[/url]</span> lub <span>[url=...]...[/url]</span> - link<br />' +
					   '<span>[code]...[/code]</span> - kod<br />' +
					   '<span>[img]...[/img]</span> - obraz<br />' +
					   '<span>[spoiler]...[/spoiler]</span> - spoiler<br />' +
					   '<span>[snapback]...[/snapback]</span> - link do komentarza o wskazanym id<br />' +
					   '<span>[quote]...[/quote]</span> lub <span>[quote name=\'...\']...[/quote]</span> lub <br /><span>[quote name=\'...\' date=\'...\']...[/quote]</span> lub <br /><span>[quote name=\'...\' date=\'...\' post=\'...\']...[/quote]</span> - komentarz',

	rating_stars : '<ul><li><img src="img/dot_transparent.gif" alt="" /></li><li><img src="img/dot_transparent.gif" alt="" /></li><li><img src="img/dot_transparent.gif" alt="" /></li><li><img src="img/dot_transparent.gif" alt="" /></li><li><img src="img/dot_transparent.gif" alt="" /></li></ul>',
	rating_empty : '<ul></ul>',
	rating_confirm : 'Czy chcesz ocenić tę publikację na '
};

/* SEARCH */

var search = {
	init : function(id)
	{
		$('#'+id).find('div.tabs li').each(function(i) { 
		 	$(this).click(function () {
		 		$('#'+id).find('input.type:first').val(i);
				$('#'+id).find('div.tabs li').removeClass('active');
				$(this).addClass('active');
				return false;
		 	});
		});
	},
	blur : function(id)
	{
		var search = $('#'+id).find('div.string input:first');
			search.focus(function () { $(this).removeClass('blur'); }).blur( function () { if (!$(this).val().length) $(this).addClass('blur'); });
	 	if (!search.val().length) search.addClass('blur');
	}
};

/* NOTICE */

var notice = {
	show : function() 
	{ 
		$('#noticeoff').hide();
		$('#noticeon').show();
		return false;		
	}
};

/* RATINGS */

var rating = {
 	id : 0,
 	value : 0,
	init : function(id, val, active) 
	{ 
		this.id = id;
		this.value = val;
		if (active)
			$('#rating').find('div.value:first').html(messages.rating_stars).find('li').each(function(i) { 
			 	$(this).mouseover(function () { rating.change(i+1); }); 
				$(this).mouseout(function () { rating.change(rating.value); }); 
				$(this).click(function () {
					if (confirm(messages.rating_confirm + (i+1)))
					{
						$.ajax({
							type: 'GET',
							cache: false,
							url: 'index.php',
							data: 'file=process&action=addrating&node=' + rating.id + '&rating=' + (i+1),
							success: function(data){
							 	if (data.length)
							 	{
									eval("var response = ("+data+")");
									if(response.code == 1)
									{
										$('#rating').find('div.votes:first').html(response.votes);
										$('#rating').find('div.info:first').html(response.info);
										$('#rating').find('div.value:first').html(messages.rating_empty);
										rating.change(response.rating);
									}
									else
										alert(response.error);
								}
							}
						});	
					}
				});
			});	
		else
			$('#rating').find('div.value:first').html(messages.rating_empty);			
		rating.change(rating.value);
	}, 
	change : function(val)
	{
		$('#rating').find('ul:first').css({backgroundPosition: '0px -'+(val*72)+'px'});
	}
};

/* WARNINIG */

var warning = {
 	id : 0,
 	view : function(mid, cid)
	{
		if (this.id == cid)
		{
			this.hide();
			return false;
		}
		else
			this.id = cid;
			
		$.ajax({
			type: 'GET',
			cache: false,
			url: 'index.php',
			data: 'do=getWarningList&qp=&cid=' + cid + '&mid=' + mid,
			success: function(data){
			 	if (data.length)
			 	{
					$('#warning').html(data).appendTo('#c'+cid).show();
				}
			}
		});	
		return false;		
	},
	warn : function(mid, cid)
	{
		if (this.id == cid)
		{
			this.hide();
			return false;
		}
		else
			this.id = cid;
			
		$.ajax({
			type: 'GET',
			cache: false,
			url: 'index.php',
			data: 'do=getWarningForm&qp=&cid=' + cid + '&mid=' + mid,
			success: function(data){
			 	if (data.length)
			 	{
					$('#warning').html(data).appendTo('#c'+cid).show();
				}
			}
		});	
		return false;		
	},
	save : function(mid, cid)
	{
		$.ajax({
			type: 'GET',
			cache: false,
			url: 'index.php',
			data: 'do=saveWarning&qp=&cid=' + cid + '&mid=' + mid + '&warn_value=' + encodeURI($('#warn_value').val()) + '&ban_value=' + encodeURI($('#ban_value').val()),
			success: function(data){
			 	if (data.length)
			 	{
					eval("var response = ("+data+")");
					alert(response.info);
					if(response.code == 1)
					{
						warning.hide();
						$('#content').find('div.w' + mid).each(function(){
   							$(this).find('span:first').html(response.warn);
   							$(this).find('img:first').attr('src',response.icon);
						});	
					}
				}
			}
		});	
		return false;			
	},
	hide : function()
	{
		this.id = 0;
		$('#warning').hide();
	}		
};

/* FORUM */

var forum = {
 	close : function(fid, action)
	{
		$.ajax({
			type: 'GET',
			cache: false,
			url: 'index.php',
			data: 'do=closeTopic&qp=&fid=' + fid + '&action=' + action,
			success: function(data){
				window.location.reload();
			}
		});
		return false;
	},
	hide : function(fid, action)
	{
		$.ajax({
			type: 'GET',
			cache: false,
			url: 'index.php',
			data: 'do=hideTopic&qp=&fid=' + fid + '&action=' + action,
			success: function(data){
				window.location.reload();
			}
		});
		return false;
	},
	glue : function(fid, action)
	{
		$.ajax({
			type: 'GET',
			cache: false,
			url: 'index.php',
			data: 'do=glueTopic&qp=&fid=' + fid + '&action=' + action,
			success: function(data){
				window.location.reload();
			}
		});
		return false;
	}
};

/* COMMENTS */

var comments = {
	lock : false,
	enlarge : function(id, ratio)
	{
		$('#'+id).height($('#'+id).height() + 100);
		return false;
	},
	report : function()
	{
		if (!this.lock)
		{	
			return confirm(messages.comments_report_confirm);		
		}
		else
			alert(messages.comments_edit_in_progress);			
		return false;
	},	
	quote : function(id)
	{
		if (!this.lock)
		{	
			$.ajax({
				type: 'GET',
				cache: false,
				url: 'index.php',
				data: 'file=process&action=quotecomment&id=' + id,
				success: function(data){
				 	if (data.length)
				 	{
						$('#comment').val(data);
						window.location.href = '#add';
					}
				}
			});		 
		}
		else
			alert(messages.comments_edit_in_progress);			
		return false;
	},
	edit : function(id)
	{
		if (!this.lock)
		{
			this.lock = true;
			$.ajax({
				type: 'GET',
				cache: false,
				url: 'index.php',
				data: 'file=process&action=editcomment&id=' + id,
				success: function(data){
				 	if (data.length)
				 	{
						var view = $('#c'+id).find('div.view:first').hide();
						var height = $(view).height();
						var edit = $('#c'+id).find('div.edit:first').html(data).show();
						edit.find('textarea:first').height(height);
					}
				}
			});	
		}
		else
			alert(messages.comments_editing_different);
		return false;
	},
	cancel : function(id)
	{
		if (this.lock)
		{		
		 	$('#c'+id).find('div.edit:first').hide().find('textarea:first').val('');
		 	$('#c'+id).find('div.view:first').show();
			this.lock = false;
		}
		return false;
	},
	update : function(id)
	{
		if (this.lock)
		{		
			$('#c'+id).find('div.edit:first').find('form:first').submit();
		}
		else
			alert(messages.comments_nothing_edited);		
		return false;
	}
};

/* EDITOR */

var editor = {
 	init : function(id) {
		var info = $(document.createElement('div')).addClass('helpinfo').html(messages.editor_help_info).appendTo($('#'+id));
		$(document.createElement('div')).html('[x]').click(function () { info.hide(); }).appendTo(info);
		$(document.createElement('span')).html('(?)').click(function () { info.show(); }).appendTo($('#'+id).find('div.help:first'));
 	}
};

/* SHARE */

var share = {
	valid: null,
	submit : function() {
		share.valid = true; 
		share.check($('#company'));
		share.check($('#name'));
		share.check($('#email'));
		share.check($('#phone'));
		
		if (!$('#test').attr('checked') && 
			!$('#contest').attr('checked') &&
			!$('#survey').attr('checked') &&
			!$('#askexpert').attr('checked'))
		{
			$('#test').parent().prev().addClass('error');
			share.valid = false;
		}
		else
			$('#test').parent().prev().removeClass('error');
			
		return share.valid;
	},
	check : function(obj) {
		if (!obj.val().length)
		{
			obj.parent().prev().addClass('error');
			share.valid = false;
		}
		else
			obj.parent().prev().removeClass('error');
	}
};
