$(document).ready(function() {
	$(".styled select, .styled input:checkbox, .styled input:radio").uniform();
	$('.sidemenu ul li a').click(function () {
		if( !$( this ).parent( ).children( "ul" ).length )
		    return true;

	    $( this ).parent().find( "ul" ).toggle( );

	    return false;
	});

	get_check_boxes( ".get_cities", "register/cities/", false );


	$( "#add_woj" ).click( function( ){
		if( $( "#woj" ).attr( "disabled" ) )
			return false;

		var copy = $( "#woj" ).parent( ).parent( ).html( ).replace( /<ul.*?>(.*?)<\/ul>/, "" );
		$( this ).parent( ).before( "<li class='clr'>" + copy + "</li>" );

		$( this ).parent( ).prev( ).find( "div:first" ).find( "select" ).appendTo( $( this ).parent( ).prev( ) );
		$( this ).parent( ).prev( ).find( "div:first" ).remove( );
		var zzz = $( this ).parent( ).prev( ).find( "select:first" );
		$.uniform.update( zzz );
		zzz.uniform();

		get_check_boxes( ".get_cities", "register/cities/", false );

		return false;
	} );

	function get_check_boxes( item, url, add_parent_id )
    {
	    $( item ).change( function( ){
			var value = $( this ).val( );
			if( value == 0 )
			{
				$( this ).next( ).html( "" );
			}
			else
			{
				var ths = $( this );

				if( add_parent_id )
				{
					var sel_id = $( this ).attr( "id" ).substring( $( this ).attr( "id" ).length-1 );
					value += "/" + sel_id;
				}

				$.get(	base_href + url + value,
						function( result ){
							if( ths.next( ).length ==  0 )
								ths.parent().after( "<ul class='checklist get_dziel'>" + result + "</ul>" );
							else
								ths.next( ).html( result );

							loaded_cities( );
						} );
			}
		} );
    }

	$( "#head_search" ).blur( function( ){
		if( $( this ).val( ) == '' ) $( this ).val( 'wyszukiwarka' );
	} );

	$( "#head_search" ).focus( function( ){
		if( $( this ).val( ) == 'wyszukiwarka' ) $( this ).val( '' );
	} );

	$( ".hp_area" ).change( function( ){
		if( $( this ).val( ) == 0 )
			return;

		$.get(	base_href + "welcome/cities/" + $( this ).val( ), function( result ){
																		$( "#city" ).parent( ).remove( );
																		$( "#clabel" ).after( result );
																		$( "#city" ).uniform( );
																		hp_cities( );
																	} );
	} );

	
	hp_cities( );
	function hp_cities( )
	{
		$( "#city" ).change( function( ){
			if( $( this ).val( ) == 0 )
				return;

			var t = $( this );
			$.get(	base_href + "welcome/cities/" + $( this ).val( ) + "/2", function( result ){
																		var li = t.parents( "li" );
																		li.after( "<li class='dziel'><label for='dziel'><strong>Dzielnica</strong></label>"+result+"</li>" )
																		$( "#dziel" ).uniform( );
																	} );
		} );
	}

	function loaded_cities( )
	{
		$( ".get_dziel input[type='checkbox']" ).click( function( ){
			if( $( this ).val( ) == 0 )
				return;

			$( this ).parent( ).find( "ul" ).remove( );

			if( $( this ).attr( "checked" ) )
			{
				var t = $( this );
				$.get(	base_href + "register/cities/" + $( this ).val( ), function( result ){
																			t.parent( ).append( "<ul>" + result + "</ul>" );
																		} );
			}
		} );
	}

	$( "#query_status" ).change( function( ){
		$.get( base_href + "/queries/change_status/" + query_id + "/" + $( this ).val( ) );
	} );

	$( "#desc" ).keypress( function( ){
		var ile = (500 - ($( this ).val( ).length ) );
		$( "#desc_len" ).html( ile );


		if( ile <= 0 )
			$( "#desc_p" ).hide( );
		else
			$( "#desc_p" ).show( );
	} );

	$( ".conta input" ).focus( function( ){
		inp_focus( $( this ) );
	} );
	$( ".conta textarea" ).focus( function( ){
		inp_focus( $( this ) );
	} );

	$( ".conta input" ).blur( function( ){
		inp_blur( $( this ) );
	} );
	$( ".conta textarea" ).blur( function( ){
		inp_blur( $( this ) );
	} );

	function inp_focus( th )
	{
		if( th.val( ) == th.attr( "alt" ) )
			th.val( "" );
	}

	function inp_blur( th )
	{
		if( th.val( ) == "" )
			th.val( th.attr( "alt" ) );
	}
});


