var t;

$(document).ready(function() {
	var loader = new Image(  );
	loader.src = base_href + "res/images/ajax-loader.gif";

	$("#map ul").find("li:last").css('background', 'none');
	$("#mlink ul").find("li:last").css('background', 'none');
	$(".cities").find("li:last").css('border-bottom', '0');

	$("a.fancy").fancybox();

	if( $(".mixed-combo").length )
	{
		$(".mixed-combo").sexyCombo({
		emptyText: "",
		autoFill: true,
		skin: "custom"
		});
	}

	get_check_boxes( ".get_sub_categories", "register/subcats/", true );
	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>" + copy + "</li>" );
		get_check_boxes( ".get_cities", "register/cities/", false );

		return false;
	} );

	$( "#poland" ).click( function( ){
		if( $( "#poland" ).attr( "checked" ) )
			$( ".get_cities" ).attr( "disabled", true );
		else
			$( ".get_cities" ).removeAttr( "disabled" );
	} );

	$( ".down_tabs li a" ).click( function( ){
		if( $( this ).parent( ).attr( "class" ) != "t_active" )
		{
			var id = $( this ).attr( "href" ).split( "/" );
			id = id[ id.length -1 ];

			$( ".padd20" ).html( loader );
			$.get(	base_href + "/welcome/last_arts/" + id,
					function( html ){
						$( ".padd20" ).html( html ).hide( ).fadeIn( "slow" );
						t = setTimeout( 'fancy_size()', 500 );
					} );

			$( ".t_active" ).removeAttr( "class" );
			$( this ).parent( ).attr( "class", "t_active" );

			return false;
		}
	} );

    if( $('.wysiwyg').length )
    {
         $('.wysiwyg').tinymce({
			document_base_url : base_href,
			script_url : base_href + '/res/scripts/tiny_mce/tiny_mce.js',

			// General options
			mode : "textareas",
			theme : "advanced",
			skin : "o2k7",
			skin_variant : "black",

			// Theme options
			theme_advanced_buttons1: "bold,italic,underline,|,formatselect,|,forecolor,formatselect,|,fontselect,|,fontsizeselect",
			theme_advanced_buttons2: "",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_path: false,
			theme_advanced_resizing : true,
			language: "pl",
			height: "300"
		});

    }

    $( ".treelist label a" ).click( function( ){
	    if( !$( this ).parents( "li" ).children( "ul" ).length )
		    return true;

	    $( ".treelist li" ).removeClass( "unroll" );
	    $( this ).parents( "li" ).addClass( "unroll" );

	    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.parent( ).next( ).length ==  0 )
								ths.parent( ).after( "<ul class='checklist'>" + result + "</ul>" );
							else
								ths.parent( ).next( ).html( result );
						} );
			}
		} );
    }

	$( "input#same" ).click( function( ){
		//is_same_invoice( $( this ) );
		
		$.get( base_href + "panel/getData", function( json ){
			var data = eval( "(" + json + ")" );

			jQuery.each( data, function( item, value ){
				$( "#" + item ).val( value );
			} );
		} )

		return false;
	} );

	function is_same_invoice( ths )
	{
		if( $( ths ).attr( "checked" ) )
			$( ths ).parents( "form" ).find( "input[type=text]" ).attr( "disabled", true );
		else
			$( ths ).parents( "form" ).find( "input[type=text]" ).removeAttr( "disabled", true );
	}

	is_same_invoice( $( ".checkbox #same" ) );

	$( ".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" ) );
	}

	$( ".datepicker" ).datepicker( );

	$.datepicker.regional['pl'] = {
		closeText: 'Zamknij',
		prevText: '&#x3c;Poprzedni',
		nextText: 'Nast\u0119pny&#x3e;',
		currentText: 'Dzi\u015b',
		monthNames: ['Stycze\u0144','Luty','Marzec','Kwiecie\u0144','Maj','Czerwiec',
		'Lipiec','Sierpie\u0144','Wrzesie\u0144','Pa\u017adziernik','Listopad','Grudzie\u0144'],
		monthNamesShort: ['Sty','Lu','Mar','Kw','Maj','Cze',
		'Lip','Sie','Wrz','Pa','Lis','Gru'],
		dayNames: ['Niedziela','Poniedzialek','Wtorek','\u015aroda','Czwartek','Pi\u0105tek','Sobota'],
		dayNamesShort: ['Nie','Pn','Wt','\u015ar','Czw','Pt','So'],
		dayNamesMin: ['N','Pn','Wt','\u015ar','Cz','Pt','So'],
		dateFormat: 'yy-mm-dd', firstDay: 1,
		isRTL: false};
	$.datepicker.setDefaults($.datepicker.regional['pl']);

	$( ".suw_code textarea, #url_to_res input" ).click( function( ){
		$( this ).select( );
	} );

	$( ".coff" ).click( function( ){
		var t = $( this );
		$.get( t.attr( "href" ), function( ){
			show_cmp_list( "compare_list", "open" );
			
			$.getJSON( base_href + "/compare/cmp_list", function( json ){
				$( ".comp_list" ).html( "" );
				$.each( json, function( name, val ){
					$( '<li><span class="left"><a href="directory/company/' + name +  '">' + val + '</a></span><span class="right"><a href="compare/delete/' + name +  '">usuń</a></span><br class="clear"></li>' ).appendTo( ".comp_list" );
					
					$( "#send_compare" ).parent( ).removeClass( "hide" );
				} );
				
				compare_ajax( );
			} );
		} );
		
		return false;
	} )
	
	$( ".show_compare_list" ).click( function( ){
		show_cmp_list( "compare_list", "open" );
		return false;
	} );
	
	compare_ajax( );
	
	$( "#send_compare" ).click( function( ){
		$.get( $( this ).attr( "href" ), function( html ){
			$( html ).appendTo( "body" );
			show_cmp_list( "compare_form", "open" );
			compare_ajax( );
		} );
		
		return false;
	} );
	
	$( "#query_status" ).change( function( ){
		$.get( base_href + "/queries/change_status/" + query_id + "/" + $( this ).val( ) );
	} );
});

