$(document).ready(function(){
	jQuery(function($){$("#u_zip ").mask("99-999");});
	jQuery(function($){$("#zip ").mask("99-999");});
	jQuery(function($){$("#end_display ").mask("9999-99-99");});
	jQuery(function($){$("#born_date ").mask("9999-99-99");});
	jQuery(function($){$("input.data_mask ").mask("9999-99-99");});
	
	$('.probe .field').mouseover( function(){
		$( this ).addClass('field_active');
	});

	$('.probe .field').mouseout( function(){
		$( this ).removeClass('field_active');
	});	

	$('textarea#desc_from_user:not(.processed)').TextAreaResizer();	
	$('textarea#desc:not(.processed)').TextAreaResizer();
	
	$('table.content tr.element').mouseover( function(){
		$( this ).addClass('onmouseover');
	});

	$('table.content tr.element').mouseout( function(){
		$( this ).removeClass('onmouseover');
	});

	$('#searchBlock').click( function(){
		$('#searchBlockForm').slideToggle();
	});



	// upload obrazka do profilu użytkownika
	var jcropOptions =
	$('form#uploadUserImage').ajaxForm({
				dataType:  'json',
				beforeSubmit: imgPreloader,
				success: imgLoadResponse
	});

	function imgPreloader(){
		$('#photo_preloader').slideDown('fast');
		$('#uploadUserImageError').css('display', 'none');
	}
	
	function imgLoadResponse(data){
		$('#photo_preloader').slideUp('fast');
		$('#user_image').val('');

		if( data.result == 0 ){ imgLoadError( data ); }
		else if( data.result == 1 ) { imgLoadSuccess( data );  }

	}

	function imgLoadSuccess( data ){
		img = '<img src="' + data.file_path + '">';
		$('#userImage').html(img);
		$('#userImageDelete').css('display', 'block');
	}

	function imgLoadError( data ){
		$('#uploadUserImageError').html( data.msg );
		$('#uploadUserImageError').css('display', 'block');
	}


	// komentarze dla artykułów

	$('#comment_show').click( function(){
		$('div.comment_input').slideToggle();
		$('div#news_trace_msg').slideUp('fast');
		$('div#comment_ok').slideUp('fast');
	});

	$('a.comment_show_to_up').click( function(){
		$('div.comment_input').slideDown();
		$('div#comment_ok').slideUp('fast');
	});

	var commentOptions =
	$('form#comment').ajaxForm({
				dataType:  'json',
				beforeSubmit: commentPreloader,
				success: commentFormResponse
	});


	function commentPreloader(){
		$('#preloader').slideDown('fast');
		$('.comment_input div.field_error').css('display', 'none');
	}

	function commentFormResponse( data ){
		$('#preloader').slideUp('fast');
		if( data.result == 1 ){ commentFormResponseOk( data ); }
		else{ commentFormResponseError( data ); }
	}

	function commentFormResponseOk( data ){
		$('div.comment_input').slideUp('fast');
		$('div#comment_ok').slideDown('fast');
		$('textarea#desc').val('');
		$('div.comment_list div#comment_none').css('display', 'none');
		$('div.comment_list').prepend( data.html);
		$('div.user_'+ data.u_id ).html('Komentarzy: '+ data.comment_count );

	}


	function commentFormResponseError( data ){
		$('.comment_input div.field_error').html( data.error );
		$('.comment_input div.field_error').css( 'display', 'block' );
		$('textarea#desc').addClass('error');
	}

	// śledzenie artykułu

	$('a#news_trace').click( function(){

		$('div.comment_input').slideUp('fast');

		if( $(this).attr('id_news') ){
			id_news = $(this).attr('id_news');
			type = 0;
		}else if( $(this).attr('id_section_news') ){
			id_news = $(this).attr('id_section_news');
			type = 1;
		}else{ return; }

		$.ajax({
			type: "POST",
			url: "/aktualnosci/sledzKomentarz/" + id_news + "/" + type,
			dataType:  'json',
			beforeSend: function(){
				$('#news_trace_preload').css('display', 'block');
			},
			success: function( msg ){
				$('#news_trace_preload').css('display', 'none');
				$('div#news_trace_msg').attr('class', msg.cls );
				$('div#news_trace_msg').html( msg.html );
				$('div#news_trace_msg').slideDown('fast');
			}
		});


		
	});
	

	// cytowanie komenatrza

	$('.comment_cit').click( function(){
		user = $( this ).parent().parent().children('div.user').children('div.name').html();
		comment = $( this ).parent().parent().children('div.comment').children('div.text').html();
		cit = '<b>Autor:</b> '+ user +'<br/>'+ comment;
		id_comment = $( this ).attr("comment");

		$('div.comment_input').slideDown();
		$('form#comment input#cit_comment').val( id_comment );
		$('#comment_input_cit div#text').html( cit );
		$('#comment_input_cit').slideDown('fast');

	});

	$('#comment_delete_cit').click( function(){
		$('#comment_input_cit').slideToggle('fast');
		$('form#comment input#cit_comment').val( '0' );
	});





	/* BEGIN - Obsługa zgłoszeń nadużycia w komentarzach  */

	$('a.abuse_link').click(function() {
			$('#abuse_form_user form').css( 'display', 'block' );
			$('#abuse_form_user div#preLoader').css('display', 'none');
			$('#abuse_form_user div#result').css( 'display', 'none' );

			id_comment = $( this ).attr("comment");
			$('#abuse_form_user input#abuse_comment').val( id_comment );
			$.blockUI({ message: $('#abuse_form_user') });
			$('.blockOverlay').attr('title','Kliknij aby wyjść').click($.unblockUI);
	});

	var commentOptions =
	$('form#abuse_form').ajaxForm({
				dataType:  'json',
				beforeSubmit: commentAbusePreloader,
				success: commentAbuseResponse
	});

	function commentAbusePreloader(){
		$('#abuse_form_user #preLoader').css('display', 'block');
	}

	function commentAbuseResponse( data ){
		$('#abuse_form_user div#result').html( data.msg );
		$('#abuse_form_user form').css( 'display', 'none' );
		$('#abuse_form_user div#result').css( 'display', 'block' );
		setTimeout($.unblockUI, 1200);
	}


	/* END - Obsługa zgłoszeń nadużycia w komentarzach  */

	$('li.section a.section_toggle').click( function(){
		$( this ).parent().children('ul').slideToggle('fast');
	});



});





function addFavorite(){
	if( document.all ){
		javascript:window.external.addFavorite( 'http://www.wspolnotamieszkaniowa.pl' , 'Portal Wspolnotamieszkaniowa.pl');
	} else{
		alert('Wymagany Internet Explorer');
	}
}


function setHomePage(){
	if( document.all ){
		this.setHomePage('http://www.wspolnotamieszkaniowa.pl');
	} else{
		alert('Wymagany Internet Explorer');
	}
}