$( document ).ready( function() {

/*	var random = Math.floor(Math.random()*3)+Math.floor(Math.random()*1);

	if(random == '0')
	{
		banner = '';
	}
	else
	{
		banner = '6';
	}

	var so = new SWFObject("/flash/banner.swf", "banner", "750", "100", "8", "#ffffff");
	so.addParam("wmode","opaque");
	so.addParam("bgcolor","#ffffff");
	so.write("banner");
	
	var random = Math.floor(Math.random()*3)+Math.floor(Math.random()*3);
	var banner;
	var background;
	
	if(random == '0')
	{
		banner = '2';
		background = "#231F20";
		height = "191";
	}
	else
	{
	    if(random == '1')
		{
		    banner = '3';
		    background = "#FFFFFF";
		    height = '200';
		}
		else
    	{
    		if(random == '2')
			{
				banner = '4';
    			background = "#FFFFFF";
    			height = '350';
			}
			else
			{
				banner = '5';
				background = '#FFFFFF';
				height = '250';
			}
    	}
	}
	
	
	if($('#banner2').length) {
		var so2 = new SWFObject("/flash/banner"+banner+".swf", "banner2", "286", height, "8", "#231F20");
		so2.addParam("wmode","opaque");
		so2.addParam("bgcolor",background);
		so2.write("banner2");
	}*/
	
	if($('#submit').length) {
		$('form').bind('submit', function() {
			$('#submit').attr('disabled', 'disabled');
			$('#submit').val('Przetwarzanie…');
			$(this).submit();
		});
	}

	$('em.required').text('*');

	$('div.notes div.notes li:last').css('padding-bottom', 0);
	$('div.notes div.notes li:last').css('border', 0);

	$('div.table tr:even').addClass('highlight');
    
    
//    if( $( 'a.delete' ) ) {
//        $('a.delete' ).click( function() {
//            window.location = $( this ).attr( 'href' );
//        } );
        
//        $( 'a.delete' ).click( function() {
//            return true;
//        } );
//        
//        $('a.delete').confirm( {
//            msg : '<h1>Czy napewno usunąć?</h1>',
//            buttons: {
//                separator: '  '
//            }
//        } );
//    }
//    
//    if( $( 'a.delete-gallery' ) ) {
//        $('a.delete-gallery' ).click( function() {
//            window.location = $( this ).attr( 'href' );
//        } );
//        
//        $('a.delete-gallery').confirm( {
//            msg : 'Czy napewno usunąć? UWAGA! Zdjęcia znajdujące się w albumie również zostaną usunięte',
//            buttons: {
//                ok : 'tak',
//                cancel : 'nie',
//                separator: '  '
//            }
//        } );
//    }
    
    
    if( $( 'a.delete' ) ) {
        $( 'a.delete' ).click( function( e ) {
            e.preventDefault();
            
            var h = $( this ).attr( 'href' );
            
            var md = document.createElement( 'div' );
            md.setAttribute( 'id', 'message' );
            md.setAttribute( 'class', "warning" );
            
            var mcd = document.createElement( 'div' );
            mcd.setAttribute( 'id', 'messageContainer' );
            
            var msd = document.createElement( 'div' );
            msd.setAttribute( 'id', 'messageSubcontainer' );
            
            var mh = document.createElement( 'h1' );
            var mht = document.createTextNode( 'Czy napewno usunąć?' );
            
            mh.appendChild( mht );
            
            var yb = document.createElement( 'button' );
            yb.setAttribute( 'id', 'yesButton' );
            var ybt = document.createTextNode( 'Tak' );
            yb.appendChild( ybt );
            
            var s = document.createTextNode( ' ' );
            
            var nb = document.createElement( 'button' );
            nb.setAttribute( 'id', 'noButton' );
            var nbt = document.createTextNode( 'Nie' );
            nb.appendChild( nbt );
            
            $( nb ).click( function() {
			    $( '#messageSubcontainer' ).remove();
			    $( '#messageContainer' ).remove();
			    $( '#message' ).remove();
			    if( $( '#ov' ) )
			    {
			        $( '#ov' ).remove();
			    }
		    } );
		    
		    $( yb ).click( function() {
		        window.location = h;
		    } );
		    
		    msd.appendChild( mh );
		    msd.appendChild( yb );
		    msd.appendChild( s );
		    msd.appendChild( nb );
		    mcd.appendChild( msd );
		    md.appendChild( mcd );
		    $( 'body' ).append( md );
        } );
    }
    
    
    if( $( 'a.delete-gallery' ) ) {
        $( 'a.delete-gallery' ).click( function( e ) {
            e.preventDefault();
            
            var h = $( this ).attr( 'href' );
            
            var md = document.createElement( 'div' );
            md.setAttribute( 'id', 'message' );
            md.setAttribute( 'class', "warning" );
            
            var mcd = document.createElement( 'div' );
            mcd.setAttribute( 'id', 'messageContainer' );
            
            var msd = document.createElement( 'div' );
            msd.setAttribute( 'id', 'messageSubcontainer' );
            
            var mh = document.createElement( 'h1' );
            var mht = document.createTextNode( 'Czy napewno usunąć?' );
            
            mh.appendChild( mht );
            
            var mc = document.createElement( 'p' );
            var mct = document.createTextNode( 'UWAGA! Zdjęcia znajdujące się w albumie również zostaną usunięte.' );
            
            mc.appendChild( mct );
            
            var yb = document.createElement( 'button' );
            yb.setAttribute( 'id', 'yesButton' );
            var ybt = document.createTextNode( 'Tak' );
            yb.appendChild( ybt );
            
            var s = document.createTextNode( ' ' );
            
            var nb = document.createElement( 'button' );
            nb.setAttribute( 'id', 'noButton' );
            var nbt = document.createTextNode( 'Nie' );
            nb.appendChild( nbt );
            
            $( nb ).click( function() {
			    $( '#messageSubcontainer' ).remove();
			    $( '#messageContainer' ).remove();
			    $( '#message' ).remove();
			    if( $( '#ov' ) )
			    {
			        $( '#ov' ).remove();
			    }
		    } );
		    
		    $( yb ).click( function() {
		        window.location = h;
		    } );
		    
		    msd.appendChild( mh );
		    msd.appendChild( mc );
		    msd.appendChild( yb );
		    msd.appendChild( s );
		    msd.appendChild( nb );
		    mcd.appendChild( msd );
		    md.appendChild( mcd );
		    $( 'body' ).append( md );
        } );
    }
    
    
	if( $( '#DogShelterId' ).length ) {
        if( !$( '#DogShelterId' ).attr( 'checked' ) ) {
            $('.shelter-cat' ).hide();
        }
        $( '#DogShelterId' ).click( function() {
            if( $( '#DogShelterId' ).attr( 'checked' ) ) {
                $('.shelter-cat' ).show();
            } else {
                $('.shelter-cat' ).hide();
            }

        } );
    }

	if($('#prevent').length) {
		$('#prevent').bind('click', function() {
			$('#prevent').replaceWith('Przetwarzanie…');
		});
	}

	$('div.table div.messages tbody tr').each(function() {
		$(this).find('td').css('color', '#3399cc');
		$(this).css('cursor', 'pointer');
		$(this).bind('click', function() {
			window.location = $(this).find('a').attr('href');
		});
		$(this).bind('mouseover', function() {
			$(this).css('background', '#e58c0c');
			$(this).find('td').css('color', '#fff');
			$(this).find('a').css('color', '#fff');
		});
		$(this).bind('mouseout', function() {
			$(this).css('background', '#e58c0c');
			$(this).find('td').css('color', '#3399cc');
			$(this).find('a').css('color', '#3399cc');
		});
	});
    
//    if( $( '#box-newest-cats' ).length ) {
//	    $( '#box-newest-cats' ).clipregion( {
//    	    'item' : 'li.set',
//    	    'prev' : '#box-newest-cats-buttons .prevSet',
//    	    'next' : '#box-newest-cats-buttons .nextSet'
//        } );
//    }
  
    if( $( '#box-newest-shelter-cats' ).length ) {
	    $( '#box-newest-shelter-cats' ).clipregion( {
    	    'item' : 'li.set',
    	    //'itemHeight' : 336,
    	    'prev' : '#box-newest-shelter-cats-buttons .prevSet',
    	    'next' : '#box-newest-shelter-cats-buttons .nextSet'
        } );
    }
    
    if( $( '#box-newest-cat-images' ).length ) {
	    $( '#box-newest-cat-images' ).clipregion( {
    	    'item' : 'li.set',
    	    //'itemHeight' : 336,
    	    'prev' : '#box-newest-cat-images-buttons .prevSet',
    	    'next' : '#box-newest-cat-images-buttons .nextSet'
        } );
    }
    
    if( $( '#box-cat-gallery-images' ).length ) {
	    $( '#box-cat-gallery-images' ).clipregion( {
    	    'item' : 'li.set',
    	    //'itemHeight' : 336,
    	    'prev' : '#box-cat-gallery-images-buttons .prevSet',
    	    'next' : '#box-cat-gallery-images-buttons .nextSet'
        } );
    }
    
    if( $( '#box-cat-friends' ).length ) {
	    $( '#box-cat-friends' ).clipregion( {
    	    'item' : 'li.set',
    	    //'itemHeight' : 336,
    	    'prev' : '#box-cat-friends-buttons .prevSet',
    	    'next' : '#box-cat-friends-buttons .nextSet'
        } );
    }
    
    if( $( '#box-cat-galleries' ).length ) {
	    $( '#box-cat-galleries' ).clipregion( {
    	    'item' : 'li.set',
    	    //'itemHeight' : 336,
    	    'prev' : '#box-cat-galleries-buttons .prevSet',
    	    'next' : '#box-cat-galleries-buttons .nextSet'
        } );
    }
    
    if( $( '#box-user-cats' ).length ) {
	    $( '#box-user-cats' ).clipregion( {
    	    'item' : 'li.set',
    	    //'itemHeight' : 336,
    	    'prev' : '#box-user-cats-buttons .prevSet',
    	    'next' : '#box-user-cats-buttons .nextSet'
        } );
    }

	if( $( '#box-newest-dupa-cats' ).length ) {
	    $( '#box-newest-dupa-cats' ).clipregion( {
    	    'item' : 'li.set',
    	    //'itemHeight' : 336,
    	    'prev' : '#box-newest-dupa-cats-buttons .prevSet',
    	    'next' : '#box-newest-dupa-cats-buttons .nextSet'
        } );
    }
   
    if( $( '#breed-list' ).length ) {
        $( '#breed-list' ).scrollme( {
            containter : '#breed-list-to-scroll',
            up : '#breed-list-scroll-up',
            down : '#breed-list-scroll-down',
            stepSize : 20,
            stepDelay : 100,
            containterH : 398
        } );
    }

	if( $( '#groups-list' ).length ) {
        $( '#groups-list' ).scrollme( {
            containter : '#groups-list-to-scroll',
            up : '#groups-list-scroll-up',
            down : '#groups-list-scroll-down',
            stepSize : 20,
            stepDelay : 100,
            containterH : 439
        } );
    }

	if( $( '#groups-list-mini' ).length ) {
        $( '#groups-list-mini' ).scrollme( {
            containter : '#groups-list-mini-to-scroll',
            up : '#groups-list-mini-scroll-up',
            down : '#groups-list-mini-scroll-down',
            stepSize : 20,
            stepDelay : 100,
            containterH : 269
        } );
    }
//    
//    if( $( 'a.main-album-edit-description' ) ) {
//        $('a.main-album-edit-description' ).click( function() {
//            window.location = $( this ).attr( 'href' );
//        } );
//        
//        $('a.main-album-edit-description').confirm( {
//            msg : 'Na pewno usunąć zdjęcie?',
//            buttons: {
//                ok : 'tak',
//                cancel : 'nie',
//                separator: '  '
//            }
//        } );
//    }
//    
    if( $( '#rateImageForm' ).length )
    {
        $( '#main-rate-form' ).miaoorater( {
            replaceEl : '#rateImageForm',
            itemList : 'cat-gallery-image-rate-list',
            item : 'cat-gallery-image-rate',
            indicator : '/img/site/availability-loading.gif'
        } );
    }
    
    if( $( '#rateVideoForm' ).length )
    {
        $( '#main-rate-form' ).miaoorater( {
            replaceEl : '#rateVideoForm',
            itemList : 'cat-video-rate-list',
            item : 'cat-video-rate',
            indicator : '/img/site/availability-loading.gif'
        } );
    }
    
    if( $( '#addDogBlogPostCommentForm' ).length )
    {
        $( '#cat-blog-posts-comment-list' ).miaoocommenter( {
            form : '#addDogBlogPostCommentForm',
            indicator : '/img/site/availability-loading.gif'
        } );
    }
    
    if( $( '#addDogGalleryImageCommentForm' ).length )
    {
        $( '#cat-gallery-images-comment-list' ).miaoocommenter( {
            form : '#addDogGalleryImageCommentForm',
            indicator : '/img/site/availability-loading.gif'
        } );
    }
    
    if( $( '#addDogVideoCommentForm' ).length )
    {
        $( '#cat-videos-comment-list' ).miaoocommenter( {
            form : '#addDogVideoCommentForm',
            indicator : '/img/site/availability-loading.gif'
        } );
    }
    /*
    if( $( '#box-newest-cat-photos' ).length ) {
            $("#box-newest-cat-photos").jCarouselLite({
                btnNext: "#box-newest-cat-photos-next",
                btnPrev: "#box-newest-cat-photos-previous",
                visible : 7,
                scroll : 3,
                circular : false
            });
    }

	if( $( '#box-newest-cat-friends-photos' ).length ) {
            $("#box-newest-cat-friends-photos").jCarouselLite({
                btnNext: "#box-newest-cat-friends-photos-next",
                btnPrev: "#box-newest-cat-friends-photos-previous",
                visible : 7,
                scroll : 3,
                circular : false
            });
    }

	if( $( '#box-newest-cat-friends-videos' ).length ) {
            $("#box-newest-cat-friends-videos").jCarouselLite({
                btnNext: "#box-newest-cat-friends-videos-next",
                btnPrev: "#box-newest-cat-friends-videos-previous",
                visible : 4,
                scroll : 2,
                circular : false
            });
    }    */


	if( $( '#box-fans-list' ).length ) {
        $( '#box-fans-list' ).scrollme( {
            containter : '#box-fans',
            up : '#box-fans-previous',
            down : '#box-fans-next',
            stepSize : 20,
            stepDelay : 100,
            containterW : 644
        } );
    }

	if( $( '#box-newest-cat-photos-list' ).length ) {
        $( '#box-newest-cat-photos-list' ).scrollme( {
            containter : '#box-newest-cat-photos',
            up : '#box-newest-cat-photos-previous',
            down : '#box-newest-cat-photos-next',
            stepSize : 20,
            stepDelay : 100,
            containterW : 644
        } );
    }

	if( $( '#box-top-boned-cats-list' ).length ) {
        $( '#box-top-boned-cats-list' ).scrollme( {
            containter : '#box-top-boned-cats',
            up : '#box-top-boned-cats-previous',
            down : '#box-top-boned-cats-next',
            stepSize : 20,
            stepDelay : 100,
            containterW : 644
        } );
    }

	if( $( '#box-highest-rated-cat-photos-list' ).length ) {
        $( '#box-highest-rated-cat-photos-list' ).scrollme( {
            containter : '#box-highest-rated-cat-photos',
            up : '#box-highest-rated-cat-photos-previous',
            down : '#box-highest-rated-cat-photos-next',
            stepSize : 20,
            stepDelay : 100,
            containterW : 644
        } );
    }

	if( $( '#box-highest-rated-cat-videos-list' ).length ) {
        $( '#box-highest-rated-cat-videos-list' ).scrollme( {
            containter : '#box-highest-rated-cat-videos',
            up : '#box-highest-rated-cat-videos-previous',
            down : '#box-highest-rated-cat-videos-next',
            stepSize : 20,
            stepDelay : 100,
            containterW : 669
        } );
    }

	if( $( '#box-newest-cat-videos-list' ).length ) {
        $( '#box-newest-cat-videos-list' ).scrollme( {
            containter : '#box-newest-cat-videos',
            up : '#box-newest-cat-videos-previous',
            down : '#box-newest-cat-videos-next',
            stepSize : 20,
            stepDelay : 100,
            containterW : 669
        } );
    }

    if( $( '#box-newest-cats-list' ).length ) {
        $( '#box-newest-cats-list' ).scrollme( {
            containter : '#box-newest-cats',
            up : '#box-newest-cats-previous',
            down : '#box-newest-cats-next',
            stepSize : 20,
            stepDelay : 100,
            containterW : 644
        } );
    }
    
	if( $( '#box-newest-cats-photos-list' ).length ) {
        $( '#box-newest-cats-photos-list' ).scrollme( {
            containter : '#box-newest-cats-photos',
            up : '#box-newest-cats-photos-previous',
            down : '#box-newest-cats-photos-next',
            stepSize : 20,
            stepDelay : 100,
            containterW : 644
        } );
    }

	if( $( '#box-newest-cats-videos-list' ).length ) {
        $( '#box-newest-cats-videos-list' ).scrollme( {
            containter : '#box-newest-cats-videos',
            up : '#box-newest-cats-videos-previous',
            down : '#box-newest-cats-videos-next',
            stepSize : 20,
            stepDelay : 100,
            containterW : 669
        } );
    }

	if( $( '#box-newest-cats-friends-photos-list' ).length ) {
        $( '#box-newest-cats-friends-photos-list' ).scrollme( {
            containter : '#box-newest-cats-friends-photos',
            up : '#box-newest-cats-friends-photos-previous',
            down : '#box-newest-cats-friends-photos-next',
            stepSize : 20,
            stepDelay : 100,
            containterW : 644
        } );
    }

	if( $( '#box-newest-cats-friends-videos-list' ).length ) {
        $( '#box-newest-cats-friends-videos-list' ).scrollme( {
            containter : '#box-newest-cats-friends-videos',
            up : '#box-newest-cats-friends-videos-previous',
            down : '#box-newest-cats-friends-videos-next',
            stepSize : 20,
            stepDelay : 100,
            containterW : 669
        } );
    }
    
    if( $( '#loginForm' ).length ) {
        $( '#loginFormLogin' ).click( function( e ) {
            e.preventDefault();
            $( '#loginForm' ).slideToggle();
        } );
    }
    
    if( $( '#basic_information_login' ).length )
    {
        $( '#basic_information_login' ).click( function( e ) {
            e.preventDefault();
			$.scrollTo( '#loginForm' );
			if( $( '#loginForm' ).css( 'display' ) == 'none' ) {
			    $( '#loginForm' ).slideDown();
			}
        } );
    }

	if( $( '#dupa' ).length )
    {
        $( '#dupa' ).click( function( e ) {
            e.preventDefault();
			$.scrollTo( '#loginForm' );
			if( $( '#loginForm' ).css( 'display' ) == 'none' ) {
			    $( '#loginForm' ).slideDown();
			}
        } );
    }
    
	if( $( '#mrrr' ).length )
    {
        $( '#mrrr' ).click( function( e ) {
            e.preventDefault();
			$.scrollTo( '#loginForm' );
			if( $( '#loginForm' ).css( 'display' ) == 'none' ) {
			    $( '#loginForm' ).slideDown();
			}
        } );
    }

    if( $( '#cat-gallery-images-comments-login' ).length )
    {
        $( '#cat-gallery-images-comments-login' ).click( function( e ) {
            e.preventDefault();
			$.scrollTo( '#loginForm' );
			if( $( '#loginForm' ).css( 'display' ) == 'none' ) {
			    $( '#loginForm' ).slideDown();
			}
        } );
    }
    
    if( $( '#messageButton' ).length )
	{
		$( '#messageButton' ).click( function( e ) {
            e.preventDefault();
			$( '#messageSubcontainer' ).remove();
			$( '#messageContainer' ).remove();
			$( '#message' ).remove();
			if( $( '#ov' ) )
			{
			    $( '#ov' ).remove();
			}
		} );
	}
    
    if( $( '#check-nick-availability' ).length ) {
        $( '#check-nick-availability' ).click( function( e ) {
            e.preventDefault();
            if( $( '#CatNick' ).val().length )
            {
                if( $( '#nick-status' ) )
                {
                    $( '#nick-status' ).hide();
                }
                $( '#nick-availability-loading' ).css('visibility', 'visible');
                
                $.getJSON( '/cats/check_nick_availability/' + $( '#CatNick' ).val() + '.js',
                    function( data ) {
                        $( '#nick-availability-loading' ).css('visibility', 'hidden');
                        
                        $( '#nick-status' ).attr( 'class', '' );
                        if( data.status ) {
                            $( '#nick-status' ).addClass( data.status );
                        }
                        
                        var nickStatusMessage = document.createElement( 'span' );
                        nickStatusMessage.setAttribute( 'id', 'nick-status-message' );
                        var m = document.createTextNode( data.message );
                        nickStatusMessage.appendChild( m );
                        $( '#nick-status' ).html( nickStatusMessage );
                        
                        if( data.suggestions ) {
                            var nickSuggestions = document.createElement( 'ul' );
                            nickSuggestions.setAttribute( 'id', 'cat-nick-suggestions' );
                            
                            $.each( data.suggestions, function( i, s ) {
                                    var suggestion = document.createElement( 'li' );
                                    var a = document.createElement( 'a' );
                                    a.setAttribute( 'href', '#' );
                                    var nick = document.createTextNode( s );
                                    a.appendChild( nick );
                                    suggestion.appendChild( a );
                                    nickSuggestions.appendChild( suggestion );
                            } );
                            
                            $( '#nick-status' ).append( nickSuggestions );
                        }
                        
                        $( '#cat-nick-suggestions a' ).click( function( e ) {
                            e.preventDefault();
                            $( '#CatNick' ).val( $( this ).text() );
                            $( '#nick-status' ).empty();
                            $( '#nick-status' ).css( 'display', 'none' );
                        } );
                        
                        $( '#nick-status' ).show();
                    } );
            }
        } );
    }
    
    if( $( '#check-login-availability' ).length ) {
        $( '#check-login-availability' ).click( function( e ) {
            e.preventDefault();
            if( $( '#RegisterUsername' ).val().length ) {
                if( $( '#login-status' ) )
                {
                    $( '#login-status' ).hide();
                }
                $( '#login-availability-loading' ).css('visibility', 'visible');
                
                $.getJSON( '/users/check_login_availability/' + $( '#RegisterUsername' ).val() + '.js',
                    function( data ) {
                        $( '#login-availability-loading' ).css('visibility', 'hidden');
                        
                        $( '#login-status' ).attr( 'class', '' );
                        if( data.status ) {
                            $( '#login-status' ).addClass( data.status );
                        }
                        
                        var loginStatusMessage = document.createElement( 'span' );
                        loginStatusMessage.setAttribute( 'id', 'login-status-message' );
                        var m = document.createTextNode( data.message );
                        loginStatusMessage.appendChild( m );
                        $( '#login-status' ).html( loginStatusMessage );
                        
                        if( data.suggestions ) {
                            var loginSuggestions = document.createElement( 'ul' );
                            loginSuggestions.setAttribute( 'id', 'user-login-suggestions' );
                            
                            $.each( data.suggestions, function( i, s ) {
                                    var suggestion = document.createElement( 'li' );
                                    var a = document.createElement( 'a' );
                                    a.setAttribute( 'href', '#' );
                                    var login = document.createTextNode( s );
                                    a.appendChild( login );
                                    suggestion.appendChild( a );
                                    loginSuggestions.appendChild( suggestion );
                            } );
                            
                            $( '#login-status' ).append( loginSuggestions );
                        }
                        
                        $( '#user-login-suggestions a' ).click( function( e ) {
                            e.preventDefault();
                            $( '#RegisterUsername' ).val( $( this ).text() );
                            $( '#login-status' ).css( 'display', 'none' );
                        } );
                        
                        $( '#login-status' ).show();
                    } );
            }
        } );
    }
    
    
    if( $( '#answer-message' ).length ) {
        
        $( '.message blockquote' ).css( 'display', 'none' );
        
        if( $( '.unread' ).size() ) {
            $( '.unread:first blockquote' ).show();
            $( '.unread:first' ).removeClass( 'unread' );
        } else {
            $( '.message:last blockquote' ).show();
        }
        
        $( '.message h3' ).click( function( e ) {
            e.preventDefault();
            $( this ).parent().removeClass( 'unread' );
            $.get( '/admin/user_messages/mark_as_read/' + $( '.message:first' ).attr( 'id' ).split( 'message-')[1] + '/' + $( this ).parent().attr( 'id' ).split( 'message-')[1] );
            $( 'blockquote', $( this ).parent() ).toggle();
            if( $( 'blockquote', $( this ).parent() ).css( 'display') == 'none' ) {
                $( '.answer-form', $( this ).parent() ).remove();
            }
        } );
        
        $( '.answer' ).click( function( e ) {
            e.preventDefault();
            
            var mid = $( this ).attr( 'id' ).split( 'answer-to-' )[1];
            
            $( '.answer-form' ).remove();
            $( '#message-' + mid + ' blockquote' ).removeClass( 'unread' );
            $( '#message-' + mid + ' blockquote' ).show();
            
            if( !$( '#answerForm' + mid ).length )
            {
                var ma = document.createElement( 'form' );
                ma.setAttribute( 'id', 'answerForm' + mid );
                ma.setAttribute( 'name', 'answerForm' + mid );
                ma.setAttribute( 'class', 'answer-form' );
                ma.setAttribute( 'method', 'POST' );
                ma.setAttribute( 'action', '/admin/user_messages/add/' + $( '#UserMessageRecipientId' ).val() + '/' + $( '.message:first' ).attr( 'id' ).split( 'message-')[1] );
                
                var mamfs = document.createElement( 'fieldset' );
                mamfs.setAttribute( 'style', 'display:none' );
                
                var mamfsi = document.createElement( 'input' );
                mamfsi.setAttribute( 'type', 'hidden' );
                mamfsi.setAttribute( 'name', '_method' );
                mamfsi.setAttribute( 'value', 'POST' );
                
                mamfs.appendChild( mamfsi );
                
                var mafs = document.createElement( 'fieldset' );

                var madl = document.createElement( 'dl' );
                
                var madt = document.createElement( 'dt' );
                
                var mal = document.createElement( 'label' );
                mal.setAttribute( 'for', 'UserMessageContent' + mid );
                
                var malt = document.createTextNode( 'Odpowiedź:' );
                
                mal.appendChild( malt );
                madt.appendChild( mal );
                
                var madd = document.createElement( 'dd' );
                
                var mat = document.createElement( 'textarea' );
                mat.setAttribute( 'name', "data[UserMessage][content]" );
                mat.setAttribute( 'id', 'UserMessageContent' + mid );
                
                madd.appendChild( mat );
                
                var mari = document.createElement( 'input' );
                mari.setAttribute( 'type', 'hidden' );
                mari.setAttribute( 'name', 'data[UserMessage][recipient_id]' );
                mari.setAttribute( 'value', $( '#UserMessageRecipientId' ).val() );
                mari.setAttribute( 'id', 'UserMessageRecipientId' + mid );
                
                var mapi = document.createElement( 'input' );
                mapi.setAttribute( 'type', 'hidden' );
                mapi.setAttribute( 'name', 'data[UserMessage][parent_id]' );
                mapi.setAttribute( 'value', mid );
                mapi.setAttribute( 'id', 'UserMessageParentId' + mid );
                
                var mad = document.createElement( 'div' );
                mad.setAttribute( 'class', 'submit' );
                
                var mas = document.createElement( 'input' );
                mas.setAttribute( 'type', 'submit' );
                mas.setAttribute( 'id', 'submit' + mid );
                mas.setAttribute( 'value', 'Wyślij' );
                
                var mao = document.createTextNode( ' lub ' );
                
                var mac = document.createElement( 'a' );
                mac.setAttribute( 'href', '#' );
                
                var mact = document.createTextNode( 'Anuluj' );
                
                mac.appendChild( mact );
                
                $( mac ).click( function( e ){
                    e.preventDefault();
                    $( '#answerForm' + mid ).remove();
                } );
                
                mad.appendChild( mas );
                mad.appendChild( mao );
                mad.appendChild( mac );
                
                madl.appendChild( madt );
                madl.appendChild( madd );
                
                mafs.appendChild( madl );
                mafs.appendChild( mari );
                mafs.appendChild( mapi );
                mafs.appendChild( mad );
                
                ma.appendChild( mamfs );
                ma.appendChild( mafs );
                
                $( '#message-' + mid ).append( ma );
            }
        } );
    }
    
    
} );

