
function MarkedCars( options ) {
	
	// RPC conenction:
	var rpc = $.Zend.jsonrpc({ url: 'my/marked-cars/ajax' });

	// containers
	var _container  = null;
	var _memo_table = null;
	
	
	function _colorize_table() {
		var tableRows = $(_memo_table).find('tr');
		var currentRow = 0;
		
		$(tableRows).each(function(){
			if( $(this).attr('class') == 'header' ) { // skip header rows
				return;
			}

			var bg = (currentRow%2) ? 'even' : 'odd'; // determin background
			$(this).attr('class', bg); // set background
			
			currentRow++;
		});
	};

	
	function _get_header_row( anchor ) {
		var tableRows = $(_memo_table).find('tr');
		var headerRow = null;
		
		$(tableRows).each(function(){
			if( $(this).attr('class') != 'header' ) { // skip non-header rows
				return;
			}
			
			if( $(this).attr('id') == 'memo-list-' + anchor ) {
				headerRow = this;
			}
		});
		
		return headerRow;
	};
	
	function _insert_header_row( anchor, title ) {
		var markup = '<tr id="memo-list-' + anchor + '" class="header">' +
					 '   <td colspan="3" class="header">' + title + '</td>' +
					 '</tr>';
		$(_memo_table).append(markup);
		
		$('#memo-list-' + anchor + " td").css({
			'padding-bottom': '3px',
			'padding-left': '15px',
			'padding-top': '3px',
			'width': '300px' 
		});
		
		$('#memo-list-' + anchor).hide().fadeIn('slow'); // display

	};
	
	function _get_insert_position( anchor ) {
		var tableRows = $(_memo_table).find('tr');
		var insertPosition = null;
		var insertHeader  = null;
		var headerFound = false;
		var insertFound = false;
		
		$(tableRows).each( function() {
			
			if( $(this).attr('id') == 'memo-list-' + anchor ) {
				insertHeader = this;
				headerFound = true;
				insertPosition = this;
				return;
			}
						
			if( $(this).attr('class') == 'header' && headerFound && !insertFound ) {
				insertFound = true;
			}
			
			if( !insertFound  ) {
				insertPosition = this;
			}
		});
		
		return insertPosition;
	};
	
	
	function _add_car_row( memoId, position ) {
		
		$.get( 'my/marked-cars/memo/id/' + memoId, function( result ) {
			// append new item
			$(position).after( result );
			
			_colorize_table();
			
			// display it
			$('#memo-list-' + memoId ).slideDown( 'normal' ); 
			
		} );
	};
	

	function _remove( id ) {
		$( '#memo-list-' + id ).slideUp( 'normal', function(){ 
			$( '#memo-list-' + id ).remove(); 

			var tableSize = $(_memo_table).children().size();
			if( tableSize == 0 ) {
				$(_container).fadeOut('slow');
			}
			
			rpc.remove(id); // trigger remove
			
			if( $('.marked-cars input[name="remove[]"]').size() <= 1 ) {
				$(_container).fadeOut('slow');
			}
		} );
	};
	
	function _flagAll( selected ) {
		$(_memo_table).find( "input[type='checkbox']" ).each( function() {
			$(this).attr('checked', selected );
		});
	};
		
	$.extend( this, {
		
		add: function( carId, userCarId, art ) {
			
			// update
			counterpixel( 197, userCarId );			
	
			var memoId = rpc.add( carId, userCarId, art );
			
			if( memoId != false ) {
				
				var position = null;
				
				if( art == 1 ) {
					var header = _get_header_row('db'); // database cars
					
					if( header == null ) { // create header if not exists
						_insert_header_row('db', "FAHRZEUGE AUS DER DATENBANK" );
					}
					
					position = _get_insert_position('db');
				}
				else if( art == 2 ) {
					var header = _get_header_row('uc'); // user cars
					
					if( header == null ) { // create header if not exists
						_insert_header_row('uc', "USERFAHRZEUGE" );
					}
					
					position = _get_insert_position('uc');
				}
				else if( art == 3 ) {
					var header = _get_header_row('cm'); // user cars
					
					if( header == null ) { // create header if not exists
						_insert_header_row('cm', "FAHRZEUGE AUS DEM AUTOMARKT" );
					}
					
					position = _get_insert_position('cm');
				}
				
				_add_car_row( memoId, position );
			}
			
			// hide button
			$('.marked-cars-add').fadeTo('slow', 0.33);
			$('.marked-cars-add').css('cursor', 'default');
			
			if( $(_container).css("display") == 'none' ) {
				$(_container).fadeIn('slow');
			};
		},
		
		remove: _remove,
		
		removeSelected: function() {
			$(_memo_table).find( "input[type='checkbox']:checked" ).each( function(){ 
				_remove( $(this).val() );
			});
			
			$('.marked-cars input[name="select-all"]').attr('checked', false);

			if( $('.marked-cars input[name="remove[]"]').size() <= 1 ) {
				$(_container).fadeOut('slow');
			}
		},
		
	
		init: function() {
			_container 	= $('.marked-cars');
			_memo_table = $('table.memo-list tbody'); 
			
			// prepare rows
			var tableRows = $(_memo_table).find('tr');
			$(tableRows).css('display','block'); // this is a display hack, so 
			// you actually can slide table rows. 

			var select_all = $('.marked-cars input[name="select-all"]');
			
			$(select_all).click( function(){
				_flagAll( $(select_all).attr('checked') ); 
			});
			
			_colorize_table();
		}
	});
};
	
var markedCars = new MarkedCars();

// init
$().ready( function() {
	markedCars.init();
});