$( window ).load( function( )
{
	fancy_size( );
} );

function fancy_size( )
{
	clearTimeout( t );

	$( ".fancy" ).each( function( ){
		var par = parseInt( $( this ).css( "height" ) );
		var img = $( this ).children( "img" ).attr( "height" );

		if( par > img )
			$( this ).children( "img" ).css( "margin-top", ( par * .5 - img * .5 ) + "px" );
	} );
}

function show_cmp_list( item, mode )
{
	i = $( "#" + item );
	
	if( mode == "open" )
	{
		i.hide().addClass( "open" ).fadeIn( "slow" );
		
		var mt = ( i[0].clientHeight / 2 ) * -1;	
		i.css( "margin-top", mt + "px"  );
		
		if( !$( "#op" ).length )
			$( "<div id='op' />" ).appendTo( "body" );
	}
	else
	{
		if( item == "compare_list" )
			i.hide().removeClass( "open" ).fadeIn( "slow" ).css( "margin-top", "10px" );
		else
		{
			$( "#compare_list" ).removeClass( "open" ).css( "margin-top", "10px" );
			i.remove( );
		}
		
		$( "#op" ).remove( );
	}
}

function compare_ajax( )
{
	$( ".comp_list .right a" ).click( function( ){
		var t = $( this );
		$.get( t.attr( "href" ), function( ){ 
			t.parents( "li" ).remove( );
			
			if( $( ".comp_list li" ).length == 0 )
			{
				$( "<li>Brak firm</li>" ).appendTo( ".comp_list" );
				$( "#send_compare" ).parent( ).addClass( "hide" );
			}
		} );
		return false;
	} );

	$( ".close_me, #add_next_cmp" ).click( function( ){
		show_cmp_list( $( this ).parents( ".open" ).attr( "id" ), "close" );
		return false;
	} );
}
